50 pinMode(fPWM1, OUTPUT);
51 pinMode(fPWM2, OUTPUT);
53 pinMode(fDiag, INPUT);
54 pinMode(fOCC, OUTPUT);
56 pinMode(fENB, OUTPUT);
171 return (analogRead(fOCM) * 4.9) / 500 * 1000;
183 return digitalRead(fDiag, HIGH);
195 fDeadBandLower = lower;
196 fDeadBandUpper = upper;
220 if (withinDeadband(percent))
224 dualAnalogWrite(fPWM1, 0, fPWM2, 0);
231 else if (fEnabled && percent < 0)
234 dualAnalogWrite(fPWM1, 0, fPWM2, fabs(percent));
239 dualAnalogWrite(fPWM1, fabs(percent), fPWM2, 0);
253 digitalWrite(fOCC, automatic ? 1 : 0);
276 static constexpr uint8_t getPrescaleCSBits(
unsigned prescale)
278 return (prescale == 0) ? 0b00000001 :
279 (prescale == 8) ? 0b00000010 :
280 (prescale == 64) ? 0b00000011 :
281 (prescale == 256) ? 0b00000100 :
282 (prescale == 1024) ? 0b00000101 : 0;
285 static inline void pinMode(uint8_t pin, uint8_t mode)
289 ::pinMode(pin, mode);
293 static inline void digitalWrite(uint8_t pin, uint8_t val)
297 Serial.println(
"digitalWrite");
298 ::digitalWrite(pin, val);
302 static inline uint8_t digitalRead(uint8_t pin, uint8_t defaultValue = 0)
304 return (pin !=
kPinNotUsed) ? ::digitalRead(pin) : defaultValue;
307 static inline int analogRead(uint8_t pin)
309 return (pin !=
kPinNotUsed) ? ::analogRead(pin) : 0;
312 static inline void dualAnalogWrite(uint8_t m1,
float v1, uint8_t m2,
float v2)
314 const auto output1{
static_cast<uint16_t
>(abs(v1) * 1023)};
315 const auto output2{
static_cast<uint16_t
>(abs(v2) * 1023)};
335 Serial.print(
"dualAnalogWrite "); Serial.print(output1); Serial.print(
", "); Serial.println(output2);
336 ::analogWrite(m1, output1, 1023);
337 ::analogWrite(m2, output2, 1023);
342 bool withinDeadband(
float value)
const
344 return value < fDeadBandUpper && value > fDeadBandLower;
347 void enableOutputs(
void)
const
349 digitalWrite(fEN, 1);
350 digitalWrite(fENB, 0);
353 void disableOutputs(
void)
const
355 digitalWrite(fEN, 0);
356 digitalWrite(fENB, 1);
359 float fDeadBandLower = 0;
360 float fDeadBandUpper = 0;
362 bool fBrakeMode =
false;
363 bool fEnabled =
false;