Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Files at this revision

API Documentation at this revision

Comitter:
dbartolovic
Date:
Wed Aug 29 14:48:43 2018 +0000
Branch:
axis_normal
Parent:
14:65734075c8d3
Child:
16:482d8f81d6f3
Commit message:
Brought consumption to 500uA

Changed in this revision

aconno_ble/aconno_ble.cpp Show annotated file Show diff for this revision Revisions of this file
aconno_ble/aconno_ble.h Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
source/main.h Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.h Show annotated file Show diff for this revision Revisions of this file
--- a/aconno_ble/aconno_ble.cpp	Wed Aug 29 10:52:27 2018 +0000
+++ b/aconno_ble/aconno_ble.cpp	Wed Aug 29 14:48:43 2018 +0000
@@ -9,6 +9,10 @@
 #include "tasks.h"
 #include "lizzy_service.h"
 
+
+/* BLE event queue size */
+#define BLE_EVENT_COUNT         (4)
+
 init_lizzy_t init_lizzy = {
     .buzz = false,
     .leds = {false ,false, false},
@@ -18,6 +22,20 @@
 LizzyService *lizzy_service;
 
 
+static EventQueue eventQueue( BLE_EVENT_COUNT * EVENTS_EVENT_SIZE );
+
+
+void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context)
+{
+    BLE &ble = context->ble;
+    eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
+}
+
+EventQueue *getBLEEventQueue(void)
+{
+    return &eventQueue;
+}
+
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     (lizzy_service->get_ble())->gap().startAdvertising();
@@ -89,6 +107,9 @@
     //ble.gap().onConnection(onConnectionCallback);         
     ble.gattServer().onDataWritten(onDataWrittenCallback);
     
+    // Setup event handling. This is needed only with services.
+    ble.onEventsToProcess(scheduleBleEventsProcessing);
+    
     advertisementPacket.header = APPLICATION_ID;
     advertisementPacket.type = 0x00;
     advertisementPacket.gyroscope[0] = (int16_t)0;
--- a/aconno_ble/aconno_ble.h	Wed Aug 29 10:52:27 2018 +0000
+++ b/aconno_ble/aconno_ble.h	Wed Aug 29 14:48:43 2018 +0000
@@ -43,6 +43,7 @@
 extern LizzyService *lizzy_service;
 
 /* Function declarations */
+EventQueue *getBLEEventQueue(void);
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params);
 void updatePayload(BLE *ble, advertising_packet *advertisementPacket);
 
--- a/source/main.cpp	Wed Aug 29 10:52:27 2018 +0000
+++ b/source/main.cpp	Wed Aug 29 14:48:43 2018 +0000
@@ -18,6 +18,7 @@
 Thread updateServiceT;
 Thread updateBuzzLedsT;
 
+DigitalOut redica(PIN_LED_RED);
 DigitalOut AccVcc(ACC_POWER_PIN);
 InterruptIn INT1(INT_PIN1);
 //InterruptIn INT2(INT_PIN2);
@@ -42,9 +43,21 @@
     bar = 1;
 }
 
+extern int untilSleep;
+void int1Handler(void)
+{   
+    untilSleep = ACTIVE_PERIOD / MEASURE_INTERVAL_MS;
+    
+    redica = !redica;
+    
+    measureT.signal_set(START_MEAS);
+}
+
 int main(){
     Lis2dh12 mems(&i2c, memsI2CAddress);
     
+    redica = 1;
+    
     tasks_init();
     
     printf("Main program started.\r\n");
@@ -57,10 +70,10 @@
     AccVcc = 1;
     wait_ms(POWER_UP_DELAY_MS);
 
-    /*
+    
     INT1.rise(int1Handler);
     NRF_GPIO->PIN_CNF[INT_PIN1] &= 0xFFFFFFF3;  // NO PullUps
-    */
+    
     
     BLE &ble = BLE::Instance();
     ble.init(bleInitComplete);
@@ -87,6 +100,8 @@
     
     while(1){
         //disableI2C();
-        ble.waitForEvent();
+        //ble.waitForEvent();
+        getBLEEventQueue()->dispatch_forever();
+        Thread::wait(0xFFFFFFFF);
     }
 }
--- a/source/main.h	Wed Aug 29 10:52:27 2018 +0000
+++ b/source/main.h	Wed Aug 29 14:48:43 2018 +0000
@@ -19,6 +19,7 @@
 #define PRINT_ON_RTT        (0)
 #define DEBUG_LED           (0)
 #define NANO_MODULE         (0)
+#define NORMAL_AXIS         (1)
 //#define LSB_VALUE           (192)
 
 #define BLE_ACTIVE_TIME_S   (0.4)
@@ -52,4 +53,6 @@
 extern Thread updateServiceT;
 extern Thread updateBuzzLedsT;
 
+void disableI2C();
+
 #endif
--- a/tasks/tasks.cpp	Wed Aug 29 10:52:27 2018 +0000
+++ b/tasks/tasks.cpp	Wed Aug 29 14:48:43 2018 +0000
@@ -86,30 +86,51 @@
 }
 #endif
 
+int untilSleep = ACTIVE_PERIOD / MEASURE_INTERVAL_MS;
 void measureF(Lis2dh12 *mems)
 {
-    while (1)
+    while(1)
     {
-        advertisementPacket.header = APPLICATION_ID;
-        advertisementPacket.type = 0x00;
-        advertisementPacket.gyroscope[0] = (int16_t)0;
-        advertisementPacket.gyroscope[1] = (int16_t)0;
-        advertisementPacket.gyroscope[2] = (int16_t)0;
-        advertisementPacket.magnetometer[0] = (int16_t)0;
-        advertisementPacket.magnetometer[1] = (int16_t)0;
-        advertisementPacket.magnetometer[2] = (int16_t)0;
+        Thread::signal_wait(START_MEAS);
+        Thread::signal_clr(START_MEAS);
+        
+        BLE::Instance().startAdvertising();
         
-        
-        advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis();
-        advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis();
-        advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis();
-        
-        advertisementPacket.acc_lsb_value = LSB_VALUE;
-        
-        updateServiceT.signal_set(MEAS_DONE);
-        bleT.signal_set(MEAS_DONE);
-        
-        wait_ms(MEASURE_INTERVAL_MS);
+        while (1)
+        {
+            advertisementPacket.header = APPLICATION_ID;
+            advertisementPacket.type = 0x00;
+            advertisementPacket.gyroscope[0] = (int16_t)0;
+            advertisementPacket.gyroscope[1] = (int16_t)0;
+            advertisementPacket.gyroscope[2] = (int16_t)0;
+            advertisementPacket.magnetometer[0] = (int16_t)0;
+            advertisementPacket.magnetometer[1] = (int16_t)0;
+            advertisementPacket.magnetometer[2] = (int16_t)0;
+            
+#if NORMAL_AXIS == 1
+            advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis();
+            advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis();
+            advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis();
+#else
+            advertisementPacket.accelerometer[0] = -(int16_t)mems->readYAxis();
+            advertisementPacket.accelerometer[1] = (int16_t)mems->readXAxis();
+            advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis();
+#endif
+            
+            advertisementPacket.acc_lsb_value = LSB_VALUE;
+            
+            updateServiceT.signal_set(MEAS_DONE);
+            bleT.signal_set(MEAS_DONE);
+            
+            wait_ms(MEASURE_INTERVAL_MS);
+            untilSleep--;
+            
+            if(untilSleep <= 0)
+            {
+                BLE::Instance().stopAdvertising();
+                break;
+            }
+        }
     }
 }
 
--- a/tasks/tasks.h	Wed Aug 29 10:52:27 2018 +0000
+++ b/tasks/tasks.h	Wed Aug 29 14:48:43 2018 +0000
@@ -6,9 +6,11 @@
 #define TEST_LEDS_BUZZ          (0)
 
 #define MEASURE_INTERVAL_MS     (100)
+#define ACTIVE_PERIOD           (10000)
 
 #define MEAS_DONE               (0x00001000)
 #define UPDATE_BUZZ_LEDS        (0x00001001)
+#define START_MEAS              (0x00010000)
 
 void tasks_init();