Diseño de Firmaware Diseañado en base a los requerimientos definidos durante la etapa de analisis.

Dependencies:   BLE_API mbed

Fork of nRF51822_SimpleControls by Gustavo Belduma

Committer:
Gustavo_Eduardo338
Date:
Tue Jun 14 19:08:23 2016 +0000
Revision:
5:a8c07a29df8a
Parent:
4:a43cbe8aed6a
Child:
6:84154a9a8717
Configuraci?n de los Pinnes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dixysleo 0:58e350255405 1 /*
dixysleo 0:58e350255405 2 Copyright (c) 2012-2014 RedBearLab
dixysleo 0:58e350255405 3
Gustavo_Eduardo338 4:a43cbe8aed6a 4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Gustavo_Eduardo338 4:a43cbe8aed6a 5 and associated documentation files (the "Software"), to deal in the Software without restriction,
Gustavo_Eduardo338 4:a43cbe8aed6a 6 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
Gustavo_Eduardo338 4:a43cbe8aed6a 7 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
dixysleo 0:58e350255405 8 subject to the following conditions:
dixysleo 0:58e350255405 9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
dixysleo 0:58e350255405 10
Gustavo_Eduardo338 4:a43cbe8aed6a 11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Gustavo_Eduardo338 4:a43cbe8aed6a 12 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
Gustavo_Eduardo338 4:a43cbe8aed6a 13 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
Gustavo_Eduardo338 4:a43cbe8aed6a 14 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
dixysleo 0:58e350255405 15 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
dixysleo 0:58e350255405 16
dixysleo 0:58e350255405 17 */
dixysleo 0:58e350255405 18
dixysleo 0:58e350255405 19 #include "mbed.h"
dixysleo 0:58e350255405 20 #include "ble/BLE.h"
barcejorge 3:53cd39af0b12 21 //#include "Servo.h"
dixysleo 0:58e350255405 22 #include "GattCallbackParamTypes.h"
dixysleo 0:58e350255405 23
dixysleo 0:58e350255405 24 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
dixysleo 0:58e350255405 25 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
dixysleo 0:58e350255405 26 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
dixysleo 0:58e350255405 27
dixysleo 0:58e350255405 28 #define TXRX_BUF_LEN 20
dixysleo 0:58e350255405 29
Gustavo_Eduardo338 5:a8c07a29df8a 30 #define DIGITAL_OUT_7 P0_17 //D7
Gustavo_Eduardo338 5:a8c07a29df8a 31 #define DIGITAL_OUT_5 P0_23 //D5
Gustavo_Eduardo338 5:a8c07a29df8a 32 #define DIGITAL_OUT_4 P0_21 //D4
Gustavo_Eduardo338 5:a8c07a29df8a 33
dixysleo 0:58e350255405 34 #define DIGITAL_IN_PIN P0_5 //A4
Gustavo_Eduardo338 5:a8c07a29df8a 35
dixysleo 0:58e350255405 36 #define PWM_PIN P0_16 //D6
barcejorge 3:53cd39af0b12 37 //#define SERVO_PIN P0_14 //D10
dixysleo 0:58e350255405 38 #define ANALOG_IN_PIN P0_6 //A5
dixysleo 0:58e350255405 39
Gustavo_Eduardo338 5:a8c07a29df8a 40 // Declarando los pines
Gustavo_Eduardo338 5:a8c07a29df8a 41 static int32_t A_0 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 42 static int32_t A_1 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 43 static int32_t A_2 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 44 static int32_t A_3 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 45 static int32_t A_4 = 0x01;
Gustavo_Eduardo338 5:a8c07a29df8a 46 static int32_t A_5 = 0x01;
dixysleo 0:58e350255405 47
Gustavo_Eduardo338 5:a8c07a29df8a 48 static int32_t D_0 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 49 static int32_t D_1 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 50 static int32_t D_2 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 51 static int32_t D_3 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 52 static int32_t D_4 = 0x01;
Gustavo_Eduardo338 5:a8c07a29df8a 53 static int32_t D_5 = 0x01;
Gustavo_Eduardo338 5:a8c07a29df8a 54 static int32_t D_6 = 0x01;
Gustavo_Eduardo338 5:a8c07a29df8a 55 static int32_t D_7 = 0x01;
Gustavo_Eduardo338 5:a8c07a29df8a 56 static int32_t D_8 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 57 static int32_t D_9 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 58 static int32_t D_10 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 59 static int32_t D_11 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 60 static int32_t D_12 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 61 static int32_t D_13 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 62 static int32_t D_14 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 63 static int32_t D_15 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 64 static int32_t D_16 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 65 static int32_t D_17 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 66 static int32_t D_18 = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 67
Gustavo_Eduardo338 5:a8c07a29df8a 68 static int32_t send_config = 0;
Gustavo_Eduardo338 5:a8c07a29df8a 69
Gustavo_Eduardo338 5:a8c07a29df8a 70 BLE ble;
Gustavo_Eduardo338 5:a8c07a29df8a 71
Gustavo_Eduardo338 5:a8c07a29df8a 72 // Digital IN
dixysleo 0:58e350255405 73 DigitalIn BUTTON(DIGITAL_IN_PIN);
Gustavo_Eduardo338 5:a8c07a29df8a 74
Gustavo_Eduardo338 5:a8c07a29df8a 75 // Digital OUT
Gustavo_Eduardo338 5:a8c07a29df8a 76 DigitalOut LED_SET_D7(DIGITAL_OUT_7);
Gustavo_Eduardo338 5:a8c07a29df8a 77 DigitalOut LED_SET_D5(DIGITAL_OUT_5);
Gustavo_Eduardo338 5:a8c07a29df8a 78 DigitalOut LED_SET_D4(DIGITAL_OUT_4);
Gustavo_Eduardo338 5:a8c07a29df8a 79
Gustavo_Eduardo338 5:a8c07a29df8a 80 // Analog IN
Gustavo_Eduardo338 5:a8c07a29df8a 81 AnalogIn ANALOG(ANALOG_IN_PIN);
Gustavo_Eduardo338 5:a8c07a29df8a 82
Gustavo_Eduardo338 5:a8c07a29df8a 83 // Analog OUT
Gustavo_Eduardo338 5:a8c07a29df8a 84
Gustavo_Eduardo338 5:a8c07a29df8a 85
dixysleo 0:58e350255405 86 PwmOut PWM(PWM_PIN);
barcejorge 3:53cd39af0b12 87 //Servo MYSERVO(SERVO_PIN);
dixysleo 0:58e350255405 88
dixysleo 0:58e350255405 89 Serial pc(USBTX, USBRX);
dixysleo 0:58e350255405 90
dixysleo 0:58e350255405 91 static uint8_t analog_enabled = 0;
dixysleo 0:58e350255405 92 static uint8_t old_state = 0;
dixysleo 0:58e350255405 93
dixysleo 0:58e350255405 94 // The Nordic UART Service
dixysleo 0:58e350255405 95 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
dixysleo 0:58e350255405 96 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
dixysleo 0:58e350255405 97 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
dixysleo 0:58e350255405 98 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
dixysleo 0:58e350255405 99
Gustavo_Eduardo338 5:a8c07a29df8a 100 // Declarando los vectores de configuracion para el mote.
Gustavo_Eduardo338 5:a8c07a29df8a 101 static const uint8_t di_conf [] = {0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, A_2, A_1, A_0}; // length: 10
Gustavo_Eduardo338 5:a8c07a29df8a 102 static const uint8_t do_conf [] = {0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, A_5, A_4, A_3}; // length: 10
Gustavo_Eduardo338 5:a8c07a29df8a 103 static const uint8_t ai_conf [] = {D_9, D_8, D_7, D_6, D_5, D_4, D_3, D_2, D_1, D_0}; // length: 10
Gustavo_Eduardo338 5:a8c07a29df8a 104 static const uint8_t ao_conf [] = {0x00, D_18, D_17, D_16, D_15, D_14, D_13, D_12, D_11, D_10}; // length: 10
Gustavo_Eduardo338 5:a8c07a29df8a 105
dixysleo 0:58e350255405 106
dixysleo 0:58e350255405 107 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
dixysleo 0:58e350255405 108 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
dixysleo 0:58e350255405 109
dixysleo 0:58e350255405 110 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
dixysleo 0:58e350255405 111 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
dixysleo 0:58e350255405 112 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
dixysleo 0:58e350255405 113 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
dixysleo 0:58e350255405 114
dixysleo 0:58e350255405 115 /*
dixysleo 0:58e350255405 116 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
dixysleo 0:58e350255405 117 {
dixysleo 0:58e350255405 118 pc.printf("Disconnected \r\n");
dixysleo 0:58e350255405 119 pc.printf("Restart advertising \r\n");
dixysleo 0:58e350255405 120 ble.startAdvertising();
dixysleo 0:58e350255405 121 }
Gustavo_Eduardo338 4:a43cbe8aed6a 122 */
dixysleo 0:58e350255405 123
dixysleo 0:58e350255405 124 // La de arriba era la original, pero se cambio para que funcione, solucion sacada de la web
dixysleo 0:58e350255405 125 // https://developer.mbed.org/forum/repo-61676-BLE_GAP_Example-community/topic/17193/
Gustavo_Eduardo338 4:a43cbe8aed6a 126 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
Gustavo_Eduardo338 4:a43cbe8aed6a 127 {
Gustavo_Eduardo338 4:a43cbe8aed6a 128 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
dixysleo 0:58e350255405 129 pc.printf("Disconnected \r\n");
dixysleo 0:58e350255405 130 pc.printf("Restart advertising \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 131 ble.startAdvertising();
Gustavo_Eduardo338 5:a8c07a29df8a 132 LED_SET_D7 = 0;
Gustavo_Eduardo338 5:a8c07a29df8a 133 send_config = 0;
Gustavo_Eduardo338 5:a8c07a29df8a 134 }
Gustavo_Eduardo338 5:a8c07a29df8a 135 // Ingresa por este metdo unicamente la primera vez que se conecta al mote.
Gustavo_Eduardo338 5:a8c07a29df8a 136 // Tomado desde: https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_LEDBlinker/file/dc392bde2b3c/main.cpp
Gustavo_Eduardo338 5:a8c07a29df8a 137 void connectionCallback(const Gap::ConnectionCallbackParams_t *)
Gustavo_Eduardo338 5:a8c07a29df8a 138 {
Gustavo_Eduardo338 5:a8c07a29df8a 139 pc.printf("connectionCallback \r\n");
Gustavo_Eduardo338 5:a8c07a29df8a 140 LED_SET_D7 = 1;
Gustavo_Eduardo338 5:a8c07a29df8a 141 send_config = 2;
dixysleo 0:58e350255405 142 }
dixysleo 0:58e350255405 143
Gustavo_Eduardo338 5:a8c07a29df8a 144 // Recepta las caracteristicas que se desea escribir en el mote.
dixysleo 0:58e350255405 145 void WrittenHandler(const GattWriteCallbackParams *Handler)
Gustavo_Eduardo338 4:a43cbe8aed6a 146 {
Gustavo_Eduardo338 5:a8c07a29df8a 147 pc.printf("WrittenHandler(const GattWriteCallbackParams *Handler) \r\n");
dixysleo 0:58e350255405 148 uint8_t buf[TXRX_BUF_LEN];
dixysleo 0:58e350255405 149 uint16_t bytesRead, index;
Gustavo_Eduardo338 4:a43cbe8aed6a 150
Gustavo_Eduardo338 4:a43cbe8aed6a 151 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) {
dixysleo 0:58e350255405 152 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
dixysleo 0:58e350255405 153 memset(txPayload, 0, TXRX_BUF_LEN);
Gustavo_Eduardo338 4:a43cbe8aed6a 154 memcpy(txPayload, buf, TXRX_BUF_LEN);
Gustavo_Eduardo338 4:a43cbe8aed6a 155
dixysleo 0:58e350255405 156 //por dixys
Gustavo_Eduardo338 4:a43cbe8aed6a 157 pc.printf("Buffer recibido desde Android: \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 158
dixysleo 0:58e350255405 159 for(index=0; index<bytesRead; index++)
dixysleo 0:58e350255405 160 pc.putc(buf[index]);
Gustavo_Eduardo338 4:a43cbe8aed6a 161
Gustavo_Eduardo338 4:a43cbe8aed6a 162 if(buf[0] == 0x01) {
Gustavo_Eduardo338 4:a43cbe8aed6a 163 if(buf[1] == 0x01) {
Gustavo_Eduardo338 5:a8c07a29df8a 164 LED_SET_D7 = 1;
Gustavo_Eduardo338 5:a8c07a29df8a 165 LED_SET_D5 = 1;
Gustavo_Eduardo338 5:a8c07a29df8a 166 LED_SET_D4 = 1;
dixysleo 0:58e350255405 167 //por dixys
dixysleo 0:58e350255405 168 pc.printf("LED SET 1 \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 169 } else {
Gustavo_Eduardo338 5:a8c07a29df8a 170 LED_SET_D7 = 0;
Gustavo_Eduardo338 5:a8c07a29df8a 171 LED_SET_D5 = 0;
Gustavo_Eduardo338 5:a8c07a29df8a 172 LED_SET_D4 = 0;
dixysleo 0:58e350255405 173 //por dixys
dixysleo 0:58e350255405 174 pc.printf("LED SET 0 \r\n");
dixysleo 0:58e350255405 175 }
Gustavo_Eduardo338 4:a43cbe8aed6a 176 } else if(buf[0] == 0xA0) {
Gustavo_Eduardo338 4:a43cbe8aed6a 177 if(buf[1] == 0x01) {
dixysleo 0:58e350255405 178 analog_enabled = 1;
dixysleo 0:58e350255405 179 //por dixys
dixysleo 0:58e350255405 180 pc.printf("ANALOG ENABLE \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 181 } else {
Gustavo_Eduardo338 5:a8c07a29df8a 182 analog_enabled = 0;
dixysleo 0:58e350255405 183 // lo vamos a poner fio enable para hacer pruebas luego lo quitamos
dixysleo 0:58e350255405 184 //por dixys
dixysleo 0:58e350255405 185 pc.printf("ANALOG DISAABLE \r\n");
Gustavo_Eduardo338 5:a8c07a29df8a 186 pc.printf("valor : %d", analog_enabled);
dixysleo 0:58e350255405 187 }
Gustavo_Eduardo338 4:a43cbe8aed6a 188 } else if(buf[0] == 0x02) {
dixysleo 0:58e350255405 189 float value = (float)buf[1]/255;
dixysleo 0:58e350255405 190 PWM = value;
dixysleo 0:58e350255405 191 //por dixys
dixysleo 0:58e350255405 192 pc.printf("PWM = %f \r\n", value);
Gustavo_Eduardo338 4:a43cbe8aed6a 193 } else if(buf[0] == 0x03) {
barcejorge 3:53cd39af0b12 194 //MYSERVO.write(buf[1]);
dixysleo 0:58e350255405 195 //por dixys
dixysleo 0:58e350255405 196 pc.printf("SERVO buffer \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 197 } else if(buf[0] == 0x04) {
dixysleo 0:58e350255405 198 analog_enabled = 0;
dixysleo 0:58e350255405 199 PWM = 0;
barcejorge 3:53cd39af0b12 200 //MYSERVO.write(0);
Gustavo_Eduardo338 5:a8c07a29df8a 201 LED_SET_D7 = 0;
Gustavo_Eduardo338 4:a43cbe8aed6a 202 old_state = 0;
dixysleo 0:58e350255405 203 //por dixys
dixysleo 0:58e350255405 204 pc.printf("opcion 4 \r\n");
dixysleo 0:58e350255405 205 }
dixysleo 0:58e350255405 206 }
dixysleo 0:58e350255405 207 }
dixysleo 0:58e350255405 208 /*
dixysleo 0:58e350255405 209 void uartCB(void)
Gustavo_Eduardo338 4:a43cbe8aed6a 210 {
Gustavo_Eduardo338 4:a43cbe8aed6a 211 while(pc.readable())
dixysleo 0:58e350255405 212 {
Gustavo_Eduardo338 4:a43cbe8aed6a 213 rx_buf[rx_len++] = pc.getc();
dixysleo 0:58e350255405 214 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
dixysleo 0:58e350255405 215 {
Gustavo_Eduardo338 4:a43cbe8aed6a 216 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
dixysleo 0:58e350255405 217 pc.printf("RecHandler \r\n");
dixysleo 0:58e350255405 218 pc.printf("Length: ");
dixysleo 0:58e350255405 219 pc.putc(rx_len);
dixysleo 0:58e350255405 220 pc.printf("\r\n");
dixysleo 0:58e350255405 221 rx_len = 0;
dixysleo 0:58e350255405 222 break;
dixysleo 0:58e350255405 223 }
dixysleo 0:58e350255405 224 }
dixysleo 0:58e350255405 225 }
dixysleo 0:58e350255405 226 */
dixysleo 0:58e350255405 227 void m_status_check_handle(void)
Gustavo_Eduardo338 4:a43cbe8aed6a 228 {
Gustavo_Eduardo338 5:a8c07a29df8a 229 uint8_t buf2[12];
dixysleo 0:58e350255405 230 uint8_t buf[3];
Gustavo_Eduardo338 5:a8c07a29df8a 231
Gustavo_Eduardo338 5:a8c07a29df8a 232 if(send_config > 0) {
Gustavo_Eduardo338 5:a8c07a29df8a 233 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), di_conf, 10);
Gustavo_Eduardo338 5:a8c07a29df8a 234 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), do_conf, 10);
Gustavo_Eduardo338 5:a8c07a29df8a 235 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), ai_conf, 10);
Gustavo_Eduardo338 5:a8c07a29df8a 236 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), ao_conf, 10);
Gustavo_Eduardo338 5:a8c07a29df8a 237 send_config -= 1;
Gustavo_Eduardo338 5:a8c07a29df8a 238 pc.printf("value: %d\r\n", send_config);
Gustavo_Eduardo338 5:a8c07a29df8a 239 }
Gustavo_Eduardo338 5:a8c07a29df8a 240
Gustavo_Eduardo338 4:a43cbe8aed6a 241 if (analog_enabled) { // if analog reading enabled
dixysleo 0:58e350255405 242 // Read and send out
dixysleo 0:58e350255405 243 float s = ANALOG;
Gustavo_Eduardo338 4:a43cbe8aed6a 244 uint16_t value = s*1024;
dixysleo 0:58e350255405 245 buf[0] = (0x0B);
dixysleo 0:58e350255405 246 buf[1] = (value >> 8);
dixysleo 0:58e350255405 247 buf[2] = (value);
Gustavo_Eduardo338 5:a8c07a29df8a 248 pc.printf("value: %d\r\n", value); // Imprimo en terminal lo que esta enviando desde el mote.
Gustavo_Eduardo338 5:a8c07a29df8a 249
Gustavo_Eduardo338 5:a8c07a29df8a 250 buf2[0] = 0xA1;
Gustavo_Eduardo338 5:a8c07a29df8a 251 buf2[1] = 0x01;
Gustavo_Eduardo338 5:a8c07a29df8a 252 buf2[2] = 0x97;
Gustavo_Eduardo338 5:a8c07a29df8a 253 buf2[3] = 0x00;
Gustavo_Eduardo338 5:a8c07a29df8a 254 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf2, 4);
Gustavo_Eduardo338 5:a8c07a29df8a 255
Gustavo_Eduardo338 4:a43cbe8aed6a 256 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
dixysleo 0:58e350255405 257 }
Gustavo_Eduardo338 4:a43cbe8aed6a 258
dixysleo 0:58e350255405 259 // If digital in changes, report the state
Gustavo_Eduardo338 4:a43cbe8aed6a 260 if (BUTTON != old_state) {
dixysleo 0:58e350255405 261 old_state = BUTTON;
Gustavo_Eduardo338 4:a43cbe8aed6a 262
Gustavo_Eduardo338 4:a43cbe8aed6a 263 if (BUTTON == 1) {
dixysleo 0:58e350255405 264 buf[0] = (0x0A);
dixysleo 0:58e350255405 265 buf[1] = (0x01);
Gustavo_Eduardo338 4:a43cbe8aed6a 266 buf[2] = (0x00);
Gustavo_Eduardo338 4:a43cbe8aed6a 267 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
Gustavo_Eduardo338 4:a43cbe8aed6a 268 } else {
dixysleo 0:58e350255405 269 buf[0] = (0x0A);
dixysleo 0:58e350255405 270 buf[1] = (0x00);
dixysleo 0:58e350255405 271 buf[2] = (0x00);
Gustavo_Eduardo338 4:a43cbe8aed6a 272 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
dixysleo 0:58e350255405 273 }
dixysleo 0:58e350255405 274 }
dixysleo 0:58e350255405 275 }
dixysleo 0:58e350255405 276
dixysleo 0:58e350255405 277 int main(void)
Gustavo_Eduardo338 4:a43cbe8aed6a 278 {
dixysleo 0:58e350255405 279 Ticker ticker;
dixysleo 0:58e350255405 280 ticker.attach_us(m_status_check_handle, 200000);
Gustavo_Eduardo338 4:a43cbe8aed6a 281
dixysleo 0:58e350255405 282 ble.init();
dixysleo 0:58e350255405 283 ble.onDisconnection(disconnectionCallback);
Gustavo_Eduardo338 5:a8c07a29df8a 284 ble.onConnection(connectionCallback);
Gustavo_Eduardo338 4:a43cbe8aed6a 285 ble.onDataWritten(WrittenHandler);
Gustavo_Eduardo338 4:a43cbe8aed6a 286
dixysleo 0:58e350255405 287 pc.baud(9600);
dixysleo 0:58e350255405 288 pc.printf("SimpleChat Init \r\n");
dixysleo 0:58e350255405 289
dixysleo 0:58e350255405 290 //pc.attach( uartCB , pc.RxIrq);
Gustavo_Eduardo338 4:a43cbe8aed6a 291
Gustavo_Eduardo338 4:a43cbe8aed6a 292 // setup advertising
dixysleo 0:58e350255405 293 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
dixysleo 0:58e350255405 294 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
dixysleo 0:58e350255405 295 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
Gustavo_Eduardo338 4:a43cbe8aed6a 296 //(const uint8_t *)"RedBearLab_1", sizeof("RedBearLab_1") - 1); // Original: Biscuit
Gustavo_Eduardo338 4:a43cbe8aed6a 297 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); // Original: Biscuit
dixysleo 0:58e350255405 298 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
Gustavo_Eduardo338 4:a43cbe8aed6a 299 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
Gustavo_Eduardo338 4:a43cbe8aed6a 300 // 100ms; in multiples of 0.625ms.
dixysleo 0:58e350255405 301 ble.setAdvertisingInterval(160);
dixysleo 0:58e350255405 302
dixysleo 0:58e350255405 303 ble.addService(uartService);
Gustavo_Eduardo338 4:a43cbe8aed6a 304
Gustavo_Eduardo338 4:a43cbe8aed6a 305 ble.startAdvertising();
Gustavo_Eduardo338 4:a43cbe8aed6a 306
dixysleo 0:58e350255405 307 pc.printf("Advertising Start \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 308
dixysleo 0:58e350255405 309 //por dixys
dixysleo 0:58e350255405 310 // para probar, luego quitar. Esto hace que cada ticker se envie un dato analogico via BLE
Gustavo_Eduardo338 5:a8c07a29df8a 311 analog_enabled = 0;
Gustavo_Eduardo338 4:a43cbe8aed6a 312
Gustavo_Eduardo338 4:a43cbe8aed6a 313 while(1) {
Gustavo_Eduardo338 4:a43cbe8aed6a 314 ble.waitForEvent();
dixysleo 0:58e350255405 315 }
Gustavo_Eduardo338 4:a43cbe8aed6a 316 }