Éric Bisson
/
Code_APP3_R
app3
Fork of Code_APP1 by
xbee.cpp@9:dc81d8ee4a97, 2017-02-14 (annotated)
- Committer:
- ericbisson
- Date:
- Tue Feb 14 04:11:07 2017 +0000
- Revision:
- 9:dc81d8ee4a97
app3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericbisson | 9:dc81d8ee4a97 | 1 | #include "xbee.h" |
ericbisson | 9:dc81d8ee4a97 | 2 | |
ericbisson | 9:dc81d8ee4a97 | 3 | Timeout timeOut; |
ericbisson | 9:dc81d8ee4a97 | 4 | PwmOut led_error(LED2); |
ericbisson | 9:dc81d8ee4a97 | 5 | void LED_TurnOff() |
ericbisson | 9:dc81d8ee4a97 | 6 | { |
ericbisson | 9:dc81d8ee4a97 | 7 | led_error = 0; |
ericbisson | 9:dc81d8ee4a97 | 8 | } |
ericbisson | 9:dc81d8ee4a97 | 9 | |
ericbisson | 9:dc81d8ee4a97 | 10 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 11 | // Fonction d'envoie de données pour ZigBee (Transmit request) |
ericbisson | 9:dc81d8ee4a97 | 12 | // Testé et fonctionnel |
ericbisson | 9:dc81d8ee4a97 | 13 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 14 | void send(Serial* COM, CArray* message) |
ericbisson | 9:dc81d8ee4a97 | 15 | { |
ericbisson | 9:dc81d8ee4a97 | 16 | short opt_size = 0; |
ericbisson | 9:dc81d8ee4a97 | 17 | COM->putc(0x7E); // Start delimiter |
ericbisson | 9:dc81d8ee4a97 | 18 | COM->putc(0x00); // length MSB is always 0 |
ericbisson | 9:dc81d8ee4a97 | 19 | |
ericbisson | 9:dc81d8ee4a97 | 20 | if (message->_FrameType == 0x08) // Les commandes AT ne reçoivent pas d'addresses |
ericbisson | 9:dc81d8ee4a97 | 21 | { |
ericbisson | 9:dc81d8ee4a97 | 22 | COM->putc(6); |
ericbisson | 9:dc81d8ee4a97 | 23 | } |
ericbisson | 9:dc81d8ee4a97 | 24 | else |
ericbisson | 9:dc81d8ee4a97 | 25 | { |
ericbisson | 9:dc81d8ee4a97 | 26 | COM->putc(message->size + 12 + message->opt_size); |
ericbisson | 9:dc81d8ee4a97 | 27 | } |
ericbisson | 9:dc81d8ee4a97 | 28 | |
ericbisson | 9:dc81d8ee4a97 | 29 | COM->putc(message->_FrameType); |
ericbisson | 9:dc81d8ee4a97 | 30 | COM->putc(0x01); // Frame ID |
ericbisson | 9:dc81d8ee4a97 | 31 | |
ericbisson | 9:dc81d8ee4a97 | 32 | if (message->_FrameType != 0x08) // Les commandes AT ne reçoivent pas d'addresses |
ericbisson | 9:dc81d8ee4a97 | 33 | { |
ericbisson | 9:dc81d8ee4a97 | 34 | COM->putc(message->_64bit.bit[0]); |
ericbisson | 9:dc81d8ee4a97 | 35 | COM->putc(message->_64bit.bit[1]); |
ericbisson | 9:dc81d8ee4a97 | 36 | COM->putc(message->_64bit.bit[2]); |
ericbisson | 9:dc81d8ee4a97 | 37 | COM->putc(message->_64bit.bit[3]); |
ericbisson | 9:dc81d8ee4a97 | 38 | COM->putc(message->_64bit.bit[4]); |
ericbisson | 9:dc81d8ee4a97 | 39 | COM->putc(message->_64bit.bit[5]); |
ericbisson | 9:dc81d8ee4a97 | 40 | COM->putc(message->_64bit.bit[6]); |
ericbisson | 9:dc81d8ee4a97 | 41 | COM->putc(message->_64bit.bit[7]); |
ericbisson | 9:dc81d8ee4a97 | 42 | COM->putc(message->_16bit.bit[0]); |
ericbisson | 9:dc81d8ee4a97 | 43 | COM->putc(message->_16bit.bit[1]); |
ericbisson | 9:dc81d8ee4a97 | 44 | |
ericbisson | 9:dc81d8ee4a97 | 45 | for (int i = 0; i < message->opt_size; i++) |
ericbisson | 9:dc81d8ee4a97 | 46 | { |
ericbisson | 9:dc81d8ee4a97 | 47 | opt_size += message->options[i]; |
ericbisson | 9:dc81d8ee4a97 | 48 | COM->putc(message->options[i]); |
ericbisson | 9:dc81d8ee4a97 | 49 | } |
ericbisson | 9:dc81d8ee4a97 | 50 | } |
ericbisson | 9:dc81d8ee4a97 | 51 | |
ericbisson | 9:dc81d8ee4a97 | 52 | for (int j = 0; j < message->size; j++) |
ericbisson | 9:dc81d8ee4a97 | 53 | { |
ericbisson | 9:dc81d8ee4a97 | 54 | COM->putc(message->_ptr[j]); |
ericbisson | 9:dc81d8ee4a97 | 55 | } |
ericbisson | 9:dc81d8ee4a97 | 56 | COM->putc(calculate_checksum(message, |
ericbisson | 9:dc81d8ee4a97 | 57 | message->_FrameType + |
ericbisson | 9:dc81d8ee4a97 | 58 | 0x01 + |
ericbisson | 9:dc81d8ee4a97 | 59 | opt_size + |
ericbisson | 9:dc81d8ee4a97 | 60 | message->_16bit.bit[0] + |
ericbisson | 9:dc81d8ee4a97 | 61 | message->_16bit.bit[1] + |
ericbisson | 9:dc81d8ee4a97 | 62 | message->_64bit.bit[0] + |
ericbisson | 9:dc81d8ee4a97 | 63 | message->_64bit.bit[1] + |
ericbisson | 9:dc81d8ee4a97 | 64 | message->_64bit.bit[2] + |
ericbisson | 9:dc81d8ee4a97 | 65 | message->_64bit.bit[3] + |
ericbisson | 9:dc81d8ee4a97 | 66 | message->_64bit.bit[4] + |
ericbisson | 9:dc81d8ee4a97 | 67 | message->_64bit.bit[5] + |
ericbisson | 9:dc81d8ee4a97 | 68 | message->_64bit.bit[6] + |
ericbisson | 9:dc81d8ee4a97 | 69 | message->_64bit.bit[7] |
ericbisson | 9:dc81d8ee4a97 | 70 | )); |
ericbisson | 9:dc81d8ee4a97 | 71 | } |
ericbisson | 9:dc81d8ee4a97 | 72 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 73 | |
ericbisson | 9:dc81d8ee4a97 | 74 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 75 | // Fonction pour la réception de données (Packet Receive) |
ericbisson | 9:dc81d8ee4a97 | 76 | // Testé et fonctionnel |
ericbisson | 9:dc81d8ee4a97 | 77 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 78 | void read(Serial* COM, CArray* returnValue) |
ericbisson | 9:dc81d8ee4a97 | 79 | { |
ericbisson | 9:dc81d8ee4a97 | 80 | char BYTE_ID = 0; |
ericbisson | 9:dc81d8ee4a97 | 81 | char opts = 0; |
ericbisson | 9:dc81d8ee4a97 | 82 | bool bIsTrame = false; |
ericbisson | 9:dc81d8ee4a97 | 83 | while (true) |
ericbisson | 9:dc81d8ee4a97 | 84 | if (COM->readable()) |
ericbisson | 9:dc81d8ee4a97 | 85 | { |
ericbisson | 9:dc81d8ee4a97 | 86 | char value = COM->getc(); |
ericbisson | 9:dc81d8ee4a97 | 87 | |
ericbisson | 9:dc81d8ee4a97 | 88 | if (bIsTrame) |
ericbisson | 9:dc81d8ee4a97 | 89 | { |
ericbisson | 9:dc81d8ee4a97 | 90 | switch (BYTE_ID) |
ericbisson | 9:dc81d8ee4a97 | 91 | { |
ericbisson | 9:dc81d8ee4a97 | 92 | case 1: |
ericbisson | 9:dc81d8ee4a97 | 93 | break; |
ericbisson | 9:dc81d8ee4a97 | 94 | case 2: // on ignore le 1, car c'est toujours 0x00 |
ericbisson | 9:dc81d8ee4a97 | 95 | returnValue->size = value - 12; |
ericbisson | 9:dc81d8ee4a97 | 96 | break; |
ericbisson | 9:dc81d8ee4a97 | 97 | case 3: |
ericbisson | 9:dc81d8ee4a97 | 98 | if (value != 0x90) |
ericbisson | 9:dc81d8ee4a97 | 99 | return; |
ericbisson | 9:dc81d8ee4a97 | 100 | returnValue->_FrameType = value; |
ericbisson | 9:dc81d8ee4a97 | 101 | break; |
ericbisson | 9:dc81d8ee4a97 | 102 | case 4: |
ericbisson | 9:dc81d8ee4a97 | 103 | case 5: |
ericbisson | 9:dc81d8ee4a97 | 104 | case 6: |
ericbisson | 9:dc81d8ee4a97 | 105 | case 7: |
ericbisson | 9:dc81d8ee4a97 | 106 | case 8: |
ericbisson | 9:dc81d8ee4a97 | 107 | case 9: |
ericbisson | 9:dc81d8ee4a97 | 108 | case 10: |
ericbisson | 9:dc81d8ee4a97 | 109 | case 11: |
ericbisson | 9:dc81d8ee4a97 | 110 | returnValue->_64bit.bit[BYTE_ID - 4] = value; |
ericbisson | 9:dc81d8ee4a97 | 111 | break; |
ericbisson | 9:dc81d8ee4a97 | 112 | case 12: |
ericbisson | 9:dc81d8ee4a97 | 113 | case 13: // <-- Addresse 16 bit de l'envoyeur ici |
ericbisson | 9:dc81d8ee4a97 | 114 | returnValue->_16bit.bit[BYTE_ID - 12] = value; |
ericbisson | 9:dc81d8ee4a97 | 115 | break; |
ericbisson | 9:dc81d8ee4a97 | 116 | case 14: // je gère pas les options pour là |
ericbisson | 9:dc81d8ee4a97 | 117 | opts = value; |
ericbisson | 9:dc81d8ee4a97 | 118 | break; |
ericbisson | 9:dc81d8ee4a97 | 119 | default: |
ericbisson | 9:dc81d8ee4a97 | 120 | if (BYTE_ID < 15 + returnValue->size) |
ericbisson | 9:dc81d8ee4a97 | 121 | { |
ericbisson | 9:dc81d8ee4a97 | 122 | if (returnValue->_ptr == NULL) |
ericbisson | 9:dc81d8ee4a97 | 123 | returnValue->_ptr = new char[returnValue->size]; |
ericbisson | 9:dc81d8ee4a97 | 124 | |
ericbisson | 9:dc81d8ee4a97 | 125 | returnValue->_ptr[BYTE_ID - 15] = value; |
ericbisson | 9:dc81d8ee4a97 | 126 | break; |
ericbisson | 9:dc81d8ee4a97 | 127 | } |
ericbisson | 9:dc81d8ee4a97 | 128 | // Finalement, le checksum. On vérifie que c'est bon |
ericbisson | 9:dc81d8ee4a97 | 129 | if (value != calculate_checksum(returnValue, |
ericbisson | 9:dc81d8ee4a97 | 130 | 0x90 + |
ericbisson | 9:dc81d8ee4a97 | 131 | opts + |
ericbisson | 9:dc81d8ee4a97 | 132 | returnValue->_16bit.bit[0] + |
ericbisson | 9:dc81d8ee4a97 | 133 | returnValue->_16bit.bit[1] + |
ericbisson | 9:dc81d8ee4a97 | 134 | returnValue->_64bit.bit[0] + |
ericbisson | 9:dc81d8ee4a97 | 135 | returnValue->_64bit.bit[1] + |
ericbisson | 9:dc81d8ee4a97 | 136 | returnValue->_64bit.bit[2] + |
ericbisson | 9:dc81d8ee4a97 | 137 | returnValue->_64bit.bit[3] + |
ericbisson | 9:dc81d8ee4a97 | 138 | returnValue->_64bit.bit[4] + |
ericbisson | 9:dc81d8ee4a97 | 139 | returnValue->_64bit.bit[5] + |
ericbisson | 9:dc81d8ee4a97 | 140 | returnValue->_64bit.bit[6] + |
ericbisson | 9:dc81d8ee4a97 | 141 | returnValue->_64bit.bit[7] |
ericbisson | 9:dc81d8ee4a97 | 142 | )) |
ericbisson | 9:dc81d8ee4a97 | 143 | { |
ericbisson | 9:dc81d8ee4a97 | 144 | // Gestion d'erreur ici? |
ericbisson | 9:dc81d8ee4a97 | 145 | led_error = 1; |
ericbisson | 9:dc81d8ee4a97 | 146 | timeOut.attach(LED_TurnOff, 1); |
ericbisson | 9:dc81d8ee4a97 | 147 | } |
ericbisson | 9:dc81d8ee4a97 | 148 | return; |
ericbisson | 9:dc81d8ee4a97 | 149 | } |
ericbisson | 9:dc81d8ee4a97 | 150 | } |
ericbisson | 9:dc81d8ee4a97 | 151 | else if (value == 0x7E) |
ericbisson | 9:dc81d8ee4a97 | 152 | bIsTrame = true; |
ericbisson | 9:dc81d8ee4a97 | 153 | else |
ericbisson | 9:dc81d8ee4a97 | 154 | return; |
ericbisson | 9:dc81d8ee4a97 | 155 | |
ericbisson | 9:dc81d8ee4a97 | 156 | //wait_ms(3); // petit sleep pour recevoir un nouveau byte |
ericbisson | 9:dc81d8ee4a97 | 157 | BYTE_ID++; |
ericbisson | 9:dc81d8ee4a97 | 158 | } |
ericbisson | 9:dc81d8ee4a97 | 159 | } |
ericbisson | 9:dc81d8ee4a97 | 160 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 161 | |
ericbisson | 9:dc81d8ee4a97 | 162 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 163 | // Fonction de checksum pour le protocole ZigBee |
ericbisson | 9:dc81d8ee4a97 | 164 | // Testé et fonctionnel |
ericbisson | 9:dc81d8ee4a97 | 165 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 166 | char calculate_checksum(CArray* message, short header_size) |
ericbisson | 9:dc81d8ee4a97 | 167 | { |
ericbisson | 9:dc81d8ee4a97 | 168 | short sum = header_size; |
ericbisson | 9:dc81d8ee4a97 | 169 | if (message != NULL) |
ericbisson | 9:dc81d8ee4a97 | 170 | { |
ericbisson | 9:dc81d8ee4a97 | 171 | for (int i = 0; i < message->size; i++) |
ericbisson | 9:dc81d8ee4a97 | 172 | { |
ericbisson | 9:dc81d8ee4a97 | 173 | sum += message->_ptr[i]; |
ericbisson | 9:dc81d8ee4a97 | 174 | } |
ericbisson | 9:dc81d8ee4a97 | 175 | } |
ericbisson | 9:dc81d8ee4a97 | 176 | return 0xFF - (sum & 0x00FF); |
ericbisson | 9:dc81d8ee4a97 | 177 | } |
ericbisson | 9:dc81d8ee4a97 | 178 | //--------------------------------------------------------------// |
ericbisson | 9:dc81d8ee4a97 | 179 | |
ericbisson | 9:dc81d8ee4a97 | 180 | void setPAN(Serial* COM, short pan_id) |
ericbisson | 9:dc81d8ee4a97 | 181 | { |
ericbisson | 9:dc81d8ee4a97 | 182 | CArray DATA_TO_SEND; |
ericbisson | 9:dc81d8ee4a97 | 183 | for (int j = 0; j < 8; j++) // Je force tout à 0 pour les addresses |
ericbisson | 9:dc81d8ee4a97 | 184 | DATA_TO_SEND._64bit.bit[j] = 0; |
ericbisson | 9:dc81d8ee4a97 | 185 | DATA_TO_SEND._16bit.bit[0] = 0; |
ericbisson | 9:dc81d8ee4a97 | 186 | DATA_TO_SEND._16bit.bit[1] = 0; |
ericbisson | 9:dc81d8ee4a97 | 187 | DATA_TO_SEND.opt_size = 0; |
ericbisson | 9:dc81d8ee4a97 | 188 | |
ericbisson | 9:dc81d8ee4a97 | 189 | DATA_TO_SEND._FrameType = 0x08; // AT Command |
ericbisson | 9:dc81d8ee4a97 | 190 | DATA_TO_SEND.size = 4; |
ericbisson | 9:dc81d8ee4a97 | 191 | DATA_TO_SEND._ptr = new char[4]; |
ericbisson | 9:dc81d8ee4a97 | 192 | DATA_TO_SEND._ptr[0] = 'I'; |
ericbisson | 9:dc81d8ee4a97 | 193 | DATA_TO_SEND._ptr[1] = 'D'; |
ericbisson | 9:dc81d8ee4a97 | 194 | DATA_TO_SEND._ptr[2] = pan_id >> 8; |
ericbisson | 9:dc81d8ee4a97 | 195 | DATA_TO_SEND._ptr[3] = pan_id & 0x00FF; |
ericbisson | 9:dc81d8ee4a97 | 196 | |
ericbisson | 9:dc81d8ee4a97 | 197 | send(COM, &DATA_TO_SEND); |
ericbisson | 9:dc81d8ee4a97 | 198 | |
ericbisson | 9:dc81d8ee4a97 | 199 | delete DATA_TO_SEND._ptr; |
ericbisson | 9:dc81d8ee4a97 | 200 | } |