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 nRF51822 mbed
Fork of KS7 by
Diff: main.cpp
- Revision:
- 6:bc960e6fd1ed
- Parent:
- 5:1730064b94bd
- Child:
- 7:9ebf125e405f
--- a/main.cpp Tue Oct 27 02:04:56 2015 +0000
+++ b/main.cpp Fri Nov 06 00:02:56 2015 +0000
@@ -2,112 +2,143 @@
#include "io.h"
#include "BLEDevice.h"
-// Advertise
-#define INTERVAL_1SEC (1000UL)
+// BLE
+#define INTERVAL_500MSEC (500UL)
+#define CONNTIMEOUT_3000MSEC (3000UL)
#define ADV_TIMEOUT (0)
-
#define DEVICE_NAME "Kitchen Scale"
#define BLE_TXPOWER_4DBM (4)
// Device Information Service (DIS) (20 character limit)
// https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml
-#define MANUFACTURER_NAME_STRING "akita11/GomiHgy" // Manufacturer Name String - shall represent the name of the manufacturer of the device.
-#define MODEL_NUMBER_STRING "Model Number" // Model Number String - shall represent the model number that is assigned by the device vendor.
-#define SERIAL_NUMBER_STRING "Serial Number" // Serial Number String - shall represent the serial number for a particular instance of the device.
-#define HARDWARE_REVISION_STRING "v1.0.0@151023" // Hardware Revision String - shall represent the hardware revision for the hardware within the device.
-#define FIRMWARE_REVISION_STRING "rev0004:edd6e262de9e" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
-#define SOFTWARE_REVISION_STRING "v0.1.00.005:151027r1" // Software Revision String - shall represent the software revision for the software within the device.
+#define MANUFACTURER_NAME_STRING "Hacarus" // Manufacturer Name String - shall represent the name of the manufacturer of the device.
+#define MODEL_NUMBER_STRING "0001" // Model Number String - shall represent the model number that is assigned by the device vendor.
+#define SERIAL_NUMBER_STRING "000780c0ffeef00d" // Serial Number String - shall represent the serial number for a particular instance of the device.
+#define FIRMWARE_REVISION_STRING "v1.00.00.000@rev0006" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
// Weight Scale Service (Original)
#define UUID_WEIGHT_SCALE_SERVICE (0x181D)
-#define UUID_WEIGHT_MEASUREMENT_CHAR (0x3A9D)
+
+// JoyStick
+#define PUSH_THRESHOLD (100)
+#define PUSH_COUNT (15)
+// Mode
+#define MODE_OFF (0) // LED OFF
+#define MODE_START (1) // LED OFF -> ON
+#define MODE_ON (2) // LED ON
+#define MODE_END (3) // LED ON -> OFF
+
+// Led
+#define LED_INTERVAL_NSEC (200000)
+#define BRIGHTNESS_ADDVALUE (0.1)
+#define BRIGHTNESS_MINVALUE (0)
+#define BRIGHTNESS_MAXVALUE (1.0)
+
+// UART for debug
#ifdef UART_DEBUG
Serial pc(P0_9, P0_8);// TX=P0_9
#define UART_BAUD_RATE (9600UL)
#endif
-/*
-Serial pc(P0_9, P0_8);// TX=P0_9
-AnalogIn vin(P0_1);
+// Properties
+io io;
+Ticker tk;
+float weight = 0;
+uint32_t scale = 0;
+int update_counter = 0;
+int push_counter = 0;
+int led_mode = MODE_OFF;
+float led_brightness = BRIGHTNESS_MINVALUE;
-int main()
-{
- pc.baud(9600);
- while(1){
- pc.printf("CONFIG=%x\r\n", NRF_ADC->CONFIG); // 0x0000046a
-// EXTREFSEL=00 (none)
-// PSEL=00000100 (use AnalogInput2)
-// REFSEL=11 (VDD with 1/3 prescale)
-// INPSEL=010 (prescale=1/3)
-// RES=10 (10bit)
- pc.printf("%f %d\r\n", vin.read(), NRF_ADC->RESULT);
- wait(0.1);
- }
-}
-*/
-
-io io;
-
-Ticker tk; // 1s timer for switch check & weighgt measure
-float weight;
-char weight_str[20];
-uint8_t fPow = 0;
-
+// BLE
BLEDevice ble;
Gap::ConnectionParams_t connectionParams;
/* Complete list of 16-bit Service IDs */
-uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE,UUID_WEIGHT_SCALE_SERVICE};
+uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE};
/* Weight Scale Service */
-GattCharacteristic WeightMeasurement(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR/*UUID_WEIGHT_MEASUREMENT_CHAR*/, (uint8_t *)&weight_str, sizeof(weight_str), sizeof(weight_str), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
-GattCharacteristic *WMSChars[] = {&WeightMeasurement };
-GattService WMS(UUID_WEIGHT_SCALE_SERVICE , WMSChars, sizeof(WMSChars) / sizeof(GattCharacteristic *));
+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, sizeof(weight), sizeof(weight),
+ 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 *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 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 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 HWVersion(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, (uint8_t *)&HARDWARE_REVISION_STRING, sizeof(HARDWARE_REVISION_STRING) - 1, sizeof(HARDWARE_REVISION_STRING) - 1,
+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 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 SWVersion(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR, (uint8_t *)&SOFTWARE_REVISION_STRING, sizeof(SOFTWARE_REVISION_STRING) - 1, sizeof(SOFTWARE_REVISION_STRING) - 1,
- GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
-GattCharacteristic *DISChars[] = {&ManuName, &ModelNum, /*&SerialNum,*/ &HWVersion, &FWVersion, &SWVersion };
+GattCharacteristic *DISChars[] = {&ManuName, &ModelNum, &SerialNum, &FWVersion};
GattService DIS(GattService::UUID_DEVICE_INFORMATION_SERVICE , DISChars, sizeof(DISChars) / sizeof(GattCharacteristic *));
- // 1s timer for switch check & weighgt measure
- void ticker_1s()
+void ticker_callback()
{
- io.analog_pow(1); // turn analog power on
- weight = io.get_weight(); // get weight [g]
- sprintf (weight_str, "%0.3f[g]", weight);
- ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(),
- (uint8_t *)&weight_str,
- sizeof(weight_str));
-/*
- // check switch, and control power flag (fPower)
- if (fPow == 0 && io.get_switch() == 1){
- while(io.get_switch() == 1); // wait until switch released
- fPow = 1;
+ bool pushing = false;
+
+ /* to confirm joystick */
+ int pushvalue = (int)(io.get_x() * 9900 + io.get_y() * 99);
+ if(pushvalue < PUSH_THRESHOLD){
+ if(++push_counter >= PUSH_COUNT){
+ pushing = true;
+ push_counter = 0;
+ }
+ }else{
+ push_counter = 0;
}
- if (fPow == 1 && io.get_switch() == 1){
- while(io.get_switch() == 1); // wait until switch released
- fPow = 0;
+
+ switch(led_mode){
+ case MODE_OFF:
+ if(pushing){
+ io.display_value = 0;
+ led_mode = MODE_START;
+ }
+ break;
+ case MODE_START:
+ led_brightness += BRIGHTNESS_ADDVALUE;
+ io.display(led_brightness);
+ if(led_brightness >= BRIGHTNESS_MAXVALUE){
+ led_mode = MODE_ON;
+ ble.startAdvertising();
+ }
+ break;
+ case MODE_ON:
+ if(!pushing){
+ io.display_value = (uint16_t)(io.get_weight() * 9999.0); // dummy display
+ if(++update_counter >= 5){
+ io.analog_pow(1); // turn analog power on
+ weight = io.get_weight() * 9999.0;
+ ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(),
+ (uint8_t *)&weight,
+ sizeof(weight));
+ io.analog_pow(0); // turn analog power off
+ update_counter = 0;
+ }
+ }else{
+ if(ble.getGapState().connected){
+ ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
+ }
+ led_mode = MODE_END;
+ update_counter = 0;
+ }
+ break;
+ case MODE_END:
+ led_brightness -= BRIGHTNESS_ADDVALUE;
+ io.display(led_brightness);
+ if(led_brightness <= BRIGHTNESS_MINVALUE){
+ led_mode = MODE_OFF;
+ }
+ break;
}
-*/
-
-// io.display_value = (uint16_t)(io.get_weight_raw() * 9999.0); // dummy display
-// io.display_value = (int)(io.get_x() * 9900 + io.get_y() * 99); // test: display joystick XY value
-// io.display_value = (uint16_t)(io.get_x() * 9999.0); // dummy display
-
-
- io.analog_pow(0); // turn analog power off
}
/*
@@ -120,56 +151,47 @@
void BLEDisconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
- ble.startAdvertising();
-}
-
-void BLEDataWrittenCallback(const GattCharacteristicWriteCBParams *params)
-{
- // Do Nothing.
-}
-
-void BLEDataReadCallback(const GattCharacteristicReadCBParams *params)
-{
- // Do Nothing.
-}
-
-void BLERadioNotificationCallback(bool radio_active)
-{
- if (radio_active == false)
- {
- // Do Nothing.
+ if(led_mode == MODE_ON){
+ ble.startAdvertising();
}
}
void BleInitialize(void){
+ uint8_t advertiseServiceID[16];
+
// Initialize
ble.init();
// Event Set
ble.onConnection(&BLEConnectionCallback);
ble.onDisconnection(&BLEDisconnectionCallback);
- //ble.onDataWritten(&BLEDataWrittenCallback);
- //ble.onDataRead(&BLEDataReadCallback);
- ble.onRadioNotification(&BLERadioNotificationCallback);
ble.getPreferredConnectionParams(&connectionParams);
- connectionParams.minConnectionInterval = INTERVAL_1SEC;
- connectionParams.maxConnectionInterval = INTERVAL_1SEC;
+ 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++){
+ 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));
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,(uint8_t *)uuid16_list, sizeof(uuid16_list));
-
- ble.setAdvertisingInterval(INTERVAL_1SEC);
+ ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+ (const uint8_t *)advertiseServiceID, sizeof(advertiseServiceID));
+
+ ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(INTERVAL_500MSEC));
ble.setAdvertisingTimeout(ADV_TIMEOUT); /* 0 is disable the advertising timeout. */
- ble.addService(WMS);
+ ble.addService(HWS);
ble.addService(DIS);
}
@@ -181,29 +203,11 @@
pc.baud(UART_BAUD_RATE);
#endif
- tk.attach(&ticker_1s, 0.1);
+ tk.attach_us(&ticker_callback, LED_INTERVAL_NSEC);
- ble.startAdvertising();
-// io.calibrate_weight(); // initial calibration
- while(1){
- io.display(1);
- for (int i = 0; i < 50; i++){
- io.display_value = (uint16_t)(io.get_weight() * 9999.0); // dummy display
- wait(0.1);
- }
- io.display(0.3);
- for (int i = 0; i < 50; i++){
- io.display_value = (int)(io.get_x() * 9900 + io.get_y() * 99); // test: display joystick XY value
- wait(0.1);
- }
-// io.display_value = (uint16_t)(io.get_weight() * 9999.0); // dummy display
-// wait(0.1);
-// for (float i = 0; i < 1.0; i+=0.05){
-// if (i <0.5) io.display(1.0-i);
-// else io.display(i); // change display brightneess
-// wait(0.1);
-// io.display_value = (uint16_t)(io.get_weight() * 9999.0); // dummy display
-// }
+ for (;; ) {
+ ble.waitForEvent();
}
}
+
\ No newline at end of file
