mbed HRM11017を使ってkonashi.jsでナイトライダー
Dependencies: BLE_API_Native_IRC mbed
Fork of BLE_RCBController by
konashi.js mbed HRM1017でもナイトライダー! http://jsdo.it/micutil/g1Hn
サンプル動画 https://www.youtube.com/watch?v=HSLdzS3sGLw
main.cpp@1:6b1c4adfe165, 2014-07-13 (annotated)
- Committer:
- micono
- Date:
- Sun Jul 13 01:36:46 2014 +0000
- Revision:
- 1:6b1c4adfe165
- Parent:
- 0:8c643bfe55b7
mbed HRM11017????konashi.js????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:8c643bfe55b7 | 1 | #include "mbed.h" |
jksoft | 0:8c643bfe55b7 | 2 | #include "nRF51822n.h" |
jksoft | 0:8c643bfe55b7 | 3 | |
micono | 1:6b1c4adfe165 | 4 | #define DBG 1 |
jksoft | 0:8c643bfe55b7 | 5 | |
jksoft | 0:8c643bfe55b7 | 6 | nRF51822n nrf; |
jksoft | 0:8c643bfe55b7 | 7 | Serial pc(USBTX, USBRX); |
jksoft | 0:8c643bfe55b7 | 8 | /* LEDs for indication: */ |
micono | 1:6b1c4adfe165 | 9 | DigitalOut knsPio[]={P0_0,P0_1,P0_2,P0_3,P0_4,P0_5,P0_6,P0_7}; |
jksoft | 0:8c643bfe55b7 | 10 | DigitalOut ConnectStateLed(LED1); |
micono | 1:6b1c4adfe165 | 11 | //PwmOut ControllerStateLed(LED2); |
micono | 1:6b1c4adfe165 | 12 | |
micono | 1:6b1c4adfe165 | 13 | static const uint16_t KONASHI_SERVICE_UUID = 0xFF00; |
micono | 1:6b1c4adfe165 | 14 | //static const uint16_t KONASHI_PIO_SETTING_UUID = 0x3000; |
micono | 1:6b1c4adfe165 | 15 | static const uint16_t KONASHI_PIO_OUTPUT_UUID = 0x3002; |
jksoft | 0:8c643bfe55b7 | 16 | |
micono | 1:6b1c4adfe165 | 17 | GattService knsService (KONASHI_SERVICE_UUID); |
micono | 1:6b1c4adfe165 | 18 | /* |
micono | 1:6b1c4adfe165 | 19 | GattCharacteristic knsPioSetting (KONASHI_PIO_SETTING_UUID,1, 1, |
micono | 1:6b1c4adfe165 | 20 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
micono | 1:6b1c4adfe165 | 21 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); */ |
micono | 1:6b1c4adfe165 | 22 | GattCharacteristic knsPioOut (KONASHI_PIO_OUTPUT_UUID,1, 1, |
micono | 1:6b1c4adfe165 | 23 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
micono | 1:6b1c4adfe165 | 24 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
jksoft | 0:8c643bfe55b7 | 25 | |
jksoft | 0:8c643bfe55b7 | 26 | /* RCBController Service */ |
micono | 1:6b1c4adfe165 | 27 | /* |
jksoft | 0:8c643bfe55b7 | 28 | static const uint16_t RCBController_service_uuid = 0xFFF0; |
jksoft | 0:8c643bfe55b7 | 29 | static const uint16_t RCBController_Characteristic_uuid = 0xFFF1; |
jksoft | 0:8c643bfe55b7 | 30 | GattService RCBControllerService (RCBController_service_uuid); |
jksoft | 0:8c643bfe55b7 | 31 | GattCharacteristic Controller (RCBController_Characteristic_uuid,10, 10, |
jksoft | 0:8c643bfe55b7 | 32 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
jksoft | 0:8c643bfe55b7 | 33 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
micono | 1:6b1c4adfe165 | 34 | */ |
jksoft | 0:8c643bfe55b7 | 35 | |
jksoft | 0:8c643bfe55b7 | 36 | /* Advertising data and parameters */ |
jksoft | 0:8c643bfe55b7 | 37 | GapAdvertisingData advData; |
jksoft | 0:8c643bfe55b7 | 38 | GapAdvertisingData scanResponse; |
jksoft | 0:8c643bfe55b7 | 39 | GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ); |
jksoft | 0:8c643bfe55b7 | 40 | |
micono | 1:6b1c4adfe165 | 41 | uint16_t uuid16_list[] = { KONASHI_SERVICE_UUID }; |
micono | 1:6b1c4adfe165 | 42 | //RCBController controller; |
micono | 1:6b1c4adfe165 | 43 | uint8_t pioSetting=0xFF; |
micono | 1:6b1c4adfe165 | 44 | uint8_t pioOut=0; |
jksoft | 0:8c643bfe55b7 | 45 | |
micono | 1:6b1c4adfe165 | 46 | void DoPio(void) { |
micono | 1:6b1c4adfe165 | 47 | uint8_t b=1,vs,vo; |
micono | 1:6b1c4adfe165 | 48 | for(int i=0;i<8;i++) { |
micono | 1:6b1c4adfe165 | 49 | vs=(pioSetting & b); |
micono | 1:6b1c4adfe165 | 50 | vo=(pioOut & b); |
micono | 1:6b1c4adfe165 | 51 | knsPio[i]=(vs>0 && vo>0); |
micono | 1:6b1c4adfe165 | 52 | b=(b<<1); |
micono | 1:6b1c4adfe165 | 53 | } |
micono | 1:6b1c4adfe165 | 54 | } |
jksoft | 0:8c643bfe55b7 | 55 | |
jksoft | 0:8c643bfe55b7 | 56 | // GapEvent |
jksoft | 0:8c643bfe55b7 | 57 | class GapEventHandler : public GapEvents |
jksoft | 0:8c643bfe55b7 | 58 | { |
jksoft | 0:8c643bfe55b7 | 59 | |
jksoft | 0:8c643bfe55b7 | 60 | virtual void onConnected(void) |
jksoft | 0:8c643bfe55b7 | 61 | { |
jksoft | 0:8c643bfe55b7 | 62 | ConnectStateLed = 0; |
micono | 1:6b1c4adfe165 | 63 | knsPio[0]=0; |
micono | 1:6b1c4adfe165 | 64 | knsPio[1]=0; |
micono | 1:6b1c4adfe165 | 65 | |
jksoft | 0:8c643bfe55b7 | 66 | #if DBG |
jksoft | 0:8c643bfe55b7 | 67 | pc.printf("Connected\n\r"); |
jksoft | 0:8c643bfe55b7 | 68 | #endif |
jksoft | 0:8c643bfe55b7 | 69 | } |
jksoft | 0:8c643bfe55b7 | 70 | |
jksoft | 0:8c643bfe55b7 | 71 | virtual void onDisconnected(void) |
jksoft | 0:8c643bfe55b7 | 72 | { |
jksoft | 0:8c643bfe55b7 | 73 | nrf.getGap().startAdvertising(advParams); |
jksoft | 0:8c643bfe55b7 | 74 | ConnectStateLed = 1; |
micono | 1:6b1c4adfe165 | 75 | knsPio[0]=1; |
micono | 1:6b1c4adfe165 | 76 | knsPio[1]=1; |
jksoft | 0:8c643bfe55b7 | 77 | #if DBG |
jksoft | 0:8c643bfe55b7 | 78 | pc.printf("Disconnected\n\r"); |
jksoft | 0:8c643bfe55b7 | 79 | #endif |
jksoft | 0:8c643bfe55b7 | 80 | } |
jksoft | 0:8c643bfe55b7 | 81 | }; |
jksoft | 0:8c643bfe55b7 | 82 | |
jksoft | 0:8c643bfe55b7 | 83 | // GattEvent |
jksoft | 0:8c643bfe55b7 | 84 | class GattServerEventHandler : public GattServerEvents |
jksoft | 0:8c643bfe55b7 | 85 | { |
jksoft | 0:8c643bfe55b7 | 86 | virtual void onDataWritten(uint16_t charHandle) |
jksoft | 0:8c643bfe55b7 | 87 | { |
micono | 1:6b1c4adfe165 | 88 | /*if (charHandle == knsPioSetting.handle) { |
micono | 1:6b1c4adfe165 | 89 | uint8_t getPioSetting; |
micono | 1:6b1c4adfe165 | 90 | nrf.getGattServer().readValue(knsPioSetting.handle, &getPioSetting, 1); |
micono | 1:6b1c4adfe165 | 91 | if(getPioSetting!=pioSetting) { |
micono | 1:6b1c4adfe165 | 92 | //DoPio(); |
micono | 1:6b1c4adfe165 | 93 | pioSetting=getPioSetting; |
micono | 1:6b1c4adfe165 | 94 | } |
micono | 1:6b1c4adfe165 | 95 | |
micono | 1:6b1c4adfe165 | 96 | } else */ if (charHandle == knsPioOut.handle) { |
micono | 1:6b1c4adfe165 | 97 | uint8_t getPioOut; |
micono | 1:6b1c4adfe165 | 98 | nrf.getGattServer().readValue(knsPioOut.handle, &getPioOut, 1); |
micono | 1:6b1c4adfe165 | 99 | #if DBG |
micono | 1:6b1c4adfe165 | 100 | pc.printf("DATA: %d %d\n\r",getPioOut,pioOut); |
micono | 1:6b1c4adfe165 | 101 | #endif |
micono | 1:6b1c4adfe165 | 102 | if(getPioOut!=pioOut) { |
micono | 1:6b1c4adfe165 | 103 | pioOut=getPioOut; |
micono | 1:6b1c4adfe165 | 104 | DoPio(); |
micono | 1:6b1c4adfe165 | 105 | } |
micono | 1:6b1c4adfe165 | 106 | } |
micono | 1:6b1c4adfe165 | 107 | /* |
jksoft | 0:8c643bfe55b7 | 108 | if (charHandle == Controller.handle) { |
jksoft | 0:8c643bfe55b7 | 109 | nrf.getGattServer().readValue(Controller.handle, &controller.data[0], sizeof(controller)); |
jksoft | 0:8c643bfe55b7 | 110 | #if DBG |
jksoft | 0:8c643bfe55b7 | 111 | 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 | 112 | controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]); |
jksoft | 0:8c643bfe55b7 | 113 | #endif |
jksoft | 0:8c643bfe55b7 | 114 | ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;; |
jksoft | 0:8c643bfe55b7 | 115 | |
jksoft | 0:8c643bfe55b7 | 116 | } |
micono | 1:6b1c4adfe165 | 117 | */ |
jksoft | 0:8c643bfe55b7 | 118 | } |
jksoft | 0:8c643bfe55b7 | 119 | }; |
jksoft | 0:8c643bfe55b7 | 120 | |
jksoft | 0:8c643bfe55b7 | 121 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 122 | /*! |
jksoft | 0:8c643bfe55b7 | 123 | @brief Program entry point |
jksoft | 0:8c643bfe55b7 | 124 | */ |
jksoft | 0:8c643bfe55b7 | 125 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 126 | int main(void) |
jksoft | 0:8c643bfe55b7 | 127 | { |
jksoft | 0:8c643bfe55b7 | 128 | #if DBG |
jksoft | 0:8c643bfe55b7 | 129 | pc.printf("Start\n\r"); |
jksoft | 0:8c643bfe55b7 | 130 | #endif |
jksoft | 0:8c643bfe55b7 | 131 | /* Setup an event handler for GAP events i.e. Client/Server connection events. */ |
jksoft | 0:8c643bfe55b7 | 132 | nrf.getGap().setEventHandler(new GapEventHandler()); |
jksoft | 0:8c643bfe55b7 | 133 | |
jksoft | 0:8c643bfe55b7 | 134 | /* Initialise the nRF51822 */ |
jksoft | 0:8c643bfe55b7 | 135 | nrf.init(); |
jksoft | 0:8c643bfe55b7 | 136 | |
jksoft | 0:8c643bfe55b7 | 137 | nrf.getGattServer().setEventHandler(new GattServerEventHandler()); |
jksoft | 0:8c643bfe55b7 | 138 | |
jksoft | 0:8c643bfe55b7 | 139 | /* Make sure we get a clean start */ |
jksoft | 0:8c643bfe55b7 | 140 | nrf.reset(); |
jksoft | 0:8c643bfe55b7 | 141 | |
jksoft | 0:8c643bfe55b7 | 142 | /* Add BLE-Only flag and complete service list to the advertising data */ |
jksoft | 0:8c643bfe55b7 | 143 | advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jksoft | 0:8c643bfe55b7 | 144 | advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
jksoft | 0:8c643bfe55b7 | 145 | (uint8_t*)uuid16_list, sizeof(uuid16_list)); |
jksoft | 0:8c643bfe55b7 | 146 | nrf.getGap().setAdvertisingData(advData, scanResponse); |
jksoft | 0:8c643bfe55b7 | 147 | |
micono | 1:6b1c4adfe165 | 148 | /* Service */ |
micono | 1:6b1c4adfe165 | 149 | //knsService.addCharacteristic(knsPioSetting); |
micono | 1:6b1c4adfe165 | 150 | knsService.addCharacteristic(knsPioOut); |
micono | 1:6b1c4adfe165 | 151 | nrf.getGattServer().addService(knsService); |
jksoft | 0:8c643bfe55b7 | 152 | |
jksoft | 0:8c643bfe55b7 | 153 | /* Start advertising (make sure you've added all your data first) */ |
jksoft | 0:8c643bfe55b7 | 154 | nrf.getGap().startAdvertising(advParams); |
jksoft | 0:8c643bfe55b7 | 155 | ConnectStateLed = 1; |
micono | 1:6b1c4adfe165 | 156 | knsPio[0]=1; |
micono | 1:6b1c4adfe165 | 157 | knsPio[1]=1; |
micono | 1:6b1c4adfe165 | 158 | //ControllerStateLed = 1; |
jksoft | 0:8c643bfe55b7 | 159 | |
jksoft | 0:8c643bfe55b7 | 160 | for (;;) |
jksoft | 0:8c643bfe55b7 | 161 | { |
jksoft | 0:8c643bfe55b7 | 162 | wait(1); |
jksoft | 0:8c643bfe55b7 | 163 | } |
jksoft | 0:8c643bfe55b7 | 164 | } |
jksoft | 0:8c643bfe55b7 | 165 |