test

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Revision:
12:880400fa5c44
Parent:
11:be439189e3f7
Child:
13:6b3cce9e588b
--- a/main.cpp	Sat Dec 05 01:14:00 2015 +0000
+++ b/main.cpp	Wed Dec 09 04:45:57 2015 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #include "io.h"
-#include "BLEDevice.h"
+#include "BLE.h"
 #include "common.h"
 
 // BLE
@@ -15,7 +15,7 @@
 #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.002@rev0009" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
+#define FIRMWARE_REVISION_STRING        "v1.00.003@rev0012" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
 
 // Weight Scale Service (Original)
 #define UUID_WEIGHT_SCALE_SERVICE       (0x181D)
@@ -59,7 +59,7 @@
 #endif
 
 // BLE 
-BLEDevice ble;
+BLE ble;
 Gap::ConnectionParams_t connectionParams;
 
 /* Complete list of 16-bit Service IDs */
@@ -148,32 +148,34 @@
 
     switch(led_mode){
         case MODE_OFF:
-            if(check_joystick()){
+            //if(check_joystick()){
 //                io.display_value = 0;
                 led_mode = MODE_START;
-            }
+            //}
         break;
         case MODE_START:
             led_brightness += BRIGHTNESS_ADDVALUE;
-//            io.display(led_brightness);
+            io.display(led_brightness);
             if(led_brightness >= BRIGHTNESS_MAXVALUE){
                 led_mode = MODE_ON;
                 ble.startAdvertising();
             }
         break;
         case MODE_ON:
-            if(!check_joystick()){
-//                io.analog_pow(1); // turn analog power on
-//                weight = io.get_weight() * 9999.0;
-//                io.display_value = (uint16_t)weight; // dummy display
+            //if(!check_joystick()){
+                io.analog_pow(1); // turn analog power on
+                wait_ms(1);
+                weight = io.get_weight() * 9999.0;
+                io.display_value = (uint16_t)weight; // dummy display
+                io.analog_pow(0); // turn analog power off 
                 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;
-                }  
-//                io.analog_pow(0); // turn analog power off 
+                }
+                /*
             }else{
                 led_mode = MODE_END;
                 if(ble.getGapState().connected){
@@ -183,10 +185,11 @@
                 }
                 update_counter = 0;
             }
+            */
         break;
         case MODE_END:
             led_brightness -= BRIGHTNESS_ADDVALUE;
-//            io.display(led_brightness);
+            io.display(led_brightness);
             if(led_brightness <= BRIGHTNESS_MINVALUE){
                 led_mode = MODE_OFF;
             }
@@ -197,18 +200,27 @@
 /*
  * BLE CallBacks
  */
-void BLEConnectionCallback(Gap::Handle_t handle, Gap::addr_type_t type, const Gap::address_t addr,const Gap::ConnectionParams_t *params)
+void BLEConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
-    ble.updateConnectionParams(handle, &connectionParams);                       
+    ble.updateConnectionParams(params->handle, params->connectionParams);                       
 }
 
-void BLEDisconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+void BLEDisconnectionCallback(const Gap::DisconnectionCallbackParams_t  *params)
 {
     if(led_mode == MODE_ON){
         ble.startAdvertising();
     }
 }
 
+void BLERadioNotificationCallback(bool radio_active)
+{
+    if (radio_active == false)
+    {
+        if(led_mode == MODE_ON){
+        }
+    }
+}
+
 void BleInitialize(void){
     
     uint8_t advertiseServiceID[16];
@@ -219,6 +231,7 @@
     // Event Set
     ble.onConnection(&BLEConnectionCallback);
     ble.onDisconnection(&BLEDisconnectionCallback);
+    ble.onRadioNotification(&BLERadioNotificationCallback);
     
     ble.getPreferredConnectionParams(&connectionParams);
     connectionParams.maxConnectionInterval = INTERVAL_500MSEC;
@@ -241,7 +254,7 @@
     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.setAdvertisingInterval(INTERVAL_500MSEC);
     ble.setAdvertisingTimeout(ADV_TIMEOUT); /* 0 is disable the advertising timeout. */
     
     ble.addService(HWS);
@@ -268,12 +281,24 @@
     io.analog_pow(1);
     io.display_value = 8888;    
 */
-    io.display(0.5); io.display_value = 1111;    
+    //io.display(0.5); io.display_value = 1111;    
+    //io.analog_pow(1);
+    /*
+    io.display(0.5); 
+    if(NRF_UICR->XTALFREQ == 0xFFFFFF00){
+        io.display_value = 3232;
+    }else if(NRF_UICR->XTALFREQ == 0xFFFFFFFF){
+        io.display_value = 1616;
+    }
+    
     io.analog_pow(1);
-    while(1){
-        DEBUG("%f %f %f\r\n", io._get_adc(0), io._get_adc(1), io._get_adc(2));
-        io.display_value = io._get_adc_raw(0) % 10000;
-    }
+    */
+    ble.startAdvertising();
+    
+    //while(1){
+    //    DEBUG("%f %f %f\r\n", io._get_adc(0), io._get_adc(1), io._get_adc(2));
+    //    io.display_value = io._get_adc_raw(0) % 10000;
+    //}
     for (;; ) {
         ble.waitForEvent();
     }