gbgfa
Dependencies: mbed-rtos mbed EthernetInterface WebSocketClient
main.cpp@0:1be517779135, 2017-02-12 (annotated)
- Committer:
- ABuche
- Date:
- Sun Feb 12 22:19:36 2017 +0000
- Revision:
- 0:1be517779135
- Child:
- 1:446da73e1118
Coord
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ABuche | 0:1be517779135 | 1 | #include "mbed.h" |
ABuche | 0:1be517779135 | 2 | #include "rtos.h" |
ABuche | 0:1be517779135 | 3 | |
ABuche | 0:1be517779135 | 4 | LocalFileSystem local("local"); |
ABuche | 0:1be517779135 | 5 | |
ABuche | 0:1be517779135 | 6 | DigitalOut led1(LED1); |
ABuche | 0:1be517779135 | 7 | DigitalOut reset(p8); |
ABuche | 0:1be517779135 | 8 | RawSerial xbee(p13, p14); |
ABuche | 0:1be517779135 | 9 | |
ABuche | 0:1be517779135 | 10 | RawSerial pc(USBTX, USBRX); |
ABuche | 0:1be517779135 | 11 | |
ABuche | 0:1be517779135 | 12 | Thread* thread1; |
ABuche | 0:1be517779135 | 13 | Thread* thread2; |
ABuche | 0:1be517779135 | 14 | Thread* thread3; |
ABuche | 0:1be517779135 | 15 | |
ABuche | 0:1be517779135 | 16 | char LED_Toggle[] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x00, 0x00, |
ABuche | 0:1be517779135 | 17 | 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x02, 0x44, 0x30, 0x00, 0x25}; //Change index 13 & 14 & 18 & 19 |
ABuche | 0:1be517779135 | 18 | |
ABuche | 0:1be517779135 | 19 | char AT_command[] = {0x7E, 0x00, 0x06, 0x09, 0x01, 0x49 , 0x44, 0x00, 0x00, 0x00}; // +DATA + CS (7 & 8) |
ABuche | 0:1be517779135 | 20 | const char AT_WR[] = {0x7E, 0x00, 0x04, 0x09, 0x01, 0x57, 0x52, 0x4C}; |
ABuche | 0:1be517779135 | 21 | const char AT_AC[] = {0x7E, 0x00, 0x04, 0x09, 0x01, 0x41, 0x43, 0x71}; |
ABuche | 0:1be517779135 | 22 | |
ABuche | 0:1be517779135 | 23 | typedef struct { |
ABuche | 0:1be517779135 | 24 | char msg[25]; |
ABuche | 0:1be517779135 | 25 | } message; |
ABuche | 0:1be517779135 | 26 | |
ABuche | 0:1be517779135 | 27 | typedef struct { |
ABuche | 0:1be517779135 | 28 | char al; |
ABuche | 0:1be517779135 | 29 | char ah; |
ABuche | 0:1be517779135 | 30 | } address; |
ABuche | 0:1be517779135 | 31 | |
ABuche | 0:1be517779135 | 32 | address addresses[3]; |
ABuche | 0:1be517779135 | 33 | char addressCounter = 0; |
ABuche | 0:1be517779135 | 34 | |
ABuche | 0:1be517779135 | 35 | Queue<message, 25> queue; |
ABuche | 0:1be517779135 | 36 | MemoryPool<message, 25> mPool; |
ABuche | 0:1be517779135 | 37 | |
ABuche | 0:1be517779135 | 38 | char checksum(char array[], char length){ |
ABuche | 0:1be517779135 | 39 | char cs = 0; |
ABuche | 0:1be517779135 | 40 | for (int i = 3;i<length - 1;i++){ |
ABuche | 0:1be517779135 | 41 | cs += array[i]; |
ABuche | 0:1be517779135 | 42 | } |
ABuche | 0:1be517779135 | 43 | return 0xFF - cs; |
ABuche | 0:1be517779135 | 44 | } |
ABuche | 0:1be517779135 | 45 | |
ABuche | 0:1be517779135 | 46 | void hotdog1(){ |
ABuche | 0:1be517779135 | 47 | while(1) { |
ABuche | 0:1be517779135 | 48 | message* msg = mPool.alloc(); |
ABuche | 0:1be517779135 | 49 | |
ABuche | 0:1be517779135 | 50 | msg->msg[0] = xbee.getc(); |
ABuche | 0:1be517779135 | 51 | |
ABuche | 0:1be517779135 | 52 | if(msg->msg[0] == 0x7E){ |
ABuche | 0:1be517779135 | 53 | msg->msg[1] = xbee.getc(); |
ABuche | 0:1be517779135 | 54 | msg->msg[2] = xbee.getc(); |
ABuche | 0:1be517779135 | 55 | |
ABuche | 0:1be517779135 | 56 | for (int i = 3;i < msg->msg[2] + 4;i++){ |
ABuche | 0:1be517779135 | 57 | msg->msg[i] = xbee.getc(); |
ABuche | 0:1be517779135 | 58 | } |
ABuche | 0:1be517779135 | 59 | |
ABuche | 0:1be517779135 | 60 | queue.put(msg); |
ABuche | 0:1be517779135 | 61 | } |
ABuche | 0:1be517779135 | 62 | |
ABuche | 0:1be517779135 | 63 | } |
ABuche | 0:1be517779135 | 64 | } |
ABuche | 0:1be517779135 | 65 | |
ABuche | 0:1be517779135 | 66 | void traitement() { |
ABuche | 0:1be517779135 | 67 | while(1) { |
ABuche | 0:1be517779135 | 68 | message* msg = (message*)queue.get().value.p; |
ABuche | 0:1be517779135 | 69 | |
ABuche | 0:1be517779135 | 70 | if(msg->msg[2] >= 0x0C && msg->msg[3] == 0x90){ |
ABuche | 0:1be517779135 | 71 | bool exists = false; |
ABuche | 0:1be517779135 | 72 | |
ABuche | 0:1be517779135 | 73 | for (int i = 0; i< sizeof(addresses);i++){ |
ABuche | 0:1be517779135 | 74 | if(addresses[i].ah == msg->msg[12] && addresses[i].al == msg->msg[13]){ |
ABuche | 0:1be517779135 | 75 | exists = true; |
ABuche | 0:1be517779135 | 76 | break; |
ABuche | 0:1be517779135 | 77 | } |
ABuche | 0:1be517779135 | 78 | } |
ABuche | 0:1be517779135 | 79 | |
ABuche | 0:1be517779135 | 80 | if (!exists) { |
ABuche | 0:1be517779135 | 81 | addresses[addressCounter].ah = msg->msg[12]; |
ABuche | 0:1be517779135 | 82 | addresses[addressCounter].al = msg->msg[13]; |
ABuche | 0:1be517779135 | 83 | pc.printf("New address: %02x %02x", addresses[addressCounter].ah, addresses[addressCounter].al); |
ABuche | 0:1be517779135 | 84 | addressCounter++; |
ABuche | 0:1be517779135 | 85 | } |
ABuche | 0:1be517779135 | 86 | |
ABuche | 0:1be517779135 | 87 | //pc.printf("%02x ", msg->msg[15]); |
ABuche | 0:1be517779135 | 88 | for(int i = 15; i < msg->msg[2] + 3;i++){ |
ABuche | 0:1be517779135 | 89 | pc.printf("%02x ", msg->msg[i]); |
ABuche | 0:1be517779135 | 90 | } |
ABuche | 0:1be517779135 | 91 | pc.printf("\r\n"); |
ABuche | 0:1be517779135 | 92 | } |
ABuche | 0:1be517779135 | 93 | mPool.free(msg); |
ABuche | 0:1be517779135 | 94 | } |
ABuche | 0:1be517779135 | 95 | } |
ABuche | 0:1be517779135 | 96 | |
ABuche | 0:1be517779135 | 97 | void flashLED() { |
ABuche | 0:1be517779135 | 98 | bool high = true; |
ABuche | 0:1be517779135 | 99 | while(1){ |
ABuche | 0:1be517779135 | 100 | Thread::signal_wait(0x1); |
ABuche | 0:1be517779135 | 101 | |
ABuche | 0:1be517779135 | 102 | for (int i = 0; i < addressCounter;i++){ |
ABuche | 0:1be517779135 | 103 | LED_Toggle[13] = addresses[i].ah; |
ABuche | 0:1be517779135 | 104 | LED_Toggle[14] = addresses[i].al; |
ABuche | 0:1be517779135 | 105 | LED_Toggle[18] = high ? 0x05 : 0x04; |
ABuche | 0:1be517779135 | 106 | LED_Toggle[19] = checksum(LED_Toggle, sizeof(LED_Toggle)); |
ABuche | 0:1be517779135 | 107 | high = !high; |
ABuche | 0:1be517779135 | 108 | led1 = !led1; |
ABuche | 0:1be517779135 | 109 | |
ABuche | 0:1be517779135 | 110 | for (int j = 0; j < sizeof(LED_Toggle);j++){ |
ABuche | 0:1be517779135 | 111 | xbee.putc(LED_Toggle[j]); |
ABuche | 0:1be517779135 | 112 | } |
ABuche | 0:1be517779135 | 113 | } |
ABuche | 0:1be517779135 | 114 | } |
ABuche | 0:1be517779135 | 115 | } |
ABuche | 0:1be517779135 | 116 | |
ABuche | 0:1be517779135 | 117 | void LEDSignal(){ |
ABuche | 0:1be517779135 | 118 | thread3->signal_set(0x1); |
ABuche | 0:1be517779135 | 119 | } |
ABuche | 0:1be517779135 | 120 | |
ABuche | 0:1be517779135 | 121 | int main() { |
ABuche | 0:1be517779135 | 122 | reset = 0; |
ABuche | 0:1be517779135 | 123 | wait(0.4); |
ABuche | 0:1be517779135 | 124 | reset = 1; |
ABuche | 0:1be517779135 | 125 | wait(1); |
ABuche | 0:1be517779135 | 126 | |
ABuche | 0:1be517779135 | 127 | int pan; |
ABuche | 0:1be517779135 | 128 | char url[128]; |
ABuche | 0:1be517779135 | 129 | |
ABuche | 0:1be517779135 | 130 | FILE* f = fopen("/local/coord.cfg", "r"); |
ABuche | 0:1be517779135 | 131 | fscanf(f,"%x", &pan); |
ABuche | 0:1be517779135 | 132 | fscanf(f,"%s", &url); |
ABuche | 0:1be517779135 | 133 | fclose(f); |
ABuche | 0:1be517779135 | 134 | |
ABuche | 0:1be517779135 | 135 | pc.printf("URL: %s", url); |
ABuche | 0:1be517779135 | 136 | |
ABuche | 0:1be517779135 | 137 | char buff[2]; |
ABuche | 0:1be517779135 | 138 | buff[0]=(pan>>8)&0xff; |
ABuche | 0:1be517779135 | 139 | buff[1]=(pan)&0xff; |
ABuche | 0:1be517779135 | 140 | |
ABuche | 0:1be517779135 | 141 | AT_command[7] = buff[0]; |
ABuche | 0:1be517779135 | 142 | AT_command[8] = buff[1]; |
ABuche | 0:1be517779135 | 143 | |
ABuche | 0:1be517779135 | 144 | pc.printf("PAN: %02x%02x", AT_command[7],AT_command[8]); |
ABuche | 0:1be517779135 | 145 | |
ABuche | 0:1be517779135 | 146 | char cs = checksum(AT_command, sizeof(AT_command)); |
ABuche | 0:1be517779135 | 147 | pc.printf("CS: %02x", cs); |
ABuche | 0:1be517779135 | 148 | |
ABuche | 0:1be517779135 | 149 | AT_command[9] = cs; |
ABuche | 0:1be517779135 | 150 | |
ABuche | 0:1be517779135 | 151 | for(char i = 0;i<sizeof(AT_command);i++){ |
ABuche | 0:1be517779135 | 152 | xbee.putc(AT_command[i]); |
ABuche | 0:1be517779135 | 153 | } |
ABuche | 0:1be517779135 | 154 | |
ABuche | 0:1be517779135 | 155 | for(char i = 0;i<sizeof(AT_WR);i++){ |
ABuche | 0:1be517779135 | 156 | xbee.putc(AT_WR[i]); |
ABuche | 0:1be517779135 | 157 | } |
ABuche | 0:1be517779135 | 158 | |
ABuche | 0:1be517779135 | 159 | for(char i = 0;i<sizeof(AT_AC);i++){ |
ABuche | 0:1be517779135 | 160 | xbee.putc(AT_AC[i]); |
ABuche | 0:1be517779135 | 161 | } |
ABuche | 0:1be517779135 | 162 | |
ABuche | 0:1be517779135 | 163 | Thread localThread1; |
ABuche | 0:1be517779135 | 164 | thread1 = &localThread1; |
ABuche | 0:1be517779135 | 165 | thread1->start(&hotdog1); |
ABuche | 0:1be517779135 | 166 | |
ABuche | 0:1be517779135 | 167 | Ticker horloge; |
ABuche | 0:1be517779135 | 168 | horloge.attach(&LEDSignal, 1.0); |
ABuche | 0:1be517779135 | 169 | |
ABuche | 0:1be517779135 | 170 | Thread localThread2; |
ABuche | 0:1be517779135 | 171 | thread2 = &localThread2; |
ABuche | 0:1be517779135 | 172 | thread2->start(&traitement); |
ABuche | 0:1be517779135 | 173 | |
ABuche | 0:1be517779135 | 174 | addresses[0].ah = 0x29; |
ABuche | 0:1be517779135 | 175 | addresses[0].al = 0x21; |
ABuche | 0:1be517779135 | 176 | addressCounter++; |
ABuche | 0:1be517779135 | 177 | |
ABuche | 0:1be517779135 | 178 | Thread localThread3; |
ABuche | 0:1be517779135 | 179 | thread3 = &localThread3; |
ABuche | 0:1be517779135 | 180 | thread3->start(&flashLED); |
ABuche | 0:1be517779135 | 181 | while(1) { |
ABuche | 0:1be517779135 | 182 | } |
ABuche | 0:1be517779135 | 183 | } |