IOTMACH / Mbed 2 deprecated nRF51822_Caracteristicas_stable

Dependencies:   BLE_API mbed

Fork of nRF51822_SimpleControls_copy by IOTMACH

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Copyright (c) 2012-2014 RedBearLab
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005 and associated documentation files (the "Software"), to deal in the Software without restriction,
00006 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00007 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
00008 subject to the following conditions:
00009 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00010 
00011 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00012 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
00013 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
00014 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00015 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016 
00017 */
00018 
00019 #include "mbed.h"
00020 #include "ble/BLE.h"
00021 #include "GattCallbackParamTypes.h"
00022 
00023 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
00024 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
00025 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
00026 
00027 #define TXRX_BUF_LEN                     20
00028 
00029 #define ON                              1
00030 #define OFF                             0
00031 #define IN                              1
00032 #define OUT                             0
00033 #define PWM                             2
00034 #define NONE                            -1
00035 
00036 //static int MODE_MOTE;
00037 //Solo puede estar definido un solo Board
00038 //#define nRF51-DK_board
00039 #define redBearLab_board
00040 
00041 #ifdef nRF51-DK_board
00042 //Aqui se define el hardware del nRF51-DK
00043 
00044 //Analog in
00045 #define A0                               P0_1   //Analog 0
00046 #define A1                               P0_2   //Analog 1
00047 #define A2                               P0_3   //Analog 2
00048 #define A3                               P0_4   //Analog 3
00049 #define A4                               P0_5   //Analog 4
00050 #define A5                               P0_6   //Analog 5
00051 
00052 //Digital
00053 
00054 #define D0                               P0_14  //Digital I/O            
00055 #define D1                               P0_12  //Digital I/O
00056 #define D2                               P0_13  //Digital I/O 
00057 #define D3                               P0_15  //Digital I/O
00058 #define D4                               P0_21  //Digital I/O  (LED 1)
00059 #define D5                               P0_22  //Digital I/O  (LED 2)
00060 #define D6                               P0_23  //Digital I/O  (LED 3)
00061 #define D7                               P0_24  //Digital I/O  (LED 4)
00062 #define D8                               P0_17  //Digital I/O  (BUTTON 1) 
00063 #define D9                               P0_19  //Digital I/O  (BUTTON 3)
00064 #define D10                              P0_18  //Digital I/O  (BUTTON 2)
00065 #define D11                              P0_20  //Digital I/O  (BUTTON 4)
00066 #define D12                              P0_7   //Digital I/O
00067 #define D13                              P0_30  //Digital I/O
00068 #define D14                              P0_8   //Digital I/O
00069 #define D15                              P0_16  //Digital I/O
00070 
00071 //Aplicacion
00072 #define LED_1                            P0_21  //Digital I/O  (LED 1)
00073 #define LED_2                            P0_22  //Digital I/O  (LED 2)
00074 #define LED_3                            P0_23  //Digital I/O  (LED 3)              
00075 #define LED_4                            P0_24  //Digital I/O  (LED 4)
00076 
00077 #define BTN_1                            P0_17  //Digital I/O  (BUTTON 1)
00078 #define BTN_2                            P0_18  //Digital I/O  (BUTTON 2)
00079 #define BTN_3                            P0_19  //Digital I/O  (BUTTON 3)
00080 #define BTN_4                            P0_20  //Digital I/O  (BUTTON 4)
00081 
00082 // Aqui se acaba la configuracion del board NRF51-DK
00083 
00084 #else
00085 //Aqui se define el hardware del redBearLab
00086 //Analog in
00087 #define A0                               P0_1   //Analog 0
00088 #define A1                               P0_2   //Analog 0
00089 #define A2                               P0_3   //Analog 0
00090 #define A3                               P0_4   //Analog 0
00091 #define A4                               P0_5   //Analog 0
00092 #define A5                               P0_6   //Analog 0
00093 
00094 //Digital
00095 #define D0                               P0_11  //Digital I/O             
00096 #define D1                               P0_9   //Digital I/O
00097 #define D2                               P0_10  //Digital I/O
00098 #define D3                               P0_8   //Digital I/O
00099 #define D4                               P0_21  //Digital I/O
00100 #define D5                               P0_23  //Digital I/O (PWM RBL)
00101 #define D6                               P0_16  //Digital I/O (PWM RBL)
00102 #define D7                               P0_17  //Digital I/O  
00103 #define D8                               P0_19  //Digital I/O             
00104 #define D9                               P0_18  //Digital I/O (PWM RBL)
00105 #define D10                              P0_14  //Digital I/O
00106 #define D11                              P0_12  //Digital I/O
00107 #define D12                              P0_13  //Digital I/O (LED RBL)
00108 #define D13                              P0_15  //Digital I/O 
00109 #define D14                              P0_29  //Digital I/O
00110 #define D15                              P0_28  //Digital I/O
00111 
00112 //Aplicacion
00113 #define LED_1                            P0_21  //Digital I/O  (LED 1)
00114 #define LED_2                            P0_23  //Digital I/O  (LED 2)
00115 #define LED_3                            P0_16  //Digital I/O  (LED 3)              
00116 #define LED_4                            P0_17  //Digital I/O  (LED 4)
00117 
00118 #define BTN_1                            P0_11  //Digital I/O  (BUTTON 1)
00119 #define BTN_2                            P0_9   //Digital I/O  (BUTTON 2)
00120 #define BTN_3                            P0_10  //Digital I/O  (BUTTON 3)
00121 #define BTN_4                            P0_8   //Digital I/O  (BUTTON 4)
00122 
00123 #endif
00124 
00125 
00126 // Aqui se acabo la definicion del board REDBEARLAB
00127 
00128 /****  Aqui se definen los pines necesarios independientemente del board seleccionado ******************
00129 
00130                      Instrucciones:
00131 Pines digitales -->
00132                     ON  --> Usado
00133                     OFF --> No sera usado
00134                     IN  --> Entrada
00135                     OUT --> Salida
00136 
00137 Pines Analogicos -->
00138                     ON  --> Usado
00139                     OFF --> No sera usado
00140                     Valor del TED en numero reales, ejemplo 39.1, sino se va a usar se queda con 0
00141 ********************************************************************************************************/
00142 
00143 #define A0_USO                           OFF     //-> Divisor de voltaje para medir bateria VDD
00144 #define A1_USO                           ON     //-> Temperatura PT1000 (Borneras Shield de pruebas) Borneras
00145 #define A2_USO                           OFF     //-> Temperatura PT1000 (Borneras Shield de pruebas)
00146 #define A3_USO                           OFF    //-> Humedad (Borneras Shield de pruebas)
00147 #define A4_USO                           OFF     //-> Humedad (Borneras Shield de pruebas)
00148 #define A5_USO                           OFF     //-> Conectarlo al LM35
00149 
00150 #define A0_TED                           39.10  //Volts,  bateria
00151 #define A1_TED                           39.10   // ºC  temperatura PT 1000
00152 #define A2_TED                           39.10   // ºC  temperatura PT 1000
00153 #define A3_TED                           39.10
00154 #define A4_TED                           39.10
00155 #define A5_TED                           39.10   // ºC  temperatura ambiental con el LM35
00156 
00157 #define D0_TYPE                          NONE    // Ejemplo: OUT
00158 #define D1_TYPE                          NONE     // Ejemplo: IN
00159 #define D2_TYPE                          NONE    // Para medir Humedad con Resistencia AC
00160 #define D3_TYPE                          NONE    // Led encienden con 1
00161 #define D4_TYPE                          OUT    // Led encienden con 0
00162 #define D5_TYPE                          NONE    // Led encienden con 0
00163 #define D6_TYPE                          PWM // Led encienden con 1
00164 #define D7_TYPE                          OUT  // Pushbuttons  (Activos en bajo)
00165 #define D8_TYPE                          IN   // Pushbuttons  (Activos en bajo)
00166 #define D9_TYPE                          NONE    // DIP Switch 1 (Activos en bajo)
00167 #define D10_TYPE                         NONE     // DIP Switch 2 (Activos en bajo)
00168 #define D11_TYPE                         NONE     // DIP Switch 3 (Activos en bajo)
00169 #define D12_TYPE                         NONE    // Para medir Humedad con Resistencia AC
00170 #define D13_TYPE                         NONE     // DIP Switch 4 (Activos en bajo)
00171 #define D14_TYPE                         NONE    // Para medir Humedad con Resistencia AC
00172 #define D15_TYPE                         NONE    // Para medir Humedad con Resistencia AC
00173 
00174 //Macros de mbed
00175 /*** Aqui Se definen todas macros en funcion de si el bit esta ON u OFF   ************
00176 
00177 Estos son las macors ya definidas:
00178 DigitalOut      LED_SET(DIGITAL_OUT_PIN);   //Modo de uso --> LED_SET = 1 o LED_SET = 0;
00179 DigitalIn       BUTTON(DIGITAL_IN_PIN);     //Modo de uso --> if (BUTTON != old_state)
00180 PwmOut          PWM(PWM_PIN);               //Modo de uso -->  PWM = value;
00181 AnalogIn        ANALOGTEMP(ANALOG_IN_PIN);  //Modo de uso -> float s = ANALOG_A0;
00182 AnalogIn        ANALOGBAT(ANALOG_IN_BAT);
00183 Servo           MYSERVO(SERVO_PIN);
00184 *************************************************************************************/
00185 
00186 //Macros analogicas
00187 AnalogIn        ANALOG_A0(A0);
00188 AnalogIn        ANALOG_A1(A1);
00189 AnalogIn        ANALOG_A2(A2);
00190 AnalogIn        ANALOG_A3(A3);
00191 AnalogIn        ANALOG_A4(A4);
00192 AnalogIn        ANALOG_A5(A5);   //Temperatura ambiental con LM35
00193 
00194 // Indica si alguien esta conectado al dispositivo.
00195 DigitalOut PIN_CONNECT(D13);
00196 
00197 #if D2_TYPE == IN
00198     DigitalIn PIN_D2(D2);
00199 #elif  D2_TYPE == OUT
00200     DigitalOut PIN_D2(D2);
00201 #else
00202     int PIN_D2 = 0;
00203 #endif
00204 
00205 #if D3_TYPE == IN
00206     DigitalIn PIN_D3(D3);
00207 #elif D3_TYPE == OUT
00208     DigitalOut PIN_D3(D3);
00209 #else
00210     int PIN_D3 = 0;
00211 #endif
00212 
00213 #if D4_TYPE == NONE
00214     static int PIN_D4 = 0;
00215 #elif D4_TYPE == IN
00216     DigitalIn PIN_D4(D4);
00217 #elif D4_TYPE == OUT
00218     DigitalOut PIN_D4(D4);
00219 #else
00220     int PIN_D4 = 0;
00221 #endif
00222 
00223 #if D5_TYPE == IN
00224     DigitalIn PIN_D5(D5);
00225 #elif D5_TYPE == OUT
00226     DigitalOut PIN_D5(D5);
00227 #elif D5_TYPE == PWM
00228     PwmOut PIN_D5(D5);
00229 #else
00230     int PIN_D5 = 0;
00231 #endif
00232 
00233 #if D6_TYPE == IN
00234     DigitalIn PIN_D6(D6);
00235 #elif D6_TYPE == OUT
00236     DigitalOut PIN_D6(D6);
00237 #elif D6_TYPE == PWM
00238     PwmOut PIN_D6(D6);
00239 #else
00240     int PIN_D6 = 0;
00241 #endif
00242 
00243 #if D7_TYPE == IN
00244     DigitalIn PIN_D7(D7);
00245 #elif D7_TYPE == OUT
00246     DigitalOut PIN_D7(D7);
00247 #else
00248     int PIN_D7 = 0;
00249 #endif
00250 
00251 #if D8_TYPE == IN
00252     DigitalIn PIN_D8(D8);
00253 #elif D8_TYPE == OUT
00254     DigitalOut PIN_D8(D8);
00255 #else
00256     int PIN_D8 = 0;
00257 #endif
00258 
00259 #if D9_TYPE == IN
00260     DigitalIn PIN_D9(D9);
00261 #elif D9_TYPE == OUT
00262     DigitalOut PIN_D9(D9);
00263 #elif D9_TYPE == PWM
00264     PwmOut PIN_D9(D9);
00265 #else
00266     int PIN_D9 = 0;
00267 #endif
00268 
00269 
00270 #if D10_TYPE == IN
00271     DigitalIn PIN_D10(D10);
00272 #elif D10_TYPE == OUT
00273     DigitalOut PIN_D10(D10);
00274 #else
00275     int PIN_D10 = 0;
00276 #endif
00277 
00278 #if D11_TYPE == IN
00279     DigitalIn PIN_D11(D11);
00280 #elif D11_TYPE == OUT
00281     DigitalOut PIN_D11(D11);
00282 #else
00283     int PIN_D11 = 0;
00284 #endif
00285 
00286 #if D12_TYPE == IN
00287     DigitalIn PIN_D12(D12);
00288 #elif D12_TYPE == OUT
00289     DigitalOut PIN_D12(D12);
00290 #else
00291     int PIN_D12 = 0;
00292 #endif
00293 
00294 #if D13_TYPE == IN
00295     DigitalIn PIN_D13(D13);
00296 #elif D13_TYPE == OUT
00297     DigitalOut PIN_D13(D13);
00298 #else
00299     int PIN_D13 = 0;
00300 #endif
00301 
00302 #if D14_TYPE == IN
00303     DigitalIn PIN_D14(D14);
00304 #elif D14_TYPE == OUT
00305     DigitalOut PIN_D14(D14);
00306 #else
00307     int PIN_D14 = 0;
00308 #endif
00309 
00310 #if D15_TYPE == IN
00311     DigitalIn PIN_D15(D15);
00312 #elif D15_TYPE == OUT
00313     DigitalOut PIN_D15(D15);
00314 #else
00315     int PIN_D15 = 0;
00316 #endif
00317 
00318 
00319 //Hasta aqui la definicion de macos
00320 //Digital Inputs --> DATA VALUE
00321 uint16_t DigitalInput_DATA = 0x0000;            //  Mapa de bits con los valores de la entrada digital segun la posicion
00322 uint16_t DigitalOutput_DATA = 0x0000;           //  Mapa de bits con los valores de la entrada digital segun la posicion
00323 uint16_t ESTADO_ENTRADAS_DIGITALES = 0x0000;    //  Mapa de bits de los estados de las entradas digitales
00324 uint16_t ESTADO_SALIDAS_DIGITALES = 0x0000;     //  Mapa de bits de las salidas digitales
00325 uint16_t ESTADO_ENTRADAS_DIGITALES_OLD = 0x0000;//  Almacena temporalmente el estado de las salidas digitales.
00326 uint16_t PAQUETE_ID  = 0;
00327 static bool CONNECTED;
00328 float AnalogInput_Ted [6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};  //Si el TED es cero, es que no está en uso
00329 
00330 //Digital input, cantidad, posiciones --> de todos, ted = 0, tipico hasta 16
00331 uint16_t DigitalInput_Pos = 0x0000;    //Mapa de bits, seran usados los bytes del TED 16 bits
00332 
00333 //Digital output, cantidad, pocisiones --> de todos, ted = 0,  tipico hasta 16
00334 uint16_t DigitalOutput_Pos = 0x0000;    //Mapa de bits, seran usados los bytes del TED 16 bits
00335 
00336 uint16_t DigitalPwm_Pos = 0x0000;       //Mapa de bits, seran usados los bytes del TED 16 bits
00337 
00338 //Digital input, cantidad, posiciones --> de todos, ted = 0, tipico hasta 16
00339 uint8_t AnalogInput_Pos = 0x0000;    //Mapa de bits, seran usados los bytes del TED 8 bits
00340 
00341 //Digital output, cantidad, pocisiones --> de todos, ted = 0,  tipico hasta 16
00342 uint8_t AnalogOutput_Pos = 0x0000;   //Mapa de bits, seran usados los bytes del TED 8 bits
00343 
00344 
00345 // Declarando los pines
00346 //static int32_t send_config = 0;
00347 
00348 static int8_t SEND_CONFIG_GENERAL = 0;
00349 static int8_t SEND_CONFIG_ANALOG_0, SEND_CONFIG_ANALOG_1, SEND_CONFIG_ANALOG_2, SEND_CONFIG_ANALOG_3, SEND_CONFIG_ANALOG_4, SEND_CONFIG_ANALOG_5;
00350 static bool publish_D0;     //  Indica si es necesario informar al gateway sobre estado de las salidas digitales
00351 static bool publish_D1;     //  Indica si es necesario informar al gateway sobre estado de las entradas digitales
00352 
00353 BLE ble;
00354 
00355 // Permite imprimir mensajes en la consola
00356 Serial pc(USBTX, USBRX);
00357 
00358 //  Para las entradas digitales (Botones)
00359 
00360 //  Para las entradas analogicas (Sensores)
00361 static float value_A5, value_A4, value_A3, value_A2, value_A1, value_A0;
00362 
00363 
00364 // The Nordic UART Service
00365 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00366 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00367 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00368 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
00369 
00370 // Trama de Configuracion de los Pines (a).
00371 static uint8_t TRAMA_CONFIG_GENERAL[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00};        //  Length 12
00372 static uint8_t TRAMA_CONFIG_ANALOG[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};    //  Length 6
00373 static uint8_t LECTURA_DIGITAL[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};        //  Length 6
00374 static uint8_t LECTURA_ANALOGICA[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};      //  Length 5
00375 
00376 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00377 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00378 
00379 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00380 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00381 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
00382 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
00383 
00384 
00385 // https://developer.mbed.org/forum/repo-61676-BLE_GAP_Example-community/topic/17193/
00386 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
00387 {
00388     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00389     pc.printf("Disconnected \r\n");
00390     pc.printf("Restart advertising \r\n");
00391     ble.startAdvertising();
00392 
00393     SEND_CONFIG_GENERAL = 0;
00394 
00395     //  En caso de no completarse el envio de la configuraciones las detenemos por completo, y dehabilitamos el envio.
00396     SEND_CONFIG_ANALOG_0 = OFF;
00397     SEND_CONFIG_ANALOG_1 = OFF;
00398     SEND_CONFIG_ANALOG_2 = OFF;
00399     SEND_CONFIG_ANALOG_3 = OFF;
00400     SEND_CONFIG_ANALOG_4 = OFF;
00401     SEND_CONFIG_ANALOG_5 = OFF;
00402     CONNECTED = false;
00403     PIN_CONNECT = 0;
00404 }
00405 
00406 //  Carga la configuración de las entradas digitales del MOTE.
00407 void readDigitalInputs_Value ()
00408 {
00409     DigitalInput_DATA = (uint16_t) (((D15_TYPE == IN ? 1:0) << 15) | ((D14_TYPE == IN?1:0)<< 14) | ((D13_TYPE == IN ?1:0) << 13) | ((D12_TYPE == IN ?1:0) << 12) | ((D11_TYPE == IN ?1:0) << 11) | ((D10_TYPE == IN? 1:0) << 10) | ((D9_TYPE == IN? 1:0) << 9) | ((D8_TYPE == IN ? 1:0) << 8) | ((D7_TYPE == IN ? 1:0) << 7) | ((D6_TYPE == IN ? 1:0) << 6) |((D5_TYPE == IN ? 1:0) << 5) | ((D4_TYPE == IN ? 1:0) << 4) | ((D3_TYPE == IN ? 1:0) << 3) | ((D2_TYPE == IN ? 1:0) << 2));
00410 }
00411 //  Crea el Mapa de bits de las posiciones ocupadas de los pines digitales de salidas.
00412 void readActuadoresDigitales()
00413 {
00414     DigitalOutput_DATA = (uint16_t)   ((D15_TYPE == OUT ? 1 : 0) << 15) | ((D14_TYPE == OUT ? 1 : 0) << 14) | ((D13_TYPE == OUT ? 1 : 0) << 13) | ((D12_TYPE == OUT ? 1 : 0) << 12);
00415     DigitalOutput_DATA |= (uint16_t)  ((D11_TYPE == OUT ? 1 : 0) << 11) | ((D10_TYPE == OUT ? 1 : 0) << 10) | ((D9_TYPE == OUT ? 1 : 0) << 9)   | ((D8_TYPE == OUT ? 1 : 0) << 8);
00416     DigitalOutput_DATA |= (uint16_t)  ((D7_TYPE == OUT ? 1 : 0) << 7)   | ((D6_TYPE == OUT ? 1 : 0) << 6)   | ((D5_TYPE == OUT ? 1 : 0) << 5) | ((D4_TYPE == OUT ? 1 : 0) << 4);
00417     DigitalOutput_DATA |= (uint16_t)  ((D3_TYPE == OUT ? 1 : 0) << 3)   | ((D2_TYPE == OUT ? 1 : 0) << 2) ;
00418 }
00419 
00420 //  Crea el Mapa de bits de los estados de los pines digitales de entrada.
00421 void cargarEstadoEntradasDigitales()
00422 {
00423     ESTADO_ENTRADAS_DIGITALES = (uint16_t)   ((D15_TYPE == IN ? PIN_D15 : 0) << 15) | ((D14_TYPE == IN ? PIN_D14 : 0) << 14) | ((D13_TYPE == IN ? PIN_D13 : 0) << 13) | ((D12_TYPE == IN ? PIN_D12 : 0) << 12);
00424     ESTADO_ENTRADAS_DIGITALES |= (uint16_t)  ((D11_TYPE == IN ? PIN_D11 : 0) << 11) | ((D10_TYPE == IN ? PIN_D10 : 0) << 10) | ((D9_TYPE == IN ? PIN_D9 : 0) << 9)   | ((D8_TYPE == IN ? PIN_D8 : 0) << 8);
00425     ESTADO_ENTRADAS_DIGITALES |= (uint16_t)  ((D7_TYPE == IN ? PIN_D7 : 0) << 7)   | ((D6_TYPE == IN ? PIN_D6 : 0) << 6)   | ((D5_TYPE == IN ? PIN_D5 : 0) << 5) | ((D4_TYPE == IN ? PIN_D4 : 0) << 4);
00426     ESTADO_ENTRADAS_DIGITALES |= (uint16_t)  ((D3_TYPE == IN ? PIN_D3 : 0) << 3)   | ((D2_TYPE == IN ? PIN_D2 : 0) << 2) ;
00427 }
00428 
00429 //  Crea el Mapa de bits de los estados de los pines digitales de salida.
00430 void cargarEstadosSalidasDigitales()
00431 {
00432     ESTADO_SALIDAS_DIGITALES = (uint16_t)   ((D15_TYPE == OUT ? PIN_D15 : 0) << 15) | ((D14_TYPE == OUT ? PIN_D14 : 0) << 14) | ((D13_TYPE == OUT ? PIN_D13 : 0) << 13) | ((D12_TYPE == OUT ? PIN_D12 : 0) << 12);
00433     ESTADO_SALIDAS_DIGITALES |= (uint16_t)  ((D11_TYPE == OUT ? PIN_D11 : 0) << 11) | ((D10_TYPE == OUT ? PIN_D10 : 0) << 10) | ((D9_TYPE == OUT ? PIN_D9 : 0) << 9)   | ((D8_TYPE == OUT ? PIN_D8 : 0) << 8);
00434     ESTADO_SALIDAS_DIGITALES |= (uint16_t)  ((D7_TYPE == OUT ? PIN_D7 : 0) << 7)   | ((D6_TYPE == OUT ? PIN_D6 : 0) << 6)   | ((D5_TYPE == OUT ? PIN_D5 : 0) << 5) | ((D4_TYPE == OUT ? PIN_D4 : 0) << 4);
00435     ESTADO_SALIDAS_DIGITALES |= (uint16_t)  ((D3_TYPE == OUT ? PIN_D3 : 0) << 3)   | ((D2_TYPE == OUT ? PIN_D2 : 0) << 2) ;
00436 }
00437 
00438 //Funcion para crear los extra bytes
00439 void makeExtraBytes_CONFIG ()
00440 {
00441     //  Teds de los Analog inputs
00442     AnalogInput_Ted [0] = A0_TED;
00443     AnalogInput_Ted [1] = A1_TED;
00444     AnalogInput_Ted [2] = A2_TED;
00445     AnalogInput_Ted [3] = A3_TED;
00446     AnalogInput_Ted [4] = A4_TED;
00447     AnalogInput_Ted [5] = A5_TED;
00448 
00449     DigitalInput_Pos =   (uint16_t) ((D15_TYPE == IN ? 1:0) << 15) | ((D14_TYPE == IN ? 1:0) << 14) | ((D13_TYPE == IN ? 1:0) << 13)| ((D12_TYPE == IN ? 1:0) << 12);
00450     DigitalInput_Pos |=  (uint16_t) ((D11_TYPE == IN ? 1:0) << 11) | ((D10_TYPE == IN ? 1:0) << 10) | ((D9_TYPE == IN ? 1:0) << 9)  | ((D8_TYPE == IN ? 1:0) << 8);
00451     DigitalInput_Pos |=  (uint16_t) ((D7_TYPE == IN ? 1:0) << 7)   | ((D6_TYPE == IN ? 1:0) << 6)   | ((D5_TYPE == IN ? 1:0) << 5)  | ((D4_TYPE == IN ? 1:0) << 4);
00452     DigitalInput_Pos |=  (uint16_t) ((D3_TYPE == IN ? 1:0) << 3)   | ((D2_TYPE == IN ? 1:0) << 2)   | ((D13_TYPE == IN ? 1:0) << 1) | ((D12_TYPE == IN ? 1:0));
00453 
00454     DigitalOutput_Pos =   (uint16_t) ((D15_TYPE == OUT ? 1:0) << 15) | ((D14_TYPE == OUT ? 1:0) << 14) | ((D13_TYPE == OUT ? 1:0) << 13)| ((D12_TYPE == OUT ? 1:0) << 12);
00455     DigitalOutput_Pos |=  (uint16_t) ((D11_TYPE == OUT ? 1:0) << 11) | ((D10_TYPE == OUT ? 1:0) << 10) | ((D9_TYPE == OUT ? 1:0) << 9)  | ((D8_TYPE == OUT ? 1:0) << 8);
00456     DigitalOutput_Pos |=  (uint16_t) ((D7_TYPE == OUT ? 1:0) << 7)   | ((D6_TYPE == OUT ? 1:0) << 6)   | ((D5_TYPE == OUT ? 1:0) << 5)  | ((D4_TYPE == OUT ? 1:0) << 4);
00457     DigitalOutput_Pos |=  (uint16_t) ((D3_TYPE == OUT ? 1:0) << 3)   | ((D2_TYPE == OUT ? 1:0) << 2)   | ((D13_TYPE == OUT ? 1:0) << 1) | ((D12_TYPE == OUT ? 1:0));
00458 
00459     DigitalPwm_Pos  =   (uint16_t) ((D15_TYPE == PWM ? 1:0) << 15)| ((D14_TYPE == PWM ? 1:0) << 14) | ((D13_TYPE == PWM ? 1:0) << 13)| ((D12_TYPE == PWM ? 1:0) << 12);
00460     DigitalPwm_Pos |=  (uint16_t) ((D11_TYPE == PWM ? 1:0) << 11) | ((D10_TYPE == PWM ? 1:0) << 10) | ((D9_TYPE == PWM ? 1:0) << 9)  | ((D8_TYPE == PWM ? 1:0) << 8);
00461     DigitalPwm_Pos |=  (uint16_t) ((D7_TYPE == PWM ? 1:0) << 7)   | ((D6_TYPE == PWM ? 1:0) << 6)   | ((D5_TYPE == PWM ? 1:0) << 5)  | ((D4_TYPE == PWM ? 1:0) << 4);
00462     DigitalPwm_Pos |=  (uint16_t) ((D3_TYPE == PWM ? 1:0) << 3)   | ((D2_TYPE == PWM ? 1:0) << 2)   | ((D13_TYPE == PWM ? 1:0) << 1) | ((D12_TYPE == PWM ? 1:0));
00463     
00464     AnalogInput_Pos = (uint16_t) ((A5_USO << 5) | (A4_USO << 4) | (A3_USO << 3) | (A2_USO << 2) | (A1_USO << 1) | (A0_USO << 0));
00465     
00466 
00467     //para probar
00468     pc.printf("DigitalInput_Pos = %x \r\n", DigitalInput_Pos);
00469     pc.printf("DigitalOutput_Pos = %x \r\n", DigitalOutput_Pos);
00470     pc.printf("AnalogInput_Pos = %x \r\n", AnalogInput_Pos);
00471     pc.printf("AnalogOutput_Pos = %x \r\n", DigitalPwm_Pos);
00472     pc.printf("---------------------------\r\n");
00473 
00474     //  Definimos la trama de configuracion general
00475     TRAMA_CONFIG_GENERAL [0] = 0xC1;                    //  Codigo de configuracion general
00476     TRAMA_CONFIG_GENERAL [1] = 0xA1;                    //  Codigo de entradas analogicas
00477     TRAMA_CONFIG_GENERAL [2] = AnalogInput_Pos;                    //  Valor de las entradas analogicas
00478     TRAMA_CONFIG_GENERAL [3] = 0xA0;                    //  Codigo de las salidas analogicas
00479     TRAMA_CONFIG_GENERAL [4] = (DigitalPwm_Pos >> 8);                    //  Valor de las salidas analogicas
00480     TRAMA_CONFIG_GENERAL [5] = (DigitalPwm_Pos);                    //  Valor de las salidas analogicas
00481     TRAMA_CONFIG_GENERAL [6] = 0xD1;                    //  Codigo de las entradas digitales
00482     TRAMA_CONFIG_GENERAL [7] = (DigitalInput_Pos >> 8);
00483     TRAMA_CONFIG_GENERAL [8] = DigitalInput_Pos;
00484     TRAMA_CONFIG_GENERAL [9] = 0xD0;                    //  Codigo de las salidas difitales
00485     TRAMA_CONFIG_GENERAL [10] = (DigitalOutput_Pos>> 8);
00486     TRAMA_CONFIG_GENERAL [11] = DigitalOutput_Pos;
00487 }
00488 
00489 
00490 
00491 // Ingresa por este metdo unicamente la primera vez que se conecta al mote.
00492 // Tomado desde: https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_LEDBlinker/file/dc392bde2b3c/main.cpp
00493 void connectionCallback(const Gap::ConnectionCallbackParams_t *)
00494 {
00495     pc.printf("connectionCallback \r\n");
00496     PIN_CONNECT = 1;
00497 
00498     makeExtraBytes_CONFIG();
00499     SEND_CONFIG_GENERAL = 1;
00500     CONNECTED = true;
00501 }
00502 
00503 // Recepta las caracteristicas que se desea escribir en el mote.
00504 void WrittenHandler(const GattWriteCallbackParams *Handler)
00505 {
00506     //pc.printf("WrittenHandler(const GattWriteCallbackParams *Handler) \r\n");
00507     uint8_t buf[TXRX_BUF_LEN];
00508     uint16_t bytesRead, index;
00509 
00510     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) {
00511         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00512         memset(txPayload, 0, TXRX_BUF_LEN);
00513         memcpy(txPayload, buf, TXRX_BUF_LEN);
00514 
00515         for(index=0; index<bytesRead; index++)
00516             pc.putc(buf[index]);
00517 
00518         //pc.printf("Leemos la trama: \r\n");
00519         //for(index=0; index<bytesRead; index++) {
00520             //pc.printf("buf[%02x]: %02x\r\n", index, buf[index]);
00521         //}
00522 
00523         // Desde el telefono desactiva el envio de tramas de configuracion general o de cada sensor.
00524         if (buf[0] == 0xDC) {              //  Dato Configuracion
00525             if(buf[1] == 0xC1) {            //  Hace referencia a la configuración general
00526                 SEND_CONFIG_GENERAL = buf[2];      // Debe ser cero, hace que ya no se vuelva a enviar la conf general del mote
00527 
00528                 //  Comprueba si estan habilitados las salidas analogicas, y las
00529                 //  inicializa a 1 para que mas adelante envie la configuracion
00530                 //  de cada sensor
00531                 SEND_CONFIG_ANALOG_0 = (A0_USO == ON)?1:0;
00532                 SEND_CONFIG_ANALOG_1 = (A1_USO == ON)?1:0;
00533                 SEND_CONFIG_ANALOG_2 = (A2_USO == ON)?1:0;
00534                 SEND_CONFIG_ANALOG_3 = (A3_USO == ON)?1:0;
00535                 SEND_CONFIG_ANALOG_4 = (A4_USO == ON)?1:0;
00536                 SEND_CONFIG_ANALOG_5 = (A5_USO == ON)?1:0;
00537                 
00538                 publish_D1 = true;     //  bandera a true, para que el Mote deba obligatoriamente enviar al gateway el estado de las entradas digitales.
00539                 publish_D0 = true;     //  bandera a true, para que el Mote deba obligatoriamente enviar al gateway el estado de las entradas digitales.
00540             }
00541 
00542             //  Maneja las Tramas de confirmacion de la configuracion detallada de cada senssor analogico
00543             if(buf[1] == 0xC2) {            //  Hace referencia a la configuracion detallada por sensor.
00544                 switch (buf[2]) {           //  Evalua la posición
00545                     case 0x00:              //  0xA0
00546                         SEND_CONFIG_ANALOG_0 = buf[3];      //  buf[3] debe ser 0, para detenerse el envió SEND_CONFIG_ANALOG_0
00547                         break;
00548                     case 0x01:              //  0xA1
00549                         SEND_CONFIG_ANALOG_1 = buf[3];      //  buf[3] debe ser 0, para detenerse el envió SEND_CONFIG_ANALOG_1
00550                         break;
00551                     case 0x02:              //  0xA2
00552                         SEND_CONFIG_ANALOG_2 = buf[3];      //  buf[3] debe ser 0, para detenerse el envió SEND_CONFIG_ANALOG_2
00553                         break;
00554                     case 0x03:              //  0xA3
00555                         SEND_CONFIG_ANALOG_3 = buf[3];      //  buf[3] debe ser 0, para detenerse el envió SEND_CONFIG_ANALOG_3
00556                         break;
00557                     case 0x04:              //  0xA4
00558                         SEND_CONFIG_ANALOG_4 = buf[3];      //  buf[3] debe ser 0, para detenerse el envió SEND_CONFIG_ANALOG_4
00559                         break;
00560                     case 0x05:              //  0xA5
00561                         SEND_CONFIG_ANALOG_5 = buf[3];      //  buf[3] debe ser 0, para detenerse el envió SEND_CONFIG_ANALOG_5
00562                         break;
00563                 }
00564             }
00565         }else if (buf[0] == 0xAD) {
00566             // Desde el gateway asignamos el nuevo valor del paquete id
00567             //  de esta manera al ser el paquete id = 0, no volvera hacer enviado de nuevo las lecturas de digital out al gateway
00568             PAQUETE_ID = (((buf[1] << 8) & 0xFF00) | (buf[2] & 0xFF));
00569         }
00570 
00571 
00572         // Verifico si es una trama de Escitura.
00573         if(buf[0] == 0xEE) {
00574             PAQUETE_ID = (((buf[1] << 8) & 0xFF00) | (buf[2] & 0xFF));
00575             //pc.printf("PAQUETE_ID: %d\r\n", PAQUETE_ID);
00576             // Verifico si es un signal Digital Out
00577             if(buf[3] == 0xD0) {
00578                 publish_D0 = true;     //  indica que el satado de las salidas digitales deben ser enviadas para informar al gateway el estado de las mismas.
00579                 //  Evaluo sobre que pin se debe actuar.
00580                 switch (buf[4]) {
00581                     case 0x02:
00582                         #if D2_TYPE == OUT
00583                             PIN_D2 = (buf[5] == 0x01) ? 1:0;
00584                         #endif
00585                         break;
00586                     case 0x03:
00587                         #if D3_TYPE == OUT
00588                             PIN_D3 = (buf[5] == 0x01) ? 1:0;
00589                         #endif
00590                         break;
00591                     case 0x04:
00592                         #if D4_TYPE == OUT
00593                             PIN_D4 = (buf[5] == 0x01) ? 1:0;
00594                         #endif
00595                         break;
00596                     case 0x05:
00597                         #if D5_TYPE == OUT
00598                             PIN_D5 = (buf[5] == 0x01) ? 1:0;
00599                         #endif
00600                         break;
00601                     case 0x06:
00602                         #if D6_TYPE == OUT
00603                             PIN_D6 = (buf[5] == 0x01) ? 1:0;
00604                         #endif
00605                         break;
00606                     case 0x07:
00607                         #if D7_TYPE == OUT
00608                             PIN_D7 = (buf[5] == 0x01) ? 1:0;
00609                         #endif
00610                         break;
00611                     case 0x08:
00612                         #if D8_TYPE == OUT
00613                             PIN_D8 = (buf[5] == 0x01) ? 1:0;
00614                         #endif
00615                         break;
00616                     case 0x09:
00617                         #if D9_TYPE == OUT
00618                             PIN_D9 = (buf[5] == 0x01) ? 1:0;
00619                         #endif
00620                         break;
00621                     case 0x10:
00622                         #if D10_TYPE == OUT
00623                             PIN_D10 = (buf[5] == 0x01) ? 1:0;
00624                         #endif
00625                         break;
00626                     case 0x11:
00627                         #if D11_TYPE == OUT
00628                             PIN_D11 = (buf[5] == 0x01) ? 1:0;
00629                         #endif
00630                         break;
00631                     case 0x12:
00632                         #if D12_TYPE == OUT
00633                             PIN_D12 = (buf[5] == 0x01) ? 1:0;
00634                         #endif
00635                         break;
00636                     case 0x13:
00637                         #if D13_TYPE == OUT
00638                             PIN_D13 = (buf[5] == 0x01) ? 1:0;
00639                         #endif
00640                         break;
00641                     case 0x14:
00642                         #if D14_TYPE == OUT
00643                             PIN_D14 = (buf[5] == 0x01) ? 1:0;
00644                         #endif
00645                         break;
00646                     case 0x15:
00647                         #if D15_TYPE == OUT
00648                             PIN_D15 = (buf[5] == 0x01) ? 1:0;
00649                         #endif
00650                         break;
00651                 }
00652                 // Verifico si es un signal Analog out
00653             } else if(buf[3] == 0xA0) {
00654                 float value = (float)buf[5]/255;
00655                 bool enviar_trama = false;
00656                 int posicion = 0x00;
00657                 switch (buf[4]) {
00658                     case 0x05:
00659                         #if D5_TYPE == PWM
00660                             PIN_D5 = value;
00661                             posicion = 0x05;
00662                             enviar_trama = true;
00663                         #endif
00664                         break;
00665                     case 0x06:
00666                         #if D6_TYPE == PWM
00667                             PIN_D6 = value;
00668                             posicion = 0x06;
00669                             enviar_trama = true;
00670                         #endif
00671                         break;
00672                     case 0x09:
00673                         #if D9_TYPE == PWM
00674                             PIN_D9 = value;
00675                             posicion = 0x09;
00676                             enviar_trama = true;
00677                         #endif
00678                         break;
00679                 }
00680                 
00681                 if (enviar_trama){
00682                     LECTURA_DIGITAL[0] = 0xDD;                                      //  Codigo Datos
00683                     LECTURA_DIGITAL[1] = 0xA0;                                      //  A0  --> Codigo Senial (Analog Output)
00684                     LECTURA_DIGITAL[2] = posicion;                                      //  Posicion de la interfaz
00685                     LECTURA_DIGITAL[3] = (int8_t)(PAQUETE_ID >> 8);                 //  primera parte del paquete
00686                     LECTURA_DIGITAL[4] = (int8_t)PAQUETE_ID ;                       //  segunda parte del paquete
00687                     LECTURA_DIGITAL[5] = buf[5];                                     //  Valor de las salidas digitales
00688                     PAQUETE_ID = 0;
00689                     pc.printf("PWM LECTURA_DIGITAL %x \r\n", LECTURA_DIGITAL);
00690                     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_DIGITAL, 6);    // Para el RTD
00691                 }
00692             }
00693         }
00694     }
00695 }
00696 
00697 
00698 /*
00699 * Desde este metodo envia las tramas al Gateway.
00700 */
00701 void m_status_check_handle(void)
00702 {   
00703     //  Verifica si alguien esta conectado para enviar las tramas a través de Bluetooth.
00704     if (CONNECTED){
00705         if (SEND_CONFIG_GENERAL == 1) {
00706             //  Envia la configuracion Genaral del Mote.
00707             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), TRAMA_CONFIG_GENERAL, 12);
00708         }
00709     
00710     //  Envio de las tramas de Configuracion de los sensores.
00711         //  Evalua si se encuentra pendiente enviar la configuracion de los sensores.
00712         if (SEND_CONFIG_ANALOG_0 == ON||SEND_CONFIG_ANALOG_1 == ON||SEND_CONFIG_ANALOG_2 == ON||SEND_CONFIG_ANALOG_3 == ON||SEND_CONFIG_ANALOG_4 == ON||SEND_CONFIG_ANALOG_5 == ON) {
00713     
00714             TRAMA_CONFIG_ANALOG[0] = (0xC2);                //  Codigo que indica que la configuracion sera por cada Pin.
00715             TRAMA_CONFIG_ANALOG[1] = (0x05);                //  Categoria puede ser: A (Actuador) | 5 (Sensor)
00716             TRAMA_CONFIG_ANALOG[2] = (0xAA);                //  Tipo de Signal AA | DD
00717     
00718             if (A0_USO == ON && SEND_CONFIG_ANALOG_0 == ON) {
00719                 TRAMA_CONFIG_ANALOG[3] = 0x00;              //  Posicion que ocupa en el mote
00720                 int16_t TED_PF = (int16_t) (256 * A0_TED);       //  Conversion Punto Fijo
00721                 TRAMA_CONFIG_ANALOG[4] = (int8_t)(TED_PF >> 8);
00722                 TRAMA_CONFIG_ANALOG[5] = (int8_t)(TED_PF);
00723                 pc.printf("SEND_CONFIG_ANALOG_0 \r\n");
00724                 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), TRAMA_CONFIG_ANALOG, 6);
00725     
00726             } else if (A1_USO == ON && SEND_CONFIG_ANALOG_1 == ON) {
00727                 TRAMA_CONFIG_ANALOG[3] = 0x01;              //  Posicion que ocupa en el mote
00728                 int16_t TED_PF = (int16_t) (256 * A1_TED);       //  Conversion Punto Fijo
00729                 TRAMA_CONFIG_ANALOG[4] = (int8_t)(TED_PF >> 8);
00730                 TRAMA_CONFIG_ANALOG[5] = (int8_t)(TED_PF);
00731                 pc.printf("SEND_CONFIG_ANALOG_1 \r\n");
00732                 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), TRAMA_CONFIG_ANALOG, 6);
00733     
00734             } else if (A2_USO == ON && SEND_CONFIG_ANALOG_2 == ON) {
00735                 TRAMA_CONFIG_ANALOG[3] = 0x02;              //  Posicion que ocupa en el mote
00736                 int16_t TED_PF = (int16_t) (256 * A2_TED);       //  Conversion Punto Fijo
00737                 TRAMA_CONFIG_ANALOG[4] = (int8_t)(TED_PF >> 8);
00738                 TRAMA_CONFIG_ANALOG[5] = (int8_t)(TED_PF);
00739                 pc.printf("SEND_CONFIG_ANALOG_2 \r\n");
00740                 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), TRAMA_CONFIG_ANALOG, 6);
00741     
00742             } else if (A3_USO == ON && SEND_CONFIG_ANALOG_3 == ON) {
00743                 TRAMA_CONFIG_ANALOG[3] = 0x03;              //  Posicion que ocupa en el mote
00744                 int16_t TED_PF = (int16_t) (256 * A3_TED);       //  Conversion Punto Fijo
00745                 TRAMA_CONFIG_ANALOG[4] = (int8_t)(TED_PF >> 8);
00746                 TRAMA_CONFIG_ANALOG[5] = (int8_t)(TED_PF);
00747                 pc.printf("SEND_CONFIG_ANALOG_3 \r\n");
00748                 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), TRAMA_CONFIG_ANALOG, 6);
00749     
00750             } else if (A4_USO == ON && SEND_CONFIG_ANALOG_4 == ON) {
00751                 TRAMA_CONFIG_ANALOG[3] = 0x04;              //  Posicion que ocupa en el mote
00752                 int16_t TED_PF = (int16_t) (256 * A4_TED);       //  Conversion Punto Fijo
00753                 TRAMA_CONFIG_ANALOG[4] = (int8_t)(TED_PF >> 8);
00754                 TRAMA_CONFIG_ANALOG[5] = (int8_t)(TED_PF);
00755                 pc.printf("SEND_CONFIG_ANALOG_4 \r\n");
00756                 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), TRAMA_CONFIG_ANALOG, 6);
00757     
00758             } else if (A5_USO == ON && SEND_CONFIG_ANALOG_5 == ON) {
00759                 TRAMA_CONFIG_ANALOG[3] = 0x05;              //  Posicion que ocupa en el mote
00760                 int16_t TED_PF = (int16_t) (256 * A5_TED);  //  Conversion Punto Fijo
00761                 TRAMA_CONFIG_ANALOG[4] = (int8_t)(TED_PF >> 8);
00762                 TRAMA_CONFIG_ANALOG[5] = (int8_t)(TED_PF);
00763                 pc.printf("SEND_CONFIG_ANALOG_5 %x , %x \r\n", TRAMA_CONFIG_ANALOG[4], TRAMA_CONFIG_ANALOG[5]);
00764                 //pc.printf("SEND_CONFIG_ANALOG_6 %x, %x, %x, %x, %x, %x, %x \r\n", TRAMA_CONFIG_ANALOG[0], TRAMA_CONFIG_ANALOG[1], TRAMA_CONFIG_ANALOG[2], TRAMA_CONFIG_ANALOG[3], TRAMA_CONFIG_ANALOG[4], TRAMA_CONFIG_ANALOG[5], TRAMA_CONFIG_ANALOG[6] );
00765                 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), TRAMA_CONFIG_ANALOG, 6);
00766             }
00767         }
00768         //  Envio de las tramas de lecturas del Mote.
00769         cargarEstadoEntradasDigitales();                      //  Leemos los estados digitales.
00770         //pc.printf("ESTADO_ENTRADAS_DIGITALES_OLD %x   -  ESTADO_ENTRADAS_DIGITALES %x \r\n", ESTADO_ENTRADAS_DIGITALES_OLD, ESTADO_ENTRADAS_DIGITALES);
00771         if (ESTADO_ENTRADAS_DIGITALES_OLD != ESTADO_ENTRADAS_DIGITALES || publish_D1) {
00772             if (publish_D1){
00773                 publish_D1 = false;
00774             }
00775             ESTADO_ENTRADAS_DIGITALES_OLD = ESTADO_ENTRADAS_DIGITALES;
00776     
00777             LECTURA_DIGITAL[0] = 0xDD;                    //  Codigo de lectura de datos
00778             LECTURA_DIGITAL[1] = 0xD1;                      //  Digital Input
00779             LECTURA_DIGITAL[2] = (int8_t)(ESTADO_ENTRADAS_DIGITALES >> 8);         //  valores de las interfaces digitales
00780             LECTURA_DIGITAL[3] = (int8_t)(ESTADO_ENTRADAS_DIGITALES);         //  valores de las interfaces digitales
00781             pc.printf("Envio LECTURA_DIGITAL %x \r\n", ESTADO_ENTRADAS_DIGITALES);  // Imprimo en terminal lo que esta enviando desde el mote.
00782     
00783             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_DIGITAL, 4);    // Para el RTD
00784             
00785         }else if (PAQUETE_ID != 0 || publish_D0) {
00786             if (publish_D0){
00787                 publish_D0 = false;
00788             }
00789             //  Lecturas digitales
00790             cargarEstadosSalidasDigitales();
00791     
00792             pc.printf("ESTADO_SALIDAS_DIGITALES: %x \r\n", ESTADO_SALIDAS_DIGITALES);
00793             LECTURA_DIGITAL[0] = 0xDD;                                      //  Codigo
00794             LECTURA_DIGITAL[1] = 0xD0;                                      //  D0  --> Digital Output
00795             LECTURA_DIGITAL[2] = (int8_t)(PAQUETE_ID >> 8);                         //  primera parte del paquete
00796             LECTURA_DIGITAL[3] = (int8_t)PAQUETE_ID ;                               //  segunda parte del paquete
00797             LECTURA_DIGITAL[4] = (int8_t)(ESTADO_SALIDAS_DIGITALES >> 8);                  //  Valor de las salidas digitales
00798             LECTURA_DIGITAL[5] = (int8_t)(ESTADO_SALIDAS_DIGITALES);                       //  Valor de las salidas digitales
00799     
00800             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_DIGITAL, 6);    // Para el RTD
00801         }
00802     
00803     //  Envio de las tramas de las lecturas de los sensores habilitados.
00804         if (SEND_CONFIG_GENERAL == 0) {
00805     
00806             LECTURA_ANALOGICA[0] = 0xDD;        //  Codigo Datos
00807             LECTURA_ANALOGICA[1] = 0xA1;        //  --> Analog Input (A1)
00808                     
00809     
00810             //  Si la configuracion ha sido enviada, envio las lecturas. Esta para cumplir con una secuencia.
00811             if (A5_USO == ON) {
00812                 float s = ANALOG_A5;
00813                 if (value_A5 != s) {
00814                     value_A5 = s;
00815     
00816                     uint16_t value = s * 1024;
00817     
00818                     float Vtemp = value * 0.0032080078125;
00819                     float Rtemp = (15111 / Vtemp) - 4630;
00820                     float Tempfinal = ((Rtemp - 1000) / 3.850);
00821     
00822                     //2- Conversion flotante a punto fijo
00823                     int16_t Temperature_PF = (int16_t) (256 * Tempfinal);
00824     
00825                     LECTURA_ANALOGICA[2] = 0x05;        //  --> Posicion del sensor
00826                     LECTURA_ANALOGICA[3] = (int8_t)(Temperature_PF >> 8);
00827                     LECTURA_ANALOGICA[4] = (int8_t)(Temperature_PF);
00828     
00829                     pc.printf("ANALOG_A5 (Format HEX): %x , LECTURA_ANALOGICA: %x \r\n", Temperature_PF, LECTURA_ANALOGICA);
00830                     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_ANALOGICA, 5);    // Para el RTD
00831                 }
00832             }
00833             if (A4_USO == ON) {
00834                 float s = ANALOG_A4;
00835                 if (value_A4 != s) {
00836                     value_A4 = s;
00837     
00838                     uint16_t value = s * 1024;
00839     
00840                     float Vtemp = value * 0.0032080078125;
00841                     float Rtemp = (15111 / Vtemp) - 4630;
00842                     float Tempfinal = ((Rtemp - 1000) / 3.850);
00843     
00844                     //2- Conversion flotante a punto fijo
00845                     int16_t Temperature_PF = (int16_t) (256 * Tempfinal);
00846     
00847                     LECTURA_ANALOGICA[2] = 0x04;        //  --> Posicion del sensor
00848                     LECTURA_ANALOGICA[3] = (int8_t)(Temperature_PF >> 8);
00849                     LECTURA_ANALOGICA[4] = (int8_t)(Temperature_PF);
00850     
00851                     pc.printf("ANALOG_A4 (Format HEX): %x , LECTURA_ANALOGICA: %x \r\n", Temperature_PF, LECTURA_ANALOGICA);
00852                     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_ANALOGICA, 5);    // Para el RTD
00853                 }
00854             }
00855             if (A3_USO == ON) {
00856                 float s = ANALOG_A3;
00857                 if (value_A3 != s) {
00858                     value_A3 = s;
00859     
00860                     uint16_t value = s * 1024;
00861     
00862                     float Vtemp = value * 0.0032080078125;
00863                     float Rtemp = (15111 / Vtemp) - 4630;
00864                     float Tempfinal = ((Rtemp - 1000) / 3.850);
00865     
00866                     //2- Conversion flotante a punto fijo
00867                     int16_t Temperature_PF = (int16_t) (256 * Tempfinal);
00868     
00869                     LECTURA_ANALOGICA[2] = 0x03;        //  --> Posicion del sensor
00870                     LECTURA_ANALOGICA[3] = (int8_t)(Temperature_PF >> 8);
00871                     LECTURA_ANALOGICA[4] = (int8_t)(Temperature_PF);
00872     
00873                     pc.printf("ANALOG_A3 (Format HEX): %x , LECTURA_ANALOGICA: %x \r\n", Temperature_PF, LECTURA_ANALOGICA);
00874                     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_ANALOGICA, 5);    // Para el RTD
00875                 }
00876             }
00877             if (A2_USO == ON) {
00878                 float s = ANALOG_A2;
00879                 if (value_A2 != s) {
00880                     value_A2 = s;
00881     
00882                     uint16_t value = s * 1024;
00883     
00884                     float Vtemp = value * 0.0032080078125;
00885                     float Rtemp = (15111 / Vtemp) - 4630;
00886                     float Tempfinal = ((Rtemp - 1000) / 3.850);
00887     
00888                     //2- Conversion flotante a punto fijo
00889                     int16_t Temperature_PF = (int16_t) (256 * Tempfinal);
00890     
00891                     LECTURA_ANALOGICA[2] = 0x02;        //  --> Posicion del sensor
00892                     LECTURA_ANALOGICA[3] = (int8_t)(Temperature_PF >> 8);
00893                     LECTURA_ANALOGICA[4] = (int8_t)(Temperature_PF);
00894     
00895                     pc.printf("ANALOG_A2 (Format HEX): %x , LECTURA_ANALOGICA: %x \r\n", Temperature_PF, LECTURA_ANALOGICA);
00896                     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_ANALOGICA, 5);    // Para el RTD
00897                 }
00898             }
00899             if (A1_USO == ON) {
00900                 float s = ANALOG_A1;
00901                 if (value_A1 != s) {
00902                     value_A1 = s;
00903     
00904                     uint16_t value = s * 1024;
00905     
00906                     float Vtemp = value * 0.0032080078125;
00907                     float Rtemp = (15111 / Vtemp) - 4630;
00908                     float Tempfinal = ((Rtemp - 1000) / 3.850);
00909     
00910                     //2- Conversion flotante a punto fijo
00911                     int16_t Temperature_PF = (int16_t) (256 * Tempfinal);
00912     
00913                     LECTURA_ANALOGICA[2] = 0x01;        //  --> Posicion del sensor
00914                     LECTURA_ANALOGICA[3] = (int8_t)(Temperature_PF >> 8);
00915                     LECTURA_ANALOGICA[4] = (int8_t)(Temperature_PF);
00916     
00917                     pc.printf("ANALOG_A1 (Format HEX): %x , LECTURA_ANALOGICA: %x \r\n", Temperature_PF, LECTURA_ANALOGICA);
00918                     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_ANALOGICA, 5);    // Para el RTD
00919                 }
00920             }
00921             if (A0_USO == ON) {
00922                 float s = ANALOG_A0;
00923                 if (value_A0 != s) {
00924                     value_A0 = s;
00925     
00926                     uint16_t value = s * 1024;
00927     
00928                     float Vtemp = value * 0.0032080078125;
00929                     float Rtemp = (15111 / Vtemp) - 4630;
00930                     float Tempfinal = ((Rtemp - 1000) / 3.850);
00931     
00932                     //2- Conversion flotante a punto fijo
00933                     int16_t Temperature_PF = (int16_t) (256 * Tempfinal);
00934     
00935                     LECTURA_ANALOGICA[2] = 0x00;        //  --> Posicion del sensor
00936                     LECTURA_ANALOGICA[3] = (int8_t)(Temperature_PF >> 8);
00937                     LECTURA_ANALOGICA[4] = (int8_t)(Temperature_PF);
00938     
00939                     pc.printf("ANALOG_A0 (Format HEX): %x , LECTURA_ANALOGICA: %x \r\n", Temperature_PF, LECTURA_ANALOGICA);
00940                     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), LECTURA_ANALOGICA, 5);    // Para el RTD
00941                 }
00942             }
00943         }
00944     }
00945 }
00946 
00947 void apagarLeds()
00948 {
00949     //  SOLO PARA NRF51822
00950     if (D3_TYPE == OUT) {
00951         //PIN_D3 = 0;
00952     }
00953     if (D4_TYPE == OUT) {
00954         //PIN_D4 = 1;
00955     }
00956     if (D5_TYPE == OUT) {
00957         //PIN_D5 = 1;
00958     }
00959     if (D6_TYPE == OUT) {
00960         //PIN_D6 = 0;
00961     }
00962 }
00963 
00964 int main(void)
00965 {
00966     Ticker ticker;
00967     ticker.attach_us(m_status_check_handle, 200000);
00968 
00969     ble.init();
00970     ble.onDisconnection(disconnectionCallback);
00971     ble.onConnection(connectionCallback);
00972     ble.onDataWritten(WrittenHandler);
00973 
00974     pc.baud(9600);
00975     pc.printf("SimpleChat Init \r\n");
00976     //pc.attach( uartCB , pc.RxIrq);
00977 
00978     // setup advertising
00979     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00980     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00981     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"Biscuit2", sizeof("Biscuit2") - 1);   // Original:  Biscuit
00982     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00983 
00984     ble.setAdvertisingInterval(160);
00985 
00986     ble.addService(uartService);
00987 
00988     ble.startAdvertising();
00989     pc.printf("Advertising Start \r\n");
00990     apagarLeds();
00991     //por dixys
00992     // para probar, luego quitar. Esto hace que cada ticker se envie un dato analogico via BLE
00993 
00994     //  valores refernenciales para los estados de los sensores.
00995     
00996     #if A0_USO == ON
00997         value_A0 = ANALOG_A0.read();
00998     #else
00999         value_A0 = 0;
01000     #endif
01001     
01002     #if A1_USO == ON
01003         value_A1 = ANALOG_A1.read();
01004     #else
01005         value_A1 = 0;
01006     #endif
01007     
01008     #if A2_USO == ON
01009         value_A2 = ANALOG_A2.read();
01010     #else
01011         value_A2 = 0;
01012     #endif
01013     
01014     #if A3_USO == ON
01015         value_A3 = ANALOG_A3.read();
01016     #else
01017         value_A3 = 0;
01018     #endif
01019     
01020     #if A4_USO == ON
01021         value_A4 = ANALOG_A4.read();
01022     #else
01023         value_A4 = 0;
01024     #endif
01025     
01026     #if A5_USO == ON
01027         value_A5 = ANALOG_A5.read();
01028     #else
01029         value_A5 = 0;
01030     #endif
01031 
01032     while(1) {
01033         ble.waitForEvent();
01034     }
01035 }