Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@5:d5349e68b9f8, 2017-02-13 (annotated)
- Committer:
- ShaolinPoutine
- Date:
- Mon Feb 13 19:54:49 2017 +0000
- Revision:
- 5:d5349e68b9f8
- Parent:
- 4:04351e4aad49
Propre
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ShaolinPoutine | 0:25f9d14d01bd | 1 | #include "mbed.h" |
| ShaolinPoutine | 0:25f9d14d01bd | 2 | #define FRAMEDELIMITER 0x7E |
| ShaolinPoutine | 1:318ad4f36a90 | 3 | #include "rtos.h" |
| ShaolinPoutine | 0:25f9d14d01bd | 4 | |
| ShaolinPoutine | 1:318ad4f36a90 | 5 | // 4269 |
| ShaolinPoutine | 0:25f9d14d01bd | 6 | DigitalOut resetswitch(p8); |
| ShaolinPoutine | 0:25f9d14d01bd | 7 | Serial xbee(p13, p14); |
| ShaolinPoutine | 0:25f9d14d01bd | 8 | Serial pc(USBTX, USBRX); |
| ShaolinPoutine | 0:25f9d14d01bd | 9 | DigitalOut myled(LED1); |
| ShaolinPoutine | 0:25f9d14d01bd | 10 | |
| ShaolinPoutine | 0:25f9d14d01bd | 11 | void printhexa(char c) |
| ShaolinPoutine | 0:25f9d14d01bd | 12 | { |
| ShaolinPoutine | 0:25f9d14d01bd | 13 | char msb = c >> 4; |
| ShaolinPoutine | 0:25f9d14d01bd | 14 | char lsb = c & 0xF; |
| ShaolinPoutine | 0:25f9d14d01bd | 15 | |
| ShaolinPoutine | 0:25f9d14d01bd | 16 | if (msb < 10) |
| ShaolinPoutine | 0:25f9d14d01bd | 17 | msb += 0x30; |
| ShaolinPoutine | 0:25f9d14d01bd | 18 | else |
| ShaolinPoutine | 0:25f9d14d01bd | 19 | msb += 0x37; |
| ShaolinPoutine | 0:25f9d14d01bd | 20 | |
| ShaolinPoutine | 0:25f9d14d01bd | 21 | if (lsb < 10) |
| ShaolinPoutine | 0:25f9d14d01bd | 22 | lsb += 0x30; |
| ShaolinPoutine | 0:25f9d14d01bd | 23 | else |
| ShaolinPoutine | 0:25f9d14d01bd | 24 | lsb += 0x37; |
| ShaolinPoutine | 0:25f9d14d01bd | 25 | |
| ShaolinPoutine | 0:25f9d14d01bd | 26 | pc.putc('0'); |
| ShaolinPoutine | 0:25f9d14d01bd | 27 | pc.putc('x'); |
| ShaolinPoutine | 0:25f9d14d01bd | 28 | pc.putc(msb); |
| ShaolinPoutine | 0:25f9d14d01bd | 29 | pc.putc(lsb); |
| ShaolinPoutine | 0:25f9d14d01bd | 30 | pc.putc(' '); |
| ShaolinPoutine | 0:25f9d14d01bd | 31 | } |
| ShaolinPoutine | 0:25f9d14d01bd | 32 | |
| ShaolinPoutine | 2:c6d623a30254 | 33 | void ATCommandResponse(int len, char* ans) |
| ShaolinPoutine | 2:c6d623a30254 | 34 | { |
| ShaolinPoutine | 2:c6d623a30254 | 35 | char total = 0x88; |
| ShaolinPoutine | 2:c6d623a30254 | 36 | char id = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 37 | total += id; |
| ShaolinPoutine | 2:c6d623a30254 | 38 | char command[2]; |
| ShaolinPoutine | 2:c6d623a30254 | 39 | command[0] = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 40 | total += command[0]; |
| ShaolinPoutine | 2:c6d623a30254 | 41 | command[1] = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 42 | total += command[1]; |
| ShaolinPoutine | 2:c6d623a30254 | 43 | char status = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 44 | total += status; |
| ShaolinPoutine | 2:c6d623a30254 | 45 | int i = 0; |
| ShaolinPoutine | 2:c6d623a30254 | 46 | len-= 4; |
| ShaolinPoutine | 2:c6d623a30254 | 47 | char data[len]; |
| ShaolinPoutine | 2:c6d623a30254 | 48 | |
| ShaolinPoutine | 2:c6d623a30254 | 49 | pc.printf("response to command %c%c", command[0], command[1]); |
| ShaolinPoutine | 2:c6d623a30254 | 50 | pc.printf(" is "); |
| ShaolinPoutine | 2:c6d623a30254 | 51 | |
| ShaolinPoutine | 2:c6d623a30254 | 52 | while (i < len) |
| ShaolinPoutine | 2:c6d623a30254 | 53 | { |
| ShaolinPoutine | 2:c6d623a30254 | 54 | if (xbee.readable()) |
| ShaolinPoutine | 2:c6d623a30254 | 55 | { |
| ShaolinPoutine | 2:c6d623a30254 | 56 | data[i] = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 57 | total += data[i]; |
| ShaolinPoutine | 2:c6d623a30254 | 58 | printhexa(data[i]); |
| ShaolinPoutine | 2:c6d623a30254 | 59 | i++; |
| ShaolinPoutine | 2:c6d623a30254 | 60 | } |
| ShaolinPoutine | 2:c6d623a30254 | 61 | } |
| ShaolinPoutine | 2:c6d623a30254 | 62 | |
| ShaolinPoutine | 2:c6d623a30254 | 63 | char checksum = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 64 | total += checksum; |
| ShaolinPoutine | 2:c6d623a30254 | 65 | // Verify checksum |
| ShaolinPoutine | 2:c6d623a30254 | 66 | if (total != 0xFF) |
| ShaolinPoutine | 2:c6d623a30254 | 67 | { |
| ShaolinPoutine | 2:c6d623a30254 | 68 | pc.printf("Checksum is wrong"); |
| ShaolinPoutine | 2:c6d623a30254 | 69 | } |
| ShaolinPoutine | 2:c6d623a30254 | 70 | pc.printf("\r\n"); |
| ShaolinPoutine | 2:c6d623a30254 | 71 | ans = data; |
| ShaolinPoutine | 2:c6d623a30254 | 72 | } |
| ShaolinPoutine | 2:c6d623a30254 | 73 | |
| ShaolinPoutine | 2:c6d623a30254 | 74 | void ModemStatus(int len, char* ans) |
| ShaolinPoutine | 2:c6d623a30254 | 75 | { |
| ShaolinPoutine | 2:c6d623a30254 | 76 | char status = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 77 | |
| ShaolinPoutine | 2:c6d623a30254 | 78 | switch (status){ |
| ShaolinPoutine | 2:c6d623a30254 | 79 | case 0 : pc.printf("Hardware reset\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 80 | case 1 : pc.printf("Watchdog timer reset\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 81 | case 2 : pc.printf("Joined network (routers and end devices)\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 82 | case 3 : pc.printf("Disassociated\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 83 | case 6 : pc.printf("Coordinator started\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 84 | case 7 : pc.printf("Network security key was updated\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 85 | case 0x0D : pc.printf("Voltage supply limit exceeded\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 86 | case 0x11 : pc.printf("Modem configuration changed while join in progress\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 87 | default : pc.printf("stack error\r\n"); break; |
| ShaolinPoutine | 2:c6d623a30254 | 88 | } |
| ShaolinPoutine | 2:c6d623a30254 | 89 | |
| ShaolinPoutine | 2:c6d623a30254 | 90 | char checksum = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 91 | |
| ShaolinPoutine | 2:c6d623a30254 | 92 | checksum += 0x8A + status; |
| ShaolinPoutine | 2:c6d623a30254 | 93 | |
| ShaolinPoutine | 2:c6d623a30254 | 94 | if (checksum != 0xFF) |
| ShaolinPoutine | 2:c6d623a30254 | 95 | { |
| ShaolinPoutine | 2:c6d623a30254 | 96 | pc.printf("Checksum is wrong\r\n"); |
| ShaolinPoutine | 2:c6d623a30254 | 97 | } |
| ShaolinPoutine | 2:c6d623a30254 | 98 | *ans = status; |
| ShaolinPoutine | 2:c6d623a30254 | 99 | } |
| ShaolinPoutine | 2:c6d623a30254 | 100 | |
| ShaolinPoutine | 5:d5349e68b9f8 | 101 | char* InterpretMessage() |
| ShaolinPoutine | 0:25f9d14d01bd | 102 | { |
| ShaolinPoutine | 0:25f9d14d01bd | 103 | char start = xbee.getc(); // = FRAMEDELIMITER |
| ShaolinPoutine | 0:25f9d14d01bd | 104 | //assert |
| ShaolinPoutine | 0:25f9d14d01bd | 105 | char len_msb = xbee.getc(); |
| ShaolinPoutine | 0:25f9d14d01bd | 106 | char len_lsb = xbee.getc(); |
| ShaolinPoutine | 1:318ad4f36a90 | 107 | |
| ShaolinPoutine | 0:25f9d14d01bd | 108 | int len = ((int) len_msb << 4) + (int) len_lsb; |
| ShaolinPoutine | 0:25f9d14d01bd | 109 | char frame_data[len]; |
| ShaolinPoutine | 2:c6d623a30254 | 110 | |
| ShaolinPoutine | 2:c6d623a30254 | 111 | // Resolving frame type |
| ShaolinPoutine | 2:c6d623a30254 | 112 | char type = xbee.getc(); |
| ShaolinPoutine | 2:c6d623a30254 | 113 | char *response; |
| ShaolinPoutine | 2:c6d623a30254 | 114 | len--; |
| ShaolinPoutine | 2:c6d623a30254 | 115 | |
| ShaolinPoutine | 2:c6d623a30254 | 116 | switch (type){ |
| ShaolinPoutine | 2:c6d623a30254 | 117 | case 0x88: ATCommandResponse(len, response); |
| ShaolinPoutine | 2:c6d623a30254 | 118 | break; |
| ShaolinPoutine | 2:c6d623a30254 | 119 | case 0x8A: ModemStatus(len, response); |
| ShaolinPoutine | 2:c6d623a30254 | 120 | break; |
| ShaolinPoutine | 2:c6d623a30254 | 121 | default: pc.printf("Please implement response of type"); |
| ShaolinPoutine | 2:c6d623a30254 | 122 | printhexa(type); |
| ShaolinPoutine | 2:c6d623a30254 | 123 | pc.printf("\r\n"); |
| ShaolinPoutine | 2:c6d623a30254 | 124 | for (int i = -1; i <len; i++) xbee.getc(); |
| ShaolinPoutine | 0:25f9d14d01bd | 125 | } |
| ShaolinPoutine | 2:c6d623a30254 | 126 | return response; |
| ShaolinPoutine | 0:25f9d14d01bd | 127 | } |
| ShaolinPoutine | 0:25f9d14d01bd | 128 | |
| ShaolinPoutine | 5:d5349e68b9f8 | 129 | void SendAtCommand(char FirstChar, char SecondChar, char *OptionalParam = NULL, int ParamLen = 0) |
| EmileArseneault | 3:5010d20e681f | 130 | { |
| EmileArseneault | 3:5010d20e681f | 131 | // Frame Type 0x08 |
| EmileArseneault | 3:5010d20e681f | 132 | // Two char as parameters |
| EmileArseneault | 3:5010d20e681f | 133 | |
| EmileArseneault | 3:5010d20e681f | 134 | char cmdtosend[10]; |
| EmileArseneault | 3:5010d20e681f | 135 | char sum = 0; |
| EmileArseneault | 3:5010d20e681f | 136 | int cmdlength = 8; |
| EmileArseneault | 3:5010d20e681f | 137 | int i = 0; |
| EmileArseneault | 3:5010d20e681f | 138 | |
| EmileArseneault | 3:5010d20e681f | 139 | cmdtosend[0] = FRAMEDELIMITER; |
| EmileArseneault | 3:5010d20e681f | 140 | cmdtosend[1] = 0x00; |
| EmileArseneault | 3:5010d20e681f | 141 | cmdtosend[2] = 0x04 + ParamLen; |
| EmileArseneault | 3:5010d20e681f | 142 | cmdtosend[3] = 0x08; |
| EmileArseneault | 3:5010d20e681f | 143 | cmdtosend[4] = 0x52; |
| EmileArseneault | 3:5010d20e681f | 144 | cmdtosend[5] = FirstChar; |
| EmileArseneault | 3:5010d20e681f | 145 | cmdtosend[6] = SecondChar; |
| EmileArseneault | 3:5010d20e681f | 146 | |
| EmileArseneault | 3:5010d20e681f | 147 | // Ajouter les parametres au message |
| EmileArseneault | 3:5010d20e681f | 148 | if(OptionalParam != NULL) |
| EmileArseneault | 3:5010d20e681f | 149 | { |
| EmileArseneault | 3:5010d20e681f | 150 | i = 0; |
| EmileArseneault | 3:5010d20e681f | 151 | cmdlength += ParamLen; |
| EmileArseneault | 3:5010d20e681f | 152 | |
| EmileArseneault | 3:5010d20e681f | 153 | while (i < ParamLen) |
| EmileArseneault | 3:5010d20e681f | 154 | { |
| EmileArseneault | 3:5010d20e681f | 155 | pc.putc((OptionalParam)[i]); |
| EmileArseneault | 3:5010d20e681f | 156 | cmdtosend[7 + i] = (OptionalParam)[i]; |
| EmileArseneault | 3:5010d20e681f | 157 | i++; |
| EmileArseneault | 3:5010d20e681f | 158 | } |
| EmileArseneault | 3:5010d20e681f | 159 | pc.printf("\r\n"); |
| EmileArseneault | 3:5010d20e681f | 160 | } |
| EmileArseneault | 3:5010d20e681f | 161 | |
| EmileArseneault | 3:5010d20e681f | 162 | // Calculate checksum |
| EmileArseneault | 3:5010d20e681f | 163 | i = 3; |
| EmileArseneault | 3:5010d20e681f | 164 | while (i < (cmdlength - 1)) |
| EmileArseneault | 3:5010d20e681f | 165 | { |
| EmileArseneault | 3:5010d20e681f | 166 | sum += cmdtosend[i]; |
| EmileArseneault | 3:5010d20e681f | 167 | i++; |
| EmileArseneault | 3:5010d20e681f | 168 | } |
| EmileArseneault | 3:5010d20e681f | 169 | cmdtosend[cmdlength - 1] = 0xFF - sum; |
| EmileArseneault | 3:5010d20e681f | 170 | |
| EmileArseneault | 3:5010d20e681f | 171 | // Envoyer la commande sur UART |
| EmileArseneault | 3:5010d20e681f | 172 | i = 0; |
| EmileArseneault | 3:5010d20e681f | 173 | while (i < cmdlength) |
| EmileArseneault | 3:5010d20e681f | 174 | { |
| EmileArseneault | 3:5010d20e681f | 175 | xbee.putc(cmdtosend[i]); |
| EmileArseneault | 3:5010d20e681f | 176 | i++; |
| EmileArseneault | 3:5010d20e681f | 177 | } |
| ShaolinPoutine | 5:d5349e68b9f8 | 178 | |
| ShaolinPoutine | 5:d5349e68b9f8 | 179 | wait(0.1); |
| EmileArseneault | 3:5010d20e681f | 180 | } |
| EmileArseneault | 3:5010d20e681f | 181 | |
| ShaolinPoutine | 5:d5349e68b9f8 | 182 | void ReadSerial() |
| ShaolinPoutine | 5:d5349e68b9f8 | 183 | { |
| ShaolinPoutine | 5:d5349e68b9f8 | 184 | // 00 13 A2 00 |
| ShaolinPoutine | 5:d5349e68b9f8 | 185 | // 40 3E 09 63 |
| ShaolinPoutine | 5:d5349e68b9f8 | 186 | SendAtCommand('S', 'H'); |
| ShaolinPoutine | 5:d5349e68b9f8 | 187 | SendAtCommand('S', 'L'); |
| ShaolinPoutine | 5:d5349e68b9f8 | 188 | } |
| ShaolinPoutine | 0:25f9d14d01bd | 189 | |
| ShaolinPoutine | 1:318ad4f36a90 | 190 | void xbee_reader() |
| ShaolinPoutine | 1:318ad4f36a90 | 191 | { |
| ShaolinPoutine | 1:318ad4f36a90 | 192 | while(1) |
| ShaolinPoutine | 1:318ad4f36a90 | 193 | { |
| ShaolinPoutine | 1:318ad4f36a90 | 194 | if (xbee.readable()) |
| ShaolinPoutine | 1:318ad4f36a90 | 195 | { |
| ShaolinPoutine | 5:d5349e68b9f8 | 196 | InterpretMessage(); |
| ShaolinPoutine | 1:318ad4f36a90 | 197 | } |
| ShaolinPoutine | 1:318ad4f36a90 | 198 | wait(0.001); |
| ShaolinPoutine | 1:318ad4f36a90 | 199 | } |
| ShaolinPoutine | 1:318ad4f36a90 | 200 | } |
| ShaolinPoutine | 1:318ad4f36a90 | 201 | |
| ShaolinPoutine | 1:318ad4f36a90 | 202 | void tick() |
| ShaolinPoutine | 1:318ad4f36a90 | 203 | { |
| ShaolinPoutine | 1:318ad4f36a90 | 204 | myled = !myled; |
| ShaolinPoutine | 1:318ad4f36a90 | 205 | } |
| ShaolinPoutine | 1:318ad4f36a90 | 206 | |
| ShaolinPoutine | 0:25f9d14d01bd | 207 | int main() { |
| ShaolinPoutine | 1:318ad4f36a90 | 208 | Thread thread(xbee_reader); |
| ShaolinPoutine | 1:318ad4f36a90 | 209 | Ticker ticker; |
| ShaolinPoutine | 1:318ad4f36a90 | 210 | ticker.attach(&tick, 1); |
| ShaolinPoutine | 0:25f9d14d01bd | 211 | pc.printf("\r\n"); |
| ShaolinPoutine | 0:25f9d14d01bd | 212 | resetswitch = 0; |
| ShaolinPoutine | 0:25f9d14d01bd | 213 | wait(0.4); |
| ShaolinPoutine | 0:25f9d14d01bd | 214 | resetswitch = 1; |
| ShaolinPoutine | 0:25f9d14d01bd | 215 | |
| ShaolinPoutine | 1:318ad4f36a90 | 216 | wait(3); |
| ShaolinPoutine | 0:25f9d14d01bd | 217 | ReadSerial(); |
| ShaolinPoutine | 0:25f9d14d01bd | 218 | |
| ShaolinPoutine | 0:25f9d14d01bd | 219 | while(1) { |
| ShaolinPoutine | 0:25f9d14d01bd | 220 | } |
| ShaolinPoutine | 0:25f9d14d01bd | 221 | } |