to cai

Dependencies:   BLE_API nRF51822

Fork of BLE_NODE_TEST by Yihui Xiong

Revision:
10:808a025c9b4f
Parent:
9:05f0b5a3a70a
--- a/main.cpp	Wed Oct 29 06:23:47 2014 +0000
+++ b/main.cpp	Wed Aug 24 07:01:59 2016 +0000
@@ -1,222 +1,184 @@
 /* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+   Copyright (c) 2006-2013 ARM Limited
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
 
 #include "mbed.h"
 #include "BLEDevice.h"
 #include "DFUService.h"
 #include "UARTService.h"
 #include "nrf_delay.h"
-#include "battery.h"
-
+//#include "battery.h"
 #define DEBUG   0
-
-
 #define LOG(...)
-#define BUTTON_DOWN     0
-#define LED_ON          0
-#define LED_OFF         1
 
-DigitalOut  blue(p18);
-DigitalOut  green(p17);
-InterruptIn button(p30);
-Battery     battery(p5);
+DigitalOut charge(p9);
+DigitalOut charge_led(p28);
+DigitalOut v3v3_led(p29);
+DigitalOut sleep_led(p30);
+AnalogIn   vol_3v3(p2);
+//AnalogIn   vol_5v(p0);
+AnalogIn   vol_bat(p1);
+AnalogIn   sense_current_5v(p4);
+AnalogIn   sense_current_bat(p3);
+
 
 BLEDevice  ble;
 UARTService *uartServicePtr;
 Ticker ticker;
 
-BusOut outputGrove(p3, p4);
-BusIn  inputGrove(p1, p2);
-BusIn  charge(p6, p7);
-
-volatile bool button_event = false;
-
 static const uint8_t SIZEOF_TX_RX_BUFFER = 32;
 uint8_t rxPayload[SIZEOF_TX_RX_BUFFER] = {0,};
 
-extern "C" void power_on();
-extern "C" void power_off();
-
-
-int button_detect();
-
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    LOG("Disconnected!\n");
-    LOG("Restarting the advertising process\n");
-    ble.startAdvertising();
+  LOG("Disconnected!\n");
+  LOG("Restarting the advertising process\n");
+  ble.startAdvertising();
 }
 
 void onDataWritten(const GattCharacteristicWriteCBParams *params)
 {
-    if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
-        uint16_t bytesRead = params->len;
-        LOG("received %u bytes\n\r", bytesRead);
-        if (bytesRead < sizeof(rxPayload)) {
-            memcpy(rxPayload, params->data, bytesRead);
-            rxPayload[bytesRead] = '\0';
-        }
-        
-        LOG("%s\n", (char *)rxPayload);
-        
+  if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
+    uint16_t bytesRead = params->len;
+    LOG("received %u bytes\n\r", bytesRead);
+    if (bytesRead < sizeof(rxPayload)) {
+      memcpy(rxPayload, params->data, bytesRead);
+      rxPayload[bytesRead] = '\0';
     }
+
+    LOG("%s\n", (char *)rxPayload);
+
+  }
 }
 
 void tick(void)
 {
-    green = !green;
-    
-    static uint8_t output = 0x01;
-    uint8_t input;
-    
-    outputGrove = output;
-    
-    button_event = true;
-    uartServicePtr->printf("battery:%3.2fV\n", battery.read());
-    
-    
-    input = inputGrove;
-    uartServicePtr->printf("o->i:%d->%d\n", output, input);
-    
-    output = 3 - output;
-    
-    uint8_t chargeStatus = charge;
-    uartServicePtr->printf("charge:%d\n", chargeStatus);
+  float v3v3;
+  float vbat;
+  float bat_current = 0;
+  float current_5v = 0;
+  /////////////bat read/////////////
+  
+  //vbat = vol_bat.read() * 19.8 * 1.13;     //for wristBand///////////
+  vbat = vol_bat.read() * 19.8*0.978 ;                  // for smartball//////////
+  uartServicePtr->printf("bat voltage is :%f\n", vbat);
+  //////////test 3v3//////////////
+  v3v3 = vol_3v3.read() * 19.8;
+  if (v3v3 > 3.14 && v3v3 < 3.47)
+  {
+    v3v3_led = 1;
+    uartServicePtr->printf("3v3 test ok:%f\n", v3v3);
+  }
+  else
+  {
+    v3v3_led = 0;
+    uartServicePtr->printf("3v3 test fail:%f\n", v3v3);
+  }
+  ///////////sleep test///////////
+  
+  for(int n=0;n<50;n++)
+  {
+  
+  bat_current = bat_current+sense_current_bat.read() * 0.1534;      
+  wait(0.01);
+  }
+  bat_current/=50;
+  bat_current*=0.977;          //////////Correction value for smartball//////////
+  //bat_current*=0.955;          //////////Correction value for wristband//////////
+  if (bat_current > 0.05 && bat_current < 0.15)
+  {
+    sleep_led = 1;
+  }
+  uartServicePtr->printf("bat pwred current:%f\n", bat_current);
+  //////////5v curren test//////////////////
+  
+  current_5v = sense_current_5v.read() * 337.5;
+ // current_5v*=0.91;                   //////////Correction value for wristband//////////
+   current_5v*=0.89;                   //////////Correction value for smartball//////////
+  if (current_5v > 50 && current_5v < 230)
+  {
+    charge_led = 1;
+    uartServicePtr->printf("charge test ok \n");
+  }
+  uartServicePtr->printf("5v charge current:%f\n", current_5v);
+  //////////push check///////////
+  if (bat_current < 0.03 && current_5v < 1)
+  {
+    charge_led = 0;
+    v3v3_led = 0;
+    sleep_led = 0;
+  }
+
 }
 
-void button_down(void)
-{
-    button_event = true;
-}
+
 
 int main(void)
 {
-    power_on();
-    blue = LED_ON;
-    green = LED_ON;
-    
-#if BUTTON_DOWN
-    button.mode(PullDown);
-    button.rise(button_down);
-#else
-    button.mode(PullUp);
-    button.fall(button_down);
-#endif
+  Serial pc(p8, p7);
+  pc.baud(115200);
+  char line_buffer[32] = {0};
+  const char *line_expected1 = "charge_1";
+  const char *line_expected2 = "charge_0";
+  charge = 0;
+  charge_led = 0;
+  v3v3_led = 0;
+  sleep_led = 0;
+  LOG("Initialising the nRF51822\n");
+  ble.init();
+  ble.onDisconnection(disconnectionCallback);
+  ble.onDataWritten(onDataWritten);
 
-    LOG("Initialising the nRF51822\n");
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    ble.onDataWritten(onDataWritten);
+  /* setup advertising */
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+  ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                  //(const uint8_t *)"WristBand Test", sizeof("WristBand Test"));
+                                    (const uint8_t *)"SmartBall Test", sizeof("SmartBall Test"));
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                   (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
 
-    /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     (const uint8_t *)"NODE TEST", sizeof("NODE TEST"));
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
-
-    //DFUService dfu(ble);
+  //DFUService dfu(ble);
 
