external UI solar on foils
Dependencies: mbed
Fork of CAN_module_ext_UI by
main.cpp@0:877c5ece70ac, 2015-08-11 (annotated)
- Committer:
- Dannis_mbed
- Date:
- Tue Aug 11 08:46:16 2015 +0000
- Revision:
- 0:877c5ece70ac
presentation version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dannis_mbed | 0:877c5ece70ac | 1 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 2 | // // |
Dannis_mbed | 0:877c5ece70ac | 3 | // File : main.cpp // |
Dannis_mbed | 0:877c5ece70ac | 4 | // Version : 0.1 // |
Dannis_mbed | 0:877c5ece70ac | 5 | // Initial : 17 June 2015 // |
Dannis_mbed | 0:877c5ece70ac | 6 | // Author : Dany Brugman // |
Dannis_mbed | 0:877c5ece70ac | 7 | // Comment : Extern UI using CANbus // |
Dannis_mbed | 0:877c5ece70ac | 8 | // // |
Dannis_mbed | 0:877c5ece70ac | 9 | // // |
Dannis_mbed | 0:877c5ece70ac | 10 | // Changelog : // |
Dannis_mbed | 0:877c5ece70ac | 11 | // Date: Name: Comment: // |
Dannis_mbed | 0:877c5ece70ac | 12 | // 17/06/2015 DNB First version // |
Dannis_mbed | 0:877c5ece70ac | 13 | // // |
Dannis_mbed | 0:877c5ece70ac | 14 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 15 | |
Dannis_mbed | 0:877c5ece70ac | 16 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 17 | // includes // |
Dannis_mbed | 0:877c5ece70ac | 18 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 19 | #include "mbed.h" |
Dannis_mbed | 0:877c5ece70ac | 20 | #include "PinDetect.h" |
Dannis_mbed | 0:877c5ece70ac | 21 | #include "ExtUI.h" |
Dannis_mbed | 0:877c5ece70ac | 22 | |
Dannis_mbed | 0:877c5ece70ac | 23 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 24 | // defines // |
Dannis_mbed | 0:877c5ece70ac | 25 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 26 | #define DEVICE_ID 20 |
Dannis_mbed | 0:877c5ece70ac | 27 | #define ACTIVE_ACK 255 |
Dannis_mbed | 0:877c5ece70ac | 28 | #define NEW_HEIGHT 1020 |
Dannis_mbed | 0:877c5ece70ac | 29 | #define REQ_STATUS 2006 |
Dannis_mbed | 0:877c5ece70ac | 30 | |
Dannis_mbed | 0:877c5ece70ac | 31 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 32 | // interrupts // |
Dannis_mbed | 0:877c5ece70ac | 33 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 34 | Ticker ticker; |
Dannis_mbed | 0:877c5ece70ac | 35 | Ticker tActiveLedOn; |
Dannis_mbed | 0:877c5ece70ac | 36 | Ticker tled2on; |
Dannis_mbed | 0:877c5ece70ac | 37 | |
Dannis_mbed | 0:877c5ece70ac | 38 | Timeout tActiveLedOff; |
Dannis_mbed | 0:877c5ece70ac | 39 | Timeout tled2off; |
Dannis_mbed | 0:877c5ece70ac | 40 | |
Dannis_mbed | 0:877c5ece70ac | 41 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 42 | // port declaration // |
Dannis_mbed | 0:877c5ece70ac | 43 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 44 | DigitalOut led1(LED1); |
Dannis_mbed | 0:877c5ece70ac | 45 | DigitalOut led2(LED2); |
Dannis_mbed | 0:877c5ece70ac | 46 | DigitalOut led4(LED4); |
Dannis_mbed | 0:877c5ece70ac | 47 | DigitalOut activeLed(p26); |
Dannis_mbed | 0:877c5ece70ac | 48 | DigitalOut emergencyLed(p25); |
Dannis_mbed | 0:877c5ece70ac | 49 | |
Dannis_mbed | 0:877c5ece70ac | 50 | //BusOut heightLeds (p13, p14, p5, p6, p7); |
Dannis_mbed | 0:877c5ece70ac | 51 | |
Dannis_mbed | 0:877c5ece70ac | 52 | //PwmOut Red(p21); |
Dannis_mbed | 0:877c5ece70ac | 53 | //PwmOut Green(p22); |
Dannis_mbed | 0:877c5ece70ac | 54 | //PwmOut Blue(p23); |
Dannis_mbed | 0:877c5ece70ac | 55 | |
Dannis_mbed | 0:877c5ece70ac | 56 | CAN CANBus(p30, p29); |
Dannis_mbed | 0:877c5ece70ac | 57 | |
Dannis_mbed | 0:877c5ece70ac | 58 | PinDetect SW_UP( p12 ); |
Dannis_mbed | 0:877c5ece70ac | 59 | PinDetect SW_DWN( p8 ); |
Dannis_mbed | 0:877c5ece70ac | 60 | PinDetect SW_MFB( p11 ); |
Dannis_mbed | 0:877c5ece70ac | 61 | |
Dannis_mbed | 0:877c5ece70ac | 62 | ExtUI eSOFUI; |
Dannis_mbed | 0:877c5ece70ac | 63 | |
Dannis_mbed | 0:877c5ece70ac | 64 | Serial debug(USBTX, USBRX); |
Dannis_mbed | 0:877c5ece70ac | 65 | |
Dannis_mbed | 0:877c5ece70ac | 66 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 67 | // global variables // |
Dannis_mbed | 0:877c5ece70ac | 68 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 69 | char counter = 0; |
Dannis_mbed | 0:877c5ece70ac | 70 | char cMessage, cStatus; |
Dannis_mbed | 0:877c5ece70ac | 71 | |
Dannis_mbed | 0:877c5ece70ac | 72 | int i; |
Dannis_mbed | 0:877c5ece70ac | 73 | int iHeight = 0; |
Dannis_mbed | 0:877c5ece70ac | 74 | |
Dannis_mbed | 0:877c5ece70ac | 75 | //const uint32_t cuLut[6] = {0x00, 0x01, 0x02, 0x04, 0x08, 0x10}; /moved to class |
Dannis_mbed | 0:877c5ece70ac | 76 | |
Dannis_mbed | 0:877c5ece70ac | 77 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 78 | // functions declarations // |
Dannis_mbed | 0:877c5ece70ac | 79 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 80 | |
Dannis_mbed | 0:877c5ece70ac | 81 | |
Dannis_mbed | 0:877c5ece70ac | 82 | |
Dannis_mbed | 0:877c5ece70ac | 83 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 84 | // functions // |
Dannis_mbed | 0:877c5ece70ac | 85 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 86 | |
Dannis_mbed | 0:877c5ece70ac | 87 | |
Dannis_mbed | 0:877c5ece70ac | 88 | |
Dannis_mbed | 0:877c5ece70ac | 89 | void send() |
Dannis_mbed | 0:877c5ece70ac | 90 | { |
Dannis_mbed | 0:877c5ece70ac | 91 | if(CANBus.write(CANMessage(1226, &counter, 1))) |
Dannis_mbed | 0:877c5ece70ac | 92 | { |
Dannis_mbed | 0:877c5ece70ac | 93 | counter++; |
Dannis_mbed | 0:877c5ece70ac | 94 | } |
Dannis_mbed | 0:877c5ece70ac | 95 | } |
Dannis_mbed | 0:877c5ece70ac | 96 | |
Dannis_mbed | 0:877c5ece70ac | 97 | /*void heightIncrease(void) |
Dannis_mbed | 0:877c5ece70ac | 98 | { |
Dannis_mbed | 0:877c5ece70ac | 99 | if (iHeight < 5) iHeight++; |
Dannis_mbed | 0:877c5ece70ac | 100 | heightLeds.write(cuLut[iHeight]); |
Dannis_mbed | 0:877c5ece70ac | 101 | } |
Dannis_mbed | 0:877c5ece70ac | 102 | |
Dannis_mbed | 0:877c5ece70ac | 103 | void heightDecrease(void) |
Dannis_mbed | 0:877c5ece70ac | 104 | { |
Dannis_mbed | 0:877c5ece70ac | 105 | if (iHeight != 0) iHeight--; |
Dannis_mbed | 0:877c5ece70ac | 106 | heightLeds.write(cuLut[iHeight]); |
Dannis_mbed | 0:877c5ece70ac | 107 | }*/ |
Dannis_mbed | 0:877c5ece70ac | 108 | |
Dannis_mbed | 0:877c5ece70ac | 109 | void switchUpPressed(void){ |
Dannis_mbed | 0:877c5ece70ac | 110 | eSOFUI.heightIncrease(); |
Dannis_mbed | 0:877c5ece70ac | 111 | debug.printf("\033[10;10fTarget height:%i\r\n", eSOFUI.getTargetHeight()); |
Dannis_mbed | 0:877c5ece70ac | 112 | } |
Dannis_mbed | 0:877c5ece70ac | 113 | |
Dannis_mbed | 0:877c5ece70ac | 114 | void switchDownPressed(void){ |
Dannis_mbed | 0:877c5ece70ac | 115 | eSOFUI.heightDecrease(); |
Dannis_mbed | 0:877c5ece70ac | 116 | debug.printf("\033[10;10fTarget height:%i\r\n", eSOFUI.getTargetHeight()); |
Dannis_mbed | 0:877c5ece70ac | 117 | } |
Dannis_mbed | 0:877c5ece70ac | 118 | |
Dannis_mbed | 0:877c5ece70ac | 119 | void switchPressed(void){ |
Dannis_mbed | 0:877c5ece70ac | 120 | //test(); |
Dannis_mbed | 0:877c5ece70ac | 121 | //cMessage = 0xFF; |
Dannis_mbed | 0:877c5ece70ac | 122 | //CANBus.write(CANMessage(100, &cMessage, 1)); |
Dannis_mbed | 0:877c5ece70ac | 123 | //debug.printf("Switch pressed: %d \r\n", cMessage); |
Dannis_mbed | 0:877c5ece70ac | 124 | } |
Dannis_mbed | 0:877c5ece70ac | 125 | |
Dannis_mbed | 0:877c5ece70ac | 126 | void switchReleased(void){ |
Dannis_mbed | 0:877c5ece70ac | 127 | cMessage = 0x00; |
Dannis_mbed | 0:877c5ece70ac | 128 | CANBus.write(CANMessage(100, &cMessage, 1)); |
Dannis_mbed | 0:877c5ece70ac | 129 | //debug.printf("Switch released: %d \r\n", cMessage); |
Dannis_mbed | 0:877c5ece70ac | 130 | } |
Dannis_mbed | 0:877c5ece70ac | 131 | |
Dannis_mbed | 0:877c5ece70ac | 132 | |
Dannis_mbed | 0:877c5ece70ac | 133 | |
Dannis_mbed | 0:877c5ece70ac | 134 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 135 | // Blink functions // |
Dannis_mbed | 0:877c5ece70ac | 136 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 137 | void tActiveLedOff_cb(void) |
Dannis_mbed | 0:877c5ece70ac | 138 | { |
Dannis_mbed | 0:877c5ece70ac | 139 | activeLed = 0; |
Dannis_mbed | 0:877c5ece70ac | 140 | } |
Dannis_mbed | 0:877c5ece70ac | 141 | |
Dannis_mbed | 0:877c5ece70ac | 142 | void tActiveLedOn_cb(void) |
Dannis_mbed | 0:877c5ece70ac | 143 | { |
Dannis_mbed | 0:877c5ece70ac | 144 | activeLed = 1; |
Dannis_mbed | 0:877c5ece70ac | 145 | tActiveLedOff.detach(); |
Dannis_mbed | 0:877c5ece70ac | 146 | tActiveLedOff.attach(&tActiveLedOff_cb, 0.05); |
Dannis_mbed | 0:877c5ece70ac | 147 | } |
Dannis_mbed | 0:877c5ece70ac | 148 | |
Dannis_mbed | 0:877c5ece70ac | 149 | void tled2off_cb(void) |
Dannis_mbed | 0:877c5ece70ac | 150 | { |
Dannis_mbed | 0:877c5ece70ac | 151 | //Green = 0.0f; |
Dannis_mbed | 0:877c5ece70ac | 152 | led1 = 0; |
Dannis_mbed | 0:877c5ece70ac | 153 | } |
Dannis_mbed | 0:877c5ece70ac | 154 | |
Dannis_mbed | 0:877c5ece70ac | 155 | void tled2on_cb(void) |
Dannis_mbed | 0:877c5ece70ac | 156 | { |
Dannis_mbed | 0:877c5ece70ac | 157 | //Green = 0.1f; |
Dannis_mbed | 0:877c5ece70ac | 158 | led1 = 1; |
Dannis_mbed | 0:877c5ece70ac | 159 | tled2off.detach(); |
Dannis_mbed | 0:877c5ece70ac | 160 | tled2off.attach(&tled2off_cb, 0.05); |
Dannis_mbed | 0:877c5ece70ac | 161 | } |
Dannis_mbed | 0:877c5ece70ac | 162 | |
Dannis_mbed | 0:877c5ece70ac | 163 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 164 | // main loop // |
Dannis_mbed | 0:877c5ece70ac | 165 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 166 | int main() { |
Dannis_mbed | 0:877c5ece70ac | 167 | debug.printf("\033[2J"); |
Dannis_mbed | 0:877c5ece70ac | 168 | debug.printf("\033[9;10fDevice ID:%i\r\n", DEVICE_ID); |
Dannis_mbed | 0:877c5ece70ac | 169 | |
Dannis_mbed | 0:877c5ece70ac | 170 | activeLed = 1; |
Dannis_mbed | 0:877c5ece70ac | 171 | //Green = 0.2f; |
Dannis_mbed | 0:877c5ece70ac | 172 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 173 | // schedule led events // |
Dannis_mbed | 0:877c5ece70ac | 174 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 175 | tActiveLedOff.attach(&tActiveLedOn_cb, 0.05); |
Dannis_mbed | 0:877c5ece70ac | 176 | tActiveLedOn.attach(&tActiveLedOn_cb, 2); |
Dannis_mbed | 0:877c5ece70ac | 177 | tled2off.attach(&tled2on_cb, 0.05); |
Dannis_mbed | 0:877c5ece70ac | 178 | tled2on.attach(&tled2on_cb, 1); |
Dannis_mbed | 0:877c5ece70ac | 179 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 180 | // switch interrupts // |
Dannis_mbed | 0:877c5ece70ac | 181 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 182 | // MFB switch |
Dannis_mbed | 0:877c5ece70ac | 183 | SW_MFB.mode( PullDown ); |
Dannis_mbed | 0:877c5ece70ac | 184 | SW_MFB.attach_asserted( &switchPressed ); |
Dannis_mbed | 0:877c5ece70ac | 185 | SW_MFB.attach_deasserted( &switchReleased ); |
Dannis_mbed | 0:877c5ece70ac | 186 | SW_MFB.setSampleFrequency(); // Defaults to 20ms. |
Dannis_mbed | 0:877c5ece70ac | 187 | // UP switch |
Dannis_mbed | 0:877c5ece70ac | 188 | SW_UP.mode( PullDown ); |
Dannis_mbed | 0:877c5ece70ac | 189 | SW_UP.attach_asserted( &switchUpPressed ); |
Dannis_mbed | 0:877c5ece70ac | 190 | SW_UP.setSampleFrequency(); // Defaults to 20ms. |
Dannis_mbed | 0:877c5ece70ac | 191 | // DOWN switch |
Dannis_mbed | 0:877c5ece70ac | 192 | SW_DWN.mode( PullDown ); |
Dannis_mbed | 0:877c5ece70ac | 193 | SW_DWN.attach_asserted( &switchDownPressed ); |
Dannis_mbed | 0:877c5ece70ac | 194 | SW_DWN.setSampleFrequency(); // Defaults to 20ms. |
Dannis_mbed | 0:877c5ece70ac | 195 | |
Dannis_mbed | 0:877c5ece70ac | 196 | //Red.period_ms(10); |
Dannis_mbed | 0:877c5ece70ac | 197 | //Green.period_ms(10); |
Dannis_mbed | 0:877c5ece70ac | 198 | //Blue.period_ms(10); |
Dannis_mbed | 0:877c5ece70ac | 199 | |
Dannis_mbed | 0:877c5ece70ac | 200 | CANBus.frequency(250000); |
Dannis_mbed | 0:877c5ece70ac | 201 | CANMessage msg; |
Dannis_mbed | 0:877c5ece70ac | 202 | //char Value; |
Dannis_mbed | 0:877c5ece70ac | 203 | |
Dannis_mbed | 0:877c5ece70ac | 204 | //Blue = 0.4f; |
Dannis_mbed | 0:877c5ece70ac | 205 | //Red = 0.4f; |
Dannis_mbed | 0:877c5ece70ac | 206 | //Green = 0.2f; |
Dannis_mbed | 0:877c5ece70ac | 207 | |
Dannis_mbed | 0:877c5ece70ac | 208 | while(1) |
Dannis_mbed | 0:877c5ece70ac | 209 | { |
Dannis_mbed | 0:877c5ece70ac | 210 | if(CANBus.read(msg)) |
Dannis_mbed | 0:877c5ece70ac | 211 | { |
Dannis_mbed | 0:877c5ece70ac | 212 | switch(msg.id) |
Dannis_mbed | 0:877c5ece70ac | 213 | { // Send system status if MainModule requests |
Dannis_mbed | 0:877c5ece70ac | 214 | case REQ_STATUS: |
Dannis_mbed | 0:877c5ece70ac | 215 | cStatus = 0xFF; // OK |
Dannis_mbed | 0:877c5ece70ac | 216 | if(msg.data[0] == 00) CANBus.write(CANMessage(DEVICE_ID, &cStatus, 1)); |
Dannis_mbed | 0:877c5ece70ac | 217 | wait_us(100); |
Dannis_mbed | 0:877c5ece70ac | 218 | break; |
Dannis_mbed | 0:877c5ece70ac | 219 | // process stepper data direction/steps |
Dannis_mbed | 0:877c5ece70ac | 220 | case DEVICE_ID: |
Dannis_mbed | 0:877c5ece70ac | 221 | if(msg.data[0] != 00) |
Dannis_mbed | 0:877c5ece70ac | 222 | { |
Dannis_mbed | 0:877c5ece70ac | 223 | tActiveLedOn.attach(&tActiveLedOn_cb, 0.5); |
Dannis_mbed | 0:877c5ece70ac | 224 | } |
Dannis_mbed | 0:877c5ece70ac | 225 | cStatus = ACTIVE_ACK; |
Dannis_mbed | 0:877c5ece70ac | 226 | CANBus.write(CANMessage(DEVICE_ID, &cStatus, 1)); // reply message received and executed |
Dannis_mbed | 0:877c5ece70ac | 227 | tActiveLedOn.attach(&tActiveLedOn_cb, 2); // indicate active |
Dannis_mbed | 0:877c5ece70ac | 228 | break; |
Dannis_mbed | 0:877c5ece70ac | 229 | |
Dannis_mbed | 0:877c5ece70ac | 230 | case NEW_HEIGHT: |
Dannis_mbed | 0:877c5ece70ac | 231 | eSOFUI.setTargetHeight((uint32_t) msg.data[0]); |
Dannis_mbed | 0:877c5ece70ac | 232 | |
Dannis_mbed | 0:877c5ece70ac | 233 | // just dump received message (debug) |
Dannis_mbed | 0:877c5ece70ac | 234 | default: |
Dannis_mbed | 0:877c5ece70ac | 235 | debug.printf("Message id: %d\r\n", msg.id); |
Dannis_mbed | 0:877c5ece70ac | 236 | debug.printf("Message length: %d\r\n", msg.len); |
Dannis_mbed | 0:877c5ece70ac | 237 | for (i=0; i < msg.len; i++) |
Dannis_mbed | 0:877c5ece70ac | 238 | { |
Dannis_mbed | 0:877c5ece70ac | 239 | debug.printf("Message %d received: %d\r\n", i, msg.data[i]); |
Dannis_mbed | 0:877c5ece70ac | 240 | } |
Dannis_mbed | 0:877c5ece70ac | 241 | break; |
Dannis_mbed | 0:877c5ece70ac | 242 | } |
Dannis_mbed | 0:877c5ece70ac | 243 | } |
Dannis_mbed | 0:877c5ece70ac | 244 | wait_ms(500); |
Dannis_mbed | 0:877c5ece70ac | 245 | } |
Dannis_mbed | 0:877c5ece70ac | 246 | } |
Dannis_mbed | 0:877c5ece70ac | 247 | |
Dannis_mbed | 0:877c5ece70ac | 248 | ////////////////////////////////////////////////////////////////////////////////////// |
Dannis_mbed | 0:877c5ece70ac | 249 | // EOF // |
Dannis_mbed | 0:877c5ece70ac | 250 | ////////////////////////////////////////////////////////////////////////////////////// |