Raspberry Pi Power Controller by LPC1114

Dependencies:   mbed

Revision:
1:10aa79121f61
Parent:
0:13c37793b4c6
Child:
2:f6265132c464
--- a/main.cpp	Tue Jan 03 06:27:05 2017 +0000
+++ b/main.cpp	Wed Jan 04 23:27:19 2017 +0000
@@ -18,14 +18,22 @@
 DigitalOut rpiPower(dp17);
 DigitalOut led(dp14);
 
+DigitalIn sw1(dp25);
+const int SW_OFF = 1;
+const int SW_ON  = 0;   // pull up
+int waitForSw1Release = 0;
+
+
 int powerMode = kCommandUp;
 int ledMode   = kLED_Flash;
+int sw1Mode   = SW_OFF;
 
 
 static char buf[MaxReceiveBufferSize];
 
 Ticker second;
-long counter = -1;
+long onTimer  = -1;
+long offTimer = -1;
 
 void powerOff() {
     rpiPower  = 0;
@@ -50,15 +58,18 @@
 
 
 void funcSecond() {
-    if (counter > 0) {
-        counter --;
-        if (counter == 0) {
-            if (powerMode == kCommandDown) {
-                powerOn();
-            } else {
-                powerOff();
-            }
-            counter = -1;      
+    if (onTimer > 0) {
+        onTimer --;
+        if (onTimer == 0) {
+            powerOn();
+            onTimer = -1;      
+        }
+    }  
+    if (offTimer > 0) {
+        offTimer --;
+        if (offTimer == 0) {
+            powerOff();
+            offTimer = -1;      
         }
     }  
     
@@ -74,37 +85,45 @@
     slave.address(kCommandAddress);
     second.attach(funcSecond, 1);
     
+    sw1.mode(PullNone);
+    
     powerOn();
     
     while(1) {
+        if (sw1 == SW_ON) {
+            wait_ms(5);
+            waitForSw1Release = 1;
+        }
+        
+        if (waitForSw1Release && sw1 == SW_OFF) {
+            if (powerMode == kCommandUp) {
+                powerOff();
+            } else {
+                powerOn();
+            }
+            waitForSw1Release = 0; 
+        }
         int status = slave.receive();
+        long t = 0;
         switch (status) {
             case I2CSlave::WriteAddressed:
                 if (slave.read(buf, MaxReceiveBufferSize)) {
                     switch(buf[0]) {
                     case kCommandUp:
-                        if (powerMode == kCommandUp) {
-                            // error already up   
-                        } else {
-                            long t = atol(buf+1);
-                            if (t > 0) {
-                                counter = t;
-                            } else if (t == 0) {
-                                powerOn();
-                            }
+                        t = atol(buf+1);
+                        if (t > 0) {
+                            onTimer = t;
+                        } else if (t == 0) {
+                            powerOn();
                         }
                         break;
                         
                     case kCommandDown:
-                        if (powerMode == kCommandDown) {
-                            // error already down   
-                        } else {
-                            long t = atol(buf+1);
-                            if (t > 0) {
-                                counter = t;
-                            } else if (t == 0) {
-                                powerOn();
-                            }
+                        t = atol(buf+1);
+                        if (t > 0) {
+                            offTimer = t;
+                        } else if (t == 0) {
+                            powerOff();
                         }
                         break;    
                     case kCommandLED: