An integrated code base for smart watch model using nrf51822. Used the efforts from Roger Clark, Goran Mahovlic, Nordic team SDKs and mbed repos on OLED. Programming: The watch prototype can be interfaced to Tiny Seeed BLE programmer (left of the Tiny Seeed BLE board). Connect SWDIO, SWD CLK, Vin and GND pins. For USB debugging also connect RX and TX pins. Used Roger's nice webpage to come up with the pin mapping and many other HW insights. http://www.rogerclark.net/arduino-on-the-id100hr-fitness-tracker/

Dependencies:   BLE_API SFE_MicroOLED_debugPrint mbed nRF51822

Fork of BLE_TemperatureAdvertising by xiao sun

Revision:
5:8c21994db8d2
Parent:
4:e5fa4c8838db
Child:
7:1b0597b7ead3
--- a/main.cpp	Fri Jul 31 09:11:43 2015 +0000
+++ b/main.cpp	Sat Dec 23 06:31:30 2017 +0000
@@ -15,87 +15,226 @@
  * limitations under the License.
  */
 
+/*
+Implementation of Dance Tracker by sandeep ponnuru. Copyright (c) 2016-2018 Sandeep Ponnuru,
+ All rights reserved.
+*/
 #include "mbed.h"
+#include "stdio.h"
+#include "SFE_MicroOLED.h"
+#include "KX22.h"
 #include "ble/BLE.h"
-#include "TMP_nrf51/TMP_nrf51.h"
+
+#define DEBUG 1 
+
+#define REV 1
+#define MINOR_REV 0
+const char DEVICE_NAME[] ="DncTrkSan";
 
-#define APP_SPECIFIC_ID_TEST 0xFEFE
+#define VIBRATE P0_7
+#define BUTTON P0_4
+//accelerometer
+#define KX_SCL  P0_16  
+#define KX_SDA  P0_14
+
+
+    #define UART_TX     P0_18
+    #define UART_RX     P0_17
+
+#define BUT_BLE 1
+#define BUT_UART 0 
+
+#define APP_SPECIFIC_ID_TEST 0xDA78
 
 #pragma pack(1)
-struct ApplicationData_t {
-    uint16_t applicationSpecificId;             /* An ID used to identify temperature value
-                                                   in the manufacture specific AD data field */
-    TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */
-};
+struct AppData_t {
+    int16_t  accX, accY, accZ;
+} appData;
 #pragma pack()
 
+//got these numbers from roger! tx in tx
+//vibrate motor
+DigitalOut vib(VIBRATE);
+//DigitalIn but(BUTTON, PullUp);
+InterruptIn but(BUTTON);
+int16_t accVal[3];
+bool butState = BUT_BLE;
+/*********OLED******************/
+//from roger :: works!!!
+
+//BLE
 BLE ble;
-TMP_nrf51 tempSensor;
-DigitalOut alivenessLED(LED1, 1);
-static bool triggerTempValueUpdate = false;
+
+/************KX ACC*************/
+
+/***********Serial*****/
+#if DEBUG
+extern Serial pc(UART_TX, UART_RX);
+#endif
+
+void start_ble();
+void stop_ble();
+void update_ble();
+
 
-void periodicCallback(void)
-{
-    /* Do blinky on LED1 while we're waiting for BLE events */
-    alivenessLED = !alivenessLED;
-    triggerTempValueUpdate = true;
-}
+//button call back function 
+void but_callback(){
 
-void accumulateApplicationData(ApplicationData_t &appData)
-{
-    appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
-    /* Read a new temperature value */
-    appData.tmpSensorValue = tempSensor.get();
+      
+    SPI* my_spi = new SPI(P0_29, NC, P0_30);
+    MicroOLED* oled = new MicroOLED(*my_spi, P0_1, P0_0, P0_2);
+    oled->init(0, 8000000);
+    oled->clear(PAGE);
+    oled->clear(ALL);
+    oled->printf("Acc values:%d,%d,%d\n",
+                               accVal[0],
+                               accVal[1],
+                               accVal[2]);
+    oled->display(); 
+    wait(2);
+    oled->clear(PAGE);
+    oled->clear(ALL);
+    oled->display(); 
+    delete(oled);
+    delete(my_spi);
+   
+    /* 
+    // handle BLE state 
+    butState = !butState;
+    if(butState==BUT_UART)
+     stop_ble();
+    else
+     start_ble(); 
+    */
 }
 
-void temperatureValueAdvertising(void)
-{
-    ApplicationData_t appData;
-    
-    accumulateApplicationData(appData);
-    //printf("Temp is %f\r\n", (float)appData.tmpSensorValue);
+void wakeup_event_cb() {
+    //nothing to do
+}
+void start_ble(){
+ 
+   ble.init();
+   ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+   ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED 
+                        | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */
+   ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING); /* Set appearance */
+   ble.gap().accumulateAdvertisingPayload(
+   GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, 
+   (uint8_t *)&appData , sizeof(AppData_t)); /* Set data as int16 array*/
+    /* Setup advertising parameters */
+   ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
+   ble.gap().setAdvertisingInterval(1000);
+    /* Start advertising */
+   ble.gap().startAdvertising();
     
-    /* Setup advertising payload */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); /* Set appearance */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); /* Set data */
-    /* Setup advertising parameters */
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(500);
-    /* Start advertising */
-    ble.gap().startAdvertising();
+    
 }
 
