Browse Source

Fix power logic

- Make default state of PWR pin to 1
- Change power timings
Alexey Edelev 4 years ago
parent
commit
9ac18f2868

+ 11 - 11
Arduino/eScooterControl/buttoncontrol.cpp

@@ -28,6 +28,7 @@
 #include "display.h"
 
 const unsigned long ReactionTime = 700;
+const unsigned long LongReactionTime = 500;
 
 /*
  * Do not use interruption here because film buttons have false positives in case of bad contact. 
@@ -41,6 +42,7 @@ ButtonControl::ButtonControl() : m_triggers(0)
   pinMode(LedPin, OUTPUT);
   pinMode(PowerPin, OUTPUT);
   pinMode(ButtonPin, INPUT_PULLUP);
+  digitalWrite(PowerPin, HIGH);
 }
 
 void ButtonControl::dispatch() {
@@ -53,24 +55,22 @@ void ButtonControl::dispatch() {
     ++m_triggers;
   }
 
+  unsigned long timeDiff = currentTime - m_triggerTime;
   m_previousState = state;
-
-  if ((currentTime - m_triggerTime) >= ReactionTime) {
-    if (m_triggers == 1 && state == LOW) {
+  if (state == LOW && timeDiff >= LongReactionTime && m_triggers == 1) {
 //      Serial.println("Power off");
-      digitalWrite(PowerPin, HIGH);
-    } else if (m_triggers == 1 && state == HIGH) {
+    //TODO: Prohibit switch off if speed > 0 || throttle is no at 0
+    digitalWrite(PowerPin, LOW);    
+  } else if (timeDiff >= ReactionTime && state == HIGH) {
+    if (m_triggers == 1) {
 //      Serial.println("Toggle led");
       toggleLedState();
-    } else if (m_triggers == 2 && state == HIGH) {
+    } else if (m_triggers == 2) {
 //      Serial.println("Toggle eco");
       //TODO: Enable eco mode
     }
-    
-    if (state == HIGH) {
-      //Action finished reset number of triggers
-      m_triggers = 0;
-    }
+    //Action finished reset number of triggers
+    m_triggers = 0;
   }
 }
 

+ 5 - 4
Arduino/eScooterControl/eScooterControl.ino

@@ -46,6 +46,11 @@ void setup() {
 //  Serial.begin(115200);
 //  Serial.println("Init");
 
+  ButtonControl::instance();
+  gButtonThread.assignCallback([](){
+    ButtonControl::instance()->dispatch();
+  });
+
   delay(1000);
   AccelerationControl::instance();
   gAcceleratorPedalThread.assignCallback([](){
@@ -73,10 +78,6 @@ void setup() {
     }
   });
 
-  ButtonControl::instance();
-  gButtonThread.assignCallback([](){
-    ButtonControl::instance()->dispatch();
-  });
 //  Serial.println("Init complete");
 }