miniSSC.h
#ifndef miniSSC_h
#define miniSSC_h
#include <Arduino.h>
#include "mapper.h"
#define miniSSCMinVal -1
#define miniSSCMaxVal 1
class miniSSC {
public:
miniSSC(void);
void initSSC(bool fast=false);
void setServo(byte servoNum,float inVal); // Value ranged from -1.0 ... 1.0
private :
byte buff[3];
byte lastVal[255];
};
#endif
miniSSC.cpp
#include "miniSSC.h"
mapper SSCMapper(miniSSCMinVal,miniSSCMaxVal,0,254);
miniSSC::miniSSC() {
byte i;
buff[0] = 255;
for(i=0;i<=254;i++)
lastVal[i] = 255;
}
void miniSSC::initSSC(bool fast) {
if (fast)
Serial.begin(9600);
else
Serial.begin(2400);
}
void miniSSC::setServo(byte servoNum,float inVal) {
buff[1] = servoNum; // Slam the servo num into the output buffer
buff[2] = byte(SSCMapper.Map(inVal)); // Scale the inVal to a byte and pop it in there..
if (servoNum<255 && buff[2] != lastVal[servoNum]) { // Now before spending time to write it out, sanity check all this.
Serial.flush(); // Maybe we need to wait 'till its done?
Serial.write(buff,3); // Everything's ok, write the buffer out.
lastVal[servoNum] = buff[2]; // update our last position for this servo.
}
}
timeObj.h
#ifndef timeObj_h
#define timeObj_h
class timeObj {
public:
timeObj(float inMs);
void setTime(float inMs,bool startNow=true); // Change the time duration for next start..
void start(void); // Start the timer "now".
void stepTime(void); // Restart the timer from last end time.
bool ding(void); // Timer has expired.
private:
unsigned long waitTime;
unsigned long startTime;
unsigned long endTime;
bool crossing;
};
#endif
timeObj.cpp
#include "timeObj.h"
#include <arduino.h>
timeObj::timeObj(float inMs) {
setTime(inMs);
startTime = 0;
endTime = 0;
crossing = false;
}
void timeObj::setTime(float inMs,bool startNow) {
waitTime = 1000 * inMs;
if (startNow) start();
}
void timeObj::start(void) {
startTime = micros();
endTime = startTime + waitTime;
crossing = endTime < startTime;
}
void timeObj::stepTime(void) {
startTime = startTime + waitTime;
endTime = startTime + waitTime;
crossing = endTime < startTime;
}
bool timeObj::ding(void) {
unsigned long now;
now = micros();
if (crossing)
return (now < startTime) && (now >= endTime);
else
return now >= endTime;
}