test
Dependencies: BLE_API nRF51822 mbed
Fork of KS7 by
Diff: main.cpp
- Branch:
- KS3
- Revision:
- 20:c3fadeef5842
- Parent:
- 19:05526fe9365e
- Child:
- 21:ab5b55d1947d
--- a/main.cpp Fri Jan 15 04:17:08 2016 +0000 +++ b/main.cpp Tue Feb 09 06:57:38 2016 +0000 @@ -20,11 +20,12 @@ // Weight Scale Service (Original) #define UUID_WEIGHT_SCALE_SERVICE (0x181D) -#define WEIGHT_COEFFICIENT (8.64) +#if defined(PCB_VER1) || defined(PCB_VER2) // Switch #define SW_THRESHOLD (0.5) #define SW_SAMPLECOUNT (3) +#endif // Mode #define MODE_OFF (0) // LED OFF @@ -44,24 +45,28 @@ float32_t weight = 0.0; uint32_t scale = 0; int update_counter = 0; +#if defined(PCB_VER1) || defined(PCB_VER2) float sw_data[SW_SAMPLECOUNT]; uint8_t sw_count = 0; +#endif int led_mode = MODE_OFF; float led_brightness = BRIGHTNESS_MINVALUE; -Timer t; - -// UART for debug - #ifdef UART_DEBUG +#if defined(PCB_VER1) || defined(PCB_VER2) Serial pc(P0_9, P0_8);// TX=P0_9 +#else +Serial pc(P0_9, P0_11);// TX=P0_9 +#endif #define UART_BAUD_RATE (9600UL) #define DEBUG(...) { pc.printf(__VA_ARGS__); } #else #define DEBUG(...) {} #endif -// BLE +Timer t; + +// BLE BLE ble; Gap::ConnectionParams_t connectionParams; @@ -71,75 +76,78 @@ /* Weight Scale Service */ static const uint8_t UUID_HACARUS_WEIGHT_CHAR[] = {0x00, 0x00, 0x2A, 0x9D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'}; GattCharacteristic WeightMeasurement (UUID(UUID_HACARUS_WEIGHT_CHAR), (uint8_t *)&weight_data, sizeof(weight_data), sizeof(weight_data), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); static const uint8_t UUID_HACARUS_SCALE_CHAR[] = {0x00, 0x00, 0x2A, 0x9E, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'}; GattCharacteristic WeightScale (UUID(UUID_HACARUS_SCALE_CHAR), (uint8_t *)&scale, sizeof(scale), sizeof(scale), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); GattCharacteristic *Chars[] = {&WeightMeasurement,&WeightScale}; static const uint8_t UUID_HACARUS_WEIGHT_SERVICE[] = {0x00, 0x00, 0x18, 0x1D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'}; GattService HWS = GattService(UUID(UUID_HACARUS_WEIGHT_SERVICE), Chars, sizeof(Chars) / sizeof(GattCharacteristic *)); /* Device Information Service */ GattCharacteristic ManuName(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)&MANUFACTURER_NAME_STRING, sizeof(MANUFACTURER_NAME_STRING) - 1, sizeof(MANUFACTURER_NAME_STRING) - 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic ModelNum(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)&MODEL_NUMBER_STRING, sizeof(MODEL_NUMBER_STRING) - 1, sizeof(MODEL_NUMBER_STRING) - 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic SerialNum(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)&SERIAL_NUMBER_STRING, sizeof(SERIAL_NUMBER_STRING) - 1, sizeof(SERIAL_NUMBER_STRING) - 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic FWVersion(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)&FIRMWARE_REVISION_STRING, sizeof(FIRMWARE_REVISION_STRING) - 1, sizeof(FIRMWARE_REVISION_STRING) - 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic *DISChars[] = {&ManuName, &ModelNum, &SerialNum, &FWVersion}; GattService DIS(GattService::UUID_DEVICE_INFORMATION_SERVICE , DISChars, sizeof(DISChars) / sizeof(GattCharacteristic *)); -// to confirm joystick -bool check_joystick(){ -/* +#ifdef PCB_VER1 +bool check_joystick() +{ float sum_data = 0; - - // ジョイスティックXのADC値(0〜1.0換算)が0.5以上ならON、0.5未満ならOFF + sw_data[sw_count] = io.get_x(); - - if(++sw_count >= SW_SAMPLECOUNT){ + + if(++sw_count >= SW_SAMPLECOUNT) { sw_count = 0; } - - for(int count = 0;count < SW_SAMPLECOUNT;count++){ + + for(int count = 0; count < SW_SAMPLECOUNT; count++) { sum_data += sw_data[count]; } - + return ((sum_data / SW_SAMPLECOUNT) >= SW_THRESHOLD); -*/ -// return(io.get_switch()); - return(0); // ignore switch } +#endif uint32_t quick_ieee11073_from_float(float data) { uint8_t exponent = 0xFE; //exponent is -2 uint32_t mantissa = (uint32_t)(data*100); - + return ( ((uint32_t)exponent) << 24) | mantissa; } -void SWInit(void){ +#if defined(PCB_VER1) || defined(PCB_VER2) +void SWInit(void) +{ // SW Initialize - for(int count = 0;count < SW_SAMPLECOUNT;count++){ + for(int count = 0; count < SW_SAMPLECOUNT; count++) { sw_data[count] = 0; } } +#endif -void AppInit(void){ - +void AppInit(void) +{ + +#if defined(PCB_VER1) || defined(PCB_VER2) SWInit(); +#endif io.analog_pow(1); - -/* - if(NRF_UICR->XTALFREQ == 0xFFFFFF00){ - io.display_value = 3232; - }else if(NRF_UICR->XTALFREQ == 0xFFFFFFFF){ - io.display_value = 1616; - } -*/ + + /* + if(NRF_UICR->XTALFREQ == 0xFFFFFF00){ + io.display_value = 3232; + }else if(NRF_UICR->XTALFREQ == 0xFFFFFFFF){ + io.display_value = 1616; + } + */ } /* @@ -147,61 +155,61 @@ */ void BLEConnectionCallback(const Gap::ConnectionCallbackParams_t *params) { - ble.updateConnectionParams(params->handle, params->connectionParams); + ble.updateConnectionParams(params->handle, params->connectionParams); } void BLEDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { - if(led_mode == MODE_ON){ + if(led_mode == MODE_ON) { ble.startAdvertising(); } } void BLERadioNotificationCallback(bool radio_active) { - if (radio_active == false) - { - if(led_mode == MODE_ON){ + if (radio_active == false) { + if(led_mode == MODE_ON) { } } } -void BleInitialize(void){ - +void BleInitialize(void) +{ + uint8_t advertiseServiceID[16]; - + // Initialize ble.init(); - + // Event Set ble.onConnection(&BLEConnectionCallback); ble.onDisconnection(&BLEDisconnectionCallback); ble.onRadioNotification(&BLERadioNotificationCallback); - + ble.getPreferredConnectionParams(&connectionParams); connectionParams.maxConnectionInterval = INTERVAL_500MSEC; connectionParams.minConnectionInterval = INTERVAL_500MSEC; connectionParams.connectionSupervisionTimeout = CONNTIMEOUT_3000MSEC; connectionParams.slaveLatency = 2; ble.setPreferredConnectionParams(&connectionParams); - + ble.setTxPower(BLE_TXPOWER_4DBM); - - for(int i=0; i<16; i++){ + + for(int i=0; i<16; i++) { advertiseServiceID[i] = UUID_HACARUS_WEIGHT_SERVICE[16 - 1 - i]; } ble.accumulateAdvertisingPayload((GapAdvertisingData::Flags)(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED)); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, - (const uint8_t *)DEVICE_NAME, - strlen(DEVICE_NAME)); + (const uint8_t *)DEVICE_NAME, + strlen(DEVICE_NAME)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)advertiseServiceID, sizeof(advertiseServiceID)); - + ble.setAdvertisingInterval(INTERVAL_500MSEC); ble.setAdvertisingTimeout(ADV_TIMEOUT); /* 0 is disable the advertising timeout. */ - + ble.addService(HWS); ble.addService(DIS); } @@ -220,81 +228,84 @@ BleInitialize(); AppInit(); - led_mode = MODE_START; - int tm = 0; + led_mode = MODE_OFF; for (;; ) { // 100msec waitForEvent t.reset(); - while(t.read_ms() < LED_INTERVAL_MSEC){ + while(t.read_ms() < LED_INTERVAL_MSEC) { t.start(); ble.waitForEvent(); t.stop(); } - io.analog_pow(1); -// pc.printf("#%d %d %d\r\n", io.get_switch(), d0, d1); - // ticker処理 - switch(led_mode){ + // ticker蜃ヲ逅・ + switch(led_mode) { case MODE_OFF: - if(check_joystick()){ + io.analog_pow(0); + io.display(0); + if(io.get_switch()) { led_mode = MODE_START; } - break; + break; case MODE_START: + io.analog_pow(1); led_brightness += BRIGHTNESS_ADDVALUE; io.display(led_brightness); - if(led_brightness >= BRIGHTNESS_MAXVALUE){ + if(led_brightness >= BRIGHTNESS_MAXVALUE) { update_counter = 0; io.calibrate_weight(); +#if defined(PCB_VER1) || defined(PCB_VER2) SWInit(); +#endif ble.startAdvertising(); led_mode = MODE_ON; } - break; + break; case MODE_ON: - if(check_joystick()){ + io.analog_pow(1); + if(io.get_switch()) { led_mode = MODE_END; - if(ble.getGapState().connected){ + if(ble.getGapState().connected) { ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION); - }else{ + } else { ble.stopAdvertising(); } - }else{ + } else { scale++; ble.updateCharacteristicValue(WeightScale.getValueAttribute().getHandle(), - (uint8_t *)&scale, - sizeof(scale)); - + (uint8_t *)&scale, + sizeof(scale)); + // weight = io.get_weight_raw() / WEIGHT_COEFFICIENT; - tm++; - if (tm == 100){ - tm = 0; - weight = (float)(rand() % 2000); -// pc.printf("w=%f\r\n", weight); - if(weight < 0){ - weight = 0; - } - io.display_value = (uint16_t)weight; // dummy display - if(++update_counter >= 5){ - weight_data = quick_ieee11073_from_float(weight); - ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(), - (uint8_t *)&weight_data, - sizeof(weight_data)); - update_counter = 0; - } +// if(weight < 0) { +// weight = 0; +// } + weight = io.get_weight(); + io.display_value = (uint16_t)weight; +// io.display_value = 8888; // for LED soldering check +#ifdef UART_DEBUG + pc.printf("%d\r\n", io._get_adc_raw(0)); +// pc.printf("weight=%f %d / %d\r\n", weight, io.display_value, io._adc0); +// pc.printf("%02x %02x %02x %02x\r\n", io._seg[0], io._seg[1], io._seg[2], io._seg[3]); +#endif + if(++update_counter >= 5) { + weight_data = quick_ieee11073_from_float(weight); + ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(), + (uint8_t *)&weight_data, + sizeof(weight_data)); + update_counter = 0; } } - break; + break; case MODE_END: led_brightness -= BRIGHTNESS_ADDVALUE; io.display(led_brightness); - if(led_brightness <= BRIGHTNESS_MINVALUE){ + if(led_brightness <= BRIGHTNESS_MINVALUE) { +#if defined(PCB_VER1) || defined(PCB_VER2) SWInit(); +#endif led_mode = MODE_OFF; } - break; + break; } - io.analog_pow(0); } } - - \ No newline at end of file