robo 8080
/
BLE_TEST_konashi
konashi/SBBLEのテスト
Fork of BLE_LoopbackUART by
Diff: main.cpp
- Revision:
- 6:9c9630cb454b
- Parent:
- 5:61109bce11fe
- Child:
- 7:60964e52810e
--- a/main.cpp Sat Aug 16 02:10:08 2014 +0000 +++ b/main.cpp Sat Aug 16 06:51:59 2014 +0000 @@ -51,7 +51,7 @@ uint16_t uuid16_list[] = { KONASHI_SERVICE_UUID }; -#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; +#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */ #if NEED_CONSOLE_OUTPUT @@ -64,8 +64,9 @@ //Serial pc(USBTX, USBRX); BLEDevice ble; DigitalOut led1(LED1); +DigitalOut led2(LED2); + BusOut ioOut(P0_0,P0_1,P0_2,P0_3,P0_4,P0_5,P0_6,P0_7); - uint8_t pioSetting[20] = {0,}; uint8_t pioPullup[20] = {0,}; uint8_t pioOutput[20] = {0,}; @@ -74,6 +75,10 @@ uint8_t pwmConfig[20] = {0,}; uint8_t pwmPeriod[20] = {0,}; uint8_t pwmDuty[20] = {0,}; +static uint32_t pwm_period[3] = {20000,20000,20000}; +static uint32_t pwm_duty[3] = {0,0,0}; +static uint8_t pwm_config = 0; +PwmOut pwm[3] = {P0_28, P0_29, P0_30}; uint8_t analogDrive[20] = {0,}; uint8_t analogRead0[20] = {0,}; @@ -173,14 +178,14 @@ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); -GattCharacteristic *konashiChars[] = { //&pioSettingCharacteristic, - //&pioPullupCharacteristic, +GattCharacteristic *konashiChars[] = {// &pioSettingCharacteristic, + // &pioPullupCharacteristic, &pioOutputCharacteristic, -/* &pioInputCharacteristic, + &pioInputCharacteristic, &pwmConfigCharacteristic, &pwmPeriodCharacteristic, &pwmDutyCharacteristic, - &analogDriveCharacteristic, +/* &analogDriveCharacteristic, &analogRead0Characteristic, &analogRead1Characteristic, &analogRead2Characteristic, @@ -190,10 +195,10 @@ &i2cReadParamCharacteristic, &i2cReadCharacteristic, &uartConfigCharacteristic, - &uartBaudrateCharacteristic,*/ + &uartBaudrateCharacteristic, &uartTxCharacteristic, &uartRxCharacteristic, -/* &hardwareRestCharacteristic, + &hardwareRestCharacteristic, &hardwareLowBatCharacteristic,*/ }; GattService konashiService(KONASHI_SERVICE_UUID, konashiChars, sizeof(konashiChars) / sizeof(GattCharacteristic *)); @@ -205,48 +210,30 @@ static char systemId = 'A'; GattCharacteristic systemID(GattCharacteristic::UUID_SYSTEM_ID_CHAR, (uint8_t *)systemId, sizeof(systemId), sizeof(systemId), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -//GattCharacteristic *systemChars[] = {&systemID }; -//GattService systemService(GattService::UUID_DEVICE_INFORMATION_SERVICE, systemChars, sizeof(systemChars) / sizeof(GattCharacteristic *)); - // MODEL static char model[31] = "mbed HRM1017"; GattCharacteristic modelID(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)model, strlen(model), strlen(model), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -//GattCharacteristic *modelChars[] = {&modelID }; -//GattService modelService(GattService::UUID_DEVICE_INFORMATION_SERVICE, modelChars, sizeof(modelChars) / sizeof(GattCharacteristic *)); - // Firmware static char fwversion[31] = "1.0"; GattCharacteristic fwChars(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, strlen(fwversion), strlen(fwversion), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -//GattCharacteristic *firmwareChars[] = {&fwChars }; -//GattService firmwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, firmwareChars, sizeof(firmwareChars) / sizeof(GattCharacteristic *)); - // Software static char swversion[31] = "1.0"; GattCharacteristic swChars(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR, (uint8_t *)swversion, strlen(swversion), strlen(swversion), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -//GattCharacteristic *softwareChars[] = {&swChars }; -//GattService softwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, softwareChars, sizeof(softwareChars) / sizeof(GattCharacteristic *)); // Hardware static char hwversion[31] = "1.0"; GattCharacteristic hwChars(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, (uint8_t *)hwversion, strlen(hwversion), strlen(hwversion), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -//GattCharacteristic *hardwareChars[] = {&hwChars }; -//GattService hardwareService(GattService::UUID_DEVICE_INFORMATION_SERVICE, hardwareChars, sizeof(hardwareChars) / sizeof(GattCharacteristic *)); // Manufacturer static char vendor[31] = "Test Company Inc."; GattCharacteristic vendorChars(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)vendor, strlen(vendor), strlen(vendor), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -//GattCharacteristic *manufacturerChars[] = {&vendorChars }; -//GattService manufacturerService(GattService::UUID_DEVICE_INFORMATION_SERVICE, manufacturerChars, sizeof(manufacturerChars) / sizeof(GattCharacteristic *)); // Serial number static char serial[31] = "1234567890"; GattCharacteristic serialChars(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)serial, strlen(serial), strlen(serial), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -//GattCharacteristic *serialNumberChars[] = {&serialChars }; -//GattService serialNumberService(GattService::UUID_DEVICE_INFORMATION_SERVICE, serialNumberChars, sizeof(serialNumberChars) / sizeof(GattCharacteristic *)); - //GattCharacteristic *informationChars[] = {&systemID, &modelID, &serialChars, &fwChars, &hwChars, &swChars, &vendorChars, }; GattCharacteristic *informationChars[] = {&modelID, &fwChars, &hwChars, &swChars, &vendorChars, }; @@ -259,21 +246,61 @@ GattCharacteristic *batteryChars[] = {&batteryPercentage }; GattService batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *)); - - void connectionCallback(uint16_t charHandle) //void connectionCallback(void) { DEBUG("Connected!\n\r"); + led2 = 0; } void disconnectionCallback(uint16_t charHandle) //void disconnectionCallback(void) { DEBUG("Disconnected!\n\r"); DEBUG("Restarting the advertising process\n\r"); + led2 = 1; ble.startAdvertising(); } +static void set_pwm_duty(int ch) +{ +// uint32_t period; + uint32_t duty; + unsigned bitmask = 1; + if(ch>=3){ + return; + } + duty=pwm_duty[ch]; + + if(pwm_config & (bitmask << ch)) + { + pwm[ch].pulsewidth_us(duty); + } + else + { + pwm[ch].pulsewidth_us(0); + } + } +static void set_pwm_period(int ch) +{ + uint32_t period,duty; + + if(ch>=3){ + return; + } + period=pwm_period[ch]; + duty=pwm_duty[ch]; + pwm[ch].period_us(period); + pwm[ch].pulsewidth_us(duty); +} +static void set_pwm_config(uint8_t config) +{ + pwm_config = config; + for(int i = 0; i < 3 ; i++) + { + set_pwm_duty(i); + } + +} void onDataWritten(uint16_t charHandle) { DEBUG("onDataWritten\n\r"); @@ -287,24 +314,44 @@ pioOutput[0]=getPioOut[0]; ioOut = getPioOut[0]; } - } else if (charHandle == uartTxCharacteristic.getHandle()) { - DEBUG("uartTxCharacteristic!\n\r"); - uint16_t bytesRead; - ble.readCharacteristicValue(uartTxCharacteristic.getHandle(), uartTx, &bytesRead); - DEBUG("ECHO: %s\n\r", (char *)uartTx); -// pc.putc(uartTx[0]); - ble.updateCharacteristicValue(uartRxCharacteristic.getHandle(), uartTx, bytesRead); - } else if (charHandle == pioSettingCharacteristic.getHandle()) { +/* } else if (charHandle == pioSettingCharacteristic.getHandle()) { DEBUG("pioSettingCharacteristic!\n\r"); } else if (charHandle == pioPullupCharacteristic.getHandle()) { - DEBUG("pioPullupCharacteristic!\n\r"); + DEBUG("pioPullupCharacteristic!\n\r");*/ } else if (charHandle == pwmConfigCharacteristic.getHandle()) { DEBUG("pwmConfigCharacteristic!\n\r"); + uint16_t bytesRead; + ble.readCharacteristicValue(pwmConfigCharacteristic.getHandle(), pwmConfig, &bytesRead); + set_pwm_config(pwmConfig[0]); } else if (charHandle == pwmPeriodCharacteristic.getHandle()) { DEBUG("pwmPeriodCharacteristic!\n\r"); + uint32_t l; + uint16_t bytesRead; + ble.readCharacteristicValue(pwmPeriodCharacteristic.getHandle(), pwmPeriod, &bytesRead); + int i=pwmPeriod[0]; + l =((uint32_t)pwmPeriod[1])<<24; + l|=((uint32_t)pwmPeriod[2])<<16; + l|=((uint32_t)pwmPeriod[3])<< 8; + l|=((uint32_t)pwmPeriod[4])<< 0; + if(i<3){ + pwmPeriod[i]=l; + set_pwm_period(i); + } } else if (charHandle == pwmDutyCharacteristic.getHandle()) { DEBUG("pwmDutyCharacteristic!\n\r"); - } else if (charHandle == analogDriveCharacteristic.getHandle()) { + uint32_t l; + uint16_t bytesRead; + ble.readCharacteristicValue(pwmDutyCharacteristic.getHandle(), pwmDuty, &bytesRead); + int i=pwmDuty[0]; + l =((uint32_t)pwmDuty[1])<<24; + l|=((uint32_t)pwmDuty[2])<<16; + l|=((uint32_t)pwmDuty[3])<< 8; + l|=((uint32_t)pwmDuty[4])<< 0; + if(i<3){ + pwm_duty[i]=l; + set_pwm_duty(i); + } +/* } else if (charHandle == analogDriveCharacteristic.getHandle()) { DEBUG("analogDriveCharacteristic!\n\r"); } else if (charHandle == i2cConfigCharacteristic.getHandle()) { DEBUG("i2cConfigCharacteristic!\n\r"); @@ -318,8 +365,15 @@ DEBUG("uartConfigCharacteristic!\n\r"); } else if (charHandle == uartBaudrateCharacteristic.getHandle()) { DEBUG("uartBaudrateCharacteristic!\n\r"); + } else if (charHandle == uartTxCharacteristic.getHandle()) { + DEBUG("uartTxCharacteristic!\n\r"); + uint16_t bytesRead; + ble.readCharacteristicValue(uartTxCharacteristic.getHandle(), uartTx, &bytesRead); + DEBUG("ECHO: %s\n\r", (char *)uartTx); +// pc.putc(uartTx[0]); + ble.updateCharacteristicValue(uartRxCharacteristic.getHandle(), uartTx, bytesRead); } else if (charHandle == hardwareRestCharacteristic.getHandle()) { - DEBUG("hardwareRestCharacteristic!\n\r"); + DEBUG("hardwareRestCharacteristic!\n\r");*/ } } @@ -345,6 +399,7 @@ int main(void) { led1 = 1; + led2 = 1; Ticker ticker; ticker.attach(periodicCallback, 1); @@ -372,8 +427,8 @@ // ble.addService(batteryService); ble.addService(informationService); -// ble.addService(konashiService); - ble.addService(konashiService1); +// ble.addService(konashiService1); + ble.addService(konashiService); while (true) { ble.waitForEvent();