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 #include "AD9850.h" 00003 #include "BLEDevice.h" 00004 00005 #define DEBUG 1 00006 00007 //----------------------------------------------------------------------- 00008 //----------------------------------------------------------------------- 00009 Ticker ticker; 00010 Serial pc(P0_9, P0_11); 00011 DigitalOut led1(P0_18); 00012 DigitalOut led2(P0_19); 00013 DigitalIn btn1(P0_16); 00014 DigitalIn btn2(P0_17); 00015 00016 AD9850 dds(P0_20, P0_25, P0_24, P0_23); 00017 00018 //----------------------------------------------------------------------- 00019 00020 uint32_t targetFreq = 10 * 1000 * 1000; // 10MHz - default 00021 uint32_t incrementFreq = 100 * 1000; // This is a 100KHz frequency increment 00022 00023 //----------------------------------------------------------------------- 00024 00025 BLEDevice ble; 00026 00027 const static char DEVICE_NAME[] = "DDS-CTRL 0.2"; 00028 static const uint16_t uuid16_list[] = {0xFFFF}; // Custom UUID, FFFF is reserved for development 00029 00030 // Set Up custom Characteristics 00031 static uint8_t deviceValue = 0; 00032 static uint8_t outputValue = 0; 00033 static uint32_t frequencyValue = 0; 00034 00035 ReadWriteArrayGattCharacteristic<uint8_t, 1> deviceChar(0xA201, &deviceValue); 00036 ReadWriteArrayGattCharacteristic<uint8_t, 1> outputChar(0xA202, &outputValue); 00037 ReadWriteArrayGattCharacteristic<uint32_t, 1> frequencyChar(0xA203, &frequencyValue); 00038 00039 // Set up custom service 00040 GattCharacteristic *characteristics[] = { 00041 &deviceChar, 00042 &outputChar, 00043 &frequencyChar, 00044 }; 00045 GattService customService(0xA001, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00046 00047 00048 //----------------------------------------------------------------------- 00049 /** 00050 * 00051 */ 00052 void connectionCallback(Gap::Handle_t Handle_t, 00053 Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, 00054 Gap::addr_type_t ownAddrType, const Gap::address_t ownAddr, 00055 const Gap::ConnectionParams_t *params) 00056 { 00057 pc.printf("connectionCallback\n"); 00058 } 00059 00060 //-------------------------------- 00061 /* 00062 * Restart advertising when phone app disconnects 00063 */ 00064 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00065 { 00066 pc.printf("disconnectionCallback\n"); 00067 ble.startAdvertising(); 00068 } 00069 00070 //-------------------------------- 00071 /* 00072 * handle writes to writeCharacteristic 00073 */ 00074 void writeCharCallback(const GattCharacteristicWriteCBParams *params) 00075 { 00076 led2 = !led2; 00077 00078 pc.printf("BLE:onDataWritten\n"); 00079 00080 if (params->charHandle == deviceChar.getValueHandle()) { 00081 memcpy(&deviceChar, params->data, params->len); 00082 pc.printf("deviceChar\n"); 00083 pc.printf("deviceChar: deviceValue: %x\n", deviceValue); 00084 00085 } else 00086 if (params->charHandle == outputChar.getValueHandle()) { 00087 memcpy(&outputValue, params->data, params->len); 00088 pc.printf("outputChar : outputValue: %x\n", outputValue); 00089 00090 } else 00091 if (params->charHandle == frequencyChar.getValueHandle()) { 00092 memcpy(&targetFreq, params->data, params->len); 00093 pc.printf("frequencyChar\n"); 00094 pc.printf("targetFreq: %d\n", targetFreq); 00095 } 00096 00097 } 00098 00099 //----------------------------------------------------------------------- 00100 00101 void tickerCallback() 00102 { 00103 led1 = !led1; 00104 } 00105 00106 char guruguru(int cnt) 00107 { 00108 static char gurus[] = {'-', '\\', '|', '/'}; 00109 return gurus[cnt % 4]; 00110 } 00111 00112 //-------------------------------- 00113 00114 int main(void) 00115 { 00116 ticker.attach(&tickerCallback, 0.5); 00117 00118 pc.baud(115200); 00119 pc.printf("\n"); 00120 pc.printf("------------------------\n"); 00121 pc.printf("DDS-CTRL start\n"); 00122 led1 = 0; led2 = 0;; 00123 00124 00125 ble.init(); 00126 ble.onConnection(connectionCallback); 00127 ble.onDisconnection(disconnectionCallback); 00128 ble.onDataWritten(writeCharCallback); 00129 00130 // setup advertising 00131 { 00132 // BLE only, no classic BT 00133 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED | 00134 GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); 00135 // advertising type 00136 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00137 // add name 00138 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, 00139 (uint8_t *)DEVICE_NAME, 00140 sizeof(DEVICE_NAME) ); 00141 // UUID's broadcast in advertising packet 00142 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 00143 (uint8_t *)uuid16_list, 00144 sizeof(uuid16_list) ); 00145 ble.setAdvertisingInterval(100); // interval is 100ms. 00146 // add my service 00147 ble.addService(customService); 00148 00149 // start advertising 00150 ble.startAdvertising(); 00151 } 00152 pc.printf("BLE init\n"); 00153 00154 00155 // DDS 初期化 00156 { 00157 dds.init(); 00158 pc.printf("\nFREQ: %d [default]\n", targetFreq); 00159 dds.setFrequency(targetFreq, 0, 0); 00160 dds.setFrequency(targetFreq, 0, 0); 00161 // TODO: なぜか最初だけ2回ださないと設定されない... 00162 } 00163 pc.printf("DDS init\n"); 00164 00165 00166 int loopCounter = 0; 00167 int lastFreq = targetFreq; 00168 while (true) { 00169 ble.waitForEvent(); 00170 00171 loopCounter++; 00172 //pc.putc(guruguru(loopCounter)); 00173 //pc.putc('\r'); 00174 pc.putc('*'); 00175 wait_ms(100); 00176 00177 if (lastFreq != targetFreq) { 00178 pc.printf("\nFREQ: %d\n", targetFreq); 00179 dds.setFrequency(targetFreq, 0, 0); 00180 00181 //gInternalStorageBuffer.freq = targetFreq; 00182 //nRF51822_internalStorage_write(); 00183 00184 lastFreq = targetFreq; 00185 } 00186 } 00187 00188 } 00189 00190 //--------------------------------------------------------- 00191
Generated on Tue Jul 12 2022 21:12:59 by
1.7.2