Raspberry Pi Power Controller by LPC1114

Dependencies:   mbed

Committer:
TareObjects
Date:
Tue Jan 03 06:27:05 2017 +0000
Revision:
0:13c37793b4c6
Child:
1:10aa79121f61
i2c??on, off, led?????

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 0:13c37793b4c6 21 int powerMode = kCommandUp;
TareObjects 0:13c37793b4c6 22 int ledMode = kLED_Flash;
TareObjects 0:13c37793b4c6 23
TareObjects 0:13c37793b4c6 24
TareObjects 0:13c37793b4c6 25 static char buf[MaxReceiveBufferSize];
TareObjects 0:13c37793b4c6 26
TareObjects 0:13c37793b4c6 27 Ticker second;
TareObjects 0:13c37793b4c6 28 long counter = -1;
TareObjects 0:13c37793b4c6 29
TareObjects 0:13c37793b4c6 30 void powerOff() {
TareObjects 0:13c37793b4c6 31 rpiPower = 0;
TareObjects 0:13c37793b4c6 32 powerMode = kCommandDown;
TareObjects 0:13c37793b4c6 33 }
TareObjects 0:13c37793b4c6 34
TareObjects 0:13c37793b4c6 35 void powerOn() {
TareObjects 0:13c37793b4c6 36 rpiPower = 1;
TareObjects 0:13c37793b4c6 37 powerMode = kCommandUp;
TareObjects 0:13c37793b4c6 38 }
TareObjects 0:13c37793b4c6 39
TareObjects 0:13c37793b4c6 40
TareObjects 0:13c37793b4c6 41 void ledOn() {
TareObjects 0:13c37793b4c6 42 led = 1;
TareObjects 0:13c37793b4c6 43 ledMode = kLED_On;
TareObjects 0:13c37793b4c6 44 }
TareObjects 0:13c37793b4c6 45
TareObjects 0:13c37793b4c6 46 void ledOff() {
TareObjects 0:13c37793b4c6 47 led = 0;
TareObjects 0:13c37793b4c6 48 ledMode = kLED_Off;
TareObjects 0:13c37793b4c6 49 }
TareObjects 0:13c37793b4c6 50
TareObjects 0:13c37793b4c6 51
TareObjects 0:13c37793b4c6 52 void funcSecond() {
TareObjects 0:13c37793b4c6 53 if (counter > 0) {
TareObjects 0:13c37793b4c6 54 counter --;
TareObjects 0:13c37793b4c6 55 if (counter == 0) {
TareObjects 0:13c37793b4c6 56 if (powerMode == kCommandDown) {
TareObjects 0:13c37793b4c6 57 powerOn();
TareObjects 0:13c37793b4c6 58 } else {
TareObjects 0:13c37793b4c6 59 powerOff();
TareObjects 0:13c37793b4c6 60 }
TareObjects 0:13c37793b4c6 61 counter = -1;
TareObjects 0:13c37793b4c6 62 }
TareObjects 0:13c37793b4c6 63 }
TareObjects 0:13c37793b4c6 64
TareObjects 0:13c37793b4c6 65 if (ledMode == kLED_Flash) {
TareObjects 0:13c37793b4c6 66 led = 1;
TareObjects 0:13c37793b4c6 67 wait_ms(10);
TareObjects 0:13c37793b4c6 68 led = 0;
TareObjects 0:13c37793b4c6 69 }
TareObjects 0:13c37793b4c6 70 }
TareObjects 0:13c37793b4c6 71
TareObjects 0:13c37793b4c6 72 int main()
TareObjects 0:13c37793b4c6 73 {
TareObjects 0:13c37793b4c6 74 slave.address(kCommandAddress);
TareObjects 0:13c37793b4c6 75 second.attach(funcSecond, 1);
TareObjects 0:13c37793b4c6 76
TareObjects 0:13c37793b4c6 77 powerOn();
TareObjects 0:13c37793b4c6 78
TareObjects 0:13c37793b4c6 79 while(1) {
TareObjects 0:13c37793b4c6 80 int status = slave.receive();
TareObjects 0:13c37793b4c6 81 switch (status) {
TareObjects 0:13c37793b4c6 82 case I2CSlave::WriteAddressed:
TareObjects 0:13c37793b4c6 83 if (slave.read(buf, MaxReceiveBufferSize)) {
TareObjects 0:13c37793b4c6 84 switch(buf[0]) {
TareObjects 0:13c37793b4c6 85 case kCommandUp:
TareObjects 0:13c37793b4c6 86 if (powerMode == kCommandUp) {
TareObjects 0:13c37793b4c6 87 // error already up
TareObjects 0:13c37793b4c6 88 } else {
TareObjects 0:13c37793b4c6 89 long t = atol(buf+1);
TareObjects 0:13c37793b4c6 90 if (t > 0) {
TareObjects 0:13c37793b4c6 91 counter = t;
TareObjects 0:13c37793b4c6 92 } else if (t == 0) {
TareObjects 0:13c37793b4c6 93 powerOn();
TareObjects 0:13c37793b4c6 94 }
TareObjects 0:13c37793b4c6 95 }
TareObjects 0:13c37793b4c6 96 break;
TareObjects 0:13c37793b4c6 97
TareObjects 0:13c37793b4c6 98 case kCommandDown:
TareObjects 0:13c37793b4c6 99 if (powerMode == kCommandDown) {
TareObjects 0:13c37793b4c6 100 // error already down
TareObjects 0:13c37793b4c6 101 } else {
TareObjects 0:13c37793b4c6 102 long t = atol(buf+1);
TareObjects 0:13c37793b4c6 103 if (t > 0) {
TareObjects 0:13c37793b4c6 104 counter = t;
TareObjects 0:13c37793b4c6 105 } else if (t == 0) {
TareObjects 0:13c37793b4c6 106 powerOn();
TareObjects 0:13c37793b4c6 107 }
TareObjects 0:13c37793b4c6 108 }
TareObjects 0:13c37793b4c6 109 break;
TareObjects 0:13c37793b4c6 110 case kCommandLED:
TareObjects 0:13c37793b4c6 111 switch (buf[1]) {
TareObjects 0:13c37793b4c6 112 case '0': ledOff(); break;
TareObjects 0:13c37793b4c6 113 case '1': ledOn(); break;
TareObjects 0:13c37793b4c6 114 case 'f': ledMode = kLED_Flash; break;
TareObjects 0:13c37793b4c6 115 }
TareObjects 0:13c37793b4c6 116 }
TareObjects 0:13c37793b4c6 117 }
TareObjects 0:13c37793b4c6 118 break;
TareObjects 0:13c37793b4c6 119
TareObjects 0:13c37793b4c6 120 default:
TareObjects 0:13c37793b4c6 121 wait_ms(1);
TareObjects 0:13c37793b4c6 122 break;
TareObjects 0:13c37793b4c6 123 }
TareObjects 0:13c37793b4c6 124 }
TareObjects 0:13c37793b4c6 125 }
TareObjects 0:13c37793b4c6 126