added

Dependencies:   mbed microbit BLE_API nRF51822

Revision:
10:053397a8dc40
Parent:
9:5f0732aa3008
Child:
11:6916c05fde52
--- a/main.cpp	Tue Sep 29 12:12:10 2015 +0000
+++ b/main.cpp	Thu Sep 06 22:56:26 2018 +0000
@@ -13,63 +13,135 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+ //////////////////////////////////////////////////////////
+///////////////////////NOTE ///////////////////////////////
+// This program uses BBC microbit as a NRF51822 board    //
+// with limited onboard functionality, mainly focused    //
+// BLE-uart/ADC/DIO/PWM capability, see pin maping below //
+///////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////
+        
 #include <string.h>
 #include "mbed.h"
 #include "BLE.h"
-
+#include "stdio.h"
 #include "UARTService.h"
 
-#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
-                               * it will have an impact on code-size and power consumption. */
 
+#define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host);                            
 #if NEED_CONSOLE_OUTPUT
-#define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
+#define BLEprintf(STR) { if (uart) uart->write(STR, strlen(STR)); }
 #else
-#define DEBUG(...) /* nothing */
-#endif /* #if NEED_CONSOLE_OUTPUT */
+#define BLEprintf(...) 
+#endif
+
+///////////////// pin table /////////////////////////
+/////////////////////////////////////////////////////
+// ubit.pin    nrf51822pin    functions      note  //
+/////////////////////////////////////////////////////
+//  P2           P0_1        ADC/PWM/DIO      2    //
+//  P1           P0_2        ADC/PWM/DIO      1    // 
+//  P0           P0_3        ADC/PWM/DIO      0    //
+//  P16          P_16            DIO               //
+//  P14          P0_25       SPI MIS/DIO           //
+//  P5           P_17        Button A/DI    pullup //
+//  P11          P_26        Button B/DI    pullup //
+
+/////////////////////////////////////////////////////
+///////////////// not mapped yet ////////////////////
+//  P20                      I2C SDA/DIO    pullup //
+//  P19                      I2C SCL/DIO    pullup //
+//  P15                      SPI MOS/DIO           //
+//  P13                      SPI SCK/DIO           //
+//  P16                                            //
+//  P8                                             //
+
+/////////////////////////////////////////////////////
+//      LED Matrix pins are not mapped             //
+/////////////////////////////////////////////////////
+
 
 BLEDevice  ble;
-DigitalOut led1(LED1);
 UARTService *uart;
+AnalogIn   ain(P0_3);
+DigitalOut   dout(P0_16);
+DigitalIn   din(P0_17);
+ 
+int dVal, dec, i,j;  
+float val,exBatt,f;
+char buffer[10];
+/////////////////// functions ///////////////////////
+
+
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    DEBUG("Disconnected!\n\r");
-    DEBUG("Restarting the advertising process\n\r");
     ble.startAdvertising();
 }
 
 void periodicCallback(void)
 {
-    led1 = !led1;
-    DEBUG("ping\r\n");
+    BLEprintf("\n");
+    BLEprintf("Battery Volt: ");
+    BLEprintf(buffer);
+   // BLEprintf("\r\n");
+    
 }
-
+///////////////////////////////////////////////
+///////////////// MAIN FUNC ///////////////////
+///////////////////////////////////////////////
+ 
 int main(void)
 {
-    led1 = 1;
     Ticker ticker;
-    ticker.attach(periodicCallback, 1);
-
-    DEBUG("Initialising the nRF51822\n\r");
+    ticker.attach(periodicCallback, 2);
     ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    
+    ble.onDisconnection(disconnectionCallback);    
     uart = new UARTService(ble);
 
-    /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+                                     (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
 
-    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+    ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. //
     ble.startAdvertising();
 
-    while (true) {
+    while (true) 
+    {
         ble.waitForEvent();
-    }
+        dout = din.read();
+        val = ain.read_u16();
+
+ 
+// 100k+22k voltage divider
+// 3.3 Vcc board voltage
+ 
+    exBatt = (122/22)*3.6*val/1023.0; // ext batt volt 
+    //exBatt =12.37;
+
+    dVal = exBatt;  
+    
+    
+    dec = (int)(exBatt * 100) % 100;
+    if(exBatt>1 && exBatt<10){i=0;}
+    if(exBatt>=10 && exBatt<100){i=1;}
+    j=i;
+    memset(buffer, 0, 10);
+    
+     while (dVal > 0)
+        {
+        buffer[i] = (dVal % 10) + '0';
+        dVal /= 10;
+        i--;
+         }
+    buffer[j+1] = '.';
+    buffer[j+2] = (dec / 10) + '0';
+    buffer[j+3] = (dec % 10) + '0';
+
+     }
+///////////// end of while(1) //////////////            
 }
+///////////// end of main  /////////////////