mbed HRM1017によるAD9850の操作

Dependencies:   BLE_API mbed nRF51822

what's this ? / 概要

あとで、ちゃんと書く(とおもう)

detail / 詳細

あとで、ちゃんと書く(とおもう)

BEL command format / BLEコマンドフォーマット

あとで、ちゃんと書く(とおもう)

BLE App for iPhone

iOS用アプリソースコード App source codes for iOS

https://github.com/ohneta/AD9850BLE

pictures / とりあえず写真など

外観

/media/uploads/ohneta/img_5653.jpg

アプリ画面

/media/uploads/ohneta/img_0101.png

more ...

Committer:
ohneta
Date:
Thu Jun 18 03:58:23 2015 +0000
Revision:
0:cc32f8c167de
1st commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ohneta 0:cc32f8c167de 1 #include "mbed.h"
ohneta 0:cc32f8c167de 2 #include "AD9850.h"
ohneta 0:cc32f8c167de 3 #include "BLEDevice.h"
ohneta 0:cc32f8c167de 4
ohneta 0:cc32f8c167de 5 #define DEBUG 1
ohneta 0:cc32f8c167de 6
ohneta 0:cc32f8c167de 7 //-----------------------------------------------------------------------
ohneta 0:cc32f8c167de 8 //-----------------------------------------------------------------------
ohneta 0:cc32f8c167de 9 Ticker ticker;
ohneta 0:cc32f8c167de 10 Serial pc(P0_9, P0_11);
ohneta 0:cc32f8c167de 11 DigitalOut led1(P0_18);
ohneta 0:cc32f8c167de 12 DigitalOut led2(P0_19);
ohneta 0:cc32f8c167de 13 DigitalIn btn1(P0_16);
ohneta 0:cc32f8c167de 14 DigitalIn btn2(P0_17);
ohneta 0:cc32f8c167de 15
ohneta 0:cc32f8c167de 16 AD9850 dds(P0_20, P0_25, P0_24, P0_23);
ohneta 0:cc32f8c167de 17
ohneta 0:cc32f8c167de 18 //-----------------------------------------------------------------------
ohneta 0:cc32f8c167de 19
ohneta 0:cc32f8c167de 20 uint32_t targetFreq = 10 * 1000 * 1000; // 10MHz - default
ohneta 0:cc32f8c167de 21 uint32_t incrementFreq = 100 * 1000; // This is a 100KHz frequency increment
ohneta 0:cc32f8c167de 22
ohneta 0:cc32f8c167de 23 //-----------------------------------------------------------------------
ohneta 0:cc32f8c167de 24
ohneta 0:cc32f8c167de 25 BLEDevice ble;
ohneta 0:cc32f8c167de 26
ohneta 0:cc32f8c167de 27 const static char DEVICE_NAME[] = "DDS-CTRL 0.2";
ohneta 0:cc32f8c167de 28 static const uint16_t uuid16_list[] = {0xFFFF}; // Custom UUID, FFFF is reserved for development
ohneta 0:cc32f8c167de 29
ohneta 0:cc32f8c167de 30 // Set Up custom Characteristics
ohneta 0:cc32f8c167de 31 static uint8_t deviceValue = 0;
ohneta 0:cc32f8c167de 32 static uint8_t outputValue = 0;
ohneta 0:cc32f8c167de 33 static uint32_t frequencyValue = 0;
ohneta 0:cc32f8c167de 34
ohneta 0:cc32f8c167de 35 ReadWriteArrayGattCharacteristic<uint8_t, 1> deviceChar(0xA201, &deviceValue);
ohneta 0:cc32f8c167de 36 ReadWriteArrayGattCharacteristic<uint8_t, 1> outputChar(0xA202, &outputValue);
ohneta 0:cc32f8c167de 37 ReadWriteArrayGattCharacteristic<uint32_t, 1> frequencyChar(0xA203, &frequencyValue);
ohneta 0:cc32f8c167de 38
ohneta 0:cc32f8c167de 39 // Set up custom service
ohneta 0:cc32f8c167de 40 GattCharacteristic *characteristics[] = {
ohneta 0:cc32f8c167de 41 &deviceChar,
ohneta 0:cc32f8c167de 42 &outputChar,
ohneta 0:cc32f8c167de 43 &frequencyChar,
ohneta 0:cc32f8c167de 44 };
ohneta 0:cc32f8c167de 45 GattService customService(0xA001, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
ohneta 0:cc32f8c167de 46
ohneta 0:cc32f8c167de 47
ohneta 0:cc32f8c167de 48 //-----------------------------------------------------------------------
ohneta 0:cc32f8c167de 49 /**
ohneta 0:cc32f8c167de 50 *
ohneta 0:cc32f8c167de 51 */
ohneta 0:cc32f8c167de 52 void connectionCallback(Gap::Handle_t Handle_t,
ohneta 0:cc32f8c167de 53 Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr,
ohneta 0:cc32f8c167de 54 Gap::addr_type_t ownAddrType, const Gap::address_t ownAddr,
ohneta 0:cc32f8c167de 55 const Gap::ConnectionParams_t *params)
ohneta 0:cc32f8c167de 56 {
ohneta 0:cc32f8c167de 57 pc.printf("connectionCallback\n");
ohneta 0:cc32f8c167de 58 }
ohneta 0:cc32f8c167de 59
ohneta 0:cc32f8c167de 60 //--------------------------------
ohneta 0:cc32f8c167de 61 /*
ohneta 0:cc32f8c167de 62 * Restart advertising when phone app disconnects
ohneta 0:cc32f8c167de 63 */
ohneta 0:cc32f8c167de 64 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
ohneta 0:cc32f8c167de 65 {
ohneta 0:cc32f8c167de 66 pc.printf("disconnectionCallback\n");
ohneta 0:cc32f8c167de 67 ble.startAdvertising();
ohneta 0:cc32f8c167de 68 }
ohneta 0:cc32f8c167de 69
ohneta 0:cc32f8c167de 70 //--------------------------------
ohneta 0:cc32f8c167de 71 /*
ohneta 0:cc32f8c167de 72 * handle writes to writeCharacteristic
ohneta 0:cc32f8c167de 73 */
ohneta 0:cc32f8c167de 74 void writeCharCallback(const GattCharacteristicWriteCBParams *params)
ohneta 0:cc32f8c167de 75 {
ohneta 0:cc32f8c167de 76 led2 = !led2;
ohneta 0:cc32f8c167de 77
ohneta 0:cc32f8c167de 78 pc.printf("BLE:onDataWritten\n");
ohneta 0:cc32f8c167de 79
ohneta 0:cc32f8c167de 80 if (params->charHandle == deviceChar.getValueHandle()) {
ohneta 0:cc32f8c167de 81 memcpy(&deviceChar, params->data, params->len);
ohneta 0:cc32f8c167de 82 pc.printf("deviceChar\n");
ohneta 0:cc32f8c167de 83 pc.printf("deviceChar: deviceValue: %x\n", deviceValue);
ohneta 0:cc32f8c167de 84
ohneta 0:cc32f8c167de 85 } else
ohneta 0:cc32f8c167de 86 if (params->charHandle == outputChar.getValueHandle()) {
ohneta 0:cc32f8c167de 87 memcpy(&outputValue, params->data, params->len);
ohneta 0:cc32f8c167de 88 pc.printf("outputChar : outputValue: %x\n", outputValue);
ohneta 0:cc32f8c167de 89
ohneta 0:cc32f8c167de 90 } else
ohneta 0:cc32f8c167de 91 if (params->charHandle == frequencyChar.getValueHandle()) {
ohneta 0:cc32f8c167de 92 memcpy(&targetFreq, params->data, params->len);
ohneta 0:cc32f8c167de 93 pc.printf("frequencyChar\n");
ohneta 0:cc32f8c167de 94 pc.printf("targetFreq: %d\n", targetFreq);
ohneta 0:cc32f8c167de 95 }
ohneta 0:cc32f8c167de 96
ohneta 0:cc32f8c167de 97 }
ohneta 0:cc32f8c167de 98
ohneta 0:cc32f8c167de 99 //-----------------------------------------------------------------------
ohneta 0:cc32f8c167de 100
ohneta 0:cc32f8c167de 101 void tickerCallback()
ohneta 0:cc32f8c167de 102 {
ohneta 0:cc32f8c167de 103 led1 = !led1;
ohneta 0:cc32f8c167de 104 }
ohneta 0:cc32f8c167de 105
ohneta 0:cc32f8c167de 106 char guruguru(int cnt)
ohneta 0:cc32f8c167de 107 {
ohneta 0:cc32f8c167de 108 static char gurus[] = {'-', '\\', '|', '/'};
ohneta 0:cc32f8c167de 109 return gurus[cnt % 4];
ohneta 0:cc32f8c167de 110 }
ohneta 0:cc32f8c167de 111
ohneta 0:cc32f8c167de 112 //--------------------------------
ohneta 0:cc32f8c167de 113
ohneta 0:cc32f8c167de 114 int main(void)
ohneta 0:cc32f8c167de 115 {
ohneta 0:cc32f8c167de 116 ticker.attach(&tickerCallback, 0.5);
ohneta 0:cc32f8c167de 117
ohneta 0:cc32f8c167de 118 pc.baud(115200);
ohneta 0:cc32f8c167de 119 pc.printf("\n");
ohneta 0:cc32f8c167de 120 pc.printf("------------------------\n");
ohneta 0:cc32f8c167de 121 pc.printf("DDS-CTRL start\n");
ohneta 0:cc32f8c167de 122 led1 = 0; led2 = 0;;
ohneta 0:cc32f8c167de 123
ohneta 0:cc32f8c167de 124
ohneta 0:cc32f8c167de 125 ble.init();
ohneta 0:cc32f8c167de 126 ble.onConnection(connectionCallback);
ohneta 0:cc32f8c167de 127 ble.onDisconnection(disconnectionCallback);
ohneta 0:cc32f8c167de 128 ble.onDataWritten(writeCharCallback);
ohneta 0:cc32f8c167de 129
ohneta 0:cc32f8c167de 130 // setup advertising
ohneta 0:cc32f8c167de 131 {
ohneta 0:cc32f8c167de 132 // BLE only, no classic BT
ohneta 0:cc32f8c167de 133 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED |
ohneta 0:cc32f8c167de 134 GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
ohneta 0:cc32f8c167de 135 // advertising type
ohneta 0:cc32f8c167de 136 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ohneta 0:cc32f8c167de 137 // add name
ohneta 0:cc32f8c167de 138 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
ohneta 0:cc32f8c167de 139 (uint8_t *)DEVICE_NAME,
ohneta 0:cc32f8c167de 140 sizeof(DEVICE_NAME) );
ohneta 0:cc32f8c167de 141 // UUID's broadcast in advertising packet
ohneta 0:cc32f8c167de 142 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
ohneta 0:cc32f8c167de 143 (uint8_t *)uuid16_list,
ohneta 0:cc32f8c167de 144 sizeof(uuid16_list) );
ohneta 0:cc32f8c167de 145 ble.setAdvertisingInterval(100); // interval is 100ms.
ohneta 0:cc32f8c167de 146 // add my service
ohneta 0:cc32f8c167de 147 ble.addService(customService);
ohneta 0:cc32f8c167de 148
ohneta 0:cc32f8c167de 149 // start advertising
ohneta 0:cc32f8c167de 150 ble.startAdvertising();
ohneta 0:cc32f8c167de 151 }
ohneta 0:cc32f8c167de 152 pc.printf("BLE init\n");
ohneta 0:cc32f8c167de 153
ohneta 0:cc32f8c167de 154
ohneta 0:cc32f8c167de 155 // DDS 初期化
ohneta 0:cc32f8c167de 156 {
ohneta 0:cc32f8c167de 157 dds.init();
ohneta 0:cc32f8c167de 158 pc.printf("\nFREQ: %d [default]\n", targetFreq);
ohneta 0:cc32f8c167de 159 dds.setFrequency(targetFreq, 0, 0);
ohneta 0:cc32f8c167de 160 dds.setFrequency(targetFreq, 0, 0);
ohneta 0:cc32f8c167de 161 // TODO: なぜか最初だけ2回ださないと設定されない...
ohneta 0:cc32f8c167de 162 }
ohneta 0:cc32f8c167de 163 pc.printf("DDS init\n");
ohneta 0:cc32f8c167de 164
ohneta 0:cc32f8c167de 165
ohneta 0:cc32f8c167de 166 int loopCounter = 0;
ohneta 0:cc32f8c167de 167 int lastFreq = targetFreq;
ohneta 0:cc32f8c167de 168 while (true) {
ohneta 0:cc32f8c167de 169 ble.waitForEvent();
ohneta 0:cc32f8c167de 170
ohneta 0:cc32f8c167de 171 loopCounter++;
ohneta 0:cc32f8c167de 172 //pc.putc(guruguru(loopCounter));
ohneta 0:cc32f8c167de 173 //pc.putc('\r');
ohneta 0:cc32f8c167de 174 pc.putc('*');
ohneta 0:cc32f8c167de 175 wait_ms(100);
ohneta 0:cc32f8c167de 176
ohneta 0:cc32f8c167de 177 if (lastFreq != targetFreq) {
ohneta 0:cc32f8c167de 178 pc.printf("\nFREQ: %d\n", targetFreq);
ohneta 0:cc32f8c167de 179 dds.setFrequency(targetFreq, 0, 0);
ohneta 0:cc32f8c167de 180
ohneta 0:cc32f8c167de 181 //gInternalStorageBuffer.freq = targetFreq;
ohneta 0:cc32f8c167de 182 //nRF51822_internalStorage_write();
ohneta 0:cc32f8c167de 183
ohneta 0:cc32f8c167de 184 lastFreq = targetFreq;
ohneta 0:cc32f8c167de 185 }
ohneta 0:cc32f8c167de 186 }
ohneta 0:cc32f8c167de 187
ohneta 0:cc32f8c167de 188 }
ohneta 0:cc32f8c167de 189
ohneta 0:cc32f8c167de 190 //---------------------------------------------------------
ohneta 0:cc32f8c167de 191