to cai
Dependencies: BLE_API nRF51822
Fork of BLE_NODE_TEST by
main.cpp
- Committer:
- wodenageshen
- Date:
- 2016-08-24
- Revision:
- 10:808a025c9b4f
- Parent:
- 9:05f0b5a3a70a
File content as of revision 10:808a025c9b4f:
/* 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. */ #include "mbed.h" #include "BLEDevice.h" #include "DFUService.h" #include "UARTService.h" #include "nrf_delay.h" //#include "battery.h" #define DEBUG 0 #define LOG(...) 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; static const uint8_t SIZEOF_TX_RX_BUFFER = 32; uint8_t rxPayload[SIZEOF_TX_RX_BUFFER] = {0,}; void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { 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); } } void tick(void) { 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; } } int main(void) { 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); /* 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)); //DFUService dfu(ble); UARTService uartService(ble); uartService.retargetStdout(); uartServicePtr = &uartService; ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ ble.startAdvertising(); ticker.attach(tick, 3); 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; } if (0 == strcmp(line_buffer, line_expected2)) { // line_matched = true; charge = 0; memset(line_buffer, 32, 0); // break; } } }