-void updateSensorValueInAdvPayload(void)
-{
-    ApplicationData_t appData;
+void stop_ble(){
+    ble.shutdown();
+    }
     
-    accumulateApplicationData(appData);
+void update_ble(){
     
-    /* Stop advertising first */
     ble.gap().stopAdvertising();
-    /* Only update temperature value field */
-    ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
-    /* Start advertising again */
-    ble.gap().startAdvertising();
-}
-
+          ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
+           (uint8_t *)&appData, sizeof(AppData_t));
+          ble.gap().startAdvertising();
+          
+    }    
+    
 int main(void)
 {
     Ticker ticker;
-    /* Enable trigger every 2 seconds */
-    ticker.attach(periodicCallback, 2);
-
-    ble.init();
-    /* Start temperature advertising */
-    temperatureValueAdvertising();
+    ticker.attach(wakeup_event_cb, 1);
+    
+    #if DEBUG
+    pc.printf("Dance Tracker v 1.2 welcomes you! \n\n \t\t c. Sandeep Ponnuru\n\n");
+    #endif
+    vib =1;
+    wait(0.25);
+    vib=0;
+    #if DEBUG
+    pc.printf("Vibration check passed\n");
     
+    pc.baud(9600);
+    pc.printf("Serial setup to 9600 bps!! \n");
+    #endif
+   // start BLE setup
+   appData.accX = 0xABAB;
+   appData.accY = 0xABAD;
+   appData.accZ = 0xABAC;
+   start_ble();
+   
+   
+     // OLED part of code
+    SPI* my_spi = new SPI(P0_29, NC, P0_30);
+    MicroOLED* oled = new MicroOLED(*my_spi, P0_1, P0_0, P0_2);
+    //delete(my_spi);
+    #if DEBUG
+    pc.printf("OLED setting up \n");
+    #endif
+    oled->init(0, 8000000);
+    #if DEBUG
+    pc.printf("OLED init \n");
+    #endif
+    oled->clear(PAGE);
+    #if DEBUG
+    pc.printf("OLED clear page \n");
+    #endif
+    oled->clear(ALL);
+    #if DEBUG
+    pc.printf("OLED clear all \n");
+    #endif
+    oled->printf("Dance Tracker version %d.%d",REV,MINOR_REV);
+    #if DEBUG
+    pc.printf("OLED print \n");
+    #endif
+    oled->display();
+    #if DEBUG
+    pc.printf("OLED set complete\n");
+    #endif
+    wait(2);   
+    
+    delete(oled);
+    delete(my_spi);
+    
+    but.fall(but_callback); 
+    int intl_cnt = 0;
     while (true) {
-        if (triggerTempValueUpdate) {
-            /* Update temperature value */
-            updateSensorValueInAdvPayload();
-            triggerTempValueUpdate = false;
-        }
-        ble.waitForEvent();
+        
+    
+            KX22* acc = new KX22(KX_SCL, KX_SDA);
+            //pc.printf("going to print vals now\n");
+            
+            
+            for(int k=0;k<3;k++)
+              accVal[k] = acc->getAccel(k);
+            delete(acc);        
+    intl_cnt++;
+    if(intl_cnt== 10)
+       { 
+    #if DEBUG
+         pc.printf("Acc values:%d,%d,%d\n",
+                               accVal[0],
+                               accVal[1],
+                               accVal[2]);
+             
+    #endif                  
+          //update the BLE payload here
+    if(butState== BUT_BLE)
+          {
+              appData.accX = accVal[0];
+          appData.accY = accVal[1];
+          appData.accZ = accVal[2];
+          update_ble();
+          }                
+         intl_cnt=0;
+         }                      
+    wait(0.1);//update of the acc variable is done at this speed
+   ble.waitForEvent();
     }
+       
+    
+    
 }