![](/media/cache/profiles/1b8833c58d3a24b62eb45027f94d093e.jpg.50x50_q85.jpg)
Knight Rider PIO sample for teckBASIC, konashi.js
Dependencies: BLE_API_Native_IRC mbed
Fork of BLE_konashi_PIO_test by
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 00012 static const uint16_t KONASHI_SERVICE_UUID = 0xFF00; 00013 GattService knsService (KONASHI_SERVICE_UUID); 00014 00015 /* 00016 static const uint16_t KONASHI_PIO_SETTING_UUID = 0x3000; 00017 GattCharacteristic knsPioSetting (KONASHI_PIO_SETTING_UUID,1, 1, 00018 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 00019 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00020 */ 00021 static const uint16_t KONASHI_PIO_OUTPUT_UUID = 0x3002; 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 /* Advertising data and parameters */ 00027 GapAdvertisingData advData; 00028 GapAdvertisingData scanResponse; 00029 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ); 00030 00031 uint16_t uuid16_list[] = { KONASHI_SERVICE_UUID }; 00032 00033 //================================== 00034 uint8_t pioSetting=0xFF; 00035 uint8_t pioOut=0; 00036 00037 void DoPio(void) { 00038 uint8_t b=1,vs,vo; 00039 for(int i=0;i<8;i++) { 00040 vs=(pioSetting & b); 00041 vo=(pioOut & b); 00042 knsPio[i]=(vs>0 && vo>0); 00043 b=(b<<1); 00044 } 00045 } 00046 //================================== 00047 00048 // GapEvent 00049 class GapEventHandler : public GapEvents 00050 { 00051 00052 virtual void onConnected(void) 00053 { 00054 ConnectStateLed = 0; 00055 knsPio[0]=0; 00056 knsPio[1]=0; 00057 00058 #if DBG 00059 pc.printf("Connected\n\r"); 00060 #endif 00061 } 00062 00063 virtual void onDisconnected(void) 00064 { 00065 nrf.getGap().startAdvertising(advParams); 00066 ConnectStateLed = 1; 00067 knsPio[0]=1; 00068 knsPio[1]=1; 00069 #if DBG 00070 pc.printf("Disconnected\n\r"); 00071 #endif 00072 } 00073 }; 00074 00075 // GattEvent 00076 class GattServerEventHandler : public GattServerEvents 00077 { 00078 virtual void onDataWritten(uint16_t charHandle) 00079 { 00080 /*if (charHandle == knsPioSetting.handle) { 00081 uint8_t getPioSetting; 00082 nrf.getGattServer().readValue(knsPioSetting.handle, &getPioSetting, 1); 00083 if(getPioSetting!=pioSetting) { 00084 //DoPio(); 00085 pioSetting=getPioSetting; 00086 } 00087 00088 } else */ 00089 if (charHandle == knsPioOut.handle) { 00090 uint8_t getPioOut; 00091 nrf.getGattServer().readValue(knsPioOut.handle, &getPioOut, 1); 00092 #if DBG 00093 pc.printf("DATA: %d %d\n\r",getPioOut,pioOut); 00094 #endif 00095 if(getPioOut!=pioOut) { 00096 pioOut=getPioOut; 00097 DoPio(); 00098 } 00099 } 00100 } 00101 }; 00102 00103 /**************************************************************************/ 00104 /*! 00105 @brief Program entry point 00106 */ 00107 /**************************************************************************/ 00108 int main(void) 00109 { 00110 #if DBG 00111 pc.printf("Start\n\r"); 00112 #endif 00113 /* Setup an event handler for GAP events i.e. Client/Server connection events. */ 00114 nrf.getGap().setEventHandler(new GapEventHandler()); 00115 00116 /* Initialise the nRF51822 */ 00117 nrf.init(); 00118 00119 nrf.getGattServer().setEventHandler(new GattServerEventHandler()); 00120 00121 /* Make sure we get a clean start */ 00122 nrf.reset(); 00123 00124 /* Add BLE-Only flag and complete service list to the advertising data */ 00125 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00126 advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 00127 (uint8_t*)uuid16_list, sizeof(uuid16_list)); 00128 nrf.getGap().setAdvertisingData(advData, scanResponse); 00129 00130 /* Service */ 00131 //knsService.addCharacteristic(knsPioSetting); 00132 knsService.addCharacteristic(knsPioOut); 00133 nrf.getGattServer().addService(knsService); 00134 00135 /* Start advertising (make sure you've added all your data first) */ 00136 nrf.getGap().startAdvertising(advParams); 00137 ConnectStateLed = 1; 00138 00139 for (;;) 00140 { 00141 wait(1); 00142 } 00143 } 00144
Generated on Tue Jul 12 2022 16:55:06 by
![doxygen](doxygen.png)