Niels Leijen / PowerControl
Revision:
2:1e3291e8294d
Parent:
1:1bae9ab7241e
Child:
3:54792b95c570
--- a/PowerControl.cpp	Wed May 10 18:45:38 2017 +0000
+++ b/PowerControl.cpp	Fri May 12 20:19:17 2017 +0000
@@ -1,19 +1,46 @@
 #include "PowerControl.h"
 
 extern RawSerial pc;
+//extern DigitalOut ledError;
+extern CAN can;
 
-PowerControl::PowerControl(PinName greenButton, PinName redButton):
-    _greenButton(greenButton), _redButton(redButton)
+PowerControl::PowerControl(PinName button):
+    _button(button), _canControl(BUCK2)
 {
-    _greenButton.mode(PullUp);
-    _greenButton.fall(this, &PowerControl::goSleep);
-    _redButton.mode(PullUp);
-    _redButton.fall(this, &PowerControl::goSleep);
+    enabled = false;
+    
+    _button.setSamplesTillHeld( 200 );
+    _button.attach_asserted_held( this, &PowerControl::goSleep );
+    _button.attach_asserted( this, &PowerControl::goWakeup );
 }
 
-void PowerControl::goSleep(void)
-{
+void PowerControl::goSleep(){
+#ifdef DEBUG
     pc.printf("going to sleep\r\n");
+#endif
+    enabled = false;
     
+    //while(!biemDisable());
+    while(!_canControl.disable()); // must be last
+    
+#ifdef DEBUG
+    pc.printf("zzzzzzzzz.....r\n");
+#endif
+
     sleep();
-}
\ No newline at end of file
+}
+
+void PowerControl::goWakeup(){
+    if(!enabled){
+#ifdef DEBUG
+        pc.printf("waking up\r\n");
+#endif
+    while(!_canControl.enable()); // must be first
+    //while(!biemEnable());
+
+
+#ifdef DEBUG
+        pc.printf("fully awake\r\n");
+#endif
+    }
+}