Advent Calendar 2015用BLEPeripheral
Dependencies: BLE_API mbed nRF51822
Fork of BLENano_SimpleControls by
Diff: main.cpp
- Revision:
- 3:793b1f3e439b
- Parent:
- 2:3cd654f42efa
diff -r 3cd654f42efa -r 793b1f3e439b main.cpp --- a/main.cpp Mon Jul 27 06:53:46 2015 +0000 +++ b/main.cpp Fri Dec 18 13:53:29 2015 +0000 @@ -19,7 +19,6 @@ #include "mbed.h" #include "ble/BLE.h" -#include "Servo.h" #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ @@ -28,24 +27,18 @@ #define TXRX_BUF_LEN 20 -#define DIGITAL_OUT_PIN P0_9 //TXD -#define DIGITAL_IN_PIN P0_10 //CTS -#define PWM_PIN P0_11 //RXD -#define SERVO_PIN P0_8 //RTS -#define ANALOG_IN_PIN P0_4 //P04 +#define PowerLED_PIN P0_4 +#define ControlLED_PIN P0_5 BLE ble; -DigitalOut LED_SET(DIGITAL_OUT_PIN); -DigitalIn BUTTON(DIGITAL_IN_PIN); -PwmOut PWM(PWM_PIN); -AnalogIn ANALOG(ANALOG_IN_PIN); -Servo MYSERVO(SERVO_PIN); +DigitalOut LED_POWER(PowerLED_PIN); +DigitalOut LED_CONTROL(ControlLED_PIN); //Serial pc(USBTX, USBRX); -static uint8_t analog_enabled = 0; -static uint8_t old_state = 0; +//static uint8_t analog_enabled = 0; +//static uint8_t old_state = 0; // The Nordic UART Service static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; @@ -69,10 +62,15 @@ GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); - +void connectionCallback(const Gap::ConnectionCallbackParams_t *param) +{ + LED_POWER=1; + } -void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) +//void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *param) { + LED_POWER=0; //pc.printf("Disconnected \r\n"); //pc.printf("Restart advertising \r\n"); ble.startAdvertising(); @@ -92,38 +90,8 @@ //for(index=0; index<bytesRead; index++) //pc.putc(buf[index]); - if(buf[0] == 0x01) - { - if(buf[1] == 0x01) - LED_SET = 1; - else - LED_SET = 0; - } - else if(buf[0] == 0xA0) - { - if(buf[1] == 0x01) - analog_enabled = 1; - else - analog_enabled = 0; - } - else if(buf[0] == 0x02) - { - float value = (float)buf[1]/255; - PWM = value; - } - else if(buf[0] == 0x03) - { - MYSERVO.write(buf[1]); - } - else if(buf[0] == 0x04) - { - analog_enabled = 0; - PWM = 0; - MYSERVO.write(0); - LED_SET = 0; - old_state = 0; - } - + if(buf[0] == 0x01) LED_CONTROL=1; + else if(buf[0] == 0x00) LED_CONTROL=0; } } /* @@ -147,47 +115,16 @@ */ void m_status_check_handle(void) { - uint8_t buf[3]; - if (analog_enabled) // if analog reading enabled - { - // Read and send out - float s = ANALOG; - uint16_t value = s*1024; - buf[0] = (0x0B); - buf[1] = (value >> 8); - buf[2] = (value); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } - - // If digital in changes, report the state - if (BUTTON != old_state) - { - old_state = BUTTON; - - if (BUTTON == 1) - { - buf[0] = (0x0A); - buf[1] = (0x01); - buf[2] = (0x00); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } - else - { - buf[0] = (0x0A); - buf[1] = (0x00); - buf[2] = (0x00); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } - } } int main(void) { - Ticker ticker; - ticker.attach_us(m_status_check_handle, 200000); +// Ticker ticker; +// ticker.attach_us(m_status_check_handle, 200000); ble.init(); + ble.onConnection(connectionCallback); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); @@ -199,7 +136,9 @@ // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, + //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, + // (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));