-    UARTService uartService(ble);
-    uartService.retargetStdout();
-    uartServicePtr = &uartService;
-    
-    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
-    ble.startAdvertising();
-    
-    blue = LED_OFF;
-    
-    ticker.attach(tick, 3);
+  UARTService uartService(ble);
+  uartService.retargetStdout();
+  uartServicePtr = &uartService;
+
+  ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+  ble.startAdvertising();
+  ticker.attach(tick, 3);
 
-    while (true) {
-        if (button_event) {
-            int click;
-            
-            blue = LED_ON;
-            click = button_detect();
-            blue = LED_OFF;
-            LOG("click type: %d\n\r", click);
-            
-            button_event = false;
-            
-            if (1 == click) {
-                blue = !blue;
-            } else if (2 == click) {
-                //green = LED_ON;
-            } else if (-1 == click) {
-                ticker.detach();
-                green = LED_OFF;
-                blue = LED_OFF;
-                while (BUTTON_DOWN == button.read()) {
-                    
-                }
-                nrf_delay_us(3000);
-                
-                power_off();
-            } else {
-                continue;
-            }
+  while (1) {
+    //  pc.puts("TESTING\r\n");
+    pc.scanf("%s", line_buffer);
+    if (0 == strcmp(line_buffer, line_expected1)) {
+      // line_matched = true;
+      charge = 1;
+      memset(line_buffer, 32, 0); //line_buffer[32] = {0};
+      //  break;
 
-        } else {
-            ble.waitForEvent();
-        }
     }
+
+    if (0 == strcmp(line_buffer, line_expected2)) {
+      // line_matched = true;
+      charge = 0;
+      memset(line_buffer, 32, 0);
+      //  break;
+    }
+
+  }
+
 }
 
-int button_detect(void)
-{
-    int t = 0;
-    
-    while (1) {
-        if (button.read() != BUTTON_DOWN) {
-            if (t < 30) {
-                return 0;     // for anti shake
-            } else {
-                break;
-            }
-        }
-        
-        if (t > 30000) {        // More than 3 seconds
-            return -1;          // long click
-        }
-        
-        t++;
-        nrf_delay_us(100);
-    }
-    
-    if (t > 4000) {             // More than 0.4 seconds
-        return 1;               // single click
-    }
-    
-    while (true) {
-        if (button.read() == BUTTON_DOWN) {
-            nrf_delay_us(1000);
-            if (button.read() == BUTTON_DOWN) {
-                return 2;      // double click
-            }
-            
-            t += 10;
-        }
-        
-        if (t > 4000) {
-            return 1;          // The interval of double click should less than 0.4 seconds, so it's single click
-        }
-        
-        t++;
-        nrf_delay_us(100);
-    }
-}