Gustavo Belduma
/
nRF51822_SimpleControls_1
Macros Corregidas
Diff: main.cpp
- Revision:
- 5:a8c07a29df8a
- Parent:
- 4:a43cbe8aed6a
- Child:
- 6:84154a9a8717
--- a/main.cpp Wed Jun 01 15:17:19 2016 +0000 +++ b/main.cpp Tue Jun 14 19:08:23 2016 +0000 @@ -1,5 +1,4 @@ /* -Primera Linea por Gustavo Belduma Copyright (c) 2012-2014 RedBearLab Permission is hereby granted, free of charge, to any person obtaining a copy of this software @@ -28,18 +27,63 @@ #define TXRX_BUF_LEN 20 -#define DIGITAL_OUT_PIN P0_17 //D7 +#define DIGITAL_OUT_7 P0_17 //D7 +#define DIGITAL_OUT_5 P0_23 //D5 +#define DIGITAL_OUT_4 P0_21 //D4 + #define DIGITAL_IN_PIN P0_5 //A4 + #define PWM_PIN P0_16 //D6 //#define SERVO_PIN P0_14 //D10 #define ANALOG_IN_PIN P0_6 //A5 -BLE ble; +// Declarando los pines +static int32_t A_0 = 0x00; +static int32_t A_1 = 0x00; +static int32_t A_2 = 0x00; +static int32_t A_3 = 0x00; +static int32_t A_4 = 0x01; +static int32_t A_5 = 0x01; -DigitalOut LED_SET(DIGITAL_OUT_PIN); +static int32_t D_0 = 0x00; +static int32_t D_1 = 0x00; +static int32_t D_2 = 0x00; +static int32_t D_3 = 0x00; +static int32_t D_4 = 0x01; +static int32_t D_5 = 0x01; +static int32_t D_6 = 0x01; +static int32_t D_7 = 0x01; +static int32_t D_8 = 0x00; +static int32_t D_9 = 0x00; +static int32_t D_10 = 0x00; +static int32_t D_11 = 0x00; +static int32_t D_12 = 0x00; +static int32_t D_13 = 0x00; +static int32_t D_14 = 0x00; +static int32_t D_15 = 0x00; +static int32_t D_16 = 0x00; +static int32_t D_17 = 0x00; +static int32_t D_18 = 0x00; + +static int32_t send_config = 0; + +BLE ble; + +// Digital IN DigitalIn BUTTON(DIGITAL_IN_PIN); + +// Digital OUT +DigitalOut LED_SET_D7(DIGITAL_OUT_7); +DigitalOut LED_SET_D5(DIGITAL_OUT_5); +DigitalOut LED_SET_D4(DIGITAL_OUT_4); + +// Analog IN +AnalogIn ANALOG(ANALOG_IN_PIN); + +// Analog OUT + + PwmOut PWM(PWM_PIN); -AnalogIn ANALOG(ANALOG_IN_PIN); //Servo MYSERVO(SERVO_PIN); Serial pc(USBTX, USBRX); @@ -53,23 +97,21 @@ static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; +// Declarando los vectores de configuracion para el mote. +static const uint8_t di_conf [] = {0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, A_2, A_1, A_0}; // length: 10 +static const uint8_t do_conf [] = {0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, A_5, A_4, A_3}; // length: 10 +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 +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 + uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; -//static uint8_t rx_buf[TXRX_BUF_LEN]; -//static uint8_t rx_len=0; - - GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); - GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); - GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; - GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); - /* void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { @@ -87,12 +129,22 @@ pc.printf("Disconnected \r\n"); pc.printf("Restart advertising \r\n"); ble.startAdvertising(); - + LED_SET_D7 = 0; + send_config = 0; +} +// Ingresa por este metdo unicamente la primera vez que se conecta al mote. +// Tomado desde: https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_LEDBlinker/file/dc392bde2b3c/main.cpp +void connectionCallback(const Gap::ConnectionCallbackParams_t *) +{ + pc.printf("connectionCallback \r\n"); + LED_SET_D7 = 1; + send_config = 2; } - +// Recepta las caracteristicas que se desea escribir en el mote. void WrittenHandler(const GattWriteCallbackParams *Handler) { + pc.printf("WrittenHandler(const GattWriteCallbackParams *Handler) \r\n"); uint8_t buf[TXRX_BUF_LEN]; uint16_t bytesRead, index; @@ -109,11 +161,15 @@ if(buf[0] == 0x01) { if(buf[1] == 0x01) { - LED_SET = 1; + LED_SET_D7 = 1; + LED_SET_D5 = 1; + LED_SET_D4 = 1; //por dixys pc.printf("LED SET 1 \r\n"); } else { - LED_SET = 0; + LED_SET_D7 = 0; + LED_SET_D5 = 0; + LED_SET_D4 = 0; //por dixys pc.printf("LED SET 0 \r\n"); } @@ -123,10 +179,11 @@ //por dixys pc.printf("ANALOG ENABLE \r\n"); } else { - //analog_enabled = 0; + analog_enabled = 0; // lo vamos a poner fio enable para hacer pruebas luego lo quitamos //por dixys pc.printf("ANALOG DISAABLE \r\n"); + pc.printf("valor : %d", analog_enabled); } } else if(buf[0] == 0x02) { float value = (float)buf[1]/255; @@ -141,12 +198,11 @@ analog_enabled = 0; PWM = 0; //MYSERVO.write(0); - LED_SET = 0; + LED_SET_D7 = 0; old_state = 0; //por dixys pc.printf("opcion 4 \r\n"); } - } } /* @@ -170,7 +226,18 @@ */ void m_status_check_handle(void) { + uint8_t buf2[12]; uint8_t buf[3]; + + if(send_config > 0) { + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), di_conf, 10); + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), do_conf, 10); + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), ai_conf, 10); + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), ao_conf, 10); + send_config -= 1; + pc.printf("value: %d\r\n", send_config); + } + if (analog_enabled) { // if analog reading enabled // Read and send out float s = ANALOG; @@ -178,6 +245,14 @@ buf[0] = (0x0B); buf[1] = (value >> 8); buf[2] = (value); + pc.printf("value: %d\r\n", value); // Imprimo en terminal lo que esta enviando desde el mote. + + buf2[0] = 0xA1; + buf2[1] = 0x01; + buf2[2] = 0x97; + buf2[3] = 0x00; + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf2, 4); + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); } @@ -206,7 +281,7 @@ ble.init(); ble.onDisconnection(disconnectionCallback); - + ble.onConnection(connectionCallback); ble.onDataWritten(WrittenHandler); pc.baud(9600); @@ -233,7 +308,7 @@ //por dixys // para probar, luego quitar. Esto hace que cada ticker se envie un dato analogico via BLE - analog_enabled = 1; + analog_enabled = 0; while(1) { ble.waitForEvent();