【これは旧バージョンです】 AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
Fork of BLE_RCBController2 by
BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
main.cpp
- Committer:
- jksoft
- Date:
- 2014-08-20
- Revision:
- 1:48f6e08a3ac2
- Parent:
- 0:8c643bfe55b7
- Child:
- 2:dd85fdc18224
File content as of revision 1:48f6e08a3ac2:
#include "mbed.h" #include "BLEDevice.h" #include "RCBController.h" #define DBG 1 BLEDevice ble; Serial pc(USBTX, USBRX); /* LEDs for indication: */ DigitalOut ConnectStateLed(LED1); PwmOut ControllerStateLed(LED2); /* RCBController Service */ static const uint16_t RCBController_service_uuid = 0xFFF0; static const uint16_t RCBController_Characteristic_uuid = 0xFFF1; uint8_t RCBControllerPayload[10] = {0,}; GattCharacteristic ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); GattCharacteristic *ControllerChars[] = {&ControllerChar}; GattService RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *)); RCBController controller; void onConnected(uint16_t h) { ConnectStateLed = 0; #if DBG pc.printf("Connected\n\r"); #endif } void onDisconnected(uint16_t h) { ble.startAdvertising(); ConnectStateLed = 1; #if DBG pc.printf("Disconnected\n\r"); #endif } // GattEvent void onDataWritten(uint16_t charHandle) { if (charHandle == ControllerChar.getHandle()) { uint16_t bytesRead; ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead); memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller)); #if DBG 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], controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]); #endif ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0; } } /**************************************************************************/ /*! @brief Program entry point */ /**************************************************************************/ int main(void) { #if DBG pc.printf("Start\n\r"); #endif ble.init(); ble.onConnection(onConnected); ble.onDisconnection(onDisconnected); ble.onDataWritten(onDataWritten); /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid)); ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ ble.startAdvertising(); ble.addService(RCBControllerService); while (true) { ble.waitForEvent(); } }