Polytech school project. RICM4 students, see http://air.imag.fr/index.php/Projets-2016-2017-Station_de_pompage_connect%C3%A9e for more information
Dependencies: SX1272Lib mbed WakeUp
Fork of SX1272PingPong by
main.cpp@14:261007103beb, 2017-02-28 (annotated)
- Committer:
- chevamax
- Date:
- Tue Feb 28 13:11:52 2017 +0000
- Revision:
- 14:261007103beb
- Parent:
- 13:edb9b443c1dd
- Child:
- 16:85fb5e37def7
Test commit mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregCr | 0:1ed39951ab7b | 1 | #include "mbed.h" |
GregCr | 4:5ece30264cd9 | 2 | #include "main.h" |
GregCr | 13:edb9b443c1dd | 3 | #include "sx1272-hal.h" |
GregCr | 8:f956dee63a56 | 4 | #include "debug.h" |
chevamax | 14:261007103beb | 5 | #include "trame.h" |
chevamax | 14:261007103beb | 6 | #include "ordre.h" |
chevamax | 14:261007103beb | 7 | #include "pompe.h" |
chevamax | 14:261007103beb | 8 | //#include "niveau.h" |
GregCr | 0:1ed39951ab7b | 9 | |
GregCr | 0:1ed39951ab7b | 10 | /* Set this flag to '1' to display debug messages on the console */ |
GregCr | 13:edb9b443c1dd | 11 | #define DEBUG_MESSAGE 1 |
chevamax | 14:261007103beb | 12 | #define ID_DEVICE 0x24 |
chevamax | 14:261007103beb | 13 | #define ID_STATION 0x55 |
GregCr | 0:1ed39951ab7b | 14 | |
GregCr | 0:1ed39951ab7b | 15 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
GregCr | 5:f2431c4fe3bb | 16 | #define USE_MODEM_LORA 1 |
GregCr | 0:1ed39951ab7b | 17 | #define USE_MODEM_FSK !USE_MODEM_LORA |
GregCr | 0:1ed39951ab7b | 18 | |
chevamax | 14:261007103beb | 19 | #define RF_FREQUENCY 865000000 // Hz |
GregCr | 0:1ed39951ab7b | 20 | #define TX_OUTPUT_POWER 14 // 14 dBm |
GregCr | 0:1ed39951ab7b | 21 | |
GregCr | 0:1ed39951ab7b | 22 | #if USE_MODEM_LORA == 1 |
GregCr | 0:1ed39951ab7b | 23 | |
GregCr | 1:126d70d374f6 | 24 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
GregCr | 0:1ed39951ab7b | 25 | // 1: 250 kHz, |
GregCr | 0:1ed39951ab7b | 26 | // 2: 500 kHz, |
GregCr | 0:1ed39951ab7b | 27 | // 3: Reserved] |
GregCr | 0:1ed39951ab7b | 28 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
GregCr | 0:1ed39951ab7b | 29 | #define LORA_CODINGRATE 1 // [1: 4/5, |
GregCr | 0:1ed39951ab7b | 30 | // 2: 4/6, |
GregCr | 0:1ed39951ab7b | 31 | // 3: 4/7, |
GregCr | 0:1ed39951ab7b | 32 | // 4: 4/8] |
GregCr | 0:1ed39951ab7b | 33 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
GregCr | 0:1ed39951ab7b | 34 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
GregCr | 0:1ed39951ab7b | 35 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
GregCr | 3:8b9e2a4df4b5 | 36 | #define LORA_FHSS_ENABLED false |
GregCr | 3:8b9e2a4df4b5 | 37 | #define LORA_NB_SYMB_HOP 4 |
GregCr | 0:1ed39951ab7b | 38 | #define LORA_IQ_INVERSION_ON false |
GregCr | 3:8b9e2a4df4b5 | 39 | #define LORA_CRC_ENABLED true |
GregCr | 3:8b9e2a4df4b5 | 40 | |
GregCr | 0:1ed39951ab7b | 41 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 42 | |
GregCr | 2:59e108728d71 | 43 | #define FSK_FDEV 25000 // Hz |
GregCr | 2:59e108728d71 | 44 | #define FSK_DATARATE 19200 // bps |
GregCr | 2:59e108728d71 | 45 | #define FSK_BANDWIDTH 50000 // Hz |
GregCr | 2:59e108728d71 | 46 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
GregCr | 0:1ed39951ab7b | 47 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
GregCr | 0:1ed39951ab7b | 48 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
GregCr | 3:8b9e2a4df4b5 | 49 | #define FSK_CRC_ENABLED true |
GregCr | 3:8b9e2a4df4b5 | 50 | |
GregCr | 0:1ed39951ab7b | 51 | #else |
GregCr | 0:1ed39951ab7b | 52 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 53 | #endif |
GregCr | 0:1ed39951ab7b | 54 | |
chevamax | 14:261007103beb | 55 | #define RX_TIMEOUT_VALUE 30000000//3500000 // in us |
GregCr | 0:1ed39951ab7b | 56 | #define BUFFER_SIZE 32 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 57 | |
GregCr | 8:f956dee63a56 | 58 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
GregCr | 3:8b9e2a4df4b5 | 59 | DigitalOut led(LED2); |
GregCr | 3:8b9e2a4df4b5 | 60 | #else |
GregCr | 3:8b9e2a4df4b5 | 61 | DigitalOut led(LED1); |
GregCr | 3:8b9e2a4df4b5 | 62 | #endif |
GregCr | 3:8b9e2a4df4b5 | 63 | |
GregCr | 0:1ed39951ab7b | 64 | /* |
GregCr | 0:1ed39951ab7b | 65 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 66 | */ |
mluis | 10:7af820d1e1df | 67 | typedef enum |
mluis | 10:7af820d1e1df | 68 | { |
mluis | 10:7af820d1e1df | 69 | LOWPOWER = 0, |
mluis | 10:7af820d1e1df | 70 | IDLE, |
mluis | 10:7af820d1e1df | 71 | |
mluis | 10:7af820d1e1df | 72 | RX, |
mluis | 10:7af820d1e1df | 73 | RX_TIMEOUT, |
mluis | 10:7af820d1e1df | 74 | RX_ERROR, |
mluis | 10:7af820d1e1df | 75 | |
mluis | 10:7af820d1e1df | 76 | TX, |
mluis | 10:7af820d1e1df | 77 | TX_TIMEOUT, |
mluis | 10:7af820d1e1df | 78 | |
mluis | 10:7af820d1e1df | 79 | CAD, |
mluis | 10:7af820d1e1df | 80 | CAD_DONE |
mluis | 10:7af820d1e1df | 81 | }AppStates_t; |
GregCr | 0:1ed39951ab7b | 82 | |
mluis | 10:7af820d1e1df | 83 | volatile AppStates_t State = LOWPOWER; |
mluis | 10:7af820d1e1df | 84 | |
mluis | 10:7af820d1e1df | 85 | /*! |
mluis | 10:7af820d1e1df | 86 | * Radio events function pointer |
mluis | 10:7af820d1e1df | 87 | */ |
mluis | 10:7af820d1e1df | 88 | static RadioEvents_t RadioEvents; |
mluis | 10:7af820d1e1df | 89 | |
mluis | 10:7af820d1e1df | 90 | /* |
mluis | 10:7af820d1e1df | 91 | * Global variables declarations |
mluis | 10:7af820d1e1df | 92 | */ |
GregCr | 13:edb9b443c1dd | 93 | SX1272MB2xAS Radio( NULL ); |
GregCr | 0:1ed39951ab7b | 94 | |
chevamax | 14:261007103beb | 95 | //const uint8_t PingMsg[] = "PING"; |
chevamax | 14:261007103beb | 96 | //const uint8_t PongMsg[] = "PONG"; |
GregCr | 0:1ed39951ab7b | 97 | |
GregCr | 0:1ed39951ab7b | 98 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 99 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 100 | |
GregCr | 5:f2431c4fe3bb | 101 | int16_t RssiValue = 0.0; |
GregCr | 5:f2431c4fe3bb | 102 | int8_t SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 103 | |
GregCr | 0:1ed39951ab7b | 104 | int main() |
GregCr | 0:1ed39951ab7b | 105 | { |
GregCr | 0:1ed39951ab7b | 106 | uint8_t i; |
GregCr | 0:1ed39951ab7b | 107 | bool isMaster = true; |
GregCr | 0:1ed39951ab7b | 108 | |
chevamax | 14:261007103beb | 109 | debug( "\n\n\r SX1272 Cuve Demo Application \n\n\r" ); |
mluis | 10:7af820d1e1df | 110 | |
mluis | 10:7af820d1e1df | 111 | // Initialize Radio driver |
mluis | 10:7af820d1e1df | 112 | RadioEvents.TxDone = OnTxDone; |
mluis | 10:7af820d1e1df | 113 | RadioEvents.RxDone = OnRxDone; |
mluis | 10:7af820d1e1df | 114 | RadioEvents.RxError = OnRxError; |
mluis | 10:7af820d1e1df | 115 | RadioEvents.TxTimeout = OnTxTimeout; |
mluis | 10:7af820d1e1df | 116 | RadioEvents.RxTimeout = OnRxTimeout; |
mluis | 10:7af820d1e1df | 117 | Radio.Init( &RadioEvents ); |
GregCr | 0:1ed39951ab7b | 118 | |
GregCr | 7:c1bbd6c56979 | 119 | // verify the connection with the board |
GregCr | 7:c1bbd6c56979 | 120 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
GregCr | 2:59e108728d71 | 121 | { |
GregCr | 7:c1bbd6c56979 | 122 | debug( "Radio could not be detected!\n\r", NULL ); |
GregCr | 7:c1bbd6c56979 | 123 | wait( 1 ); |
GregCr | 2:59e108728d71 | 124 | } |
GregCr | 7:c1bbd6c56979 | 125 | |
GregCr | 13:edb9b443c1dd | 126 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
GregCr | 13:edb9b443c1dd | 127 | |
GregCr | 0:1ed39951ab7b | 128 | Radio.SetChannel( RF_FREQUENCY ); |
GregCr | 0:1ed39951ab7b | 129 | |
GregCr | 0:1ed39951ab7b | 130 | #if USE_MODEM_LORA == 1 |
GregCr | 3:8b9e2a4df4b5 | 131 | |
GregCr | 7:c1bbd6c56979 | 132 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 133 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 134 | |
GregCr | 0:1ed39951ab7b | 135 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
GregCr | 0:1ed39951ab7b | 136 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
GregCr | 0:1ed39951ab7b | 137 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 3:8b9e2a4df4b5 | 138 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 7:c1bbd6c56979 | 139 | LORA_IQ_INVERSION_ON, 2000000 ); |
GregCr | 0:1ed39951ab7b | 140 | |
GregCr | 0:1ed39951ab7b | 141 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
GregCr | 0:1ed39951ab7b | 142 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
mluis | 9:e764990e45df | 143 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
GregCr | 3:8b9e2a4df4b5 | 144 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 3:8b9e2a4df4b5 | 145 | LORA_IQ_INVERSION_ON, true ); |
GregCr | 0:1ed39951ab7b | 146 | |
GregCr | 0:1ed39951ab7b | 147 | #elif USE_MODEM_FSK == 1 |
GregCr | 0:1ed39951ab7b | 148 | |
GregCr | 7:c1bbd6c56979 | 149 | debug("\n\n\r > FSK Mode < \n\n\r"); |
GregCr | 0:1ed39951ab7b | 150 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
GregCr | 0:1ed39951ab7b | 151 | FSK_DATARATE, 0, |
GregCr | 0:1ed39951ab7b | 152 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
GregCr | 7:c1bbd6c56979 | 153 | FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); |
GregCr | 0:1ed39951ab7b | 154 | |
GregCr | 0:1ed39951ab7b | 155 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
GregCr | 0:1ed39951ab7b | 156 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
mluis | 9:e764990e45df | 157 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
GregCr | 3:8b9e2a4df4b5 | 158 | 0, 0, false, true ); |
GregCr | 0:1ed39951ab7b | 159 | |
GregCr | 0:1ed39951ab7b | 160 | #else |
GregCr | 0:1ed39951ab7b | 161 | |
GregCr | 0:1ed39951ab7b | 162 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 163 | |
GregCr | 0:1ed39951ab7b | 164 | #endif |
GregCr | 1:126d70d374f6 | 165 | |
chevamax | 14:261007103beb | 166 | debug_if( DEBUG_MESSAGE, "Starting Cuve loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 167 | |
GregCr | 3:8b9e2a4df4b5 | 168 | led = 0; |
chevamax | 14:261007103beb | 169 | |
chevamax | 14:261007103beb | 170 | DigitalOut pompe(PC_14); |
chevamax | 14:261007103beb | 171 | |
chevamax | 14:261007103beb | 172 | //Pompe pompe(PC_14); |
chevamax | 14:261007103beb | 173 | |
chevamax | 14:261007103beb | 174 | DigitalIn p1(PC_15);//(PC_15); //Poid fort |
chevamax | 14:261007103beb | 175 | DigitalIn p2(PH_0); |
chevamax | 14:261007103beb | 176 | DigitalIn p3(PH_1); //Poid faible |
chevamax | 14:261007103beb | 177 | |
chevamax | 14:261007103beb | 178 | //Niveau cuve(); |
chevamax | 14:261007103beb | 179 | //char niveau = (char) ((p1.read()<<2) | (p2.read()<<1) | p3.read()); |
chevamax | 14:261007103beb | 180 | TrameData dataPaquet(ID_DEVICE, 63, (char) pompe.read(), 0x1F, 0xF); |
chevamax | 14:261007103beb | 181 | |
chevamax | 14:261007103beb | 182 | bool enAttente = false; |
chevamax | 14:261007103beb | 183 | |
chevamax | 14:261007103beb | 184 | //TODO |
chevamax | 14:261007103beb | 185 | // Distinguer deux temps. Il faut pouvoir s'endormir une heure, mais aussi écouter pendant 5 minutes |
GregCr | 0:1ed39951ab7b | 186 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 187 | |
GregCr | 0:1ed39951ab7b | 188 | while( 1 ) |
GregCr | 0:1ed39951ab7b | 189 | { |
GregCr | 0:1ed39951ab7b | 190 | switch( State ) |
GregCr | 0:1ed39951ab7b | 191 | { |
GregCr | 0:1ed39951ab7b | 192 | case RX: |
chevamax | 14:261007103beb | 193 | //If the board is the master, it have the initiative |
chevamax | 14:261007103beb | 194 | if( enAttente == true ) |
GregCr | 0:1ed39951ab7b | 195 | { |
chevamax | 14:261007103beb | 196 | debug("1\r\n"); |
GregCr | 0:1ed39951ab7b | 197 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 198 | { |
chevamax | 14:261007103beb | 199 | debug("2\r\n"); |
chevamax | 14:261007103beb | 200 | debug((const char* )Buffer); |
chevamax | 14:261007103beb | 201 | debug(" "); |
chevamax | 14:261007103beb | 202 | debug((const char* ) ID_DEVICE); |
chevamax | 14:261007103beb | 203 | Ordre trameRecue((char*)Buffer); |
chevamax | 14:261007103beb | 204 | //Si message recu alors trouver ordre correspondant et faire traitement |
chevamax | 14:261007103beb | 205 | if( trameRecue.getIdRecepteur() == (char) ID_DEVICE ) |
GregCr | 0:1ed39951ab7b | 206 | { |
chevamax | 14:261007103beb | 207 | if( trameRecue.getIdEmetteur() == (char) ID_STATION ){ |
chevamax | 14:261007103beb | 208 | //Ordre pour nous |
chevamax | 14:261007103beb | 209 | led = !led; |
chevamax | 14:261007103beb | 210 | debug( "...Ordre recu \r\n" ); |
chevamax | 14:261007103beb | 211 | Ordre o((char *) Buffer); |
chevamax | 14:261007103beb | 212 | |
chevamax | 14:261007103beb | 213 | //TODO executer ordre |
chevamax | 14:261007103beb | 214 | debug("Executer Ordre\r\n"); |
chevamax | 14:261007103beb | 215 | |
chevamax | 14:261007103beb | 216 | //On attend 5 minutes |
chevamax | 14:261007103beb | 217 | debug("Dormir\r\n"); |
chevamax | 14:261007103beb | 218 | wait(10); |
chevamax | 14:261007103beb | 219 | enAttente = false; |
chevamax | 14:261007103beb | 220 | Radio.Rx( RX_TIMEOUT_VALUE ); |
chevamax | 14:261007103beb | 221 | }else{ |
chevamax | 14:261007103beb | 222 | debug("autre station\r\n"); |
GregCr | 0:1ed39951ab7b | 223 | } |
GregCr | 2:59e108728d71 | 224 | } |
GregCr | 2:59e108728d71 | 225 | else // valid reception but neither a PING or a PONG message |
GregCr | 2:59e108728d71 | 226 | { // Set device as master ans start again |
chevamax | 14:261007103beb | 227 | debug("Reception message pour quelqu'un d'autre\r\n"); |
GregCr | 2:59e108728d71 | 228 | isMaster = true; |
chevamax | 14:261007103beb | 229 | Radio.Rx( RX_TIMEOUT_VALUE ); //La on reattend pour 5 minutes à chaque foi que qq'un envoit un message c'est pas top.... |
GregCr | 2:59e108728d71 | 230 | } |
GregCr | 0:1ed39951ab7b | 231 | } |
GregCr | 0:1ed39951ab7b | 232 | } |
chevamax | 14:261007103beb | 233 | else //enAttente ==false donc on envoit les donnees |
GregCr | 0:1ed39951ab7b | 234 | { |
chevamax | 14:261007103beb | 235 | //Recuperer les données capteurs |
chevamax | 14:261007103beb | 236 | //dataPaquet.mettreAJourEtatPompe(pompe.etat()); |
chevamax | 14:261007103beb | 237 | //dataPaquet.mettreAJourNiveauCuve(cuve.getNiveauCuve()); |
chevamax | 14:261007103beb | 238 | debug("envoie1\r\n"); |
chevamax | 14:261007103beb | 239 | dataPaquet.mettreAJourEtatPompe((char) 0); |
chevamax | 14:261007103beb | 240 | dataPaquet.mettreAJourNiveauCuve((char) 0x1F); |
chevamax | 14:261007103beb | 241 | dataPaquet.mettreAJourNiveauBatterie((char) 0xF); //TODO |
chevamax | 14:261007103beb | 242 | |
chevamax | 14:261007103beb | 243 | char * trame = dataPaquet.creerTrame(); |
chevamax | 14:261007103beb | 244 | debug( trame); |
chevamax | 14:261007103beb | 245 | strcpy( ( char* ) Buffer, trame); |
chevamax | 14:261007103beb | 246 | for (i = TAILLE_TRAME_DATA; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 247 | { |
chevamax | 14:261007103beb | 248 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 249 | } |
chevamax | 14:261007103beb | 250 | wait_ms( 10 ); |
chevamax | 14:261007103beb | 251 | // Send the data |
chevamax | 14:261007103beb | 252 | Radio.Send( Buffer, BufferSize ); |
chevamax | 14:261007103beb | 253 | enAttente = true; |
GregCr | 0:1ed39951ab7b | 254 | } |
GregCr | 0:1ed39951ab7b | 255 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 256 | break; |
GregCr | 3:8b9e2a4df4b5 | 257 | case TX: |
GregCr | 3:8b9e2a4df4b5 | 258 | led = !led; |
chevamax | 14:261007103beb | 259 | debug( "Envoi des donnees...\r\n" ); |
GregCr | 0:1ed39951ab7b | 260 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 261 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 262 | break; |
GregCr | 0:1ed39951ab7b | 263 | case RX_TIMEOUT: |
chevamax | 14:261007103beb | 264 | debug("envoie2\r\n"); |
chevamax | 14:261007103beb | 265 | dataPaquet.mettreAJourEtatPompe((char) 0); |
chevamax | 14:261007103beb | 266 | dataPaquet.mettreAJourNiveauCuve((char) 0x1F); |
chevamax | 14:261007103beb | 267 | dataPaquet.mettreAJourNiveauBatterie((char) 0xF); //TODO |
chevamax | 14:261007103beb | 268 | |
chevamax | 14:261007103beb | 269 | char * trame = dataPaquet.creerTrame(); |
chevamax | 14:261007103beb | 270 | debug( trame); |
chevamax | 14:261007103beb | 271 | strcpy( ( char* ) Buffer, trame); |
chevamax | 14:261007103beb | 272 | for (i = TAILLE_TRAME_DATA; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 273 | { |
chevamax | 14:261007103beb | 274 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 275 | } |
chevamax | 14:261007103beb | 276 | wait_ms( 10 ); |
chevamax | 14:261007103beb | 277 | // Send the data |
chevamax | 14:261007103beb | 278 | Radio.Send( Buffer, BufferSize ); |
chevamax | 14:261007103beb | 279 | enAttente = true; |
chevamax | 14:261007103beb | 280 | //On attend 5 minutes |
chevamax | 14:261007103beb | 281 | //debug("Dormir\r\n"); |
chevamax | 14:261007103beb | 282 | //wait(10); |
chevamax | 14:261007103beb | 283 | //enAttente = false; |
chevamax | 14:261007103beb | 284 | //Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 285 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 286 | break; |
GregCr | 0:1ed39951ab7b | 287 | case RX_ERROR: |
chevamax | 14:261007103beb | 288 | //Erreur CRC, faire une demande de renvoi |
chevamax | 14:261007103beb | 289 | //TODO |
GregCr | 7:c1bbd6c56979 | 290 | // We have received a Packet with a CRC error, send reply as if packet was correct |
chevamax | 14:261007103beb | 291 | /*if( isMaster == true ) |
GregCr | 0:1ed39951ab7b | 292 | { |
GregCr | 0:1ed39951ab7b | 293 | // Send the next PING frame |
GregCr | 7:c1bbd6c56979 | 294 | strcpy( ( char* )Buffer, ( char* )PingMsg ); |
GregCr | 0:1ed39951ab7b | 295 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 0:1ed39951ab7b | 296 | { |
GregCr | 0:1ed39951ab7b | 297 | Buffer[i] = i - 4; |
GregCr | 0:1ed39951ab7b | 298 | } |
GregCr | 0:1ed39951ab7b | 299 | wait_ms( 10 ); |
GregCr | 0:1ed39951ab7b | 300 | Radio.Send( Buffer, BufferSize ); |
GregCr | 0:1ed39951ab7b | 301 | } |
GregCr | 0:1ed39951ab7b | 302 | else |
GregCr | 0:1ed39951ab7b | 303 | { |
GregCr | 2:59e108728d71 | 304 | // Send the next PONG frame |
GregCr | 7:c1bbd6c56979 | 305 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
GregCr | 2:59e108728d71 | 306 | for( i = 4; i < BufferSize; i++ ) |
GregCr | 2:59e108728d71 | 307 | { |
GregCr | 2:59e108728d71 | 308 | Buffer[i] = i - 4; |
GregCr | 2:59e108728d71 | 309 | } |
GregCr | 2:59e108728d71 | 310 | wait_ms( 10 ); |
GregCr | 2:59e108728d71 | 311 | Radio.Send( Buffer, BufferSize ); |
chevamax | 14:261007103beb | 312 | }*/ |
chevamax | 14:261007103beb | 313 | debug("Erreur CRC \r\n"); |
chevamax | 14:261007103beb | 314 | Radio.Rx( RX_TIMEOUT_VALUE ); |
chevamax | 14:261007103beb | 315 | |
GregCr | 0:1ed39951ab7b | 316 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 317 | break; |
GregCr | 0:1ed39951ab7b | 318 | case TX_TIMEOUT: |
GregCr | 0:1ed39951ab7b | 319 | Radio.Rx( RX_TIMEOUT_VALUE ); |
GregCr | 0:1ed39951ab7b | 320 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 321 | break; |
GregCr | 0:1ed39951ab7b | 322 | case LOWPOWER: |
GregCr | 0:1ed39951ab7b | 323 | break; |
GregCr | 0:1ed39951ab7b | 324 | default: |
GregCr | 0:1ed39951ab7b | 325 | State = LOWPOWER; |
GregCr | 0:1ed39951ab7b | 326 | break; |
GregCr | 0:1ed39951ab7b | 327 | } |
GregCr | 0:1ed39951ab7b | 328 | } |
GregCr | 0:1ed39951ab7b | 329 | } |
GregCr | 0:1ed39951ab7b | 330 | |
GregCr | 0:1ed39951ab7b | 331 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 332 | { |
GregCr | 5:f2431c4fe3bb | 333 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 334 | State = TX; |
GregCr | 7:c1bbd6c56979 | 335 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 336 | } |
GregCr | 0:1ed39951ab7b | 337 | |
GregCr | 4:5ece30264cd9 | 338 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
GregCr | 0:1ed39951ab7b | 339 | { |
GregCr | 0:1ed39951ab7b | 340 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 341 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 342 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 343 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 344 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 345 | State = RX; |
GregCr | 7:c1bbd6c56979 | 346 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 347 | } |
GregCr | 0:1ed39951ab7b | 348 | |
GregCr | 0:1ed39951ab7b | 349 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 350 | { |
GregCr | 0:1ed39951ab7b | 351 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 352 | State = TX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 353 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 354 | } |
GregCr | 0:1ed39951ab7b | 355 | |
GregCr | 0:1ed39951ab7b | 356 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 357 | { |
GregCr | 0:1ed39951ab7b | 358 | Radio.Sleep( ); |
GregCr | 1:126d70d374f6 | 359 | Buffer[ BufferSize ] = 0; |
GregCr | 0:1ed39951ab7b | 360 | State = RX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 361 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 362 | } |
GregCr | 0:1ed39951ab7b | 363 | |
GregCr | 0:1ed39951ab7b | 364 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 365 | { |
GregCr | 0:1ed39951ab7b | 366 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 367 | State = RX_ERROR; |
GregCr | 7:c1bbd6c56979 | 368 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 369 | } |
GregCr | 3:8b9e2a4df4b5 | 370 |