Raspberry Pi Power Controller by LPC1114

Dependencies:   mbed

Committer:
TareObjects
Date:
Wed Jan 04 23:27:19 2017 +0000
Revision:
1:10aa79121f61
Parent:
0:13c37793b4c6
Child:
2:f6265132c464
timer on / timer off???????????fix; sw1????on / off???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TareObjects 0:13c37793b4c6 1 #include "mbed.h"
TareObjects 0:13c37793b4c6 2
TareObjects 0:13c37793b4c6 3
TareObjects 0:13c37793b4c6 4 #define kCommandAddress 0x50
TareObjects 0:13c37793b4c6 5 #define MaxReceiveBufferSize 100
TareObjects 0:13c37793b4c6 6
TareObjects 0:13c37793b4c6 7 #define kCommandUp 'u' // i2c command : up - power on
TareObjects 0:13c37793b4c6 8 #define kCommandDown 'd' // : down - power off
TareObjects 0:13c37793b4c6 9 #define kCommandLED 'l' // : led - on/off/flash
TareObjects 0:13c37793b4c6 10
TareObjects 0:13c37793b4c6 11 #define kLED_Off 0 // LED off
TareObjects 0:13c37793b4c6 12 #define kLED_On 1 // LED on
TareObjects 0:13c37793b4c6 13 #define kLED_Flash 2 // LED flash
TareObjects 0:13c37793b4c6 14
TareObjects 0:13c37793b4c6 15
TareObjects 0:13c37793b4c6 16 I2CSlave slave(dp5, dp27);
TareObjects 0:13c37793b4c6 17
TareObjects 0:13c37793b4c6 18 DigitalOut rpiPower(dp17);
TareObjects 0:13c37793b4c6 19 DigitalOut led(dp14);
TareObjects 0:13c37793b4c6 20
TareObjects 1:10aa79121f61 21 DigitalIn sw1(dp25);
TareObjects 1:10aa79121f61 22 const int SW_OFF = 1;
TareObjects 1:10aa79121f61 23 const int SW_ON = 0; // pull up
TareObjects 1:10aa79121f61 24 int waitForSw1Release = 0;
TareObjects 1:10aa79121f61 25
TareObjects 1:10aa79121f61 26
TareObjects 0:13c37793b4c6 27 int powerMode = kCommandUp;
TareObjects 0:13c37793b4c6 28 int ledMode = kLED_Flash;
TareObjects 1:10aa79121f61 29 int sw1Mode = SW_OFF;
TareObjects 0:13c37793b4c6 30
TareObjects 0:13c37793b4c6 31
TareObjects 0:13c37793b4c6 32 static char buf[MaxReceiveBufferSize];
TareObjects 0:13c37793b4c6 33
TareObjects 0:13c37793b4c6 34 Ticker second;
TareObjects 1:10aa79121f61 35 long onTimer = -1;
TareObjects 1:10aa79121f61 36 long offTimer = -1;
TareObjects 0:13c37793b4c6 37
TareObjects 0:13c37793b4c6 38 void powerOff() {
TareObjects 0:13c37793b4c6 39 rpiPower = 0;
TareObjects 0:13c37793b4c6 40 powerMode = kCommandDown;
TareObjects 0:13c37793b4c6 41 }
TareObjects 0:13c37793b4c6 42
TareObjects 0:13c37793b4c6 43 void powerOn() {
TareObjects 0:13c37793b4c6 44 rpiPower = 1;
TareObjects 0:13c37793b4c6 45 powerMode = kCommandUp;
TareObjects 0:13c37793b4c6 46 }
TareObjects 0:13c37793b4c6 47
TareObjects 0:13c37793b4c6 48
TareObjects 0:13c37793b4c6 49 void ledOn() {
TareObjects 0:13c37793b4c6 50 led = 1;
TareObjects 0:13c37793b4c6 51 ledMode = kLED_On;
TareObjects 0:13c37793b4c6 52 }
TareObjects 0:13c37793b4c6 53
TareObjects 0:13c37793b4c6 54 void ledOff() {
TareObjects 0:13c37793b4c6 55 led = 0;
TareObjects 0:13c37793b4c6 56 ledMode = kLED_Off;
TareObjects 0:13c37793b4c6 57 }
TareObjects 0:13c37793b4c6 58
TareObjects 0:13c37793b4c6 59
TareObjects 0:13c37793b4c6 60 void funcSecond() {
TareObjects 1:10aa79121f61 61 if (onTimer > 0) {
TareObjects 1:10aa79121f61 62 onTimer --;
TareObjects 1:10aa79121f61 63 if (onTimer == 0) {
TareObjects 1:10aa79121f61 64 powerOn();
TareObjects 1:10aa79121f61 65 onTimer = -1;
TareObjects 1:10aa79121f61 66 }
TareObjects 1:10aa79121f61 67 }
TareObjects 1:10aa79121f61 68 if (offTimer > 0) {
TareObjects 1:10aa79121f61 69 offTimer --;
TareObjects 1:10aa79121f61 70 if (offTimer == 0) {
TareObjects 1:10aa79121f61 71 powerOff();
TareObjects 1:10aa79121f61 72 offTimer = -1;
TareObjects 0:13c37793b4c6 73 }
TareObjects 0:13c37793b4c6 74 }
TareObjects 0:13c37793b4c6 75
TareObjects 0:13c37793b4c6 76 if (ledMode == kLED_Flash) {
TareObjects 0:13c37793b4c6 77 led = 1;
TareObjects 0:13c37793b4c6 78 wait_ms(10);
TareObjects 0:13c37793b4c6 79 led = 0;
TareObjects 0:13c37793b4c6 80 }
TareObjects 0:13c37793b4c6 81 }
TareObjects 0:13c37793b4c6 82
TareObjects 0:13c37793b4c6 83 int main()
TareObjects 0:13c37793b4c6 84 {
TareObjects 0:13c37793b4c6 85 slave.address(kCommandAddress);
TareObjects 0:13c37793b4c6 86 second.attach(funcSecond, 1);
TareObjects 0:13c37793b4c6 87
TareObjects 1:10aa79121f61 88 sw1.mode(PullNone);
TareObjects 1:10aa79121f61 89
TareObjects 0:13c37793b4c6 90 powerOn();
TareObjects 0:13c37793b4c6 91
TareObjects 0:13c37793b4c6 92 while(1) {
TareObjects 1:10aa79121f61 93 if (sw1 == SW_ON) {
TareObjects 1:10aa79121f61 94 wait_ms(5);
TareObjects 1:10aa79121f61 95 waitForSw1Release = 1;
TareObjects 1:10aa79121f61 96 }
TareObjects 1:10aa79121f61 97
TareObjects 1:10aa79121f61 98 if (waitForSw1Release && sw1 == SW_OFF) {
TareObjects 1:10aa79121f61 99 if (powerMode == kCommandUp) {
TareObjects 1:10aa79121f61 100 powerOff();
TareObjects 1:10aa79121f61 101 } else {
TareObjects 1:10aa79121f61 102 powerOn();
TareObjects 1:10aa79121f61 103 }
TareObjects 1:10aa79121f61 104 waitForSw1Release = 0;
TareObjects 1:10aa79121f61 105 }
TareObjects 0:13c37793b4c6 106 int status = slave.receive();
TareObjects 1:10aa79121f61 107 long t = 0;
TareObjects 0:13c37793b4c6 108 switch (status) {
TareObjects 0:13c37793b4c6 109 case I2CSlave::WriteAddressed:
TareObjects 0:13c37793b4c6 110 if (slave.read(buf, MaxReceiveBufferSize)) {
TareObjects 0:13c37793b4c6 111 switch(buf[0]) {
TareObjects 0:13c37793b4c6 112 case kCommandUp:
TareObjects 1:10aa79121f61 113 t = atol(buf+1);
TareObjects 1:10aa79121f61 114 if (t > 0) {
TareObjects 1:10aa79121f61 115 onTimer = t;
TareObjects 1:10aa79121f61 116 } else if (t == 0) {
TareObjects 1:10aa79121f61 117 powerOn();
TareObjects 0:13c37793b4c6 118 }
TareObjects 0:13c37793b4c6 119 break;
TareObjects 0:13c37793b4c6 120
TareObjects 0:13c37793b4c6 121 case kCommandDown:
TareObjects 1:10aa79121f61 122 t = atol(buf+1);
TareObjects 1:10aa79121f61 123 if (t > 0) {
TareObjects 1:10aa79121f61 124 offTimer = t;
TareObjects 1:10aa79121f61 125 } else if (t == 0) {
TareObjects 1:10aa79121f61 126 powerOff();
TareObjects 0:13c37793b4c6 127 }
TareObjects 0:13c37793b4c6 128 break;
TareObjects 0:13c37793b4c6 129 case kCommandLED:
TareObjects 0:13c37793b4c6 130 switch (buf[1]) {
TareObjects 0:13c37793b4c6 131 case '0': ledOff(); break;
TareObjects 0:13c37793b4c6 132 case '1': ledOn(); break;
TareObjects 0:13c37793b4c6 133 case 'f': ledMode = kLED_Flash; break;
TareObjects 0:13c37793b4c6 134 }
TareObjects 0:13c37793b4c6 135 }
TareObjects 0:13c37793b4c6 136 }
TareObjects 0:13c37793b4c6 137 break;
TareObjects 0:13c37793b4c6 138
TareObjects 0:13c37793b4c6 139 default:
TareObjects 0:13c37793b4c6 140 wait_ms(1);
TareObjects 0:13c37793b4c6 141 break;
TareObjects 0:13c37793b4c6 142 }
TareObjects 0:13c37793b4c6 143 }
TareObjects 0:13c37793b4c6 144 }
TareObjects 0:13c37793b4c6 145