Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 #include "mbed.h" 00002 00003 #include <I2C.h> 00004 #include "BLEDevice.h" 00005 #include "FunctionGenCommands.h" 00006 00007 // prototypes 00008 void connectionCallback(Gap::Handle_t Handle_t, 00009 Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, 00010 Gap::addr_type_t ownAddrType, const Gap::address_t ownAddr, 00011 const Gap::ConnectionParams_t *params); 00012 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason); 00013 void writeCharCallback(const GattCharacteristicWriteCBParams *params); 00014 void lookme(); 00015 void tickerCallback(); 00016 00017 //void commandPrint(FGCommand *command); 00018 00019 //--------------------------------------------------------- 00020 00021 Ticker ticker; 00022 Serial pc(P0_9, P0_11); 00023 DigitalOut led1(P0_18); 00024 DigitalOut led2(P0_19); 00025 00026 DigitalOut lookmeOut(P0_21); 00027 I2C i2c(P0_22, P0_20); 00028 const int i2cSlaveAddr = 0x62; 00029 00030 //--------------------------------------------------------- 00031 00032 BLEDevice ble; 00033 00034 const static char DEVICE_NAME[] = "mbedFuncGen 0.01"; 00035 static const uint16_t uuid16_list[] = {0xFFFF}; // Custom UUID, FFFF is reserved for development 00036 00037 // Set Up custom Characteristics 00038 static uint8_t deviceValue = 0; 00039 static uint8_t outputValue = 0; 00040 static uint32_t frequencyValue = 0; 00041 static WaveformInfo waveformInfoValue; 00042 static WaveformBlock waveformBlockValue; 00043 00044 WriteOnlyArrayGattCharacteristic<uint8_t, 0> resetChar(CHARACTERISTICS_UUID_RESET, NULL); 00045 ReadWriteArrayGattCharacteristic<uint8_t, 1> deviceChar(CHARACTERISTICS_UUID_DEVICE, &deviceValue); 00046 ReadWriteArrayGattCharacteristic<uint8_t, 1> outputChar(CHARACTERISTICS_UUID_OUTPUT, &outputValue); 00047 ReadWriteArrayGattCharacteristic<uint32_t, 1> frequencyChar(CHARACTERISTICS_UUID_FREQUENCY, &frequencyValue); 00048 WriteOnlyArrayGattCharacteristic<WaveformInfo, 1> waveformInfoChar(CHARACTERISTICS_UUID_WAVEFORMINFO, &waveformInfoValue); 00049 WriteOnlyArrayGattCharacteristic<WaveformBlock, 1> waveformBlockChar(CHARACTERISTICS_UUID_WAVEFORMBLOCK, &waveformBlockValue); 00050 WriteOnlyArrayGattCharacteristic<uint8_t, 0> waveformToBufferChar(CHARACTERISTICS_UUID_WAVEFORMTOBUFFER, NULL); 00051 00052 // Set up custom service 00053 GattCharacteristic *characteristics[] = { 00054 &resetChar, 00055 &deviceChar, 00056 &outputChar, 00057 &frequencyChar, 00058 &waveformInfoChar, 00059 &waveformBlockChar, 00060 &waveformToBufferChar 00061 }; 00062 GattService customService( 0xA000, 00063 characteristics, 00064 sizeof(characteristics) / sizeof(GattCharacteristic *) ); 00065 00066 00067 //--------------------------------------------------------- 00068 /** 00069 * 00070 */ 00071 void connectionCallback(Gap::Handle_t Handle_t, 00072 Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, 00073 Gap::addr_type_t ownAddrType, const Gap::address_t ownAddr, 00074 const Gap::ConnectionParams_t *params) 00075 { 00076 pc.printf("connectionCallback\n"); 00077 } 00078 00079 //--------------------------------------------------------- 00080 /* 00081 * Restart advertising when phone app disconnects 00082 */ 00083 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00084 { 00085 pc.printf("disconnectionCallback\n"); 00086 ble.startAdvertising(); 00087 } 00088 00089 //--------------------------------------------------------- 00090 //--------------------------------------------------------- 00091 /* 00092 * handle writes to writeCharacteristic 00093 */ 00094 void writeCharCallback(const GattCharacteristicWriteCBParams *params) 00095 { 00096 led2 = !led2; 00097 00098 FGPacket fgPacket; 00099 00100 pc.printf("--------\n"); 00101 pc.printf("writeCharCallback\n"); 00102 { 00103 pc.printf("Data received: length = %d, data = ", params->len); 00104 for(int x = 0; x < params->len; x++) { 00105 pc.printf("%02x ", params->data[x]); 00106 } 00107 pc.printf("\n"); 00108 } 00109 00110 if (params->charHandle == resetChar.getValueHandle()) { 00111 pc.printf("resetChar\n"); 00112 00113 fgPacket.header.command = FGCommand_Reset; 00114 fgPacket.header.length = sizeof(FGHeader) + sizeof(FGCommandReset); 00115 //fgPacket.body.commandReset = NULL; 00116 00117 } else 00118 if (params->charHandle == deviceChar.getValueHandle()) { 00119 memcpy(&deviceValue, params->data, params->len); 00120 pc.printf("deviceChar : deviceValue: %x\n", deviceValue); 00121 00122 fgPacket.header.command = FGCommand_Device; 00123 fgPacket.header.length = sizeof(FGHeader) + sizeof(FGCommandDevice); 00124 fgPacket.body.commandDevice.device = deviceValue; 00125 00126 } else 00127 if (params->charHandle == outputChar.getValueHandle()) { 00128 memcpy(&outputValue, params->data, params->len); 00129 pc.printf("outputChar : outputValue: %x\n", outputValue); 00130 00131 fgPacket.header.command = FGCommand_Output; 00132 fgPacket.header.length = sizeof(FGHeader) + sizeof(FGCommandOutput); 00133 fgPacket.body.commandOutput.on = outputValue; 00134 00135 } else 00136 if (params->charHandle == frequencyChar.getValueHandle()) { 00137 memcpy(&frequencyValue, params->data, params->len); 00138 pc.printf("frequencyChar\n"); 00139 pc.printf("frequencyValue: %d (0x%x)\n", frequencyValue, frequencyValue); 00140 00141 fgPacket.header.command = FGCommand_Frequency; 00142 fgPacket.header.length = sizeof(FGHeader) + sizeof(FGCommandFrequency); 00143 fgPacket.body.commandFrequency.frequency = frequencyValue; 00144 00145 } else 00146 if (params->charHandle == waveformInfoChar.getValueHandle()) { 00147 memcpy(&waveformInfoValue, params->data, params->len); 00148 pc.printf("waveformInfoChar\n"); 00149 pc.printf("FGCommandWaveformInfo.waveSize: %d\n", waveformInfoValue.waveSize); 00150 pc.printf(" bitPerData: %d\n", waveformInfoValue.bitPerData); 00151 pc.printf(" blockSize: %d\n", waveformInfoValue.blockSize); 00152 pc.printf(" blockMaxNum: %d\n", waveformInfoValue.blockMaxNum); 00153 00154 fgPacket.header.command = FGCommand_WaveformInfo; 00155 fgPacket.header.length = sizeof(FGHeader) + sizeof(FGCommandWaveformInfo); 00156 fgPacket.body.commandWaveformInfo = waveformInfoValue; 00157 00158 } else 00159 if (params->charHandle == waveformBlockChar.getValueHandle()) { 00160 memcpy(&waveformBlockValue, params->data, params->len); 00161 pc.printf("waveformBlockChar\n"); 00162 pc.printf("waveformBlockChar.blockNo: %d\n", waveformBlockValue.blockNo); 00163 pc.printf(" length: %d\n", waveformBlockValue.length); 00164 pc.printf(" buffer: "); 00165 for (int i = 0; i < waveformBlockValue.length; i++) { 00166 pc.printf("%02x ", waveformBlockValue.buffer[i]); 00167 } 00168 pc.printf("\n"); 00169 00170 fgPacket.header.command = FGCommand_WaveformBlock; 00171 fgPacket.header.length = sizeof(FGHeader) + sizeof(FGCommandWaveformBlock); 00172 fgPacket.body.commandWaveformBlock = waveformBlockValue; 00173 00174 } else 00175 if (params->charHandle == waveformToBufferChar.getValueHandle()) { 00176 pc.printf("waveformToBufferChar : non\n"); 00177 00178 fgPacket.header.command = FGCommand_WaveformToBuffer; 00179 fgPacket.header.length = sizeof(FGHeader) + sizeof(FGCommand_WaveformToBuffer); 00180 //fgPacket.body.commandWaveformToBuffer = NULL; 00181 } 00182 00183 00184 // I2C送信 00185 { 00186 lookme(); 00187 int ret = i2c.write(i2cSlaveAddr, (const char *)&fgPacket, fgPacket.header.length); 00188 pc.printf("I2C ret: %d \n", ret); 00189 00190 //ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len); 00191 } 00192 } 00193 00194 //--------------------------------------------------------- 00195 // 要調整 00196 void lookme() 00197 { 00198 lookmeOut = 0; 00199 wait_ms(5); 00200 lookmeOut = 1; 00201 wait_ms(5); 00202 lookmeOut = 0; 00203 } 00204 00205 //--------------------------------------------------------- 00206 unsigned long gTickerCount = 0; 00207 00208 void tickerCallback() 00209 { 00210 led1 = !led1; 00211 gTickerCount++; 00212 } 00213 00214 //--------------------------------------------------------- 00215 /* 00216 * main loop 00217 */ 00218 int main(void) 00219 { 00220 ticker.attach(&tickerCallback, 0.5); 00221 i2c.frequency(100000); 00222 00223 pc.baud(115200); 00224 pc.printf("\n"); 00225 pc.printf("mbed function generator BLE translator\n"); 00226 pc.printf("--\n"); 00227 00228 00229 ble.init(); 00230 ble.onConnection(connectionCallback); 00231 ble.onDisconnection(disconnectionCallback); 00232 00233 ble.onDataWritten(writeCharCallback); // 書き込まれたときのコールバック 00234 //ble_error_t err = ble.onDataRead(readCharCallback); // 読み込まれたときのコールバック 00235 00236 /* 00237 void onDataSent (void(*callback)(unsigned count)) 00238 void onDataWritten (void(*callback)(const GattCharacteristicWriteCBParams *eventDataP)) 00239 ble_error_t onDataRead (void(*callback)(const GattCharacteristicReadCBParams *eventDataP)) 00240 void onRadioNotification (Gap::RadioNotificationEventCallback_t callback) 00241 00242 */ 00243 00244 // setup advertising 00245 { 00246 // BLE only, no classic BT 00247 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED | 00248 GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); 00249 // advertising type 00250 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00251 // add name 00252 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, 00253 (uint8_t *)DEVICE_NAME, 00254 sizeof(DEVICE_NAME) ); 00255 // UUID's broadcast in advertising packet 00256 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 00257 (uint8_t *)uuid16_list, 00258 sizeof(uuid16_list) ); 00259 ble.setAdvertisingInterval(100); // interval is 100ms. 00260 // add my service 00261 ble.addService(customService); 00262 00263 // start advertising 00264 ble.startAdvertising(); 00265 } 00266 00267 00268 00269 while (true) { 00270 ble.waitForEvent(); 00271 #if 0 00272 { 00273 led2 = 0; wait(0.1); 00274 led2 = 1; wait(0.2); 00275 led2 = 0; wait(0.1); 00276 led2 = 1; wait(0.2); 00277 led2 = 0; wait(0.1); 00278 led2 = 1; wait(0.2); 00279 led2 = 0; wait(0.1); 00280 led2 = 1; wait(0.2); 00281 led2 = 0; 00282 } 00283 #endif 00284 led2 = !led2; 00285 } 00286 }
Generated on Sat Jul 16 2022 03:25:36 by
1.7.2