Kenji Arai / Mbed 2 deprecated BLE_Temp_Vdd_via_UART_TY

Dependencies:   BLE_API mbed nRF51822 nRF51_Vdd

Fork of BLE_Temp_Vdd_via_UART by Kenji Arai

Revision:
10:38852d243fb2
Parent:
9:5f0732aa3008
Child:
11:da2dc9a847a8
--- a/main.cpp	Tue Sep 29 12:12:10 2015 +0000
+++ b/main.cpp	Sun Jan 24 04:21:09 2016 +0000
@@ -14,62 +14,122 @@
  * limitations under the License.
  */
 
+/*
+ *      January   6th, 2016     Modified by Kenji Arai
+ *      January  24th, 2016
+ *                              http://www.page.sannet.ne.jp/kenjia/index.html
+ *                              http://mbed.org/users/kenjiArai/
+ *
+ *      Original:
+ *          1) BLE_UARTConsole
+ *             https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_UARTConsole/
+ *          2) BLE_HTM_by_InTempSensr_pub
+ *             https://developer.mbed.org/users/takafuminaka/code/BLE_HTM_by_InTempSensr_pub/
+ *             Tanks Takafumi Naka san!
+ *      Tested Controller Device:
+ *          iPhone6 UART application in nRF Toolbox
+ *          https://itunes.apple.com/jp/app/nrf-toolbox/id820906058?mt=8
+ *          Please check data on "Show log" screen
+ */
+
+//  Include ---------------------------------------------------------------------------------------
 #include <string.h>
 #include "mbed.h"
 #include "BLE.h"
+#include "UARTService.h"
+#include "nrf_soc.h"            // for internal Thermo sensoer
+#include "nRF51_Vdd.h"          // Read nRF51 Vdd voltage
 
-#include "UARTService.h"
+//  Definition ------------------------------------------------------------------------------------
 
-#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. */
+//  Object ----------------------------------------------------------------------------------------
+BLE         ble;
+UARTService *uart;
+nRF51_Vdd   vdd(3.6f, 2.6f);
+
+//  RAM -------------------------------------------------------------------------------------------
+static volatile bool triggerSensorPolling = false; 
 
-#if NEED_CONSOLE_OUTPUT
-#define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
-#else
-#define DEBUG(...) /* nothing */
-#endif /* #if NEED_CONSOLE_OUTPUT */
+//  ROM / Constant data ---------------------------------------------------------------------------
+const char *deviceName = "mbedBLE";
+
+//  Function prototypes ---------------------------------------------------------------------------
+void Update_Values(void);
 
-BLEDevice  ble;
-DigitalOut led1(LED1);
-UARTService *uart;
-
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    DEBUG("Disconnected!\n\r");
-    DEBUG("Restarting the advertising process\n\r");
+//-------------------------------------------------------------------------------------------------
+//  Control Program
+//-------------------------------------------------------------------------------------------------
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
+    if (uart){
+        uart->writeString("Disconnected!\r\n");
+        uart->writeString("Restarting the advertising process\r\n");
+    }
     ble.startAdvertising();
 }
 
-void periodicCallback(void)
-{
-    led1 = !led1;
-    DEBUG("ping\r\n");
+void periodicCallback(void){
+    triggerSensorPolling = true;
 }
 
-int main(void)
-{
-    led1 = 1;
+int main(void){
+    // Set priodic interrupt
     Ticker ticker;
-    ticker.attach(periodicCallback, 1);
-
-    DEBUG("Initialising the nRF51822\n\r");
+    ticker.attach(periodicCallback, 5);
+    // Opening screen
+    printf("\r\nInitialising the nRF51822\r\n");
+    printf("Temperature and CPU Vdd voltage via UART service\r\n");
+    // setup BLE
     ble.init();
+    ble.setDeviceName((const uint8_t *)deviceName);
     ble.onDisconnection(disconnectionCallback);
-    
+    // BLE Uart
     uart = new UARTService(ble);
-
-    /* setup advertising */
+    // 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 *)deviceName,
+                                     strlen(deviceName));
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
-
+                                     (const uint8_t *)UARTServiceUUID_reversed,
+                                      sizeof(UARTServiceUUID_reversed));
     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     ble.startAdvertising();
-
-    while (true) {
-        ble.waitForEvent();
+    while (true){
+        if (triggerSensorPolling){
+            triggerSensorPolling = false;
+            Update_Values();           
+        } else {
+            ble.waitForEvent();
+        }
     }
 }
+
+void Update_Values(void){
+    char buf[32];
+    int32_t p_temp;
+    float temperature;
+
+    // Update a temperature (inside nRF51822 chip)
+    sd_temp_get(&p_temp);
+    temperature = float(p_temp)/4.0f - 16.0f;
+    //           12      89
+    sprintf(buf,"T:%+4.1fdC", temperature);
+    if (uart){
+        uart->writeString(buf);
+    }
+    printf(buf);
+    // Update a Vdd voltage
+    //           01234     9
+    sprintf(buf,",Vdd:%3.2fV  ", vdd.read_real_value());
+    if (uart){
+        uart->writeString(buf);
+    }
+    printf(buf);
+    //           123456789 0
+    sprintf(buf,"Charge:%d%% \r\n", vdd.read());
+    if (uart){
+        uart->writeString(buf);
+    }
+    printf(buf);
+}