test

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Revision:
16:fc66feb533d0
Parent:
15:544e0ce3c1d9
Child:
17:994c1f037198
--- a/main.cpp	Thu Dec 17 04:12:30 2015 +0000
+++ b/main.cpp	Thu Dec 17 23:48:23 2015 +0000
@@ -15,16 +15,13 @@
 #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.005@rev0015" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
+#define FIRMWARE_REVISION_STRING        "v1.00.007@rev0016" // 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 WEIGHT_COEFFICIENT              (8.64)
 
-// JoyStick
-#define PUSH_MINTHRESHOLD       (0.2)
-#define PUSH_MAXTHRESHOLD       (0.6)
-#define PUSH_COUNT              (3)
-
+// Switch
 #define SW_THRESHOLD            (0.5)
 #define SW_SAMPLECOUNT          (3)
 
@@ -35,19 +32,17 @@
 #define MODE_END    (3) // LED ON -> OFF
 
 // Led
-#define LED_INTERVAL_NSEC   (100000)
+#define LED_INTERVAL_MSEC   (100)
 #define BRIGHTNESS_ADDVALUE (0.1)
 #define BRIGHTNESS_MINVALUE (0.0)
 #define BRIGHTNESS_MAXVALUE (1.0)
 
 // Properties
 io io;
-Ticker tk;
 uint32_t weight_data;
 float32_t weight = 0.0;
 uint32_t scale = 0;
 int update_counter = 0;
-int push_counter = 0;
 float sw_data[SW_SAMPLECOUNT];
 uint8_t sw_count = 0;
 int led_mode = MODE_OFF;
@@ -121,78 +116,25 @@
     return ( ((uint32_t)exponent) << 24) | mantissa;
 }
 
-void ticker_callback()
-{
+void SWInit(void){
+    // SW Initialize
+    for(int count = 0;count < SW_SAMPLECOUNT;count++){
+        sw_data[count] = 0;
+    }
+}
 
-//        pc.printf("!!!\r\n");
+void AppInit(void){
+    
+    SWInit();
+    io.analog_pow(1);
+      
 /*
-    while(1){
-    adc_dat[0] = 0x01; // config reg.
-//    adc_dat[1] = 0xc4 | (ch << 4); // single conv. & FS=+-2.048V & conv.
-    adc_dat[1] = 0xc4;
-    adc_dat[2] = 0x83; // 8SPS & disable comp.
-    pc.printf("adc_dat: %02x %02x\r\n", adc_dat[0], adc_dat[1]);
-//    i2c.write(I2C_ADDR_ADS1115, adc_dat, 3);
-    adc_dat[0] = 0x00;
-//    i2c.write(I2C_ADDR_ADS1115, adc_dat, 1);
-//    i2c.read(I2C_ADDR_ADS1115, adc_dat, 2);
-    uint8_t dh, dl;
-    dh = adc_dat[0];
-    dl = adc_dat[1];
-        pc.printf("%d %d\r\n", dh, dl);
-//    pc.printf("ADC result=%02x %02x\r\n", dh, dl);
+    if(NRF_UICR->XTALFREQ == 0xFFFFFF00){
+        io.display_value = 3232;
+    }else if(NRF_UICR->XTALFREQ == 0xFFFFFFFF){
+        io.display_value = 1616;
     }
 */
-
-    switch(led_mode){
-        case MODE_OFF:
-            if(check_joystick()){
-//              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){
-                update_counter = 0;
-                led_mode = MODE_ON;
-                ble.startAdvertising();
-            }
-        break;
-        case MODE_ON:
-            if(!check_joystick()){
-                led_mode = MODE_END;
-                if(ble.getGapState().connected){
-                    ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
-                }else{
-                    ble.stopAdvertising();
-                }
-                return;
-            }
-            //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;
-            }
-            
-        break;
-        case MODE_END:
-            led_brightness -= BRIGHTNESS_ADDVALUE;
-            io.display(led_brightness);
-            if(led_brightness <= BRIGHTNESS_MINVALUE){
-                led_mode = MODE_OFF;
-            }
-        break;
-    }
 }
 
 /*
@@ -271,32 +213,16 @@
 #endif
 
     BleInitialize();
-    
-    // SW Initialize
-    for(int count = 0;count < SW_SAMPLECOUNT;count++){
-        sw_data[count] = 0;
-    }
-    
-    
-/*
-    //tk.attach_us(&ticker_callback, LED_INTERVAL_NSEC);
-    if(NRF_UICR->XTALFREQ == 0xFFFFFF00){
-        io.display_value = 3232;
-    }else if(NRF_UICR->XTALFREQ == 0xFFFFFFFF){
-        io.display_value = 1616;
-    }
-*/
-    
-    io.analog_pow(1);
+    AppInit();
 
     for (;; ) {
         // 100msec waitForEvent
         t.reset();
-        t.start();
-        while(t.read_ms() < 100){
+        while(t.read_ms() < LED_INTERVAL_MSEC){
+            t.start();
             ble.waitForEvent();
+            t.stop();
         }
-        t.stop();
         
         // ticker処理
         switch(led_mode){
@@ -310,12 +236,14 @@
                 io.display(led_brightness);
                 if(led_brightness >= BRIGHTNESS_MAXVALUE){
                     update_counter = 0;
+                    io.calibrate_weight();
+                    SWInit();
+                    ble.startAdvertising();
                     led_mode = MODE_ON;
-                    ble.startAdvertising();
                 }
             break;
             case MODE_ON:
-                if(!check_joystick()){
+                if(check_joystick()){
                     led_mode = MODE_END;
                     if(ble.getGapState().connected){
                         ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
@@ -323,7 +251,10 @@
                         ble.stopAdvertising();
                     }
                 }else{
-                    weight = io.get_weight() * 9999.0;
+                    weight = io.get_weight() / WEIGHT_COEFFICIENT;
+                    if(weight < 0){
+                        weight = 0;
+                    }
                     io.display_value = (uint16_t)weight; // dummy display
                     if(++update_counter >= 5){
                         weight_data = quick_ieee11073_from_float(weight);
@@ -338,6 +269,7 @@
                 led_brightness -= BRIGHTNESS_ADDVALUE;
                 io.display(led_brightness);
                 if(led_brightness <= BRIGHTNESS_MINVALUE){
+                    SWInit();
                     led_mode = MODE_OFF;
                 }
             break;