JKSoftさんのを少しだけモディファイ。 左アナログ上下でLED2の明るさ変更

Dependencies:   BLE_API_Native_IRC mbed

Fork of BLE_RCBController by Junichi Katsu

Committer:
micono
Date:
Sat Jul 12 15:32:26 2014 +0000
Revision:
1:653130ebf8fd
Parent:
0:8c643bfe55b7
?????LED??; ????????LED???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8c643bfe55b7 1 #include "mbed.h"
jksoft 0:8c643bfe55b7 2 #include "nRF51822n.h"
jksoft 0:8c643bfe55b7 3 #include "RCBController.h"
jksoft 0:8c643bfe55b7 4
jksoft 0:8c643bfe55b7 5 #define DBG 0
jksoft 0:8c643bfe55b7 6
jksoft 0:8c643bfe55b7 7 nRF51822n nrf;
jksoft 0:8c643bfe55b7 8 Serial pc(USBTX, USBRX);
jksoft 0:8c643bfe55b7 9 /* LEDs for indication: */
jksoft 0:8c643bfe55b7 10 DigitalOut ConnectStateLed(LED1);
jksoft 0:8c643bfe55b7 11 PwmOut ControllerStateLed(LED2);
jksoft 0:8c643bfe55b7 12
jksoft 0:8c643bfe55b7 13
jksoft 0:8c643bfe55b7 14 /* RCBController Service */
jksoft 0:8c643bfe55b7 15 static const uint16_t RCBController_service_uuid = 0xFFF0;
jksoft 0:8c643bfe55b7 16 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
jksoft 0:8c643bfe55b7 17 GattService RCBControllerService (RCBController_service_uuid);
jksoft 0:8c643bfe55b7 18 GattCharacteristic Controller (RCBController_Characteristic_uuid,10, 10,
jksoft 0:8c643bfe55b7 19 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
jksoft 0:8c643bfe55b7 20 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
jksoft 0:8c643bfe55b7 21
jksoft 0:8c643bfe55b7 22 /* Advertising data and parameters */
jksoft 0:8c643bfe55b7 23 GapAdvertisingData advData;
jksoft 0:8c643bfe55b7 24 GapAdvertisingData scanResponse;
jksoft 0:8c643bfe55b7 25 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
jksoft 0:8c643bfe55b7 26
jksoft 0:8c643bfe55b7 27 uint16_t uuid16_list[] = { RCBController_service_uuid };
jksoft 0:8c643bfe55b7 28
jksoft 0:8c643bfe55b7 29 RCBController controller;
jksoft 0:8c643bfe55b7 30
jksoft 0:8c643bfe55b7 31 // GapEvent
jksoft 0:8c643bfe55b7 32 class GapEventHandler : public GapEvents
jksoft 0:8c643bfe55b7 33 {
jksoft 0:8c643bfe55b7 34
jksoft 0:8c643bfe55b7 35 virtual void onConnected(void)
jksoft 0:8c643bfe55b7 36 {
micono 1:653130ebf8fd 37 ConnectStateLed = 1;
jksoft 0:8c643bfe55b7 38 #if DBG
jksoft 0:8c643bfe55b7 39 pc.printf("Connected\n\r");
jksoft 0:8c643bfe55b7 40 #endif
jksoft 0:8c643bfe55b7 41 }
jksoft 0:8c643bfe55b7 42
jksoft 0:8c643bfe55b7 43 virtual void onDisconnected(void)
jksoft 0:8c643bfe55b7 44 {
jksoft 0:8c643bfe55b7 45 nrf.getGap().startAdvertising(advParams);
jksoft 0:8c643bfe55b7 46 ConnectStateLed = 1;
jksoft 0:8c643bfe55b7 47 #if DBG
jksoft 0:8c643bfe55b7 48 pc.printf("Disconnected\n\r");
jksoft 0:8c643bfe55b7 49 #endif
jksoft 0:8c643bfe55b7 50 }
jksoft 0:8c643bfe55b7 51 };
jksoft 0:8c643bfe55b7 52
jksoft 0:8c643bfe55b7 53 // GattEvent
jksoft 0:8c643bfe55b7 54 class GattServerEventHandler : public GattServerEvents
jksoft 0:8c643bfe55b7 55 {
jksoft 0:8c643bfe55b7 56 virtual void onDataWritten(uint16_t charHandle)
jksoft 0:8c643bfe55b7 57 {
jksoft 0:8c643bfe55b7 58 if (charHandle == Controller.handle) {
jksoft 0:8c643bfe55b7 59 nrf.getGattServer().readValue(Controller.handle, &controller.data[0], sizeof(controller));
jksoft 0:8c643bfe55b7 60 #if DBG
jksoft 0:8c643bfe55b7 61 pc.printf("DATA:%d %d %d %d %d %d %d %d %d %d\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4],
jksoft 0:8c643bfe55b7 62 controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
jksoft 0:8c643bfe55b7 63 #endif
micono 1:653130ebf8fd 64 //ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;
micono 1:653130ebf8fd 65 ControllerStateLed = (float)controller.status.LeftAnalogUD / 255.0;
jksoft 0:8c643bfe55b7 66
jksoft 0:8c643bfe55b7 67 }
jksoft 0:8c643bfe55b7 68
jksoft 0:8c643bfe55b7 69 }
jksoft 0:8c643bfe55b7 70 };
jksoft 0:8c643bfe55b7 71
jksoft 0:8c643bfe55b7 72 /**************************************************************************/
jksoft 0:8c643bfe55b7 73 /*!
jksoft 0:8c643bfe55b7 74 @brief Program entry point
jksoft 0:8c643bfe55b7 75 */
jksoft 0:8c643bfe55b7 76 /**************************************************************************/
jksoft 0:8c643bfe55b7 77 int main(void)
jksoft 0:8c643bfe55b7 78 {
jksoft 0:8c643bfe55b7 79 #if DBG
jksoft 0:8c643bfe55b7 80 pc.printf("Start\n\r");
jksoft 0:8c643bfe55b7 81 #endif
jksoft 0:8c643bfe55b7 82 /* Setup an event handler for GAP events i.e. Client/Server connection events. */
jksoft 0:8c643bfe55b7 83 nrf.getGap().setEventHandler(new GapEventHandler());
jksoft 0:8c643bfe55b7 84
jksoft 0:8c643bfe55b7 85 /* Initialise the nRF51822 */
jksoft 0:8c643bfe55b7 86 nrf.init();
jksoft 0:8c643bfe55b7 87
jksoft 0:8c643bfe55b7 88 nrf.getGattServer().setEventHandler(new GattServerEventHandler());
jksoft 0:8c643bfe55b7 89
jksoft 0:8c643bfe55b7 90 /* Make sure we get a clean start */
jksoft 0:8c643bfe55b7 91 nrf.reset();
jksoft 0:8c643bfe55b7 92
jksoft 0:8c643bfe55b7 93 /* Add BLE-Only flag and complete service list to the advertising data */
jksoft 0:8c643bfe55b7 94 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jksoft 0:8c643bfe55b7 95 advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
jksoft 0:8c643bfe55b7 96 (uint8_t*)uuid16_list, sizeof(uuid16_list));
jksoft 0:8c643bfe55b7 97 nrf.getGap().setAdvertisingData(advData, scanResponse);
jksoft 0:8c643bfe55b7 98
jksoft 0:8c643bfe55b7 99 /* RCBController Service */
jksoft 0:8c643bfe55b7 100 RCBControllerService.addCharacteristic(Controller);
jksoft 0:8c643bfe55b7 101 nrf.getGattServer().addService(RCBControllerService);
jksoft 0:8c643bfe55b7 102
jksoft 0:8c643bfe55b7 103 /* Start advertising (make sure you've added all your data first) */
jksoft 0:8c643bfe55b7 104 nrf.getGap().startAdvertising(advParams);
micono 1:653130ebf8fd 105 ConnectStateLed = 0;
jksoft 0:8c643bfe55b7 106 ControllerStateLed = 1;
jksoft 0:8c643bfe55b7 107
jksoft 0:8c643bfe55b7 108 for (;;)
jksoft 0:8c643bfe55b7 109 {
jksoft 0:8c643bfe55b7 110 wait(1);
jksoft 0:8c643bfe55b7 111 }
jksoft 0:8c643bfe55b7 112 }
jksoft 0:8c643bfe55b7 113