Advent Calendar 2015用BLEPeripheral
Dependencies: BLE_API mbed nRF51822
Fork of BLENano_SimpleControls by
Revision 3:793b1f3e439b, committed 2015-12-18
- Comitter:
- yueee_yt
- Date:
- Fri Dec 18 13:53:29 2015 +0000
- Parent:
- 2:3cd654f42efa
- Commit message:
- Advent Calndar 2015
Changed in this revision
--- a/BLE_API.lib Mon Jul 27 06:53:46 2015 +0000 +++ b/BLE_API.lib Fri Dec 18 13:53:29 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#4cd3b18607ec +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#bfc5b9b6ecf5
--- a/Servo.cpp Mon Jul 27 06:53:46 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -/* - -Copyright (c) 2012-2014 RedBearLab - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software -and associated documentation files (the "Software"), to deal in the Software without restriction, -including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -*/ - -#include "Servo.h" - -Servo::Servo(PinName pin) : _servo(pin) -{ - _servo.period_ms(20); -} - -Servo::~Servo(void) -{ - -} - -void Servo::write(unsigned char degree) -{ - convert(degree); - _servo.pulsewidth_us(pulse); -} - -void Servo::convert(unsigned char degree) -{ - // 0~180 degree correspond to 500~2500 - pulse = degree * 11 + 500; -}
--- a/Servo.h Mon Jul 27 06:53:46 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - -Copyright (c) 2012-2014 RedBearLab - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software -and associated documentation files (the "Software"), to deal in the Software without restriction, -including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -*/ - -#ifndef _SERVO_H -#define _SERVO_H - -#include "mbed.h" - -class Servo -{ -public: - Servo(PinName pin); - ~Servo(void); - - void write(unsigned char degree); - -private: - void convert(unsigned char degree); - - PwmOut _servo; - unsigned int pulse; -}; - -#endif \ No newline at end of file
--- 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));
--- a/mbed.bld Mon Jul 27 06:53:46 2015 +0000 +++ b/mbed.bld Fri Dec 18 13:53:29 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/bad568076d81 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b \ No newline at end of file
--- a/nRF51822.lib Mon Jul 27 06:53:46 2015 +0000 +++ b/nRF51822.lib Fri Dec 18 13:53:29 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#c7adea3c1e37 +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#3cc0718d98d0