@@ -268,10 +268,33 @@ AnalogInput::AnalogInput(uint8_t p)
268268
269269bool AnalogInput::read () {
270270 bool changed = false ;
271+
271272 if (Pin != NOT_A_PIN) {
272- const int current = this ->position ;
273+ const int previous = this ->position ;
273274 this ->position = analogRead (Pin);
274- if (current != this ->position ) changed = true ;
275+
276+ // check if value is different for 'changed' flag
277+ if (previous != this ->position ) {
278+
279+ const int rMin = isInverted () ? getMax () : getMin ();
280+ const int rMax = isInverted () ? getMin () : getMax ();
281+
282+ if (
283+ // if the previous value was under the minimum range
284+ // and the current value is as well, no change
285+ !(previous < rMin && this ->position < rMin) &&
286+
287+ // if the previous value was over the maximum range
288+ // and the current value is as well, no change
289+ !(previous > rMax && this ->position > rMax)
290+ )
291+ {
292+ // otherwise, the current value is either within the
293+ // range limits *or* it has changed from one extreme
294+ // to the other. Either way, mark it changed!
295+ changed = true ;
296+ }
297+ }
275298 }
276299 return changed;
277300}
@@ -311,15 +334,19 @@ void AnalogInput::setCalibration(AnalogInput::Calibration newCal) {
311334// #########################################################
312335
313336Pedals::Pedals (AnalogInput* dataPtr, uint8_t nPedals, uint8_t detectPin)
314- : pedalData(dataPtr), NumPedals(nPedals), detector(detectPin)
337+ :
338+ pedalData (dataPtr),
339+ NumPedals (nPedals),
340+ detector (detectPin),
341+ changed (false )
315342{}
316343
317344void Pedals::begin () {
318345 update (); // set initial pedal position
319346}
320347
321348bool Pedals::update () {
322- bool changed = false ;
349+ changed = false ;
323350
324351 detector.poll ();
325352 if (detector.getState () == DeviceConnection::Connected) {
@@ -420,8 +447,61 @@ void Pedals::serialCalibration(Stream& iface) {
420447 pedalData[i].read (); // read position
421448 pedalCal[i].max = pedalData[i].getPositionRaw (); // set max to the recorded position
422449 }
450+
451+ // deadzone options
452+ iface.println (separator);
423453 iface.println ();
424454
455+ float DeadzoneMin = 0.01 ; // by default, 1% (trying to keep things responsive)
456+ float DeadzoneMax = 0.025 ; // by default, 2.5%
457+
458+ iface.println (F (" These settings are optional. Send 'y' to customize. Send any other character to continue with the default values." ));
459+
460+ iface.print (F (" * Pedal Travel Deadzone, Start: \t " ));
461+ iface.print (DeadzoneMin);
462+ iface.println (F (" (Used to avoid the pedal always being slightly pressed)" ));
463+
464+ iface.print (F (" * Pedal Travel Deadzone, End: \t " ));
465+ iface.print (DeadzoneMax);
466+ iface.println (F (" (Used to guarantee that the pedal can be fully pressed)" ));
467+
468+ iface.println ();
469+
470+ waitClient (iface);
471+
472+ if (iface.read () == ' y' ) {
473+ iface.println (F (" Set the pedal travel starting deadzone as a floating point percentage." ));
474+ readFloat (DeadzoneMin, iface);
475+ iface.println ();
476+
477+ iface.println (F (" Set the pedal travel ending deadzone as a floating point percentage." ));
478+ readFloat (DeadzoneMax, iface);
479+ iface.println ();
480+ }
481+
482+ flushClient (iface);
483+
484+ // calculate deadzone offsets
485+ for (int i = 0 ; (i < getNumPedals ()) && (i < MaxPedals); i++) {
486+ auto &cMin = pedalCal[i].min ;
487+ auto &cMax = pedalCal[i].max ;
488+
489+ const int range = abs (cMax - cMin);
490+ const int dzMin = DeadzoneMin * (float )range;
491+ const int dzMax = DeadzoneMax * (float )range;
492+
493+ // non-inverted
494+ if (cMax >= cMin) {
495+ cMax -= dzMax; // 'cut' into the range so it limits sooner
496+ cMin += dzMin;
497+ }
498+ // inverted
499+ else {
500+ cMax += dzMax;
501+ cMin -= dzMin;
502+ }
503+ }
504+
425505 // print finished calibration
426506 iface.println (F (" Here is your calibration:" ));
427507 iface.println (separator);
@@ -487,7 +567,7 @@ LogitechPedals::LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchP
487567 // taken from calibrating my own pedals. the springs are pretty stiff so while
488568 // this covers the whole travel range, users may want to back it down for casual
489569 // use (esp. for the brake travel)
490- this ->setCalibration ({ 904 , 48 }, { 949 , 286 }, { 881 , 59 });
570+ this ->setCalibration ({ 904 , 48 }, { 944 , 286 }, { 881 , 59 });
491571}
492572
493573LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals (uint8_t gasPin, uint8_t brakePin, uint8_t detectPin)
0 commit comments