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:
Wed Jun 01 15:17:19 2016 +0000
Revision:
4:a43cbe8aed6a
Parent:
3:53cd39af0b12
Child:
5:a8c07a29df8a
Reformat code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dixysleo 0:58e350255405 1 /*
Gustavo_Eduardo338 1:eda9c65b6db9 2 Primera Linea por Gustavo Belduma
dixysleo 0:58e350255405 3 Copyright (c) 2012-2014 RedBearLab
dixysleo 0:58e350255405 4
Gustavo_Eduardo338 4:a43cbe8aed6a 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Gustavo_Eduardo338 4:a43cbe8aed6a 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
Gustavo_Eduardo338 4:a43cbe8aed6a 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
Gustavo_Eduardo338 4:a43cbe8aed6a 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
dixysleo 0:58e350255405 9 subject to the following conditions:
dixysleo 0:58e350255405 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
dixysleo 0:58e350255405 11
Gustavo_Eduardo338 4:a43cbe8aed6a 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Gustavo_Eduardo338 4:a43cbe8aed6a 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
Gustavo_Eduardo338 4:a43cbe8aed6a 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
Gustavo_Eduardo338 4:a43cbe8aed6a 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
dixysleo 0:58e350255405 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
dixysleo 0:58e350255405 17
dixysleo 0:58e350255405 18 */
dixysleo 0:58e350255405 19
dixysleo 0:58e350255405 20 #include "mbed.h"
dixysleo 0:58e350255405 21 #include "ble/BLE.h"
barcejorge 3:53cd39af0b12 22 //#include "Servo.h"
dixysleo 0:58e350255405 23 #include "GattCallbackParamTypes.h"
dixysleo 0:58e350255405 24
dixysleo 0:58e350255405 25 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
dixysleo 0:58e350255405 26 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
dixysleo 0:58e350255405 27 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
dixysleo 0:58e350255405 28
dixysleo 0:58e350255405 29 #define TXRX_BUF_LEN 20
dixysleo 0:58e350255405 30
dixysleo 0:58e350255405 31 #define DIGITAL_OUT_PIN P0_17 //D7
dixysleo 0:58e350255405 32 #define DIGITAL_IN_PIN P0_5 //A4
dixysleo 0:58e350255405 33 #define PWM_PIN P0_16 //D6
barcejorge 3:53cd39af0b12 34 //#define SERVO_PIN P0_14 //D10
dixysleo 0:58e350255405 35 #define ANALOG_IN_PIN P0_6 //A5
dixysleo 0:58e350255405 36
dixysleo 0:58e350255405 37 BLE ble;
dixysleo 0:58e350255405 38
dixysleo 0:58e350255405 39 DigitalOut LED_SET(DIGITAL_OUT_PIN);
dixysleo 0:58e350255405 40 DigitalIn BUTTON(DIGITAL_IN_PIN);
dixysleo 0:58e350255405 41 PwmOut PWM(PWM_PIN);
dixysleo 0:58e350255405 42 AnalogIn ANALOG(ANALOG_IN_PIN);
barcejorge 3:53cd39af0b12 43 //Servo MYSERVO(SERVO_PIN);
dixysleo 0:58e350255405 44
dixysleo 0:58e350255405 45 Serial pc(USBTX, USBRX);
dixysleo 0:58e350255405 46
dixysleo 0:58e350255405 47 static uint8_t analog_enabled = 0;
dixysleo 0:58e350255405 48 static uint8_t old_state = 0;
dixysleo 0:58e350255405 49
dixysleo 0:58e350255405 50 // The Nordic UART Service
dixysleo 0:58e350255405 51 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 52 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 53 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 54 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 55
dixysleo 0:58e350255405 56
dixysleo 0:58e350255405 57 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
dixysleo 0:58e350255405 58 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
dixysleo 0:58e350255405 59
dixysleo 0:58e350255405 60 //static uint8_t rx_buf[TXRX_BUF_LEN];
dixysleo 0:58e350255405 61 //static uint8_t rx_len=0;
dixysleo 0:58e350255405 62
dixysleo 0:58e350255405 63
dixysleo 0:58e350255405 64 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
Gustavo_Eduardo338 4:a43cbe8aed6a 65
dixysleo 0:58e350255405 66 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
Gustavo_Eduardo338 4:a43cbe8aed6a 67
dixysleo 0:58e350255405 68 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
dixysleo 0:58e350255405 69
dixysleo 0:58e350255405 70 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
dixysleo 0:58e350255405 71
dixysleo 0:58e350255405 72
dixysleo 0:58e350255405 73 /*
dixysleo 0:58e350255405 74 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
dixysleo 0:58e350255405 75 {
dixysleo 0:58e350255405 76 pc.printf("Disconnected \r\n");
dixysleo 0:58e350255405 77 pc.printf("Restart advertising \r\n");
dixysleo 0:58e350255405 78 ble.startAdvertising();
dixysleo 0:58e350255405 79 }
Gustavo_Eduardo338 4:a43cbe8aed6a 80 */
dixysleo 0:58e350255405 81
dixysleo 0:58e350255405 82 // La de arriba era la original, pero se cambio para que funcione, solucion sacada de la web
dixysleo 0:58e350255405 83 // https://developer.mbed.org/forum/repo-61676-BLE_GAP_Example-community/topic/17193/
Gustavo_Eduardo338 4:a43cbe8aed6a 84 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
Gustavo_Eduardo338 4:a43cbe8aed6a 85 {
Gustavo_Eduardo338 4:a43cbe8aed6a 86 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
dixysleo 0:58e350255405 87 pc.printf("Disconnected \r\n");
dixysleo 0:58e350255405 88 pc.printf("Restart advertising \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 89 ble.startAdvertising();
Gustavo_Eduardo338 4:a43cbe8aed6a 90
dixysleo 0:58e350255405 91 }
dixysleo 0:58e350255405 92
dixysleo 0:58e350255405 93
dixysleo 0:58e350255405 94 void WrittenHandler(const GattWriteCallbackParams *Handler)
Gustavo_Eduardo338 4:a43cbe8aed6a 95 {
dixysleo 0:58e350255405 96 uint8_t buf[TXRX_BUF_LEN];
dixysleo 0:58e350255405 97 uint16_t bytesRead, index;
Gustavo_Eduardo338 4:a43cbe8aed6a 98
Gustavo_Eduardo338 4:a43cbe8aed6a 99 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) {
dixysleo 0:58e350255405 100 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
dixysleo 0:58e350255405 101 memset(txPayload, 0, TXRX_BUF_LEN);
Gustavo_Eduardo338 4:a43cbe8aed6a 102 memcpy(txPayload, buf, TXRX_BUF_LEN);
Gustavo_Eduardo338 4:a43cbe8aed6a 103
dixysleo 0:58e350255405 104 //por dixys
Gustavo_Eduardo338 4:a43cbe8aed6a 105 pc.printf("Buffer recibido desde Android: \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 106
dixysleo 0:58e350255405 107 for(index=0; index<bytesRead; index++)
dixysleo 0:58e350255405 108 pc.putc(buf[index]);
Gustavo_Eduardo338 4:a43cbe8aed6a 109
Gustavo_Eduardo338 4:a43cbe8aed6a 110 if(buf[0] == 0x01) {
Gustavo_Eduardo338 4:a43cbe8aed6a 111 if(buf[1] == 0x01) {
dixysleo 0:58e350255405 112 LED_SET = 1;
dixysleo 0:58e350255405 113 //por dixys
dixysleo 0:58e350255405 114 pc.printf("LED SET 1 \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 115 } else {
Gustavo_Eduardo338 4:a43cbe8aed6a 116 LED_SET = 0;
dixysleo 0:58e350255405 117 //por dixys
dixysleo 0:58e350255405 118 pc.printf("LED SET 0 \r\n");
dixysleo 0:58e350255405 119 }
Gustavo_Eduardo338 4:a43cbe8aed6a 120 } else if(buf[0] == 0xA0) {
Gustavo_Eduardo338 4:a43cbe8aed6a 121 if(buf[1] == 0x01) {
dixysleo 0:58e350255405 122 analog_enabled = 1;
dixysleo 0:58e350255405 123 //por dixys
dixysleo 0:58e350255405 124 pc.printf("ANALOG ENABLE \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 125 } else {
dixysleo 0:58e350255405 126 //analog_enabled = 0;
dixysleo 0:58e350255405 127 // lo vamos a poner fio enable para hacer pruebas luego lo quitamos
dixysleo 0:58e350255405 128 //por dixys
dixysleo 0:58e350255405 129 pc.printf("ANALOG DISAABLE \r\n");
dixysleo 0:58e350255405 130 }
Gustavo_Eduardo338 4:a43cbe8aed6a 131 } else if(buf[0] == 0x02) {
dixysleo 0:58e350255405 132 float value = (float)buf[1]/255;
dixysleo 0:58e350255405 133 PWM = value;
dixysleo 0:58e350255405 134 //por dixys
dixysleo 0:58e350255405 135 pc.printf("PWM = %f \r\n", value);
Gustavo_Eduardo338 4:a43cbe8aed6a 136 } else if(buf[0] == 0x03) {
barcejorge 3:53cd39af0b12 137 //MYSERVO.write(buf[1]);
dixysleo 0:58e350255405 138 //por dixys
dixysleo 0:58e350255405 139 pc.printf("SERVO buffer \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 140 } else if(buf[0] == 0x04) {
dixysleo 0:58e350255405 141 analog_enabled = 0;
dixysleo 0:58e350255405 142 PWM = 0;
barcejorge 3:53cd39af0b12 143 //MYSERVO.write(0);
dixysleo 0:58e350255405 144 LED_SET = 0;
Gustavo_Eduardo338 4:a43cbe8aed6a 145 old_state = 0;
dixysleo 0:58e350255405 146 //por dixys
dixysleo 0:58e350255405 147 pc.printf("opcion 4 \r\n");
dixysleo 0:58e350255405 148 }
dixysleo 0:58e350255405 149
dixysleo 0:58e350255405 150 }
dixysleo 0:58e350255405 151 }
dixysleo 0:58e350255405 152 /*
dixysleo 0:58e350255405 153 void uartCB(void)
Gustavo_Eduardo338 4:a43cbe8aed6a 154 {
Gustavo_Eduardo338 4:a43cbe8aed6a 155 while(pc.readable())
dixysleo 0:58e350255405 156 {
Gustavo_Eduardo338 4:a43cbe8aed6a 157 rx_buf[rx_len++] = pc.getc();
dixysleo 0:58e350255405 158 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
dixysleo 0:58e350255405 159 {
Gustavo_Eduardo338 4:a43cbe8aed6a 160 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
dixysleo 0:58e350255405 161 pc.printf("RecHandler \r\n");
dixysleo 0:58e350255405 162 pc.printf("Length: ");
dixysleo 0:58e350255405 163 pc.putc(rx_len);
dixysleo 0:58e350255405 164 pc.printf("\r\n");
dixysleo 0:58e350255405 165 rx_len = 0;
dixysleo 0:58e350255405 166 break;
dixysleo 0:58e350255405 167 }
dixysleo 0:58e350255405 168 }
dixysleo 0:58e350255405 169 }
dixysleo 0:58e350255405 170 */
dixysleo 0:58e350255405 171 void m_status_check_handle(void)
Gustavo_Eduardo338 4:a43cbe8aed6a 172 {
dixysleo 0:58e350255405 173 uint8_t buf[3];
Gustavo_Eduardo338 4:a43cbe8aed6a 174 if (analog_enabled) { // if analog reading enabled
dixysleo 0:58e350255405 175 // Read and send out
dixysleo 0:58e350255405 176 float s = ANALOG;
Gustavo_Eduardo338 4:a43cbe8aed6a 177 uint16_t value = s*1024;
dixysleo 0:58e350255405 178 buf[0] = (0x0B);
dixysleo 0:58e350255405 179 buf[1] = (value >> 8);
dixysleo 0:58e350255405 180 buf[2] = (value);
Gustavo_Eduardo338 4:a43cbe8aed6a 181 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
dixysleo 0:58e350255405 182 }
Gustavo_Eduardo338 4:a43cbe8aed6a 183
dixysleo 0:58e350255405 184 // If digital in changes, report the state
Gustavo_Eduardo338 4:a43cbe8aed6a 185 if (BUTTON != old_state) {
dixysleo 0:58e350255405 186 old_state = BUTTON;
Gustavo_Eduardo338 4:a43cbe8aed6a 187
Gustavo_Eduardo338 4:a43cbe8aed6a 188 if (BUTTON == 1) {
dixysleo 0:58e350255405 189 buf[0] = (0x0A);
dixysleo 0:58e350255405 190 buf[1] = (0x01);
Gustavo_Eduardo338 4:a43cbe8aed6a 191 buf[2] = (0x00);
Gustavo_Eduardo338 4:a43cbe8aed6a 192 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
Gustavo_Eduardo338 4:a43cbe8aed6a 193 } else {
dixysleo 0:58e350255405 194 buf[0] = (0x0A);
dixysleo 0:58e350255405 195 buf[1] = (0x00);
dixysleo 0:58e350255405 196 buf[2] = (0x00);
Gustavo_Eduardo338 4:a43cbe8aed6a 197 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
dixysleo 0:58e350255405 198 }
dixysleo 0:58e350255405 199 }
dixysleo 0:58e350255405 200 }
dixysleo 0:58e350255405 201
dixysleo 0:58e350255405 202 int main(void)
Gustavo_Eduardo338 4:a43cbe8aed6a 203 {
dixysleo 0:58e350255405 204 Ticker ticker;
dixysleo 0:58e350255405 205 ticker.attach_us(m_status_check_handle, 200000);
Gustavo_Eduardo338 4:a43cbe8aed6a 206
dixysleo 0:58e350255405 207 ble.init();
dixysleo 0:58e350255405 208 ble.onDisconnection(disconnectionCallback);
Gustavo_Eduardo338 4:a43cbe8aed6a 209
Gustavo_Eduardo338 4:a43cbe8aed6a 210 ble.onDataWritten(WrittenHandler);
Gustavo_Eduardo338 4:a43cbe8aed6a 211
dixysleo 0:58e350255405 212 pc.baud(9600);
dixysleo 0:58e350255405 213 pc.printf("SimpleChat Init \r\n");
dixysleo 0:58e350255405 214
dixysleo 0:58e350255405 215 //pc.attach( uartCB , pc.RxIrq);
Gustavo_Eduardo338 4:a43cbe8aed6a 216
Gustavo_Eduardo338 4:a43cbe8aed6a 217 // setup advertising
dixysleo 0:58e350255405 218 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
dixysleo 0:58e350255405 219 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
dixysleo 0:58e350255405 220 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
Gustavo_Eduardo338 4:a43cbe8aed6a 221 //(const uint8_t *)"RedBearLab_1", sizeof("RedBearLab_1") - 1); // Original: Biscuit
Gustavo_Eduardo338 4:a43cbe8aed6a 222 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); // Original: Biscuit
dixysleo 0:58e350255405 223 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
Gustavo_Eduardo338 4:a43cbe8aed6a 224 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
Gustavo_Eduardo338 4:a43cbe8aed6a 225 // 100ms; in multiples of 0.625ms.
dixysleo 0:58e350255405 226 ble.setAdvertisingInterval(160);
dixysleo 0:58e350255405 227
dixysleo 0:58e350255405 228 ble.addService(uartService);
Gustavo_Eduardo338 4:a43cbe8aed6a 229
Gustavo_Eduardo338 4:a43cbe8aed6a 230 ble.startAdvertising();
Gustavo_Eduardo338 4:a43cbe8aed6a 231
dixysleo 0:58e350255405 232 pc.printf("Advertising Start \r\n");
Gustavo_Eduardo338 4:a43cbe8aed6a 233
dixysleo 0:58e350255405 234 //por dixys
dixysleo 0:58e350255405 235 // para probar, luego quitar. Esto hace que cada ticker se envie un dato analogico via BLE
dixysleo 0:58e350255405 236 analog_enabled = 1;
Gustavo_Eduardo338 4:a43cbe8aed6a 237
Gustavo_Eduardo338 4:a43cbe8aed6a 238 while(1) {
Gustavo_Eduardo338 4:a43cbe8aed6a 239 ble.waitForEvent();
dixysleo 0:58e350255405 240 }
Gustavo_Eduardo338 4:a43cbe8aed6a 241 }