test

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Committer:
gomihgy
Date:
Tue Nov 24 09:57:19 2015 +0000
Revision:
9:a7bd5ae66d6d
Parent:
8:32be16b1eaf2
Child:
10:95c1e5a218d5
v1.00.002; Android??Hacarus Weight Measurement???????????FLOAT???????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akita11 0:08c8325a21ec 1 #include "mbed.h"
akita11 0:08c8325a21ec 2 #include "io.h"
gomihgy 5:1730064b94bd 3 #include "BLEDevice.h"
gomihgy 9:a7bd5ae66d6d 4 #include "common.h"
akita11 0:08c8325a21ec 5
gomihgy 6:bc960e6fd1ed 6 // BLE
gomihgy 6:bc960e6fd1ed 7 #define INTERVAL_500MSEC (500UL)
gomihgy 6:bc960e6fd1ed 8 #define CONNTIMEOUT_3000MSEC (3000UL)
gomihgy 5:1730064b94bd 9 #define ADV_TIMEOUT (0)
gomihgy 5:1730064b94bd 10 #define DEVICE_NAME "Kitchen Scale"
gomihgy 5:1730064b94bd 11 #define BLE_TXPOWER_4DBM (4)
gomihgy 5:1730064b94bd 12
gomihgy 5:1730064b94bd 13 // Device Information Service (DIS) (20 character limit)
gomihgy 5:1730064b94bd 14 // https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml
gomihgy 6:bc960e6fd1ed 15 #define MANUFACTURER_NAME_STRING "Hacarus" // Manufacturer Name String - shall represent the name of the manufacturer of the device.
gomihgy 6:bc960e6fd1ed 16 #define MODEL_NUMBER_STRING "0001" // Model Number String - shall represent the model number that is assigned by the device vendor.
gomihgy 6:bc960e6fd1ed 17 #define SERIAL_NUMBER_STRING "000780c0ffeef00d" // Serial Number String - shall represent the serial number for a particular instance of the device.
gomihgy 9:a7bd5ae66d6d 18 #define FIRMWARE_REVISION_STRING "v1.00.002@rev0009" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
gomihgy 5:1730064b94bd 19
gomihgy 5:1730064b94bd 20 // Weight Scale Service (Original)
gomihgy 5:1730064b94bd 21 #define UUID_WEIGHT_SCALE_SERVICE (0x181D)
gomihgy 6:bc960e6fd1ed 22
gomihgy 6:bc960e6fd1ed 23 // JoyStick
gomihgy 7:9ebf125e405f 24 #define PUSH_MINTHRESHOLD (0.2)
gomihgy 7:9ebf125e405f 25 #define PUSH_MAXTHRESHOLD (0.6)
gomihgy 7:9ebf125e405f 26 #define PUSH_COUNT (15)
gomihgy 5:1730064b94bd 27
gomihgy 6:bc960e6fd1ed 28 // Mode
gomihgy 6:bc960e6fd1ed 29 #define MODE_OFF (0) // LED OFF
gomihgy 6:bc960e6fd1ed 30 #define MODE_START (1) // LED OFF -> ON
gomihgy 6:bc960e6fd1ed 31 #define MODE_ON (2) // LED ON
gomihgy 6:bc960e6fd1ed 32 #define MODE_END (3) // LED ON -> OFF
gomihgy 6:bc960e6fd1ed 33
gomihgy 6:bc960e6fd1ed 34 // Led
gomihgy 7:9ebf125e405f 35 #define LED_INTERVAL_NSEC (100000)
gomihgy 6:bc960e6fd1ed 36 #define BRIGHTNESS_ADDVALUE (0.1)
gomihgy 7:9ebf125e405f 37 #define BRIGHTNESS_MINVALUE (0.0)
gomihgy 6:bc960e6fd1ed 38 #define BRIGHTNESS_MAXVALUE (1.0)
gomihgy 6:bc960e6fd1ed 39
gomihgy 6:bc960e6fd1ed 40 // UART for debug
gomihgy 5:1730064b94bd 41 #ifdef UART_DEBUG
gomihgy 5:1730064b94bd 42 Serial pc(P0_9, P0_8);// TX=P0_9
gomihgy 5:1730064b94bd 43 #define UART_BAUD_RATE (9600UL)
gomihgy 5:1730064b94bd 44 #endif
akita11 4:edd6e262de9e 45
gomihgy 6:bc960e6fd1ed 46 // Properties
gomihgy 6:bc960e6fd1ed 47 io io;
gomihgy 6:bc960e6fd1ed 48 Ticker tk;
gomihgy 9:a7bd5ae66d6d 49 uint32_t weight_data;
gomihgy 9:a7bd5ae66d6d 50 float32_t weight = 0.0;
gomihgy 6:bc960e6fd1ed 51 uint32_t scale = 0;
gomihgy 6:bc960e6fd1ed 52 int update_counter = 0;
gomihgy 6:bc960e6fd1ed 53 int push_counter = 0;
gomihgy 6:bc960e6fd1ed 54 int led_mode = MODE_OFF;
gomihgy 6:bc960e6fd1ed 55 float led_brightness = BRIGHTNESS_MINVALUE;
akita11 4:edd6e262de9e 56
gomihgy 6:bc960e6fd1ed 57 // BLE
gomihgy 5:1730064b94bd 58 BLEDevice ble;
gomihgy 5:1730064b94bd 59 Gap::ConnectionParams_t connectionParams;
gomihgy 5:1730064b94bd 60
gomihgy 5:1730064b94bd 61 /* Complete list of 16-bit Service IDs */
gomihgy 6:bc960e6fd1ed 62 uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE};
gomihgy 5:1730064b94bd 63
gomihgy 5:1730064b94bd 64 /* Weight Scale Service */
gomihgy 6:bc960e6fd1ed 65 static const uint8_t UUID_HACARUS_WEIGHT_CHAR[] = {0x00, 0x00, 0x2A, 0x9D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'};
gomihgy 9:a7bd5ae66d6d 66 GattCharacteristic WeightMeasurement (UUID(UUID_HACARUS_WEIGHT_CHAR), (uint8_t *)&weight_data, sizeof(weight_data), sizeof(weight_data),
gomihgy 6:bc960e6fd1ed 67 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
gomihgy 6:bc960e6fd1ed 68 static const uint8_t UUID_HACARUS_SCALE_CHAR[] = {0x00, 0x00, 0x2A, 0x9E, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'};
gomihgy 6:bc960e6fd1ed 69 GattCharacteristic WeightScale (UUID(UUID_HACARUS_SCALE_CHAR), (uint8_t *)&scale, sizeof(scale), sizeof(scale),
gomihgy 6:bc960e6fd1ed 70 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
gomihgy 6:bc960e6fd1ed 71 GattCharacteristic *Chars[] = {&WeightMeasurement,&WeightScale};
gomihgy 6:bc960e6fd1ed 72 static const uint8_t UUID_HACARUS_WEIGHT_SERVICE[] = {0x00, 0x00, 0x18, 0x1D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'};
gomihgy 6:bc960e6fd1ed 73 GattService HWS = GattService(UUID(UUID_HACARUS_WEIGHT_SERVICE), Chars, sizeof(Chars) / sizeof(GattCharacteristic *));
gomihgy 5:1730064b94bd 74
gomihgy 5:1730064b94bd 75 /* Device Information Service */
gomihgy 5:1730064b94bd 76 GattCharacteristic ManuName(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)&MANUFACTURER_NAME_STRING, sizeof(MANUFACTURER_NAME_STRING) - 1, sizeof(MANUFACTURER_NAME_STRING) - 1,
gomihgy 5:1730064b94bd 77 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
gomihgy 5:1730064b94bd 78 GattCharacteristic ModelNum(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)&MODEL_NUMBER_STRING, sizeof(MODEL_NUMBER_STRING) - 1, sizeof(MODEL_NUMBER_STRING) - 1,
gomihgy 5:1730064b94bd 79 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
gomihgy 6:bc960e6fd1ed 80 GattCharacteristic SerialNum(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)&SERIAL_NUMBER_STRING, sizeof(SERIAL_NUMBER_STRING) - 1, sizeof(SERIAL_NUMBER_STRING) - 1,
gomihgy 5:1730064b94bd 81 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
gomihgy 5:1730064b94bd 82 GattCharacteristic FWVersion(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)&FIRMWARE_REVISION_STRING, sizeof(FIRMWARE_REVISION_STRING) - 1, sizeof(FIRMWARE_REVISION_STRING) - 1,
gomihgy 5:1730064b94bd 83 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
gomihgy 6:bc960e6fd1ed 84 GattCharacteristic *DISChars[] = {&ManuName, &ModelNum, &SerialNum, &FWVersion};
gomihgy 5:1730064b94bd 85 GattService DIS(GattService::UUID_DEVICE_INFORMATION_SERVICE , DISChars, sizeof(DISChars) / sizeof(GattCharacteristic *));
gomihgy 5:1730064b94bd 86
gomihgy 7:9ebf125e405f 87 /* to confirm joystick */
gomihgy 7:9ebf125e405f 88 bool check_joystick(){
gomihgy 6:bc960e6fd1ed 89 bool pushing = false;
gomihgy 7:9ebf125e405f 90 float value_x,value_y;
gomihgy 7:9ebf125e405f 91
gomihgy 7:9ebf125e405f 92 value_x = io.get_x();
gomihgy 7:9ebf125e405f 93 value_y = io.get_y();
gomihgy 6:bc960e6fd1ed 94
gomihgy 7:9ebf125e405f 95 if(PUSH_MINTHRESHOLD >= value_x || PUSH_MAXTHRESHOLD <= value_x){
gomihgy 7:9ebf125e405f 96 pushing = true;
gomihgy 7:9ebf125e405f 97 }
gomihgy 7:9ebf125e405f 98 else if(PUSH_MINTHRESHOLD >= value_y || PUSH_MAXTHRESHOLD <= value_y){
gomihgy 7:9ebf125e405f 99 pushing = true;
gomihgy 7:9ebf125e405f 100 }
gomihgy 7:9ebf125e405f 101
gomihgy 7:9ebf125e405f 102 if(pushing){
gomihgy 6:bc960e6fd1ed 103 if(++push_counter >= PUSH_COUNT){
gomihgy 6:bc960e6fd1ed 104 push_counter = 0;
gomihgy 7:9ebf125e405f 105 return true;
gomihgy 6:bc960e6fd1ed 106 }
gomihgy 6:bc960e6fd1ed 107 }else{
gomihgy 6:bc960e6fd1ed 108 push_counter = 0;
akita11 0:08c8325a21ec 109 }
gomihgy 6:bc960e6fd1ed 110
gomihgy 7:9ebf125e405f 111 return false;
gomihgy 7:9ebf125e405f 112 }
gomihgy 7:9ebf125e405f 113
gomihgy 9:a7bd5ae66d6d 114 uint32_t quick_ieee11073_from_float(float data)
gomihgy 9:a7bd5ae66d6d 115 {
gomihgy 9:a7bd5ae66d6d 116 uint8_t exponent = 0xFE; //exponent is -2
gomihgy 9:a7bd5ae66d6d 117 uint32_t mantissa = (uint32_t)(data*100);
gomihgy 9:a7bd5ae66d6d 118
gomihgy 9:a7bd5ae66d6d 119 return ( ((uint32_t)exponent) << 24) | mantissa;
gomihgy 9:a7bd5ae66d6d 120 }
gomihgy 9:a7bd5ae66d6d 121
gomihgy 7:9ebf125e405f 122 void ticker_callback()
gomihgy 7:9ebf125e405f 123 {
gomihgy 6:bc960e6fd1ed 124 switch(led_mode){
gomihgy 6:bc960e6fd1ed 125 case MODE_OFF:
gomihgy 8:32be16b1eaf2 126 if(check_joystick()){
gomihgy 6:bc960e6fd1ed 127 io.display_value = 0;
gomihgy 6:bc960e6fd1ed 128 led_mode = MODE_START;
gomihgy 6:bc960e6fd1ed 129 }
gomihgy 6:bc960e6fd1ed 130 break;
gomihgy 6:bc960e6fd1ed 131 case MODE_START:
gomihgy 6:bc960e6fd1ed 132 led_brightness += BRIGHTNESS_ADDVALUE;
gomihgy 6:bc960e6fd1ed 133 io.display(led_brightness);
gomihgy 6:bc960e6fd1ed 134 if(led_brightness >= BRIGHTNESS_MAXVALUE){
gomihgy 6:bc960e6fd1ed 135 led_mode = MODE_ON;
gomihgy 6:bc960e6fd1ed 136 ble.startAdvertising();
gomihgy 6:bc960e6fd1ed 137 }
gomihgy 6:bc960e6fd1ed 138 break;
gomihgy 6:bc960e6fd1ed 139 case MODE_ON:
gomihgy 8:32be16b1eaf2 140 if(!check_joystick()){
gomihgy 9:a7bd5ae66d6d 141 io.analog_pow(1); // turn analog power on
gomihgy 9:a7bd5ae66d6d 142 weight = io.get_weight() * 9999.0;
gomihgy 9:a7bd5ae66d6d 143 io.display_value = (uint16_t)weight; // dummy display
gomihgy 6:bc960e6fd1ed 144 if(++update_counter >= 5){
gomihgy 9:a7bd5ae66d6d 145 weight_data = quick_ieee11073_from_float(weight);
gomihgy 6:bc960e6fd1ed 146 ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(),
gomihgy 9:a7bd5ae66d6d 147 (uint8_t *)&weight_data,
gomihgy 9:a7bd5ae66d6d 148 sizeof(weight_data));
gomihgy 6:bc960e6fd1ed 149 update_counter = 0;
gomihgy 9:a7bd5ae66d6d 150 }
gomihgy 9:a7bd5ae66d6d 151 io.analog_pow(0); // turn analog power off
gomihgy 6:bc960e6fd1ed 152 }else{
gomihgy 8:32be16b1eaf2 153 led_mode = MODE_END;
gomihgy 6:bc960e6fd1ed 154 if(ble.getGapState().connected){
gomihgy 6:bc960e6fd1ed 155 ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
gomihgy 8:32be16b1eaf2 156 }else{
gomihgy 8:32be16b1eaf2 157 ble.stopAdvertising();
gomihgy 6:bc960e6fd1ed 158 }
gomihgy 6:bc960e6fd1ed 159 update_counter = 0;
gomihgy 6:bc960e6fd1ed 160 }
gomihgy 6:bc960e6fd1ed 161 break;
gomihgy 6:bc960e6fd1ed 162 case MODE_END:
gomihgy 6:bc960e6fd1ed 163 led_brightness -= BRIGHTNESS_ADDVALUE;
gomihgy 6:bc960e6fd1ed 164 io.display(led_brightness);
gomihgy 6:bc960e6fd1ed 165 if(led_brightness <= BRIGHTNESS_MINVALUE){
gomihgy 6:bc960e6fd1ed 166 led_mode = MODE_OFF;
gomihgy 6:bc960e6fd1ed 167 }
gomihgy 6:bc960e6fd1ed 168 break;
akita11 0:08c8325a21ec 169 }
akita11 0:08c8325a21ec 170 }
akita11 0:08c8325a21ec 171
gomihgy 5:1730064b94bd 172 /*
gomihgy 5:1730064b94bd 173 * BLE CallBacks
gomihgy 5:1730064b94bd 174 */
gomihgy 5:1730064b94bd 175 void BLEConnectionCallback(Gap::Handle_t handle, Gap::addr_type_t type, const Gap::address_t addr,const Gap::ConnectionParams_t *params)
gomihgy 5:1730064b94bd 176 {
gomihgy 5:1730064b94bd 177 ble.updateConnectionParams(handle, &connectionParams);
gomihgy 5:1730064b94bd 178 }
gomihgy 5:1730064b94bd 179
gomihgy 5:1730064b94bd 180 void BLEDisconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
gomihgy 5:1730064b94bd 181 {
gomihgy 6:bc960e6fd1ed 182 if(led_mode == MODE_ON){
gomihgy 6:bc960e6fd1ed 183 ble.startAdvertising();
gomihgy 5:1730064b94bd 184 }
gomihgy 5:1730064b94bd 185 }
gomihgy 5:1730064b94bd 186
gomihgy 5:1730064b94bd 187 void BleInitialize(void){
gomihgy 5:1730064b94bd 188
gomihgy 6:bc960e6fd1ed 189 uint8_t advertiseServiceID[16];
gomihgy 6:bc960e6fd1ed 190
gomihgy 5:1730064b94bd 191 // Initialize
gomihgy 5:1730064b94bd 192 ble.init();
gomihgy 5:1730064b94bd 193
gomihgy 5:1730064b94bd 194 // Event Set
gomihgy 5:1730064b94bd 195 ble.onConnection(&BLEConnectionCallback);
gomihgy 5:1730064b94bd 196 ble.onDisconnection(&BLEDisconnectionCallback);
gomihgy 5:1730064b94bd 197
gomihgy 5:1730064b94bd 198 ble.getPreferredConnectionParams(&connectionParams);
gomihgy 6:bc960e6fd1ed 199 connectionParams.maxConnectionInterval = INTERVAL_500MSEC;
gomihgy 6:bc960e6fd1ed 200 connectionParams.minConnectionInterval = INTERVAL_500MSEC;
gomihgy 6:bc960e6fd1ed 201 connectionParams.connectionSupervisionTimeout = CONNTIMEOUT_3000MSEC;
gomihgy 6:bc960e6fd1ed 202 connectionParams.slaveLatency = 2;
gomihgy 5:1730064b94bd 203 ble.setPreferredConnectionParams(&connectionParams);
gomihgy 5:1730064b94bd 204
gomihgy 5:1730064b94bd 205 ble.setTxPower(BLE_TXPOWER_4DBM);
gomihgy 6:bc960e6fd1ed 206
gomihgy 6:bc960e6fd1ed 207 for(int i=0; i<16; i++){
gomihgy 6:bc960e6fd1ed 208 advertiseServiceID[i] = UUID_HACARUS_WEIGHT_SERVICE[16 - 1 - i];
gomihgy 6:bc960e6fd1ed 209 }
gomihgy 6:bc960e6fd1ed 210
gomihgy 5:1730064b94bd 211 ble.accumulateAdvertisingPayload((GapAdvertisingData::Flags)(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED));
gomihgy 5:1730064b94bd 212 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
gomihgy 5:1730064b94bd 213 (const uint8_t *)DEVICE_NAME,
gomihgy 5:1730064b94bd 214 strlen(DEVICE_NAME));
gomihgy 5:1730064b94bd 215 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
gomihgy 6:bc960e6fd1ed 216 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
gomihgy 6:bc960e6fd1ed 217 (const uint8_t *)advertiseServiceID, sizeof(advertiseServiceID));
gomihgy 6:bc960e6fd1ed 218
gomihgy 6:bc960e6fd1ed 219 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(INTERVAL_500MSEC));
gomihgy 5:1730064b94bd 220 ble.setAdvertisingTimeout(ADV_TIMEOUT); /* 0 is disable the advertising timeout. */
gomihgy 5:1730064b94bd 221
gomihgy 6:bc960e6fd1ed 222 ble.addService(HWS);
gomihgy 5:1730064b94bd 223 ble.addService(DIS);
gomihgy 5:1730064b94bd 224 }
gomihgy 5:1730064b94bd 225
akita11 0:08c8325a21ec 226 int main()
akita11 0:08c8325a21ec 227 {
gomihgy 5:1730064b94bd 228 BleInitialize();
gomihgy 5:1730064b94bd 229
gomihgy 5:1730064b94bd 230 #ifdef UART_DEBUG
gomihgy 5:1730064b94bd 231 pc.baud(UART_BAUD_RATE);
gomihgy 5:1730064b94bd 232 #endif
gomihgy 5:1730064b94bd 233
gomihgy 6:bc960e6fd1ed 234 tk.attach_us(&ticker_callback, LED_INTERVAL_NSEC);
gomihgy 5:1730064b94bd 235
gomihgy 6:bc960e6fd1ed 236 for (;; ) {
gomihgy 6:bc960e6fd1ed 237 ble.waitForEvent();
akita11 0:08c8325a21ec 238 }
akita11 0:08c8325a21ec 239 }
gomihgy 6:bc960e6fd1ed 240
akita11 0:08c8325a21ec 241