Shahariar Hossain / Mbed 2 deprecated uBit_BLE_UART_Voltmeter

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_UARTConsole by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  //////////////////////////////////////////////////////////
00017 ///////////////////////NOTE ///////////////////////////////
00018 // This program uses BBC microbit as a NRF51822 board    //
00019 // with limited onboard functionality, mainly focused    //
00020 // BLE-uart/ADC/DIO/PWM capability, see pin maping below //
00021 ///////////////////////////////////////////////////////////
00022 ///////////////////////////////////////////////////////////
00023         
00024 #include <string.h>
00025 #include "mbed.h"
00026 #include "BLE.h"
00027 #include "stdio.h"
00028 #include "UARTService.h"
00029 
00030 
00031 #define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host);                            
00032 #if NEED_CONSOLE_OUTPUT
00033 #define BLEprintf(STR) { if (uart) uart->write(STR, strlen(STR)); }
00034 #else
00035 #define BLEprintf(...) 
00036 #endif
00037 
00038 ///////////////// pin table /////////////////////////
00039 /////////////////////////////////////////////////////
00040 // ubit.pin    nrf51822pin    functions      note  //
00041 /////////////////////////////////////////////////////
00042 //  P2           P0_1        ADC/PWM/DIO      2    //
00043 //  P1           P0_2        ADC/PWM/DIO      1    // 
00044 //  P0           P0_3        ADC/PWM/DIO      0    //
00045 //  P16          P_16            DIO               //
00046 //  P14          P0_25       SPI MIS/DIO           //
00047 //  P5           P_17        Button A/DI    pullup //
00048 //  P11          P_26        Button B/DI    pullup //
00049 
00050 /////////////////////////////////////////////////////
00051 ///////////////// not mapped yet ////////////////////
00052 //  P20                      I2C SDA/DIO    pullup //
00053 //  P19                      I2C SCL/DIO    pullup //
00054 //  P15                      SPI MOS/DIO           //
00055 //  P13                      SPI SCK/DIO           //
00056 //  P16                                            //
00057 //  P8                                             //
00058 
00059 /////////////////////////////////////////////////////
00060 //      LED Matrix pins are not mapped             //
00061 /////////////////////////////////////////////////////
00062 
00063 
00064 BLEDevice  ble;
00065 UARTService *uart;
00066 AnalogIn   ain(P0_3);
00067 DigitalOut   dout(P0_16);
00068 DigitalIn   din(P0_17);
00069  
00070 int dVal, dec, i,j;  
00071 float val,exBatt,f;
00072 char buffer[10];
00073 /////////////////// functions ///////////////////////
00074 
00075 
00076 
00077 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00078 {
00079     ble.startAdvertising();
00080 }
00081 
00082 void periodicCallback(void)
00083 {
00084     BLEprintf("\n");
00085     BLEprintf("Battery Volt: ");
00086     BLEprintf(buffer);
00087    // BLEprintf("\r\n");
00088     
00089 }
00090 ///////////////////////////////////////////////
00091 ///////////////// MAIN FUNC ///////////////////
00092 ///////////////////////////////////////////////
00093  
00094 int main(void)
00095 {
00096     Ticker ticker;
00097     ticker.attach(periodicCallback, 2);
00098     ble.init();
00099     ble.onDisconnection(disconnectionCallback);    
00100     uart = new UARTService(ble);
00101 
00102     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00103     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00104     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00105                                      (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1);
00106     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00107                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00108 
00109     ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. //
00110     ble.startAdvertising();
00111 
00112     while (true) 
00113     {
00114         ble.waitForEvent();
00115         dout = din.read();
00116         val = ain.read_u16();
00117 
00118  
00119 // 100k+22k voltage divider
00120 // 3.3 Vcc board voltage
00121  
00122     exBatt = (122/22)*3.6*val/1023.0; // ext batt volt 
00123     //exBatt =12.37;
00124 
00125     dVal = exBatt;  
00126     
00127     
00128     dec = (int)(exBatt * 100) % 100;
00129     if(exBatt>1 && exBatt<10){i=0;}
00130     if(exBatt>=10 && exBatt<100){i=1;}
00131     j=i;
00132     memset(buffer, 0, 10);
00133     
00134      while (dVal > 0)
00135         {
00136         buffer[i] = (dVal % 10) + '0';
00137         dVal /= 10;
00138         i--;
00139          }
00140     buffer[j+1] = '.';
00141     buffer[j+2] = (dec / 10) + '0';
00142     buffer[j+3] = (dec % 10) + '0';
00143 
00144      }
00145 ///////////// end of while(1) //////////////            
00146 }
00147 ///////////// end of main  /////////////////