JPEG compressor

Dependencies:   SDL_lib SX1276Lib mbed

Committer:
miruga27
Date:
Thu Sep 22 00:01:55 2016 +0000
Revision:
1:f0c646dfe574
Child:
2:f256eebcade8
H261_encoder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
miruga27 1:f0c646dfe574 1 //Author: Miguel Ruiz García
miruga27 1:f0c646dfe574 2 //Company: University of Cantabria. 2016
miruga27 1:f0c646dfe574 3 //mail: mrg47@alumnos.unican.es
miruga27 1:f0c646dfe574 4 //code for TX & RX
miruga27 1:f0c646dfe574 5 #include "mbed.h"
miruga27 1:f0c646dfe574 6 #include "lora2.h"
miruga27 1:f0c646dfe574 7 #include "sx1276-hal.h"
miruga27 1:f0c646dfe574 8 #include "debug.h"
miruga27 1:f0c646dfe574 9 //#include "functions.h"
miruga27 1:f0c646dfe574 10 /* Set this flag to '1' to display debug messages on the console */
miruga27 1:f0c646dfe574 11 #define DEBUG_MESSAGE 1
miruga27 1:f0c646dfe574 12
miruga27 1:f0c646dfe574 13
miruga27 1:f0c646dfe574 14 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
miruga27 1:f0c646dfe574 15 #define USE_MODEM_LORA 1
miruga27 1:f0c646dfe574 16 #define USE_MODEM_FSK !USE_MODEM_LORA
miruga27 1:f0c646dfe574 17
miruga27 1:f0c646dfe574 18 #define RF_FREQUENCY 869000000 // Hz
miruga27 1:f0c646dfe574 19 #define TX_OUTPUT_POWER 14 // 14 dBm
miruga27 1:f0c646dfe574 20
miruga27 1:f0c646dfe574 21 #if USE_MODEM_LORA == 1
miruga27 1:f0c646dfe574 22
miruga27 1:f0c646dfe574 23 #define LORA_BANDWIDTH 1 // [0: 125 kHz,
miruga27 1:f0c646dfe574 24 // 1: 250 kHz,
miruga27 1:f0c646dfe574 25 // 2: 500 kHz,
miruga27 1:f0c646dfe574 26 // 3: Reserved]
miruga27 1:f0c646dfe574 27 #define LORA_SPREADING_FACTOR 10 // [SF7..SF12]
miruga27 1:f0c646dfe574 28 #define LORA_CODINGRATE 1 // [1: 4/5,
miruga27 1:f0c646dfe574 29 // 2: 4/6,
miruga27 1:f0c646dfe574 30 // 3: 4/7,
miruga27 1:f0c646dfe574 31 // 4: 4/8]
miruga27 1:f0c646dfe574 32 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
miruga27 1:f0c646dfe574 33 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
miruga27 1:f0c646dfe574 34 #define LORA_FIX_LENGTH_PAYLOAD_ON false
miruga27 1:f0c646dfe574 35 #define LORA_FHSS_ENABLED true
miruga27 1:f0c646dfe574 36 #define LORA_NB_SYMB_HOP 4
miruga27 1:f0c646dfe574 37 #define LORA_IQ_INVERSION_ON false
miruga27 1:f0c646dfe574 38 #define LORA_CRC_ENABLED true
miruga27 1:f0c646dfe574 39
miruga27 1:f0c646dfe574 40 #elif USE_MODEM_FSK == 1
miruga27 1:f0c646dfe574 41
miruga27 1:f0c646dfe574 42 #define FSK_FDEV 25000 // Hz
miruga27 1:f0c646dfe574 43 #define FSK_DATARATE 19200 // bps
miruga27 1:f0c646dfe574 44 #define FSK_BANDWIDTH 50000 // Hz
miruga27 1:f0c646dfe574 45 #define FSK_AFC_BANDWIDTH 83333 // Hz
miruga27 1:f0c646dfe574 46 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
miruga27 1:f0c646dfe574 47 #define FSK_FIX_LENGTH_PAYLOAD_ON false
miruga27 1:f0c646dfe574 48 #define FSK_CRC_ENABLED true
miruga27 1:f0c646dfe574 49
miruga27 1:f0c646dfe574 50 #else
miruga27 1:f0c646dfe574 51 #error "Please define a modem in the compiler options."
miruga27 1:f0c646dfe574 52 #endif
miruga27 1:f0c646dfe574 53
miruga27 1:f0c646dfe574 54 #define RX_TIMEOUT_VALUE 3500000 // in us
miruga27 1:f0c646dfe574 55 #define BUFFER_SIZE 255 // Define the payload size here [min:1 max:255]
miruga27 1:f0c646dfe574 56
miruga27 1:f0c646dfe574 57 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
miruga27 1:f0c646dfe574 58 DigitalOut led(LED2);
miruga27 1:f0c646dfe574 59 #else
miruga27 1:f0c646dfe574 60 DigitalOut led(LED1);
miruga27 1:f0c646dfe574 61 #endif
miruga27 1:f0c646dfe574 62
miruga27 1:f0c646dfe574 63
miruga27 1:f0c646dfe574 64 /*
miruga27 1:f0c646dfe574 65 * Global variables declarations
miruga27 1:f0c646dfe574 66 */
miruga27 1:f0c646dfe574 67 typedef enum
miruga27 1:f0c646dfe574 68 {
miruga27 1:f0c646dfe574 69 LOWPOWER = 0,
miruga27 1:f0c646dfe574 70 IDLE,
miruga27 1:f0c646dfe574 71
miruga27 1:f0c646dfe574 72 RX,
miruga27 1:f0c646dfe574 73 RX_TIMEOUT,
miruga27 1:f0c646dfe574 74 RX_ERROR,
miruga27 1:f0c646dfe574 75
miruga27 1:f0c646dfe574 76 TX,
miruga27 1:f0c646dfe574 77 TX_TIMEOUT,
miruga27 1:f0c646dfe574 78
miruga27 1:f0c646dfe574 79 CAD,
miruga27 1:f0c646dfe574 80 CAD_DONE
miruga27 1:f0c646dfe574 81 }AppStates_t;
miruga27 1:f0c646dfe574 82
miruga27 1:f0c646dfe574 83 volatile AppStates_t State = LOWPOWER;
miruga27 1:f0c646dfe574 84
miruga27 1:f0c646dfe574 85 /*!
miruga27 1:f0c646dfe574 86 * Radio events function pointer
miruga27 1:f0c646dfe574 87 */
miruga27 1:f0c646dfe574 88 static RadioEvents_t RadioEvents;
miruga27 1:f0c646dfe574 89
miruga27 1:f0c646dfe574 90 /*
miruga27 1:f0c646dfe574 91 * Global variables declarations
miruga27 1:f0c646dfe574 92 */
miruga27 1:f0c646dfe574 93 SX1276MB1xAS Radio( NULL );
miruga27 1:f0c646dfe574 94
miruga27 1:f0c646dfe574 95 const uint8_t PingMsg[] = "PING";
miruga27 1:f0c646dfe574 96 const uint8_t PongMsg[] = "PONG";
miruga27 1:f0c646dfe574 97 const uint8_t AckMsg[] = "ACK";
miruga27 1:f0c646dfe574 98 const uint8_t EOPMsg[] = "EOP";
miruga27 1:f0c646dfe574 99 const uint8_t RfSMsg[] = "RfS";
miruga27 1:f0c646dfe574 100 int py=0,pu=0,pv=0,py2=0,pu2=0,pv2=0;
miruga27 1:f0c646dfe574 101 int py_ant=0,pu_ant=0,pv_ant=0;
miruga27 1:f0c646dfe574 102 int flag_y=0,flag_u=0,flag_v=0;
miruga27 1:f0c646dfe574 103 uint16_t BufferSize = BUFFER_SIZE;
miruga27 1:f0c646dfe574 104 uint8_t Buffer[BUFFER_SIZE];
miruga27 1:f0c646dfe574 105 //uint8_t Buffer2[4];
miruga27 1:f0c646dfe574 106
miruga27 1:f0c646dfe574 107 int16_t RssiValue = 0.0;
miruga27 1:f0c646dfe574 108 int8_t SnrValue = 0.0;
miruga27 1:f0c646dfe574 109
miruga27 1:f0c646dfe574 110 bool first_y=true,first_u=true,first_v=true;
miruga27 1:f0c646dfe574 111 void lora(short int *temp1,short int index1,short int *temp2,short int index2,short int *temp3,short int index3)
miruga27 1:f0c646dfe574 112 {
miruga27 1:f0c646dfe574 113 //index1=300;index2=300;index3=300;
miruga27 1:f0c646dfe574 114 uint8_t i;
miruga27 1:f0c646dfe574 115 bool isMaster = true;//MASTER (TX) OR SLAVE(RX) ?
miruga27 1:f0c646dfe574 116
miruga27 1:f0c646dfe574 117 //pc.baud(BPS);
miruga27 1:f0c646dfe574 118 //pc.format (BITS,SerialBase::None, STOP_BITS) ;
miruga27 1:f0c646dfe574 119
miruga27 1:f0c646dfe574 120
miruga27 1:f0c646dfe574 121
miruga27 1:f0c646dfe574 122 //debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" );
miruga27 1:f0c646dfe574 123 //pc.printf("\n\n\r SX1276 Image transfer Application \n\n\r");
miruga27 1:f0c646dfe574 124
miruga27 1:f0c646dfe574 125 // Initialize Radio driver
miruga27 1:f0c646dfe574 126 RadioEvents.TxDone = OnTxDone;
miruga27 1:f0c646dfe574 127 RadioEvents.RxDone = OnRxDone;
miruga27 1:f0c646dfe574 128 RadioEvents.RxError = OnRxError;
miruga27 1:f0c646dfe574 129 RadioEvents.TxTimeout = OnTxTimeout;
miruga27 1:f0c646dfe574 130 RadioEvents.RxTimeout = OnRxTimeout;
miruga27 1:f0c646dfe574 131 RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
miruga27 1:f0c646dfe574 132 Radio.Init( &RadioEvents );
miruga27 1:f0c646dfe574 133
miruga27 1:f0c646dfe574 134 // verify the connection with the board
miruga27 1:f0c646dfe574 135 goto start;
miruga27 1:f0c646dfe574 136 while( Radio.Read( REG_VERSION ) == 0x00 )
miruga27 1:f0c646dfe574 137 {
miruga27 1:f0c646dfe574 138 //debug( "Radio could not be detected!\n\r", NULL );
miruga27 1:f0c646dfe574 139 //pc.printf( "Radio could not be detected!\n\r", NULL );
miruga27 1:f0c646dfe574 140 wait( 1 );
miruga27 1:f0c646dfe574 141 }
miruga27 1:f0c646dfe574 142 start:
miruga27 1:f0c646dfe574 143 //debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" );
miruga27 1:f0c646dfe574 144 //debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" );
miruga27 1:f0c646dfe574 145
miruga27 1:f0c646dfe574 146 Radio.SetChannel( HoppingFrequencies[0] );
miruga27 1:f0c646dfe574 147
miruga27 1:f0c646dfe574 148 #if USE_MODEM_LORA == 1
miruga27 1:f0c646dfe574 149
miruga27 1:f0c646dfe574 150 //debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
miruga27 1:f0c646dfe574 151 //debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
miruga27 1:f0c646dfe574 152
miruga27 1:f0c646dfe574 153 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
miruga27 1:f0c646dfe574 154 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
miruga27 1:f0c646dfe574 155 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
miruga27 1:f0c646dfe574 156 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
miruga27 1:f0c646dfe574 157 LORA_IQ_INVERSION_ON, 4000000 );
miruga27 1:f0c646dfe574 158
miruga27 1:f0c646dfe574 159 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
miruga27 1:f0c646dfe574 160 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
miruga27 1:f0c646dfe574 161 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
miruga27 1:f0c646dfe574 162 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
miruga27 1:f0c646dfe574 163 LORA_IQ_INVERSION_ON, true );
miruga27 1:f0c646dfe574 164
miruga27 1:f0c646dfe574 165 #elif USE_MODEM_FSK == 1
miruga27 1:f0c646dfe574 166
miruga27 1:f0c646dfe574 167 debug("\n\n\r > FSK Mode < \n\n\r");
miruga27 1:f0c646dfe574 168 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
miruga27 1:f0c646dfe574 169 FSK_DATARATE, 0,
miruga27 1:f0c646dfe574 170 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
miruga27 1:f0c646dfe574 171 FSK_CRC_ENABLED, 0, 0, 0, 3000000 );
miruga27 1:f0c646dfe574 172
miruga27 1:f0c646dfe574 173 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
miruga27 1:f0c646dfe574 174 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
miruga27 1:f0c646dfe574 175 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
miruga27 1:f0c646dfe574 176 0, 0, false, true );
miruga27 1:f0c646dfe574 177
miruga27 1:f0c646dfe574 178 #else
miruga27 1:f0c646dfe574 179
miruga27 1:f0c646dfe574 180 #error "Please define a modem in the compiler options."
miruga27 1:f0c646dfe574 181
miruga27 1:f0c646dfe574 182 #endif
miruga27 1:f0c646dfe574 183
miruga27 1:f0c646dfe574 184 //debug_if( DEBUG_MESSAGE, "Starting image transmission loop\r\n" );
miruga27 1:f0c646dfe574 185 //pc.printf("Starting image transmission loop\r\n");
miruga27 1:f0c646dfe574 186
miruga27 1:f0c646dfe574 187 led = 0;
miruga27 1:f0c646dfe574 188
miruga27 1:f0c646dfe574 189 Radio.Rx( RX_TIMEOUT_VALUE );
miruga27 1:f0c646dfe574 190 State=TX;
miruga27 1:f0c646dfe574 191 while( 1 )
miruga27 1:f0c646dfe574 192 {
miruga27 1:f0c646dfe574 193 switch( State )
miruga27 1:f0c646dfe574 194 {
miruga27 1:f0c646dfe574 195 case RX:
miruga27 1:f0c646dfe574 196 if( isMaster == true )
miruga27 1:f0c646dfe574 197 {
miruga27 1:f0c646dfe574 198 //debug( "waiting for response....\r\n" );
miruga27 1:f0c646dfe574 199 if( BufferSize > 0 )
miruga27 1:f0c646dfe574 200 {
miruga27 1:f0c646dfe574 201
miruga27 1:f0c646dfe574 202 if( strncmp( ( const char* )Buffer, ( const char* )AckMsg, 3 ) != 0 )
miruga27 1:f0c646dfe574 203 {
miruga27 1:f0c646dfe574 204 led = !led;
miruga27 1:f0c646dfe574 205 //debug( "Packet acknoledged....\r\n" );
miruga27 1:f0c646dfe574 206 // pc.printf( "Packet acknoledged....\r\n" );
miruga27 1:f0c646dfe574 207 // Send the next frame
miruga27 1:f0c646dfe574 208 State = TX;
miruga27 1:f0c646dfe574 209 }
miruga27 1:f0c646dfe574 210
miruga27 1:f0c646dfe574 211 else if( strncmp( ( const char* )Buffer, ( const char* )RfSMsg, 3 ) == 0 ){
miruga27 1:f0c646dfe574 212
miruga27 1:f0c646dfe574 213 led = !led;
miruga27 1:f0c646dfe574 214 debug( "Retransmit packet....\r\n" );
miruga27 1:f0c646dfe574 215 // pc.printf( "Retransmit packet....\r\n" );
miruga27 1:f0c646dfe574 216 // Send the next frame
miruga27 1:f0c646dfe574 217 State = TX;
miruga27 1:f0c646dfe574 218 py=py_ant; pu=pu_ant; pv=pv_ant;//Si hay que reenviar el paquete se reestablecen los valores de punteros.
miruga27 1:f0c646dfe574 219 }
miruga27 1:f0c646dfe574 220
miruga27 1:f0c646dfe574 221 else // valid reception but neither a message
miruga27 1:f0c646dfe574 222 { // Set device as master ans start again
miruga27 1:f0c646dfe574 223 debug( "no message received...\r\n" );
miruga27 1:f0c646dfe574 224 // pc.printf( "hola...\r\n" );
miruga27 1:f0c646dfe574 225 isMaster = true;//this intruction is DANGEROUS!!!!!!
miruga27 1:f0c646dfe574 226 Radio.Rx( RX_TIMEOUT_VALUE );
miruga27 1:f0c646dfe574 227 }
miruga27 1:f0c646dfe574 228 }
miruga27 1:f0c646dfe574 229 }
miruga27 1:f0c646dfe574 230 else
miruga27 1:f0c646dfe574 231 {
miruga27 1:f0c646dfe574 232 if( BufferSize > 0 )
miruga27 1:f0c646dfe574 233 {
miruga27 1:f0c646dfe574 234
miruga27 1:f0c646dfe574 235 led = !led;
miruga27 1:f0c646dfe574 236 debug( "Reading... \r\n" );
miruga27 1:f0c646dfe574 237 // pc.printf( "Reading... \r\n" );
miruga27 1:f0c646dfe574 238
miruga27 1:f0c646dfe574 239 int pointer=0;
miruga27 1:f0c646dfe574 240 int length_y=0,length_v=0,length_u=0;
miruga27 1:f0c646dfe574 241 for(pointer=0;pointer<254;pointer+=4){
miruga27 1:f0c646dfe574 242 if(flag_y==0) {//se comprueba si se ha cargado en el buffer de transmisión cada una de las componentes
miruga27 1:f0c646dfe574 243 if(first_y == true) {//EL PRIMER DATO ES LA LONGITUD
miruga27 1:f0c646dfe574 244 length_y=0xff & Buffer[pointer+1];
miruga27 1:f0c646dfe574 245 length_y=length_y<<8;
miruga27 1:f0c646dfe574 246 length_y=length_y | Buffer[pointer];
miruga27 1:f0c646dfe574 247 first_y = false;
miruga27 1:f0c646dfe574 248 temp1=(short int *)malloc(sizeof(short int)*length_y);
miruga27 1:f0c646dfe574 249 pointer-=2;//this is to avoid lose the first byte of the component
miruga27 1:f0c646dfe574 250 //in the cycle 'pointer+=4' but in the first package of the component there´s no a pair [length,value]
miruga27 1:f0c646dfe574 251
miruga27 1:f0c646dfe574 252 }
miruga27 1:f0c646dfe574 253 else{
miruga27 1:f0c646dfe574 254 if(Buffer[pointer]!='\t'){//VERIFY the character is not EOC (end of component y u v)
miruga27 1:f0c646dfe574 255
miruga27 1:f0c646dfe574 256 short int num=0;
miruga27 1:f0c646dfe574 257 num=0xff & Buffer[pointer+1];
miruga27 1:f0c646dfe574 258 num=num<<8;
miruga27 1:f0c646dfe574 259 num=num | Buffer[pointer];
miruga27 1:f0c646dfe574 260 *(temp1+py2)=num;
miruga27 1:f0c646dfe574 261 py2++;
miruga27 1:f0c646dfe574 262
miruga27 1:f0c646dfe574 263 if (py2 > length_y) {flag_y=1;}
miruga27 1:f0c646dfe574 264
miruga27 1:f0c646dfe574 265 num=0xff & Buffer[pointer+3];
miruga27 1:f0c646dfe574 266 num=num<<8;
miruga27 1:f0c646dfe574 267 num=num | Buffer[pointer+2];
miruga27 1:f0c646dfe574 268 *(temp1+py2)=num;
miruga27 1:f0c646dfe574 269 py2++;
miruga27 1:f0c646dfe574 270
miruga27 1:f0c646dfe574 271 if (py2 > length_y) {flag_y=1;}//if all bytes of the component are read, flag raised
miruga27 1:f0c646dfe574 272 //originally designed with goto.
miruga27 1:f0c646dfe574 273 }
miruga27 1:f0c646dfe574 274 else {
miruga27 1:f0c646dfe574 275 flag_y=1;
miruga27 1:f0c646dfe574 276 }
miruga27 1:f0c646dfe574 277 }
miruga27 1:f0c646dfe574 278 }
miruga27 1:f0c646dfe574 279
miruga27 1:f0c646dfe574 280
miruga27 1:f0c646dfe574 281 else if(flag_u==0){
miruga27 1:f0c646dfe574 282 if(first_u == true) {
miruga27 1:f0c646dfe574 283 length_u=0xff & Buffer[pointer+1];
miruga27 1:f0c646dfe574 284 length_u=length_u<<8;
miruga27 1:f0c646dfe574 285 length_u=length_u | Buffer[pointer];
miruga27 1:f0c646dfe574 286 first_u = false;
miruga27 1:f0c646dfe574 287 temp2=(short int *)malloc(sizeof(short int)*length_u);
miruga27 1:f0c646dfe574 288 pointer-=2;
miruga27 1:f0c646dfe574 289 }
miruga27 1:f0c646dfe574 290 else{
miruga27 1:f0c646dfe574 291 if(Buffer[pointer]!='\t'){//VERIFY the character is not "end of component"
miruga27 1:f0c646dfe574 292
miruga27 1:f0c646dfe574 293 short int num=0;
miruga27 1:f0c646dfe574 294 num=0xff & Buffer[pointer+1];
miruga27 1:f0c646dfe574 295 num=num<<8;
miruga27 1:f0c646dfe574 296 num=num | Buffer[pointer];
miruga27 1:f0c646dfe574 297 *(temp2+pu2)=num;
miruga27 1:f0c646dfe574 298 pu2++;
miruga27 1:f0c646dfe574 299
miruga27 1:f0c646dfe574 300 if (pu2 > length_u) {flag_u=1;}
miruga27 1:f0c646dfe574 301
miruga27 1:f0c646dfe574 302 num=0xff & Buffer[pointer+3];
miruga27 1:f0c646dfe574 303 num=num<<8;
miruga27 1:f0c646dfe574 304 num=num | Buffer[pointer+2];
miruga27 1:f0c646dfe574 305 *(temp2+pu2)=num;
miruga27 1:f0c646dfe574 306 pu2++;
miruga27 1:f0c646dfe574 307
miruga27 1:f0c646dfe574 308 if (pu2 > length_u) {flag_u=1;}
miruga27 1:f0c646dfe574 309 }
miruga27 1:f0c646dfe574 310 else {
miruga27 1:f0c646dfe574 311 flag_u=1;
miruga27 1:f0c646dfe574 312 }
miruga27 1:f0c646dfe574 313 }
miruga27 1:f0c646dfe574 314 }
miruga27 1:f0c646dfe574 315
miruga27 1:f0c646dfe574 316 else{
miruga27 1:f0c646dfe574 317 if(first_v == true) {
miruga27 1:f0c646dfe574 318 length_v=0xff & Buffer[pointer+1];
miruga27 1:f0c646dfe574 319 length_v=length_v<<8;
miruga27 1:f0c646dfe574 320 length_v=length_v | Buffer[pointer];
miruga27 1:f0c646dfe574 321 first_v = false;
miruga27 1:f0c646dfe574 322 temp3=(short int *)malloc(sizeof(short int)*length_v);
miruga27 1:f0c646dfe574 323 pointer-=2;
miruga27 1:f0c646dfe574 324 }
miruga27 1:f0c646dfe574 325 else{
miruga27 1:f0c646dfe574 326 if(Buffer[pointer]!='\n'){//VERIFY the character is not EOT
miruga27 1:f0c646dfe574 327
miruga27 1:f0c646dfe574 328 short int num=0;
miruga27 1:f0c646dfe574 329 num=0xff & Buffer[pointer+1];
miruga27 1:f0c646dfe574 330 num=num<<8;
miruga27 1:f0c646dfe574 331 num=num | Buffer[pointer];
miruga27 1:f0c646dfe574 332 *(temp3+pv2)=num;
miruga27 1:f0c646dfe574 333 py2++;
miruga27 1:f0c646dfe574 334
miruga27 1:f0c646dfe574 335 if (pv2 > length_v) {flag_v=1;}
miruga27 1:f0c646dfe574 336
miruga27 1:f0c646dfe574 337 num=0xff & Buffer[pointer+3];
miruga27 1:f0c646dfe574 338 num=num<<8;
miruga27 1:f0c646dfe574 339 num=num | Buffer[pointer+2];
miruga27 1:f0c646dfe574 340 *(temp3+pv2)=num;
miruga27 1:f0c646dfe574 341 pv2++;
miruga27 1:f0c646dfe574 342
miruga27 1:f0c646dfe574 343 if (pv2 > length_v) {flag_v=1;}
miruga27 1:f0c646dfe574 344 }
miruga27 1:f0c646dfe574 345 else {
miruga27 1:f0c646dfe574 346 flag_v=1;
miruga27 1:f0c646dfe574 347 break;
miruga27 1:f0c646dfe574 348 }
miruga27 1:f0c646dfe574 349 }
miruga27 1:f0c646dfe574 350 }
miruga27 1:f0c646dfe574 351
miruga27 1:f0c646dfe574 352
miruga27 1:f0c646dfe574 353 }
miruga27 1:f0c646dfe574 354 if(flag_y==1 && flag_u==1 && flag_v ==1){
miruga27 1:f0c646dfe574 355 strcpy( ( char* )Buffer, ( char* )AckMsg );
miruga27 1:f0c646dfe574 356 // We fill the buffer with numbers for the payload
miruga27 1:f0c646dfe574 357 for( i = 4; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 358 {
miruga27 1:f0c646dfe574 359 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 360 }
miruga27 1:f0c646dfe574 361 wait_ms( 10 );
miruga27 1:f0c646dfe574 362 Radio.Send( Buffer, BufferSize );
miruga27 1:f0c646dfe574 363
miruga27 1:f0c646dfe574 364 first_y=true;first_u=true;first_v=true;
miruga27 1:f0c646dfe574 365
miruga27 1:f0c646dfe574 366 // debug( "Packet received \r\n" );
miruga27 1:f0c646dfe574 367 // pc.printf( "Packet received \r\n" );
miruga27 1:f0c646dfe574 368 State = LOWPOWER;
miruga27 1:f0c646dfe574 369 goto end;
miruga27 1:f0c646dfe574 370
miruga27 1:f0c646dfe574 371 }
miruga27 1:f0c646dfe574 372
miruga27 1:f0c646dfe574 373 State=TX;
miruga27 1:f0c646dfe574 374 }
miruga27 1:f0c646dfe574 375 }
miruga27 1:f0c646dfe574 376
miruga27 1:f0c646dfe574 377 break;
miruga27 1:f0c646dfe574 378 case TX:
miruga27 1:f0c646dfe574 379 led = !led;
miruga27 1:f0c646dfe574 380 if( isMaster == true )
miruga27 1:f0c646dfe574 381 {
miruga27 1:f0c646dfe574 382 //debug( "transmission of packet (320x8)...\r\n" );
miruga27 1:f0c646dfe574 383 //pc.printf( "transmission of packet (320x8)...\r\n" );
miruga27 1:f0c646dfe574 384 int pointer=0;
miruga27 1:f0c646dfe574 385
miruga27 1:f0c646dfe574 386
miruga27 1:f0c646dfe574 387 py_ant=py;pu_ant=pu;pv_ant=pv;
miruga27 1:f0c646dfe574 388 for(pointer=0;pointer<254;pointer+=2){
miruga27 1:f0c646dfe574 389
miruga27 1:f0c646dfe574 390 if(flag_y==0) {//se comprueba si se ha cargado en el buffer de transmisión cada una de las componentes
miruga27 1:f0c646dfe574 391 if(first_y == true) {//en primer lugar se manda la longitud de la componente y u v
miruga27 1:f0c646dfe574 392 Buffer[pointer]=index1&0xff;
miruga27 1:f0c646dfe574 393 Buffer[pointer+1]=(index1>>8)&0xff;
miruga27 1:f0c646dfe574 394 first_y = false;
miruga27 1:f0c646dfe574 395 //debug( "transmitting y length...\r\n" );
miruga27 1:f0c646dfe574 396 }
miruga27 1:f0c646dfe574 397 else{
miruga27 1:f0c646dfe574 398 Buffer[pointer]=*(temp1+py) & 0x00ff;
miruga27 1:f0c646dfe574 399 Buffer[pointer+1]=(*(temp1+py)>>8) & 0x00ff;
miruga27 1:f0c646dfe574 400 py++;
miruga27 1:f0c646dfe574 401 // debug( "transmitting y data..\r\n" );
miruga27 1:f0c646dfe574 402 if(py >= index1) {
miruga27 1:f0c646dfe574 403 flag_y=1;
miruga27 1:f0c646dfe574 404 Buffer[pointer+2]='\t';
miruga27 1:f0c646dfe574 405 pointer+=2;
miruga27 1:f0c646dfe574 406 }
miruga27 1:f0c646dfe574 407 }
miruga27 1:f0c646dfe574 408 }
miruga27 1:f0c646dfe574 409 else if(flag_u==0){
miruga27 1:f0c646dfe574 410 if(first_u == true) {
miruga27 1:f0c646dfe574 411 Buffer[pointer]=index2&0xff;
miruga27 1:f0c646dfe574 412 Buffer[pointer+1]=(index2>>8)&0xff;
miruga27 1:f0c646dfe574 413 first_u = false;
miruga27 1:f0c646dfe574 414 //debug( "transmitting u length...\r\n" );
miruga27 1:f0c646dfe574 415 }
miruga27 1:f0c646dfe574 416 else{
miruga27 1:f0c646dfe574 417 Buffer[pointer]=*(temp2+pu) & 0x00ff;
miruga27 1:f0c646dfe574 418 Buffer[pointer+1]=(*(temp2+pu)>>8) & 0x00ff;
miruga27 1:f0c646dfe574 419 pu++;
miruga27 1:f0c646dfe574 420 //debug( "transmitting u data...\r\n" );
miruga27 1:f0c646dfe574 421 if(pu>= index2) {
miruga27 1:f0c646dfe574 422 flag_u=1;
miruga27 1:f0c646dfe574 423 Buffer[pointer+2]='\t';
miruga27 1:f0c646dfe574 424 pointer+=2;
miruga27 1:f0c646dfe574 425 }
miruga27 1:f0c646dfe574 426 }
miruga27 1:f0c646dfe574 427 }
miruga27 1:f0c646dfe574 428
miruga27 1:f0c646dfe574 429 else{
miruga27 1:f0c646dfe574 430 if(first_v == true) {
miruga27 1:f0c646dfe574 431 Buffer[pointer]=index2&0xff;
miruga27 1:f0c646dfe574 432 Buffer[pointer+1]=(index2>>8)&0xff;
miruga27 1:f0c646dfe574 433 first_v = false;
miruga27 1:f0c646dfe574 434 //debug( "transmitting v length...\r\n" );
miruga27 1:f0c646dfe574 435 }
miruga27 1:f0c646dfe574 436 else{
miruga27 1:f0c646dfe574 437 Buffer[pointer]=*(temp3+pv) & 0x00ff;
miruga27 1:f0c646dfe574 438 Buffer[pointer+1]=(*(temp3+pv)>>8) & 0x00ff;
miruga27 1:f0c646dfe574 439 pv++;
miruga27 1:f0c646dfe574 440 //debug( "transmitting v data ...\r\n" );
miruga27 1:f0c646dfe574 441 if(pv >= index3) {
miruga27 1:f0c646dfe574 442 flag_v=1;
miruga27 1:f0c646dfe574 443 Buffer[pointer+2]='\n';//salto de línea es EOT
miruga27 1:f0c646dfe574 444 pointer+=2;
miruga27 1:f0c646dfe574 445 break;
miruga27 1:f0c646dfe574 446 }
miruga27 1:f0c646dfe574 447 }
miruga27 1:f0c646dfe574 448 }
miruga27 1:f0c646dfe574 449
miruga27 1:f0c646dfe574 450
miruga27 1:f0c646dfe574 451 }
miruga27 1:f0c646dfe574 452
miruga27 1:f0c646dfe574 453 if(flag_y==1 && flag_u==1 && flag_v==1){
miruga27 1:f0c646dfe574 454 Buffer[pointer]='\n';//salto de línea es EOT
miruga27 1:f0c646dfe574 455 flag_y=0;flag_u=0;flag_v=0;
miruga27 1:f0c646dfe574 456 //debug( "finished transmission ...\r\n" );
miruga27 1:f0c646dfe574 457 first_y=true;first_u=true;first_v=true;
miruga27 1:f0c646dfe574 458 goto end;
miruga27 1:f0c646dfe574 459 for( i = Buffer[pointer]; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 460 {
miruga27 1:f0c646dfe574 461 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 462 }
miruga27 1:f0c646dfe574 463 }
miruga27 1:f0c646dfe574 464 else{
miruga27 1:f0c646dfe574 465 for( i = Buffer[pointer]; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 466 {
miruga27 1:f0c646dfe574 467 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 468 }
miruga27 1:f0c646dfe574 469 }
miruga27 1:f0c646dfe574 470
miruga27 1:f0c646dfe574 471
miruga27 1:f0c646dfe574 472
miruga27 1:f0c646dfe574 473 wait_ms( 10 );
miruga27 1:f0c646dfe574 474 //debug( "Sending packet...\r\n" );
miruga27 1:f0c646dfe574 475 Radio.Send( Buffer, BufferSize );
miruga27 1:f0c646dfe574 476
miruga27 1:f0c646dfe574 477 // pc.printf( "Sending packet...\r\n" );
miruga27 1:f0c646dfe574 478 }
miruga27 1:f0c646dfe574 479
miruga27 1:f0c646dfe574 480 else
miruga27 1:f0c646dfe574 481 {
miruga27 1:f0c646dfe574 482 //debug( "hola...\r\n" );
miruga27 1:f0c646dfe574 483 //pc.printf( "hola...\r\n" );
miruga27 1:f0c646dfe574 484 strcpy( ( char* )Buffer, ( char* )AckMsg );
miruga27 1:f0c646dfe574 485 for( i = 4; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 486 {
miruga27 1:f0c646dfe574 487 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 488 }
miruga27 1:f0c646dfe574 489 wait_ms( 10 );
miruga27 1:f0c646dfe574 490 Radio.Send( Buffer, BufferSize );
miruga27 1:f0c646dfe574 491 Radio.Rx( RX_TIMEOUT_VALUE );
miruga27 1:f0c646dfe574 492 }
miruga27 1:f0c646dfe574 493
miruga27 1:f0c646dfe574 494 State = RX;
miruga27 1:f0c646dfe574 495 break;
miruga27 1:f0c646dfe574 496 case RX_TIMEOUT:
miruga27 1:f0c646dfe574 497 if( isMaster == true )
miruga27 1:f0c646dfe574 498 {
miruga27 1:f0c646dfe574 499 //debug( "hola rx_timeout...\r\n" );
miruga27 1:f0c646dfe574 500 //pc.printf( "hola...\r\n" );
miruga27 1:f0c646dfe574 501 // Send the next PING frame
miruga27 1:f0c646dfe574 502 strcpy( ( char* )Buffer, ( char* )RfSMsg );
miruga27 1:f0c646dfe574 503 for( i = 4; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 504 {
miruga27 1:f0c646dfe574 505 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 506 }
miruga27 1:f0c646dfe574 507 wait_ms( 10 );
miruga27 1:f0c646dfe574 508 Radio.Send( Buffer, BufferSize );
miruga27 1:f0c646dfe574 509 }
miruga27 1:f0c646dfe574 510 else
miruga27 1:f0c646dfe574 511 {
miruga27 1:f0c646dfe574 512 //debug( "hola...\r\n" );
miruga27 1:f0c646dfe574 513 //pc.printf( "hola...\r\n" );
miruga27 1:f0c646dfe574 514 strcpy( ( char* )Buffer, ( char* )RfSMsg );
miruga27 1:f0c646dfe574 515 for( i = 4; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 516 {
miruga27 1:f0c646dfe574 517 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 518 }
miruga27 1:f0c646dfe574 519 wait_ms( 10 );
miruga27 1:f0c646dfe574 520 Radio.Send( Buffer, BufferSize );
miruga27 1:f0c646dfe574 521 Radio.Rx( RX_TIMEOUT_VALUE );
miruga27 1:f0c646dfe574 522 }
miruga27 1:f0c646dfe574 523 State = LOWPOWER;
miruga27 1:f0c646dfe574 524 break;
miruga27 1:f0c646dfe574 525
miruga27 1:f0c646dfe574 526 case RX_ERROR:
miruga27 1:f0c646dfe574 527 // We have received a Packet with a CRC error, request packet again.
miruga27 1:f0c646dfe574 528 if( isMaster == true )
miruga27 1:f0c646dfe574 529 {
miruga27 1:f0c646dfe574 530
miruga27 1:f0c646dfe574 531 //debug( "hola rx_error...\r\n" );
miruga27 1:f0c646dfe574 532 //pc.printf( "hola...\r\n" );
miruga27 1:f0c646dfe574 533 strcpy( ( char* )Buffer, ( char* )RfSMsg);
miruga27 1:f0c646dfe574 534 for( i = 4; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 535 {
miruga27 1:f0c646dfe574 536 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 537 }
miruga27 1:f0c646dfe574 538
miruga27 1:f0c646dfe574 539 wait_ms( 10 );
miruga27 1:f0c646dfe574 540 Radio.Send( Buffer, BufferSize );
miruga27 1:f0c646dfe574 541 State = LOWPOWER;
miruga27 1:f0c646dfe574 542 }
miruga27 1:f0c646dfe574 543 else
miruga27 1:f0c646dfe574 544 {
miruga27 1:f0c646dfe574 545 //debug( "hola ...\r\n" );
miruga27 1:f0c646dfe574 546 //pc.printf( "hola...\r\n" );
miruga27 1:f0c646dfe574 547 // Send the RfS again
miruga27 1:f0c646dfe574 548 strcpy( ( char* )Buffer, ( char* )RfSMsg);
miruga27 1:f0c646dfe574 549 for( i = 4; i < BufferSize; i++ )
miruga27 1:f0c646dfe574 550 {
miruga27 1:f0c646dfe574 551 Buffer[i] = i - 4;
miruga27 1:f0c646dfe574 552 }
miruga27 1:f0c646dfe574 553 py=py_ant;pu=pu_ant;pv=pv_ant;
miruga27 1:f0c646dfe574 554 wait_ms( 10 );
miruga27 1:f0c646dfe574 555 Radio.Send( Buffer, BufferSize );
miruga27 1:f0c646dfe574 556 State = LOWPOWER;
miruga27 1:f0c646dfe574 557 }
miruga27 1:f0c646dfe574 558 //State = LOWPOWER;
miruga27 1:f0c646dfe574 559 break;
miruga27 1:f0c646dfe574 560 case TX_TIMEOUT:
miruga27 1:f0c646dfe574 561 Radio.Rx( RX_TIMEOUT_VALUE );
miruga27 1:f0c646dfe574 562 State = LOWPOWER;
miruga27 1:f0c646dfe574 563 break;
miruga27 1:f0c646dfe574 564 case LOWPOWER:
miruga27 1:f0c646dfe574 565 break;
miruga27 1:f0c646dfe574 566 default:
miruga27 1:f0c646dfe574 567 State = LOWPOWER;
miruga27 1:f0c646dfe574 568 break;
miruga27 1:f0c646dfe574 569 }
miruga27 1:f0c646dfe574 570 }
miruga27 1:f0c646dfe574 571 end:
miruga27 1:f0c646dfe574 572 int a;
miruga27 1:f0c646dfe574 573 }
miruga27 1:f0c646dfe574 574
miruga27 1:f0c646dfe574 575 void OnTxDone( void )
miruga27 1:f0c646dfe574 576 {
miruga27 1:f0c646dfe574 577 Radio.SetChannel( HoppingFrequencies[0] );
miruga27 1:f0c646dfe574 578 Radio.Sleep( );
miruga27 1:f0c646dfe574 579 State = TX;
miruga27 1:f0c646dfe574 580 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
miruga27 1:f0c646dfe574 581 }
miruga27 1:f0c646dfe574 582
miruga27 1:f0c646dfe574 583 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
miruga27 1:f0c646dfe574 584 {
miruga27 1:f0c646dfe574 585 Radio.SetChannel( HoppingFrequencies[0] );
miruga27 1:f0c646dfe574 586 Radio.Sleep( );
miruga27 1:f0c646dfe574 587 BufferSize = size;
miruga27 1:f0c646dfe574 588 memcpy( Buffer, payload, BufferSize );
miruga27 1:f0c646dfe574 589 RssiValue = rssi;
miruga27 1:f0c646dfe574 590 SnrValue = snr;
miruga27 1:f0c646dfe574 591 State = RX;
miruga27 1:f0c646dfe574 592 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
miruga27 1:f0c646dfe574 593 }
miruga27 1:f0c646dfe574 594
miruga27 1:f0c646dfe574 595 void OnTxTimeout( void )
miruga27 1:f0c646dfe574 596 {
miruga27 1:f0c646dfe574 597 Radio.SetChannel( HoppingFrequencies[0] );
miruga27 1:f0c646dfe574 598 Radio.Sleep( );
miruga27 1:f0c646dfe574 599 State = TX_TIMEOUT;
miruga27 1:f0c646dfe574 600 //debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
miruga27 1:f0c646dfe574 601 }
miruga27 1:f0c646dfe574 602
miruga27 1:f0c646dfe574 603 void OnRxTimeout( void )
miruga27 1:f0c646dfe574 604 {
miruga27 1:f0c646dfe574 605 Radio.SetChannel( HoppingFrequencies[0] );
miruga27 1:f0c646dfe574 606 Radio.Sleep( );
miruga27 1:f0c646dfe574 607 Buffer[ BufferSize ] = 0;
miruga27 1:f0c646dfe574 608 State = RX_TIMEOUT;
miruga27 1:f0c646dfe574 609 //debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
miruga27 1:f0c646dfe574 610 }
miruga27 1:f0c646dfe574 611
miruga27 1:f0c646dfe574 612 void OnRxError( void )
miruga27 1:f0c646dfe574 613 {
miruga27 1:f0c646dfe574 614 Radio.SetChannel( HoppingFrequencies[0] );
miruga27 1:f0c646dfe574 615 Radio.Sleep( );
miruga27 1:f0c646dfe574 616 State = RX_ERROR;
miruga27 1:f0c646dfe574 617 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
miruga27 1:f0c646dfe574 618 }
miruga27 1:f0c646dfe574 619
miruga27 1:f0c646dfe574 620 void OnFhssChangeChannel( uint8_t channelIndex )
miruga27 1:f0c646dfe574 621 {
miruga27 1:f0c646dfe574 622 Radio.SetChannel( HoppingFrequencies[channelIndex] );
miruga27 1:f0c646dfe574 623 debug_if( DEBUG_MESSAGE, "F%d-", channelIndex);
miruga27 1:f0c646dfe574 624 }