test

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Revision:
14:81347f8f0bda
Parent:
13:6b3cce9e588b
Child:
15:544e0ce3c1d9
--- a/main.cpp	Wed Dec 09 04:51:26 2015 +0000
+++ b/main.cpp	Thu Dec 17 04:02:23 2015 +0000
@@ -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.003@rev0013" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
+#define FIRMWARE_REVISION_STRING        "v1.00.004@rev0014" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
 
 // Weight Scale Service (Original)
 #define UUID_WEIGHT_SCALE_SERVICE       (0x181D)
@@ -23,7 +23,10 @@
 // JoyStick
 #define PUSH_MINTHRESHOLD       (0.2)
 #define PUSH_MAXTHRESHOLD       (0.6)
-#define PUSH_COUNT              (15)
+#define PUSH_COUNT              (3)
+
+#define SW_THRESHOLD            (0.5)
+#define SW_SAMPLECOUNT          (3)
 
 // Mode
 #define MODE_OFF    (0) // LED OFF
@@ -45,9 +48,13 @@
 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;
 float led_brightness = BRIGHTNESS_MINVALUE;
 
+Timer t;
+
 // UART for debug
 
 #ifdef UART_DEBUG
@@ -90,29 +97,20 @@
 
 /* to confirm joystick */
 bool check_joystick(){
-    bool pushing = false;
-    float value_x,value_y;
-    
-//    value_x = io.get_x();
-//    value_y = io.get_y();
+    float sum_data = 0;
     
-    if(PUSH_MINTHRESHOLD >= value_x || PUSH_MAXTHRESHOLD <= value_x){
-        pushing = true;
-    }
-    else if(PUSH_MINTHRESHOLD >= value_y || PUSH_MAXTHRESHOLD <= value_y){
-        pushing = true;
+    // ジョイスティックXのADC値(0〜1.0換算)が0.5以上ならON、0.5未満ならOFF
+    sw_data[sw_count] = io.get_x();
+    
+    if(++sw_count >= SW_SAMPLECOUNT){
+        sw_count = 0;
     }
     
-    if(pushing){
-        if(++push_counter >= PUSH_COUNT){
-            push_counter = 0;
-            return true;
-        }
-    }else{
-        push_counter = 0;
+    for(int count = 0;count < SW_SAMPLECOUNT;count++){
+        sum_data += sw_data[count];
     }
     
-    return false;
+    return ((sum_data / SW_SAMPLECOUNT) >= SW_THRESHOLD);
 }
 
 uint32_t quick_ieee11073_from_float(float data)
@@ -148,44 +146,44 @@
 
     switch(led_mode){
         case MODE_OFF:
-            //if(check_joystick()){
-//                io.display_value = 0;
+            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()){
-                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;
-                }
-                /*
-            }else{
+            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;
@@ -274,33 +272,75 @@
 
     BleInitialize();
     
-    tk.attach_us(&ticker_callback, LED_INTERVAL_NSEC);
+    // SW Initialize
+    for(int count = 0;count < SW_SAMPLECOUNT;count++){
+        sw_data[count] = 0;
+    }
+    
+    
 /*
-    // test for Full Power Consumption
-    io.display(1);
-    io.analog_pow(1);
-    io.display_value = 8888;    
-*/
-    //io.display(0.5); io.display_value = 1111;    
-    //io.analog_pow(1);
-    /*
-    io.display(0.5); 
+    //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);
-    */
-    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();
+        // 100msec waitForEvent
+        t.reset();
+        t.start();
+        while(t.read_ms() < 100){
+            ble.waitForEvent();
+        }
+        
+        // ticker処理
+        switch(led_mode){
+            case MODE_OFF:
+                if(check_joystick()){
+                    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();
+                    }
+                }else{
+                    weight = io.get_weight() * 9999.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;
+                    }
+                }
+            break;
+            case MODE_END:
+                led_brightness -= BRIGHTNESS_ADDVALUE;
+                io.display(led_brightness);
+                if(led_brightness <= BRIGHTNESS_MINVALUE){
+                    led_mode = MODE_OFF;
+                }
+            break;
+        }
     }
 }