Release for tas lab

Dependencies:   BLE_API mbed

Fork of BLE_RCBController by Junichi Katsu

Committer:
suzukimitsuhiro
Date:
Thu Mar 19 03:59:36 2015 +0000
Revision:
3:6a9f4ea2e7c6
Parent:
2:dd85fdc18224
Child:
4:06b0361c5975
release 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.
suzukimitsuhiro 3:6a9f4ea2e7c6 44 const int deg180msec = 2350; // msec.
suzukimitsuhiro 3:6a9f4ea2e7c6 45
suzukimitsuhiro 3:6a9f4ea2e7c6 46 void SwOn(void)
suzukimitsuhiro 3:6a9f4ea2e7c6 47 {
suzukimitsuhiro 3:6a9f4ea2e7c6 48 float range = (float)(deg180msec - deg0msec)/1000000; //0.00175
suzukimitsuhiro 3:6a9f4ea2e7c6 49 float pwitdh;
suzukimitsuhiro 3:6a9f4ea2e7c6 50 for(int i=128;i<210;i++){
suzukimitsuhiro 3:6a9f4ea2e7c6 51 pwitdh = (float)(i*range)/256+(float)deg0msec/1000000;
suzukimitsuhiro 3:6a9f4ea2e7c6 52 servo1.pulsewidth(pwitdh);
suzukimitsuhiro 3:6a9f4ea2e7c6 53 wait(0.001);
suzukimitsuhiro 3:6a9f4ea2e7c6 54 }
suzukimitsuhiro 3:6a9f4ea2e7c6 55 wait(0.5);
suzukimitsuhiro 3:6a9f4ea2e7c6 56 for(int i=210;i>128;i--){
suzukimitsuhiro 3:6a9f4ea2e7c6 57 pwitdh = (float)(i*range)/256+ (float)deg0msec/1000000;
suzukimitsuhiro 3:6a9f4ea2e7c6 58 servo1.pulsewidth(pwitdh);
suzukimitsuhiro 3:6a9f4ea2e7c6 59 wait(0.001);
suzukimitsuhiro 3:6a9f4ea2e7c6 60 }
suzukimitsuhiro 3:6a9f4ea2e7c6 61 servo1.pulsewidth( 0.00140f );
suzukimitsuhiro 3:6a9f4ea2e7c6 62
suzukimitsuhiro 3:6a9f4ea2e7c6 63 }
suzukimitsuhiro 3:6a9f4ea2e7c6 64 void SwOff(void)
suzukimitsuhiro 3:6a9f4ea2e7c6 65 {
suzukimitsuhiro 3:6a9f4ea2e7c6 66 float range = (float)(deg180msec - deg0msec)/1000000; //0.00175
suzukimitsuhiro 3:6a9f4ea2e7c6 67 float pwitdh;
suzukimitsuhiro 3:6a9f4ea2e7c6 68 for(int i=128;i>40;i--){
suzukimitsuhiro 3:6a9f4ea2e7c6 69 pwitdh = (float)(i*range)/256+(float)deg0msec/1000000;
suzukimitsuhiro 3:6a9f4ea2e7c6 70 servo1.pulsewidth(pwitdh);
suzukimitsuhiro 3:6a9f4ea2e7c6 71 wait(0.001);
suzukimitsuhiro 3:6a9f4ea2e7c6 72 }
suzukimitsuhiro 3:6a9f4ea2e7c6 73 wait(0.5);
suzukimitsuhiro 3:6a9f4ea2e7c6 74 for(int i=40;i<128;i++){
suzukimitsuhiro 3:6a9f4ea2e7c6 75 pwitdh = (float)(i*range)/256+ (float)deg0msec/1000000;
suzukimitsuhiro 3:6a9f4ea2e7c6 76 servo1.pulsewidth(pwitdh);
suzukimitsuhiro 3:6a9f4ea2e7c6 77 wait(0.001);
suzukimitsuhiro 3:6a9f4ea2e7c6 78 }
suzukimitsuhiro 3:6a9f4ea2e7c6 79 servo1.pulsewidth( 0.00140f );
suzukimitsuhiro 3:6a9f4ea2e7c6 80 }
jksoft 1:48f6e08a3ac2 81
jksoft 0:8c643bfe55b7 82
jksoft 0:8c643bfe55b7 83 // GattEvent
jksoft 1:48f6e08a3ac2 84 void onDataWritten(uint16_t charHandle)
jksoft 0:8c643bfe55b7 85 {
jksoft 1:48f6e08a3ac2 86 if (charHandle == ControllerChar.getHandle()) {
jksoft 1:48f6e08a3ac2 87 uint16_t bytesRead;
jksoft 1:48f6e08a3ac2 88 ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead);
jksoft 1:48f6e08a3ac2 89 memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
jksoft 0:8c643bfe55b7 90 #if DBG
jksoft 1:48f6e08a3ac2 91
jksoft 1:48f6e08a3ac2 92 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 93 controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
jksoft 0:8c643bfe55b7 94 #endif
suzukimitsuhiro 3:6a9f4ea2e7c6 95
suzukimitsuhiro 3:6a9f4ea2e7c6 96 if (controller.data[0] ==1){
suzukimitsuhiro 3:6a9f4ea2e7c6 97 SwOn();
suzukimitsuhiro 3:6a9f4ea2e7c6 98 }
suzukimitsuhiro 3:6a9f4ea2e7c6 99 else if (controller.data[1] == 0x40){
suzukimitsuhiro 3:6a9f4ea2e7c6 100 SwOff();
suzukimitsuhiro 3:6a9f4ea2e7c6 101 }
suzukimitsuhiro 3:6a9f4ea2e7c6 102 /*
suzukimitsuhiro 3:6a9f4ea2e7c6 103 // servo1 = (float)controller.status.LeftAnalogLR / 255.0;
suzukimitsuhiro 3:6a9f4ea2e7c6 104 float pwitdh = (float)controller.status.LeftAnalogLR / 255.0;
suzukimitsuhiro 3:6a9f4ea2e7c6 105 float range = (float)(deg180msec - deg0msec)/1000000; //0.00175
suzukimitsuhiro 3:6a9f4ea2e7c6 106
suzukimitsuhiro 3:6a9f4ea2e7c6 107 pwitdh = pwitdh *range+(float)deg0msec/1000000;
suzukimitsuhiro 3:6a9f4ea2e7c6 108 pc.printf("pwitdh %f\n\r",pwitdh);
suzukimitsuhiro 3:6a9f4ea2e7c6 109 servo1.pulsewidth(pwitdh);
jksoft 1:48f6e08a3ac2 110 }
suzukimitsuhiro 3:6a9f4ea2e7c6 111 */
suzukimitsuhiro 3:6a9f4ea2e7c6 112 }
suzukimitsuhiro 3:6a9f4ea2e7c6 113
jksoft 1:48f6e08a3ac2 114 }
jksoft 0:8c643bfe55b7 115
jksoft 0:8c643bfe55b7 116 /**************************************************************************/
jksoft 0:8c643bfe55b7 117 /*!
jksoft 0:8c643bfe55b7 118 @brief Program entry point
jksoft 0:8c643bfe55b7 119 */
jksoft 0:8c643bfe55b7 120 /**************************************************************************/
jksoft 0:8c643bfe55b7 121 int main(void)
jksoft 0:8c643bfe55b7 122 {
jksoft 0:8c643bfe55b7 123 #if DBG
suzukimitsuhiro 3:6a9f4ea2e7c6 124 ConnectStateLed = 1;
suzukimitsuhiro 3:6a9f4ea2e7c6 125 pc.baud(115200);
jksoft 0:8c643bfe55b7 126 pc.printf("Start\n\r");
jksoft 0:8c643bfe55b7 127 #endif
jksoft 1:48f6e08a3ac2 128
jksoft 2:dd85fdc18224 129 ble.init();
jksoft 1:48f6e08a3ac2 130 ble.onConnection(onConnected);
jksoft 1:48f6e08a3ac2 131 ble.onDisconnection(onDisconnected);
jksoft 1:48f6e08a3ac2 132 ble.onDataWritten(onDataWritten);
jksoft 1:48f6e08a3ac2 133
jksoft 1:48f6e08a3ac2 134 /* setup advertising */
jksoft 1:48f6e08a3ac2 135 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jksoft 1:48f6e08a3ac2 136 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jksoft 1:48f6e08a3ac2 137 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
jksoft 1:48f6e08a3ac2 138 (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1);
jksoft 1:48f6e08a3ac2 139 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
jksoft 1:48f6e08a3ac2 140 (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid));
jksoft 0:8c643bfe55b7 141
jksoft 1:48f6e08a3ac2 142 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
jksoft 1:48f6e08a3ac2 143 ble.startAdvertising();
jksoft 0:8c643bfe55b7 144
jksoft 1:48f6e08a3ac2 145 ble.addService(RCBControllerService);
suzukimitsuhiro 3:6a9f4ea2e7c6 146 servo1.period_ms(20);
suzukimitsuhiro 3:6a9f4ea2e7c6 147 servo1.pulsewidth( 0.00140f );
jksoft 0:8c643bfe55b7 148
jksoft 1:48f6e08a3ac2 149 while (true) {
jksoft 1:48f6e08a3ac2 150 ble.waitForEvent();
jksoft 0:8c643bfe55b7 151 }
jksoft 0:8c643bfe55b7 152 }
jksoft 0:8c643bfe55b7 153