mbed HRM11017を使ってkonashi.jsでナイトライダー

Dependencies:   BLE_API_Native_IRC mbed

Fork of BLE_RCBController by Junichi Katsu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "nRF51822n.h"
00003 
00004 #define DBG 1
00005 
00006 nRF51822n   nrf;
00007 Serial  pc(USBTX, USBRX);
00008 /* LEDs for indication: */
00009 DigitalOut knsPio[]={P0_0,P0_1,P0_2,P0_3,P0_4,P0_5,P0_6,P0_7};
00010 DigitalOut  ConnectStateLed(LED1);
00011 //PwmOut  ControllerStateLed(LED2);
00012 
00013 static const uint16_t KONASHI_SERVICE_UUID = 0xFF00;
00014 //static const uint16_t KONASHI_PIO_SETTING_UUID = 0x3000;
00015 static const uint16_t KONASHI_PIO_OUTPUT_UUID = 0x3002;
00016 
00017 GattService         knsService (KONASHI_SERVICE_UUID);
00018 /*
00019 GattCharacteristic  knsPioSetting (KONASHI_PIO_SETTING_UUID,1, 1,
00020                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
00021                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); */
00022 GattCharacteristic  knsPioOut (KONASHI_PIO_OUTPUT_UUID,1, 1,
00023                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
00024                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00025 
00026 /* RCBController Service */
00027 /*
00028 static const uint16_t RCBController_service_uuid = 0xFFF0;
00029 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
00030 GattService         RCBControllerService (RCBController_service_uuid);
00031 GattCharacteristic  Controller (RCBController_Characteristic_uuid,10, 10,
00032                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
00033                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00034 */
00035 
00036 /* Advertising data and parameters */
00037 GapAdvertisingData   advData;
00038 GapAdvertisingData   scanResponse;
00039 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
00040 
00041 uint16_t    uuid16_list[] = { KONASHI_SERVICE_UUID };
00042 //RCBController controller;
00043 uint8_t pioSetting=0xFF;
00044 uint8_t pioOut=0;
00045 
00046 void DoPio(void) {
00047     uint8_t b=1,vs,vo;
00048     for(int i=0;i<8;i++) {
00049         vs=(pioSetting & b);
00050         vo=(pioOut & b);
00051         knsPio[i]=(vs>0 && vo>0);
00052         b=(b<<1);
00053     }
00054 }
00055 
00056 // GapEvent
00057 class GapEventHandler : public GapEvents
00058 {
00059      
00060     virtual void onConnected(void)
00061     {
00062         ConnectStateLed = 0;
00063         knsPio[0]=0;
00064         knsPio[1]=0;
00065 
00066 #if DBG
00067         pc.printf("Connected\n\r");
00068 #endif
00069     }
00070 
00071     virtual void onDisconnected(void)
00072     {
00073         nrf.getGap().startAdvertising(advParams);
00074         ConnectStateLed = 1;
00075         knsPio[0]=1;
00076         knsPio[1]=1;
00077 #if DBG
00078         pc.printf("Disconnected\n\r");
00079 #endif
00080     }
00081 };
00082 
00083 // GattEvent
00084 class GattServerEventHandler : public GattServerEvents
00085 {
00086     virtual void onDataWritten(uint16_t charHandle)
00087     {
00088         /*if (charHandle == knsPioSetting.handle) {
00089             uint8_t getPioSetting;
00090             nrf.getGattServer().readValue(knsPioSetting.handle, &getPioSetting, 1);
00091             if(getPioSetting!=pioSetting) {
00092                 //DoPio();
00093                 pioSetting=getPioSetting;
00094             }
00095             
00096         } else */ if (charHandle == knsPioOut.handle) {
00097             uint8_t getPioOut;
00098             nrf.getGattServer().readValue(knsPioOut.handle, &getPioOut, 1);
00099             #if DBG
00100                 pc.printf("DATA: %d %d\n\r",getPioOut,pioOut);
00101             #endif
00102             if(getPioOut!=pioOut) {
00103                 pioOut=getPioOut;
00104                 DoPio();
00105             }
00106         }
00107 /*
00108         if (charHandle == Controller.handle) {
00109             nrf.getGattServer().readValue(Controller.handle, &controller.data[0], sizeof(controller));
00110 #if DBG
00111             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],
00112                                                                controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
00113 #endif
00114             ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;;
00115             
00116         }
00117 */       
00118     }
00119 };
00120 
00121 /**************************************************************************/
00122 /*!
00123     @brief  Program entry point
00124 */
00125 /**************************************************************************/
00126 int main(void)
00127 {
00128 #if DBG
00129         pc.printf("Start\n\r");
00130 #endif
00131     /* Setup an event handler for GAP events i.e. Client/Server connection events. */
00132     nrf.getGap().setEventHandler(new GapEventHandler());
00133     
00134     /* Initialise the nRF51822 */
00135     nrf.init();
00136     
00137     nrf.getGattServer().setEventHandler(new GattServerEventHandler());
00138 
00139     /* Make sure we get a clean start */
00140     nrf.reset();    
00141 
00142     /* Add BLE-Only flag and complete service list to the advertising data */
00143     advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00144     advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 
00145                    (uint8_t*)uuid16_list, sizeof(uuid16_list));
00146     nrf.getGap().setAdvertisingData(advData, scanResponse);
00147     
00148     /* Service */
00149     //knsService.addCharacteristic(knsPioSetting);
00150     knsService.addCharacteristic(knsPioOut);
00151     nrf.getGattServer().addService(knsService);
00152 
00153     /* Start advertising (make sure you've added all your data first) */
00154     nrf.getGap().startAdvertising(advParams);
00155     ConnectStateLed = 1;
00156         knsPio[0]=1;
00157         knsPio[1]=1;
00158     //ControllerStateLed = 1;
00159 
00160     for (;;)
00161     {
00162         wait(1);
00163     }
00164 }
00165