Koichi Kurahashi
/
RPi_1114
Raspberry Pi Power Controller by LPC1114
main.cpp@3:b7b9d3d4eed8, 2017-01-16 (annotated)
- Committer:
- TareObjects
- Date:
- Mon Jan 16 02:00:32 2017 +0000
- Revision:
- 3:b7b9d3d4eed8
- Parent:
- 2:f6265132c464
- Child:
- 4:7117eb230044
?????????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TareObjects | 0:13c37793b4c6 | 1 | #include "mbed.h" |
TareObjects | 0:13c37793b4c6 | 2 | |
TareObjects | 0:13c37793b4c6 | 3 | |
TareObjects | 2:f6265132c464 | 4 | const int kMaxInteger = 32767; |
TareObjects | 2:f6265132c464 | 5 | const float fFloatScaleValue = 1024.0; // factor to convert long <-> float |
TareObjects | 2:f6265132c464 | 6 | |
TareObjects | 2:f6265132c464 | 7 | // |
TareObjects | 2:f6265132c464 | 8 | // common definition |
TareObjects | 2:f6265132c464 | 9 | // |
TareObjects | 2:f6265132c464 | 10 | |
TareObjects | 0:13c37793b4c6 | 11 | #define kCommandAddress 0x50 |
TareObjects | 2:f6265132c464 | 12 | |
TareObjects | 0:13c37793b4c6 | 13 | |
TareObjects | 2:f6265132c464 | 14 | // internal gpio map |
TareObjects | 2:f6265132c464 | 15 | // |
TareObjects | 2:f6265132c464 | 16 | // dp5 : SDA - RPi's SDA / pad |
TareObjects | 2:f6265132c464 | 17 | // dp14 : on board LED |
TareObjects | 2:f6265132c464 | 18 | // dp15 : rx - RPi's TXD |
TareObjects | 2:f6265132c464 | 19 | // dp16 : tx - RPi's RXD |
TareObjects | 2:f6265132c464 | 20 | // dp17 : out - RPi's power switch |
TareObjects | 2:f6265132c464 | 21 | // dp23 : reset - RPi's GPIO24 |
TareObjects | 2:f6265132c464 | 22 | // dp24 : isp - RPI's GPIO23 |
TareObjects | 2:f6265132c464 | 23 | // dp25 : in - sw1 / pad |
TareObjects | 2:f6265132c464 | 24 | // dp27 : SCL - RPi's SCL / pad |
TareObjects | 2:f6265132c464 | 25 | |
TareObjects | 2:f6265132c464 | 26 | |
TareObjects | 0:13c37793b4c6 | 27 | |
TareObjects | 2:f6265132c464 | 28 | // pad map |
TareObjects | 2:f6265132c464 | 29 | // |
TareObjects | 2:f6265132c464 | 30 | // dp1 digital Pwm : Pwm |
TareObjects | 2:f6265132c464 | 31 | // dp2 digital Pwm : Pwm |
TareObjects | 2:f6265132c464 | 32 | // dp4 digital analog : digital in |
TareObjects | 2:f6265132c464 | 33 | // dp5 I2C(SDA) : RPi(master)'s SDA / pad |
TareObjects | 2:f6265132c464 | 34 | // dp6 digital : digital in |
TareObjects | 2:f6265132c464 | 35 | // dp9 digital analog : analog |
TareObjects | 2:f6265132c464 | 36 | // dp10 digital analog : analog |
TareObjects | 2:f6265132c464 | 37 | // dp11 digital analog : analog |
TareObjects | 2:f6265132c464 | 38 | // dp13 digital analog |
TareObjects | 2:f6265132c464 | 39 | // do18 digital Pwm : Pwm |
TareObjects | 2:f6265132c464 | 40 | // dp25 digital in : sw1 / pad |
TareObjects | 2:f6265132c464 | 41 | // dp26 digital : digital out |
TareObjects | 2:f6265132c464 | 42 | // dp27 I2C(SCL) : RPi(master)'s SCL / pad |
TareObjects | 2:f6265132c464 | 43 | // dp28 digital : digital out |
TareObjects | 2:f6265132c464 | 44 | |
TareObjects | 2:f6265132c464 | 45 | |
TareObjects | 0:13c37793b4c6 | 46 | |
TareObjects | 0:13c37793b4c6 | 47 | |
TareObjects | 2:f6265132c464 | 48 | // |
TareObjects | 2:f6265132c464 | 49 | // I2C typical command format |
TareObjects | 2:f6265132c464 | 50 | // |
TareObjects | 2:f6265132c464 | 51 | // [0] = command |
TareObjects | 2:f6265132c464 | 52 | // [1] = target ch |
TareObjects | 2:f6265132c464 | 53 | // [2] = format |
TareObjects | 2:f6265132c464 | 54 | // 0 : binary |
TareObjects | 2:f6265132c464 | 55 | // [3...] : long |
TareObjects | 2:f6265132c464 | 56 | // 1 : ascii string |
TareObjects | 2:f6265132c464 | 57 | // [3...] : c string |
TareObjects | 2:f6265132c464 | 58 | // |
TareObjects | 2:f6265132c464 | 59 | // endian : little endian |
TareObjects | 2:f6265132c464 | 60 | // |
TareObjects | 2:f6265132c464 | 61 | |
TareObjects | 2:f6265132c464 | 62 | #define kCommandUp 0x10 // i2c command : RPi power on |
TareObjects | 2:f6265132c464 | 63 | #define kCommandDown 0x11 // : RPi power off |
TareObjects | 2:f6265132c464 | 64 | |
TareObjects | 2:f6265132c464 | 65 | #define kCommandLED 0x20 // : led - on/off/flash |
TareObjects | 2:f6265132c464 | 66 | |
TareObjects | 2:f6265132c464 | 67 | #define kCommandAnalogRead 0x30 // : read analog port |
TareObjects | 2:f6265132c464 | 68 | #define kCommandAnalogReset 0x31 // : reset read buffer |
TareObjects | 2:f6265132c464 | 69 | #define kCommandAnalogStart 0x32 // : start fill buffer |
TareObjects | 2:f6265132c464 | 70 | #define kCommandAnalogStop 0x33 // : stop fill buffer |
TareObjects | 2:f6265132c464 | 71 | #define kCommandAnalogLoad 0x34 // : load analog data |
TareObjects | 2:f6265132c464 | 72 | |
TareObjects | 2:f6265132c464 | 73 | #define kCommandPwm 0x40 // : output Pwm |
TareObjects | 2:f6265132c464 | 74 | #define kCommandPwmPeriod 0x41 // : set Pwm period in uSec |
TareObjects | 2:f6265132c464 | 75 | |
TareObjects | 2:f6265132c464 | 76 | #define kCommandDigitalIn 0x50 // : input digital |
TareObjects | 2:f6265132c464 | 77 | |
TareObjects | 2:f6265132c464 | 78 | #define kCommandDigitalOut 0x60 // : output digital |
TareObjects | 2:f6265132c464 | 79 | |
TareObjects | 2:f6265132c464 | 80 | |
TareObjects | 2:f6265132c464 | 81 | #define kCommandModeBinary 0x00 // binary mode |
TareObjects | 2:f6265132c464 | 82 | #define kCommandModeASCII 0x10 // ascii mode |
TareObjects | 2:f6265132c464 | 83 | |
TareObjects | 2:f6265132c464 | 84 | // |
TareObjects | 2:f6265132c464 | 85 | // I2C |
TareObjects | 2:f6265132c464 | 86 | // |
TareObjects | 2:f6265132c464 | 87 | |
TareObjects | 2:f6265132c464 | 88 | #define MaxReceiveBufferSize 100 // i2c receiving buffer length |
TareObjects | 2:f6265132c464 | 89 | |
TareObjects | 0:13c37793b4c6 | 90 | I2CSlave slave(dp5, dp27); |
TareObjects | 0:13c37793b4c6 | 91 | |
TareObjects | 2:f6265132c464 | 92 | |
TareObjects | 2:f6265132c464 | 93 | |
TareObjects | 2:f6265132c464 | 94 | // |
TareObjects | 2:f6265132c464 | 95 | // RPi Power Controll |
TareObjects | 2:f6265132c464 | 96 | // |
TareObjects | 2:f6265132c464 | 97 | // [0] command |
TareObjects | 2:f6265132c464 | 98 | // turn on : kCommandUp |
TareObjects | 2:f6265132c464 | 99 | // turn off : kCommandDown |
TareObjects | 2:f6265132c464 | 100 | // [1] ch |
TareObjects | 2:f6265132c464 | 101 | // always 0 |
TareObjects | 2:f6265132c464 | 102 | // [2] format |
TareObjects | 2:f6265132c464 | 103 | // 0 : binary mode |
TareObjects | 2:f6265132c464 | 104 | // 1 : c string mode |
TareObjects | 2:f6265132c464 | 105 | // [3...] data |
TareObjects | 2:f6265132c464 | 106 | // binary mode : little endian long |
TareObjects | 2:f6265132c464 | 107 | // ascii mode : c string |
TareObjects | 2:f6265132c464 | 108 | // |
TareObjects | 2:f6265132c464 | 109 | |
TareObjects | 0:13c37793b4c6 | 110 | DigitalOut rpiPower(dp17); |
TareObjects | 0:13c37793b4c6 | 111 | DigitalOut led(dp14); |
TareObjects | 0:13c37793b4c6 | 112 | |
TareObjects | 2:f6265132c464 | 113 | int powerMode = kCommandUp; |
TareObjects | 1:10aa79121f61 | 114 | |
TareObjects | 2:f6265132c464 | 115 | void powerOff() |
TareObjects | 2:f6265132c464 | 116 | { |
TareObjects | 0:13c37793b4c6 | 117 | rpiPower = 0; |
TareObjects | 0:13c37793b4c6 | 118 | powerMode = kCommandDown; |
TareObjects | 0:13c37793b4c6 | 119 | } |
TareObjects | 0:13c37793b4c6 | 120 | |
TareObjects | 2:f6265132c464 | 121 | void powerOn() |
TareObjects | 2:f6265132c464 | 122 | { |
TareObjects | 0:13c37793b4c6 | 123 | rpiPower = 1; |
TareObjects | 0:13c37793b4c6 | 124 | powerMode = kCommandUp; |
TareObjects | 0:13c37793b4c6 | 125 | } |
TareObjects | 0:13c37793b4c6 | 126 | |
TareObjects | 0:13c37793b4c6 | 127 | |
TareObjects | 2:f6265132c464 | 128 | |
TareObjects | 2:f6265132c464 | 129 | // led modes |
TareObjects | 2:f6265132c464 | 130 | // |
TareObjects | 2:f6265132c464 | 131 | // [0] command |
TareObjects | 2:f6265132c464 | 132 | // led comtrol |
TareObjects | 2:f6265132c464 | 133 | // [1] ch |
TareObjects | 2:f6265132c464 | 134 | // always 0 |
TareObjects | 2:f6265132c464 | 135 | // [2] mode |
TareObjects | 2:f6265132c464 | 136 | // 0 : turn off |
TareObjects | 2:f6265132c464 | 137 | // 1 : turn on |
TareObjects | 2:f6265132c464 | 138 | // 2 : flash |
TareObjects | 2:f6265132c464 | 139 | // |
TareObjects | 2:f6265132c464 | 140 | |
TareObjects | 2:f6265132c464 | 141 | #define kLED_Off 0 // LED off |
TareObjects | 2:f6265132c464 | 142 | #define kLED_On 1 // LED on |
TareObjects | 2:f6265132c464 | 143 | #define kLED_Flash 2 // LED flash |
TareObjects | 2:f6265132c464 | 144 | |
TareObjects | 2:f6265132c464 | 145 | int ledMode = kLED_Flash; |
TareObjects | 2:f6265132c464 | 146 | |
TareObjects | 2:f6265132c464 | 147 | |
TareObjects | 2:f6265132c464 | 148 | void ledOn() |
TareObjects | 2:f6265132c464 | 149 | { |
TareObjects | 0:13c37793b4c6 | 150 | led = 1; |
TareObjects | 0:13c37793b4c6 | 151 | ledMode = kLED_On; |
TareObjects | 0:13c37793b4c6 | 152 | } |
TareObjects | 0:13c37793b4c6 | 153 | |
TareObjects | 2:f6265132c464 | 154 | void ledOff() |
TareObjects | 2:f6265132c464 | 155 | { |
TareObjects | 0:13c37793b4c6 | 156 | led = 0; |
TareObjects | 0:13c37793b4c6 | 157 | ledMode = kLED_Off; |
TareObjects | 0:13c37793b4c6 | 158 | } |
TareObjects | 0:13c37793b4c6 | 159 | |
TareObjects | 0:13c37793b4c6 | 160 | |
TareObjects | 2:f6265132c464 | 161 | |
TareObjects | 2:f6265132c464 | 162 | |
TareObjects | 2:f6265132c464 | 163 | // |
TareObjects | 2:f6265132c464 | 164 | // on board Switch |
TareObjects | 2:f6265132c464 | 165 | // |
TareObjects | 2:f6265132c464 | 166 | DigitalIn sw1(dp25); |
TareObjects | 2:f6265132c464 | 167 | const int SW_OFF = 1; |
TareObjects | 2:f6265132c464 | 168 | const int SW_ON = 0; // pull up |
TareObjects | 2:f6265132c464 | 169 | |
TareObjects | 2:f6265132c464 | 170 | int sw1Mode = SW_OFF; |
TareObjects | 2:f6265132c464 | 171 | int waitForSw1Release = 0; |
TareObjects | 2:f6265132c464 | 172 | |
TareObjects | 2:f6265132c464 | 173 | |
TareObjects | 2:f6265132c464 | 174 | // |
TareObjects | 2:f6265132c464 | 175 | // ADC |
TareObjects | 2:f6265132c464 | 176 | // |
TareObjects | 2:f6265132c464 | 177 | |
TareObjects | 2:f6265132c464 | 178 | #define kMaxAnalogChannels 3 // analog read channels .... never change |
TareObjects | 2:f6265132c464 | 179 | #define kMinAnalogPeriod 10000 // 10mSec is minimum |
TareObjects | 2:f6265132c464 | 180 | #define kMaxAnalogPeriod 10000000 // 10Sec is maximum |
TareObjects | 2:f6265132c464 | 181 | |
TareObjects | 2:f6265132c464 | 182 | const int kMaxAnalogBufferSize = 256; // analog read buffer size |
TareObjects | 2:f6265132c464 | 183 | |
TareObjects | 2:f6265132c464 | 184 | typedef struct { |
TareObjects | 2:f6265132c464 | 185 | unsigned short count; |
TareObjects | 2:f6265132c464 | 186 | unsigned short buffer[kMaxAnalogBufferSize]; |
TareObjects | 2:f6265132c464 | 187 | } AnalogBuffer; |
TareObjects | 2:f6265132c464 | 188 | |
TareObjects | 2:f6265132c464 | 189 | AnalogBuffer analogBuffer[kMaxAnalogChannels]; |
TareObjects | 2:f6265132c464 | 190 | |
TareObjects | 2:f6265132c464 | 191 | AnalogIn analogIn[kMaxAnalogChannels] = {dp9, dp10, dp11}; |
TareObjects | 2:f6265132c464 | 192 | Ticker analogTicker[kMaxAnalogChannels]; |
TareObjects | 2:f6265132c464 | 193 | //Ticker t1, t2, t0; |
TareObjects | 2:f6265132c464 | 194 | |
TareObjects | 2:f6265132c464 | 195 | |
TareObjects | 2:f6265132c464 | 196 | void analogTickerVectors(int ch) { |
TareObjects | 2:f6265132c464 | 197 | if (ch >= 0 && ch < kMaxAnalogChannels) { |
TareObjects | 2:f6265132c464 | 198 | AnalogBuffer *p = &analogBuffer[ch]; |
TareObjects | 2:f6265132c464 | 199 | unsigned short count = p->count; |
TareObjects | 2:f6265132c464 | 200 | if (count < (kMaxAnalogBufferSize-1)) { |
TareObjects | 2:f6265132c464 | 201 | float f = analogIn[ch]; |
TareObjects | 2:f6265132c464 | 202 | unsigned short v = f * fFloatScaleValue; |
TareObjects | 2:f6265132c464 | 203 | p->buffer[count++] = v; |
TareObjects | 2:f6265132c464 | 204 | p->count = count; |
TareObjects | 2:f6265132c464 | 205 | } |
TareObjects | 2:f6265132c464 | 206 | } |
TareObjects | 2:f6265132c464 | 207 | } |
TareObjects | 2:f6265132c464 | 208 | |
TareObjects | 2:f6265132c464 | 209 | |
TareObjects | 2:f6265132c464 | 210 | |
TareObjects | 2:f6265132c464 | 211 | void analogTickerVector0() |
TareObjects | 2:f6265132c464 | 212 | { |
TareObjects | 2:f6265132c464 | 213 | analogTickerVectors(0); |
TareObjects | 2:f6265132c464 | 214 | } |
TareObjects | 2:f6265132c464 | 215 | |
TareObjects | 2:f6265132c464 | 216 | void analogTickerVector1() |
TareObjects | 2:f6265132c464 | 217 | { |
TareObjects | 2:f6265132c464 | 218 | analogTickerVectors(1); |
TareObjects | 2:f6265132c464 | 219 | } |
TareObjects | 2:f6265132c464 | 220 | |
TareObjects | 2:f6265132c464 | 221 | void analogTickerVector2() |
TareObjects | 2:f6265132c464 | 222 | { |
TareObjects | 2:f6265132c464 | 223 | analogTickerVectors(2); |
TareObjects | 2:f6265132c464 | 224 | } |
TareObjects | 2:f6265132c464 | 225 | |
TareObjects | 2:f6265132c464 | 226 | |
TareObjects | 2:f6265132c464 | 227 | |
TareObjects | 2:f6265132c464 | 228 | float execAnalogIn(int ch) |
TareObjects | 2:f6265132c464 | 229 | { |
TareObjects | 2:f6265132c464 | 230 | if (ch >= 0 && kMaxAnalogChannels) { |
TareObjects | 2:f6265132c464 | 231 | return analogIn[ch]; |
TareObjects | 2:f6265132c464 | 232 | } |
TareObjects | 2:f6265132c464 | 233 | return -1; |
TareObjects | 2:f6265132c464 | 234 | } |
TareObjects | 2:f6265132c464 | 235 | |
TareObjects | 2:f6265132c464 | 236 | |
TareObjects | 2:f6265132c464 | 237 | int execAnalogReset(int ch) |
TareObjects | 2:f6265132c464 | 238 | { |
TareObjects | 2:f6265132c464 | 239 | if (ch >= 0 && ch < kMaxAnalogChannels) { |
TareObjects | 2:f6265132c464 | 240 | analogTicker[ch].detach(); |
TareObjects | 2:f6265132c464 | 241 | analogBuffer[ch].count = 0; |
TareObjects | 2:f6265132c464 | 242 | return 0; |
TareObjects | 2:f6265132c464 | 243 | } |
TareObjects | 2:f6265132c464 | 244 | return -1; |
TareObjects | 2:f6265132c464 | 245 | } |
TareObjects | 2:f6265132c464 | 246 | |
TareObjects | 2:f6265132c464 | 247 | |
TareObjects | 2:f6265132c464 | 248 | int execAnalogStart(int ch, long value) |
TareObjects | 2:f6265132c464 | 249 | { |
TareObjects | 2:f6265132c464 | 250 | if (ch >=0 && ch < kMaxAnalogChannels) { |
TareObjects | 2:f6265132c464 | 251 | analogBuffer[ch].count = 0; |
TareObjects | 2:f6265132c464 | 252 | value *= 1000; |
TareObjects | 2:f6265132c464 | 253 | if (value < kMinAnalogPeriod || value > kMaxAnalogPeriod) { |
TareObjects | 2:f6265132c464 | 254 | value = 1000; // 1 sec |
TareObjects | 2:f6265132c464 | 255 | } |
TareObjects | 2:f6265132c464 | 256 | switch (ch) { |
TareObjects | 2:f6265132c464 | 257 | case 0: analogTicker[ch].attach_us(&analogTickerVector0, value); break; |
TareObjects | 2:f6265132c464 | 258 | case 1: analogTicker[ch].attach_us(&analogTickerVector1, value); break; |
TareObjects | 2:f6265132c464 | 259 | case 2: analogTicker[ch].attach_us(&analogTickerVector2, value); break; |
TareObjects | 2:f6265132c464 | 260 | } |
TareObjects | 2:f6265132c464 | 261 | return 0; |
TareObjects | 2:f6265132c464 | 262 | } |
TareObjects | 2:f6265132c464 | 263 | |
TareObjects | 2:f6265132c464 | 264 | return -1; |
TareObjects | 2:f6265132c464 | 265 | } |
TareObjects | 2:f6265132c464 | 266 | |
TareObjects | 2:f6265132c464 | 267 | |
TareObjects | 2:f6265132c464 | 268 | int execAnalogStop(int ch) |
TareObjects | 2:f6265132c464 | 269 | { |
TareObjects | 2:f6265132c464 | 270 | if (ch >= 0 && ch < kMaxAnalogChannels) { |
TareObjects | 2:f6265132c464 | 271 | analogTicker[ch].detach(); |
TareObjects | 2:f6265132c464 | 272 | return 0; |
TareObjects | 2:f6265132c464 | 273 | } |
TareObjects | 2:f6265132c464 | 274 | return -1; |
TareObjects | 2:f6265132c464 | 275 | } |
TareObjects | 2:f6265132c464 | 276 | |
TareObjects | 2:f6265132c464 | 277 | |
TareObjects | 2:f6265132c464 | 278 | |
TareObjects | 2:f6265132c464 | 279 | |
TareObjects | 2:f6265132c464 | 280 | |
TareObjects | 2:f6265132c464 | 281 | |
TareObjects | 2:f6265132c464 | 282 | |
TareObjects | 2:f6265132c464 | 283 | |
TareObjects | 2:f6265132c464 | 284 | |
TareObjects | 2:f6265132c464 | 285 | |
TareObjects | 2:f6265132c464 | 286 | |
TareObjects | 2:f6265132c464 | 287 | |
TareObjects | 2:f6265132c464 | 288 | |
TareObjects | 2:f6265132c464 | 289 | |
TareObjects | 2:f6265132c464 | 290 | |
TareObjects | 2:f6265132c464 | 291 | |
TareObjects | 2:f6265132c464 | 292 | // |
TareObjects | 2:f6265132c464 | 293 | // Digital Read |
TareObjects | 2:f6265132c464 | 294 | // |
TareObjects | 2:f6265132c464 | 295 | const int kMaxDigitalInChannels = 2; |
TareObjects | 2:f6265132c464 | 296 | |
TareObjects | 2:f6265132c464 | 297 | DigitalIn digitalIn[kMaxDigitalInChannels] = {dp4,dp6}; |
TareObjects | 2:f6265132c464 | 298 | |
TareObjects | 2:f6265132c464 | 299 | int execDigitalIn(int ch) |
TareObjects | 2:f6265132c464 | 300 | { |
TareObjects | 2:f6265132c464 | 301 | if (ch >= 0 && ch < kMaxDigitalInChannels) { |
TareObjects | 2:f6265132c464 | 302 | return digitalIn[ch]; |
TareObjects | 2:f6265132c464 | 303 | } |
TareObjects | 2:f6265132c464 | 304 | return -1; |
TareObjects | 2:f6265132c464 | 305 | } |
TareObjects | 2:f6265132c464 | 306 | |
TareObjects | 2:f6265132c464 | 307 | |
TareObjects | 2:f6265132c464 | 308 | |
TareObjects | 2:f6265132c464 | 309 | // |
TareObjects | 2:f6265132c464 | 310 | // Digital Write |
TareObjects | 2:f6265132c464 | 311 | // |
TareObjects | 2:f6265132c464 | 312 | |
TareObjects | 2:f6265132c464 | 313 | const int kMaxDigitalOutChannels = 2; |
TareObjects | 2:f6265132c464 | 314 | |
TareObjects | 2:f6265132c464 | 315 | DigitalOut digitalOut[kMaxDigitalOutChannels] = {dp26,dp28}; |
TareObjects | 2:f6265132c464 | 316 | |
TareObjects | 2:f6265132c464 | 317 | int execDigitalOut(int ch, long value) |
TareObjects | 2:f6265132c464 | 318 | { |
TareObjects | 2:f6265132c464 | 319 | if (ch >= 0 && ch < kMaxDigitalOutChannels) { |
TareObjects | 2:f6265132c464 | 320 | digitalOut[ch] = value == 0 ? 0 : 1; |
TareObjects | 2:f6265132c464 | 321 | return 0; |
TareObjects | 2:f6265132c464 | 322 | } |
TareObjects | 2:f6265132c464 | 323 | return -1; |
TareObjects | 2:f6265132c464 | 324 | } |
TareObjects | 2:f6265132c464 | 325 | |
TareObjects | 2:f6265132c464 | 326 | |
TareObjects | 2:f6265132c464 | 327 | // |
TareObjects | 2:f6265132c464 | 328 | // Pwm |
TareObjects | 2:f6265132c464 | 329 | // |
TareObjects | 2:f6265132c464 | 330 | |
TareObjects | 2:f6265132c464 | 331 | const int kMaxPwmChannels = 3; |
TareObjects | 2:f6265132c464 | 332 | |
TareObjects | 2:f6265132c464 | 333 | PwmOut Pwm[kMaxPwmChannels] = {dp1, dp2, dp18}; |
TareObjects | 2:f6265132c464 | 334 | |
TareObjects | 2:f6265132c464 | 335 | int execSetPwmPeriod(int ch, long value) |
TareObjects | 2:f6265132c464 | 336 | { |
TareObjects | 3:b7b9d3d4eed8 | 337 | if (ch >= 0 && ch < kMaxPwmChannels && value >= 0 && value <= kMaxInteger) { |
TareObjects | 2:f6265132c464 | 338 | Pwm[ch].period_us(value); |
TareObjects | 2:f6265132c464 | 339 | return 0; |
TareObjects | 2:f6265132c464 | 340 | } |
TareObjects | 2:f6265132c464 | 341 | return -1; |
TareObjects | 2:f6265132c464 | 342 | } |
TareObjects | 2:f6265132c464 | 343 | |
TareObjects | 2:f6265132c464 | 344 | int execPwmOut(int ch, long value) |
TareObjects | 2:f6265132c464 | 345 | { |
TareObjects | 3:b7b9d3d4eed8 | 346 | if (ch >= 0 && ch < kMaxPwmChannels) { |
TareObjects | 2:f6265132c464 | 347 | float f = value / fFloatScaleValue; |
TareObjects | 2:f6265132c464 | 348 | Pwm[ch] = f; |
TareObjects | 2:f6265132c464 | 349 | return 0; |
TareObjects | 2:f6265132c464 | 350 | } |
TareObjects | 2:f6265132c464 | 351 | |
TareObjects | 2:f6265132c464 | 352 | return -1; |
TareObjects | 2:f6265132c464 | 353 | } |
TareObjects | 2:f6265132c464 | 354 | |
TareObjects | 2:f6265132c464 | 355 | |
TareObjects | 2:f6265132c464 | 356 | |
TareObjects | 2:f6265132c464 | 357 | |
TareObjects | 2:f6265132c464 | 358 | // |
TareObjects | 2:f6265132c464 | 359 | // clock for RPi's timer and LED Flashing |
TareObjects | 2:f6265132c464 | 360 | // |
TareObjects | 2:f6265132c464 | 361 | Ticker second; |
TareObjects | 2:f6265132c464 | 362 | long onTimer = -1; |
TareObjects | 2:f6265132c464 | 363 | long offTimer = -1; |
TareObjects | 2:f6265132c464 | 364 | |
TareObjects | 2:f6265132c464 | 365 | void funcSecond() |
TareObjects | 2:f6265132c464 | 366 | { |
TareObjects | 1:10aa79121f61 | 367 | if (onTimer > 0) { |
TareObjects | 1:10aa79121f61 | 368 | onTimer --; |
TareObjects | 1:10aa79121f61 | 369 | if (onTimer == 0) { |
TareObjects | 1:10aa79121f61 | 370 | powerOn(); |
TareObjects | 2:f6265132c464 | 371 | onTimer = -1; |
TareObjects | 1:10aa79121f61 | 372 | } |
TareObjects | 2:f6265132c464 | 373 | } |
TareObjects | 1:10aa79121f61 | 374 | if (offTimer > 0) { |
TareObjects | 1:10aa79121f61 | 375 | offTimer --; |
TareObjects | 1:10aa79121f61 | 376 | if (offTimer == 0) { |
TareObjects | 1:10aa79121f61 | 377 | powerOff(); |
TareObjects | 2:f6265132c464 | 378 | offTimer = -1; |
TareObjects | 0:13c37793b4c6 | 379 | } |
TareObjects | 2:f6265132c464 | 380 | } |
TareObjects | 2:f6265132c464 | 381 | |
TareObjects | 0:13c37793b4c6 | 382 | if (ledMode == kLED_Flash) { |
TareObjects | 0:13c37793b4c6 | 383 | led = 1; |
TareObjects | 0:13c37793b4c6 | 384 | wait_ms(10); |
TareObjects | 0:13c37793b4c6 | 385 | led = 0; |
TareObjects | 2:f6265132c464 | 386 | } |
TareObjects | 0:13c37793b4c6 | 387 | } |
TareObjects | 0:13c37793b4c6 | 388 | |
TareObjects | 2:f6265132c464 | 389 | |
TareObjects | 2:f6265132c464 | 390 | |
TareObjects | 2:f6265132c464 | 391 | // |
TareObjects | 2:f6265132c464 | 392 | // main routine |
TareObjects | 2:f6265132c464 | 393 | // |
TareObjects | 2:f6265132c464 | 394 | |
TareObjects | 2:f6265132c464 | 395 | static char buf[MaxReceiveBufferSize]; |
TareObjects | 2:f6265132c464 | 396 | |
TareObjects | 0:13c37793b4c6 | 397 | int main() |
TareObjects | 0:13c37793b4c6 | 398 | { |
TareObjects | 2:f6265132c464 | 399 | char strBuf[16]; |
TareObjects | 2:f6265132c464 | 400 | char prevCommand = 0; |
TareObjects | 2:f6265132c464 | 401 | char prevCh = 0; |
TareObjects | 2:f6265132c464 | 402 | char prevMode = 0; |
TareObjects | 2:f6265132c464 | 403 | |
TareObjects | 2:f6265132c464 | 404 | for (int i = 0; i < kMaxAnalogChannels; i++) { |
TareObjects | 2:f6265132c464 | 405 | analogBuffer[i].count = 0; |
TareObjects | 2:f6265132c464 | 406 | } |
TareObjects | 2:f6265132c464 | 407 | |
TareObjects | 0:13c37793b4c6 | 408 | slave.address(kCommandAddress); |
TareObjects | 0:13c37793b4c6 | 409 | second.attach(funcSecond, 1); |
TareObjects | 2:f6265132c464 | 410 | |
TareObjects | 1:10aa79121f61 | 411 | sw1.mode(PullNone); |
TareObjects | 2:f6265132c464 | 412 | |
TareObjects | 0:13c37793b4c6 | 413 | powerOn(); |
TareObjects | 2:f6265132c464 | 414 | |
TareObjects | 0:13c37793b4c6 | 415 | while(1) { |
TareObjects | 1:10aa79121f61 | 416 | if (sw1 == SW_ON) { |
TareObjects | 1:10aa79121f61 | 417 | wait_ms(5); |
TareObjects | 1:10aa79121f61 | 418 | waitForSw1Release = 1; |
TareObjects | 1:10aa79121f61 | 419 | } |
TareObjects | 2:f6265132c464 | 420 | |
TareObjects | 1:10aa79121f61 | 421 | if (waitForSw1Release && sw1 == SW_OFF) { |
TareObjects | 1:10aa79121f61 | 422 | if (powerMode == kCommandUp) { |
TareObjects | 1:10aa79121f61 | 423 | powerOff(); |
TareObjects | 1:10aa79121f61 | 424 | } else { |
TareObjects | 1:10aa79121f61 | 425 | powerOn(); |
TareObjects | 1:10aa79121f61 | 426 | } |
TareObjects | 2:f6265132c464 | 427 | waitForSw1Release = 0; |
TareObjects | 1:10aa79121f61 | 428 | } |
TareObjects | 2:f6265132c464 | 429 | |
TareObjects | 0:13c37793b4c6 | 430 | int status = slave.receive(); |
TareObjects | 2:f6265132c464 | 431 | long value = 0; |
TareObjects | 0:13c37793b4c6 | 432 | switch (status) { |
TareObjects | 2:f6265132c464 | 433 | case I2CSlave::WriteAddressed: { |
TareObjects | 0:13c37793b4c6 | 434 | if (slave.read(buf, MaxReceiveBufferSize)) { |
TareObjects | 2:f6265132c464 | 435 | char command = buf[0]; |
TareObjects | 2:f6265132c464 | 436 | char ch = buf[1]; |
TareObjects | 2:f6265132c464 | 437 | char mode = buf[2]; |
TareObjects | 2:f6265132c464 | 438 | value = 0; |
TareObjects | 2:f6265132c464 | 439 | if (mode == kCommandModeBinary) { |
TareObjects | 2:f6265132c464 | 440 | long *p = (long *)buf+3; |
TareObjects | 2:f6265132c464 | 441 | value = *p; |
TareObjects | 2:f6265132c464 | 442 | if (value < 0) value = 0; |
TareObjects | 2:f6265132c464 | 443 | } else if (mode == kCommandModeASCII) { |
TareObjects | 2:f6265132c464 | 444 | value = atol(buf+3); |
TareObjects | 2:f6265132c464 | 445 | } |
TareObjects | 2:f6265132c464 | 446 | |
TareObjects | 2:f6265132c464 | 447 | switch(command) { |
TareObjects | 2:f6265132c464 | 448 | case kCommandUp: |
TareObjects | 2:f6265132c464 | 449 | if (value > 0) { |
TareObjects | 2:f6265132c464 | 450 | onTimer = value; |
TareObjects | 2:f6265132c464 | 451 | } else if (value == 0) { |
TareObjects | 2:f6265132c464 | 452 | powerOn(); |
TareObjects | 2:f6265132c464 | 453 | } |
TareObjects | 2:f6265132c464 | 454 | break; |
TareObjects | 2:f6265132c464 | 455 | |
TareObjects | 2:f6265132c464 | 456 | case kCommandDown: |
TareObjects | 2:f6265132c464 | 457 | if (value > 0) { |
TareObjects | 2:f6265132c464 | 458 | offTimer = value; |
TareObjects | 2:f6265132c464 | 459 | } else if (value == 0) { |
TareObjects | 2:f6265132c464 | 460 | powerOff(); |
TareObjects | 2:f6265132c464 | 461 | } |
TareObjects | 2:f6265132c464 | 462 | break; |
TareObjects | 2:f6265132c464 | 463 | case kCommandLED: |
TareObjects | 2:f6265132c464 | 464 | switch (mode) { |
TareObjects | 2:f6265132c464 | 465 | case 0: |
TareObjects | 2:f6265132c464 | 466 | ledOff(); |
TareObjects | 2:f6265132c464 | 467 | break; |
TareObjects | 2:f6265132c464 | 468 | case 1: |
TareObjects | 2:f6265132c464 | 469 | ledOn(); |
TareObjects | 2:f6265132c464 | 470 | break; |
TareObjects | 2:f6265132c464 | 471 | case 2: |
TareObjects | 2:f6265132c464 | 472 | ledMode = kLED_Flash; |
TareObjects | 2:f6265132c464 | 473 | break; |
TareObjects | 2:f6265132c464 | 474 | } |
TareObjects | 2:f6265132c464 | 475 | break; |
TareObjects | 2:f6265132c464 | 476 | |
TareObjects | 2:f6265132c464 | 477 | case kCommandAnalogReset: |
TareObjects | 2:f6265132c464 | 478 | execAnalogReset(ch); |
TareObjects | 2:f6265132c464 | 479 | break; |
TareObjects | 2:f6265132c464 | 480 | |
TareObjects | 2:f6265132c464 | 481 | case kCommandAnalogStart: |
TareObjects | 2:f6265132c464 | 482 | execAnalogStart(ch, value); |
TareObjects | 2:f6265132c464 | 483 | break; |
TareObjects | 2:f6265132c464 | 484 | |
TareObjects | 2:f6265132c464 | 485 | case kCommandAnalogStop: |
TareObjects | 2:f6265132c464 | 486 | execAnalogStop(ch); |
TareObjects | 2:f6265132c464 | 487 | break; |
TareObjects | 2:f6265132c464 | 488 | |
TareObjects | 2:f6265132c464 | 489 | case kCommandPwm: |
TareObjects | 2:f6265132c464 | 490 | execPwmOut(ch, value); |
TareObjects | 2:f6265132c464 | 491 | break; |
TareObjects | 2:f6265132c464 | 492 | |
TareObjects | 2:f6265132c464 | 493 | case kCommandPwmPeriod: |
TareObjects | 2:f6265132c464 | 494 | execSetPwmPeriod(ch, value); |
TareObjects | 2:f6265132c464 | 495 | break; |
TareObjects | 2:f6265132c464 | 496 | |
TareObjects | 2:f6265132c464 | 497 | case kCommandDigitalOut: |
TareObjects | 2:f6265132c464 | 498 | execDigitalOut(ch, value); |
TareObjects | 2:f6265132c464 | 499 | break; |
TareObjects | 2:f6265132c464 | 500 | |
TareObjects | 2:f6265132c464 | 501 | case kCommandDigitalIn: |
TareObjects | 2:f6265132c464 | 502 | case kCommandAnalogRead: |
TareObjects | 2:f6265132c464 | 503 | case kCommandAnalogLoad: |
TareObjects | 2:f6265132c464 | 504 | ledOn(); |
TareObjects | 2:f6265132c464 | 505 | prevCommand = command; |
TareObjects | 2:f6265132c464 | 506 | prevCh = ch; |
TareObjects | 2:f6265132c464 | 507 | prevMode = mode; |
TareObjects | 2:f6265132c464 | 508 | break; |
TareObjects | 0:13c37793b4c6 | 509 | |
TareObjects | 2:f6265132c464 | 510 | default: |
TareObjects | 2:f6265132c464 | 511 | prevCommand = 0; |
TareObjects | 2:f6265132c464 | 512 | break; |
TareObjects | 2:f6265132c464 | 513 | |
TareObjects | 0:13c37793b4c6 | 514 | } |
TareObjects | 0:13c37793b4c6 | 515 | } |
TareObjects | 0:13c37793b4c6 | 516 | break; |
TareObjects | 2:f6265132c464 | 517 | } |
TareObjects | 2:f6265132c464 | 518 | |
TareObjects | 2:f6265132c464 | 519 | case I2CSlave::ReadAddressed: { |
TareObjects | 2:f6265132c464 | 520 | |
TareObjects | 2:f6265132c464 | 521 | switch(prevCommand) { |
TareObjects | 2:f6265132c464 | 522 | case kCommandAnalogRead: |
TareObjects | 2:f6265132c464 | 523 | float f = execAnalogIn(prevCh); |
TareObjects | 2:f6265132c464 | 524 | if (prevMode == kCommandModeBinary) { |
TareObjects | 2:f6265132c464 | 525 | value = f * fFloatScaleValue; // 10bit = 1024.0 |
TareObjects | 2:f6265132c464 | 526 | slave.write((const char *)&value, sizeof(value)); |
TareObjects | 2:f6265132c464 | 527 | } else { |
TareObjects | 2:f6265132c464 | 528 | sprintf(strBuf, "%f", f); |
TareObjects | 2:f6265132c464 | 529 | slave.write(strBuf, strlen(strBuf)+1); |
TareObjects | 2:f6265132c464 | 530 | } |
TareObjects | 2:f6265132c464 | 531 | break; |
TareObjects | 2:f6265132c464 | 532 | |
TareObjects | 2:f6265132c464 | 533 | case kCommandAnalogLoad: // no ascii mode |
TareObjects | 2:f6265132c464 | 534 | if (prevCh < kMaxAnalogChannels) { |
TareObjects | 2:f6265132c464 | 535 | AnalogBuffer *p = &analogBuffer[prevCh]; |
TareObjects | 2:f6265132c464 | 536 | // __disable_irq(); |
TareObjects | 2:f6265132c464 | 537 | unsigned short count = p->count; |
TareObjects | 2:f6265132c464 | 538 | slave.write((const char *)p, sizeof(unsigned short)*(count+1)); |
TareObjects | 2:f6265132c464 | 539 | // __enable_irq(); |
TareObjects | 2:f6265132c464 | 540 | p->count = 0; |
TareObjects | 2:f6265132c464 | 541 | } |
TareObjects | 2:f6265132c464 | 542 | break; |
TareObjects | 2:f6265132c464 | 543 | |
TareObjects | 2:f6265132c464 | 544 | |
TareObjects | 2:f6265132c464 | 545 | case kCommandDigitalIn: |
TareObjects | 2:f6265132c464 | 546 | value = execDigitalIn(prevCh); |
TareObjects | 2:f6265132c464 | 547 | if (prevMode == kCommandModeBinary) { |
TareObjects | 2:f6265132c464 | 548 | slave.write((const char *)&value, sizeof(value)); |
TareObjects | 2:f6265132c464 | 549 | } else { |
TareObjects | 2:f6265132c464 | 550 | sprintf(strBuf, "%ld", value); |
TareObjects | 2:f6265132c464 | 551 | slave.write((const char *)strBuf, strlen(strBuf)+1); |
TareObjects | 2:f6265132c464 | 552 | } |
TareObjects | 2:f6265132c464 | 553 | break; |
TareObjects | 2:f6265132c464 | 554 | } |
TareObjects | 2:f6265132c464 | 555 | slave.stop(); |
TareObjects | 2:f6265132c464 | 556 | break; |
TareObjects | 2:f6265132c464 | 557 | } |
TareObjects | 0:13c37793b4c6 | 558 | |
TareObjects | 0:13c37793b4c6 | 559 | default: |
TareObjects | 0:13c37793b4c6 | 560 | wait_ms(1); |
TareObjects | 0:13c37793b4c6 | 561 | break; |
TareObjects | 0:13c37793b4c6 | 562 | } |
TareObjects | 0:13c37793b4c6 | 563 | } |
TareObjects | 2:f6265132c464 | 564 | } |