Release for tas lab

Dependencies:   BLE_API mbed

Fork of BLE_RCBController by Junichi Katsu

Committer:
tasdevelop
Date:
Thu Mar 19 06:52:09 2015 +0000
Revision:
4:06b0361c5975
Parent:
3:6a9f4ea2e7c6
KabeSwitch for taslab

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8c643bfe55b7 1 #include "mbed.h"
jksoft 1:48f6e08a3ac2 2 #include "BLEDevice.h"
jksoft 0:8c643bfe55b7 3 #include "RCBController.h"
jksoft 0:8c643bfe55b7 4
jksoft 1:48f6e08a3ac2 5 #define DBG 1
jksoft 0:8c643bfe55b7 6
jksoft 1:48f6e08a3ac2 7 BLEDevice ble;
jksoft 0:8c643bfe55b7 8 Serial pc(USBTX, USBRX);
jksoft 0:8c643bfe55b7 9 /* LEDs for indication: */
jksoft 0:8c643bfe55b7 10 DigitalOut ConnectStateLed(LED1);
suzukimitsuhiro 3:6a9f4ea2e7c6 11 PwmOut servo1(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 1:48f6e08a3ac2 17 uint8_t RCBControllerPayload[10] = {0,};
jksoft 1:48f6e08a3ac2 18
jksoft 1:48f6e08a3ac2 19 GattCharacteristic ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10,
jksoft 0:8c643bfe55b7 20 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
jksoft 0:8c643bfe55b7 21 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
jksoft 1:48f6e08a3ac2 22 GattCharacteristic *ControllerChars[] = {&ControllerChar};
jksoft 1:48f6e08a3ac2 23 GattService RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
jksoft 0:8c643bfe55b7 24
jksoft 0:8c643bfe55b7 25 RCBController controller;
jksoft 0:8c643bfe55b7 26
jksoft 1:48f6e08a3ac2 27 void onConnected(uint16_t h)
jksoft 0:8c643bfe55b7 28 {
jksoft 1:48f6e08a3ac2 29 ConnectStateLed = 0;
jksoft 0:8c643bfe55b7 30 #if DBG
jksoft 1:48f6e08a3ac2 31 pc.printf("Connected\n\r");
jksoft 0:8c643bfe55b7 32 #endif
jksoft 1:48f6e08a3ac2 33 }
jksoft 0:8c643bfe55b7 34
jksoft 1:48f6e08a3ac2 35 void onDisconnected(uint16_t h)
jksoft 1:48f6e08a3ac2 36 {
jksoft 1:48f6e08a3ac2 37 ble.startAdvertising();
jksoft 1:48f6e08a3ac2 38 ConnectStateLed = 1;
jksoft 0:8c643bfe55b7 39 #if DBG
jksoft 1:48f6e08a3ac2 40 pc.printf("Disconnected\n\r");
jksoft 0:8c643bfe55b7 41 #endif
jksoft 1:48f6e08a3ac2 42 }
suzukimitsuhiro 3:6a9f4ea2e7c6 43 const int deg0msec = 600; // msec.
tasdevelop 4:06b0361c5975 44 const int deg180msec = 2200; // msec.
suzukimitsuhiro 3:6a9f4ea2e7c6 45
tasdevelop 4:06b0361c5975 46 //In float degree -90 to +90
tasdevelop 4:06b0361c5975 47 void SetServoDegree(float degree)
suzukimitsuhiro 3:6a9f4ea2e7c6 48 {
tasdevelop 4:06b0361c5975 49 float range = (float)(deg180msec - deg0msec) / 180;
tasdevelop 4:06b0361c5975 50 float pwitdh = (float)((degree+90)*range)+(float)deg0msec;
tasdevelop 4:06b0361c5975 51 servo1.pulsewidth(pwitdh/1000000);
suzukimitsuhiro 3:6a9f4ea2e7c6 52 }
jksoft 1:48f6e08a3ac2 53
jksoft 0:8c643bfe55b7 54 // GattEvent
jksoft 1:48f6e08a3ac2 55 void onDataWritten(uint16_t charHandle)
jksoft 0:8c643bfe55b7 56 {
jksoft 1:48f6e08a3ac2 57 if (charHandle == ControllerChar.getHandle()) {
jksoft 1:48f6e08a3ac2 58 uint16_t bytesRead;
jksoft 1:48f6e08a3ac2 59 ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead);
jksoft 1:48f6e08a3ac2 60 memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
jksoft 0:8c643bfe55b7 61 #if DBG
jksoft 1:48f6e08a3ac2 62
jksoft 1:48f6e08a3ac2 63 pc.printf("DATA:%02X %02X %d %d %d %d %d %d %d %02X\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4],
jksoft 0:8c643bfe55b7 64 controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
jksoft 0:8c643bfe55b7 65 #endif
suzukimitsuhiro 3:6a9f4ea2e7c6 66
tasdevelop 4:06b0361c5975 67 if (controller.data[0] == 1){
tasdevelop 4:06b0361c5975 68 SetServoDegree(-45);
tasdevelop 4:06b0361c5975 69 }
tasdevelop 4:06b0361c5975 70 else if (controller.data[1] == 0x40){
tasdevelop 4:06b0361c5975 71 SetServoDegree(45);
tasdevelop 4:06b0361c5975 72 }
tasdevelop 4:06b0361c5975 73 else if (controller.data[1] == 0 && controller.data[0] == 0){
tasdevelop 4:06b0361c5975 74 SetServoDegree(0);
tasdevelop 4:06b0361c5975 75 }
tasdevelop 4:06b0361c5975 76 }
jksoft 1:48f6e08a3ac2 77 }
jksoft 0:8c643bfe55b7 78
jksoft 0:8c643bfe55b7 79 /**************************************************************************/
jksoft 0:8c643bfe55b7 80 /*!
jksoft 0:8c643bfe55b7 81 @brief Program entry point
jksoft 0:8c643bfe55b7 82 */
jksoft 0:8c643bfe55b7 83 /**************************************************************************/
jksoft 0:8c643bfe55b7 84 int main(void)
jksoft 0:8c643bfe55b7 85 {
jksoft 0:8c643bfe55b7 86 #if DBG
suzukimitsuhiro 3:6a9f4ea2e7c6 87 ConnectStateLed = 1;
suzukimitsuhiro 3:6a9f4ea2e7c6 88 pc.baud(115200);
jksoft 0:8c643bfe55b7 89 pc.printf("Start\n\r");
jksoft 0:8c643bfe55b7 90 #endif
jksoft 1:48f6e08a3ac2 91
jksoft 2:dd85fdc18224 92 ble.init();
jksoft 1:48f6e08a3ac2 93 ble.onConnection(onConnected);
jksoft 1:48f6e08a3ac2 94 ble.onDisconnection(onDisconnected);
jksoft 1:48f6e08a3ac2 95 ble.onDataWritten(onDataWritten);
jksoft 1:48f6e08a3ac2 96
jksoft 1:48f6e08a3ac2 97 /* setup advertising */
jksoft 1:48f6e08a3ac2 98 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jksoft 1:48f6e08a3ac2 99 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jksoft 1:48f6e08a3ac2 100 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
jksoft 1:48f6e08a3ac2 101 (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1);
jksoft 1:48f6e08a3ac2 102 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
jksoft 1:48f6e08a3ac2 103 (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid));
jksoft 0:8c643bfe55b7 104
jksoft 1:48f6e08a3ac2 105 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
jksoft 1:48f6e08a3ac2 106 ble.startAdvertising();
jksoft 0:8c643bfe55b7 107
jksoft 1:48f6e08a3ac2 108 ble.addService(RCBControllerService);
suzukimitsuhiro 3:6a9f4ea2e7c6 109 servo1.period_ms(20);
tasdevelop 4:06b0361c5975 110 SetServoDegree(0);
jksoft 0:8c643bfe55b7 111
jksoft 1:48f6e08a3ac2 112 while (true) {
jksoft 1:48f6e08a3ac2 113 ble.waitForEvent();
jksoft 0:8c643bfe55b7 114 }
jksoft 0:8c643bfe55b7 115 }
jksoft 0:8c643bfe55b7 116