Marumbo Sichinga / Mbed 2 deprecated SmartWatchTopicDisplay

Dependencies:   mbed

Revision:
3:ab4d9bcf8dd1
Parent:
2:a128db827b61
Child:
4:63bf2f3d54a5
--- a/main.cpp	Fri Jul 09 20:27:22 2021 +0000
+++ b/main.cpp	Sat Jul 10 19:45:53 2021 +0000
@@ -1,3 +1,5 @@
+    
+
 #include "mbed.h"
 #include "C12832.h"
 #include "Sht31.h"
@@ -6,35 +8,35 @@
 #include "mbed_events.h"
 #include "LoRaWANInterface.h"
 #include "SX1276_LoRaRadio.h"
-
+ 
 // Device credentials, register device as OTAA in The Things Network and copy credentials here
 static uint8_t DEV_EUI[] = { 0x26, 0x10, 0x20, 0x20, 0x11, 0x11, 0x20, 0x20 };
 static uint8_t APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x74, 0x72 };
 static uint8_t APP_KEY[] = { 0x75, 0x57, 0x56, 0x65, 0xB1, 0x8A, 0x17, 0x52, 0x1B, 0x2D, 0x45, 0xD9, 0xFC, 0x90, 0x9E, 0x05 };
-
+ 
 // The port we're sending and receiving on
 #define MBED_CONF_LORA_APP_PORT     15
-
+ 
 // Peripherals (LoRa radio, temperature sensor and button)
 SX1276_LoRaRadio radio(D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9, NC, NC, NC, NC, A4, NC, NC);
 static EventQueue ev_queue;
-
+ 
 // Constructing Mbed LoRaWANInterface and passing it down the radio object.
 static LoRaWANInterface lorawan(radio);
-
+ 
 // Application specific callbacks
 static lorawan_app_callbacks_t callbacks;
-
+ 
 // LoRaWAN stack event handler
 static void lora_event_handler(lorawan_event_t event);
-
+ 
 static void send_message(int topicId, long int duration, string group) {
     uint8_t tx_buffer[50] = { 0 };
     
-    char *deviceId = "My Device";
+    string deviceId = "My Device";
     long int timestamp = 1234567892;
     int value = 2;
-      sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\",\"TopicId\":%d,\"Group\":%s}}", deviceId, topicId, group.c_str());
+      sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\",\"TopicId\":%d,\"Group\":%s}}", deviceId.c_str(), topicId, group.c_str());
     // Sending strings over LoRaWAN is not recommended
    // sprintf((char*) tx_buffer, "{\"topicId\":%d, \"duration\":%ld,\"group\":\"%s\"}", topicId, duration, group.c_str());
     int packet_len = strlen((char*) tx_buffer);
@@ -51,7 +53,7 @@
      callbacks.events = mbed::callback(lora_event_handler);
     lorawan.add_app_callbacks(&callbacks);
 }
-
+ 
 int initialize_lora() {
     if (DEV_EUI[0] == 0x0 && DEV_EUI[1] == 0x0 && DEV_EUI[2] == 0x0 && DEV_EUI[3] == 0x0 && DEV_EUI[4] == 0x0 && DEV_EUI[5] == 0x0 && DEV_EUI[6] == 0x0 && DEV_EUI[7] == 0x0) {
         printf("Set your LoRaWAN credentials first!\n");
@@ -79,37 +81,38 @@
     connect_params.connection_u.otaa.app_eui = APP_EUI;
     connect_params.connection_u.otaa.app_key = APP_KEY;
     connect_params.connection_u.otaa.nb_trials = 3;
-
+ 
     lorawan_status_t retcode = lorawan.connect(connect_params);
-
+ 
     if (retcode == LORAWAN_STATUS_OK ||
         retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
     } else {
         printf("Connection error, code = %d\n", retcode);
         return -1;
     }
-
+ 
     printf("Connection - In Progress ...\r\n");
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
     return 0;
 }
-
-
-
+ 
+ 
+ 
 //declare lcd
 C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); 
-
+ 
 //humidity and temperature sensor
 Sht31 sht31(I2C_SDA, I2C_SCL);
-
+ 
 //declare components
 //InterruptIn pushButton(BUTTON1); //p11
 InterruptIn pushButton(p11);
 DigitalOut whiteLed(p8);
 DigitalOut redLed(p9);
 DigitalOut blueLed(p10);
-
+DigitalOut led2(LED2);
+ 
 void State1();
 void State2();
 void Mode1();
@@ -120,12 +123,12 @@
 void StopButtonTimer();
 void DurationCheck();
 void NextTopic();
-
+ 
 //topics and content
 string content[5][2]= {{"Topic 1","Content 1"},{"Topic 2","Content 2"},{"Topic 3","Content 3"},{"Topic 4","Content 4"},{"Topic 5","Content 5"}};
 string companyName = "Godfrey&SonsTech";
+ 
 
-Timer t;
 int direction = 0; // Forward is 0 False and Reverse 1true
 int current_topic = 0;;
 time_t loraTimerStart;
@@ -134,13 +137,14 @@
 time_t buttonTimerStop;
 int loraDuration = 0;
 int buttonDuration = 0;
-
-
-
+Ticker flipper;
+ 
+ 
+ 
 volatile bool running = true;
-
-
-
+ 
+ 
+ 
 void PrintTemperatureAndTime() {
         
         time_t seconds = time(NULL);
@@ -153,7 +157,6 @@
         lcd.locate(3, 13);
         lcd.printf("Date/Time: %s", ctime(&seconds));
         // turn on LED if the temperature is above 25 degrees
-        wait(0.5f);
     }
     
 void WelcomeMessage(string companyName){
@@ -176,7 +179,7 @@
     lcd.printf(lcdPrint.c_str()); // Write text into LCD buffer
     // Clear LCD
 }
-
+ 
 void StartButtonTimer(){
     printf("Button Timer start \n");
     buttonTimerStart = time(NULL);
@@ -185,24 +188,24 @@
     
   
 }
-
+ 
 void StopButtonTimer(){
     printf("Button timer stop \n");
     loraTimerStop =time(NULL); 
     buttonTimerStop = time(NULL);
-    printf("Button timer stop: %d \n",buttonTimerStop);
+    printf("Button timer stop: %ld \n",buttonTimerStop);
     wait(0.5);
     
     loraDuration = loraTimerStop - loraTimerStart;
     
     buttonDuration = buttonTimerStop - buttonTimerStart;
-    printf("Duration Lora: %d \n", loraDuration); 
-    printf("Duration Button: %d \n", buttonDuration); 
+    printf("Duration Lora: %ld \n", loraDuration); 
+    printf("Duration Button: %ld \n", buttonDuration); 
     
     DurationCheck();
    
 }
-
+ 
 void NextTopic(){
     if(direction == 0){
         if(current_topic == 4){
@@ -220,7 +223,7 @@
     
     Mode2();
 }
-
+ 
 void DurationCheck(){
     printf("duration check details: \n");
     printf("Duration Lora: %d \n", loraDuration); 
@@ -251,15 +254,15 @@
     }
     
 }
-
-
+ 
+ 
 void State1(){
     current_topic = 0;
     PrintTemperatureAndTime();
     wait(3);
     State2();
 }
-
+ 
 void State2(){
     printf("state 2 .. \n");
     
@@ -272,31 +275,22 @@
  
     State1();
 }
-
+ 
 void PrintContent(){
-loraTimerStart = 0;
- loraTimerStop= 0;
- buttonTimerStart = 0;
-buttonTimerStop = 0;
-    
     loraTimerStart = time(NULL);
+    Display(content[current_topic][1]);
+
+    pushButton.fall(callback(&StopButtonTimer)); 
+    pushButton.rise(callback(&StartButtonTimer));
     
-    while(1){
-        Display(content[current_topic][1]);
-        wait(0.5);
-        pushButton.mode(PullUp);
-        pushButton.fall(callback(&StopButtonTimer)); 
-        pushButton.rise(callback(&StartButtonTimer));
-    }
 }
-
+ 
 void CheckDirection(){
     
     if(direction == 0) //O is a forward direction
     {
         redLed = 0;
         blueLed = 1;
-        wait(0.5);
         PrintContent();
     }
     
@@ -307,55 +301,44 @@
         PrintContent();
     }
 }
-
-void blink(DigitalOut *led) {
-    while (running) {
-        *led = !*led;
-        wait(1);
-        pushButton.fall(callback(&Mode2));
-        State1();
-    }
+ 
+void BlinkWhiteLed() {
+    
+    whiteLed = !whiteLed;
+    led2 = !led2;
+    
+    
 }
-
+ 
 void Mode1(){
-    while (1){
-        redLed = 0;
-        blueLed = 0;
-        direction = 0;
-        current_topic = 0;
-         printf("Mode 1... \n");
+    
+        printf("Mode 1... \n");
         printf("current topic %s \n", content[current_topic][0].c_str());
-        blink(&whiteLed);
-        wait(0.5);
-    }
+       
+        flipper.attach(&BlinkWhiteLed, 1.0); 
+        pushButton.fall(callback(&Mode2));
+        
+        State1();
 }
-
+ 
 void Mode2(){
-    while (1){
+  
     printf("Mode 2... \n");
     printf("current topic %s \n", content[current_topic][0].c_str());
     lcd.cls();
     CheckDirection();
-    wait(0.5);
-    
     Mode1();
     
-    }
 }
+ 
+int main() {
 
-int main() {
-    while (1) {
-        
         Display("Initializing ..............");
         initialize_lora();
-       
-     
         WelcomeMessage(companyName);
-      
-        wait_ms(500);
-    }
+  
 }
-
+ 
 // Event handler
 static void lora_event_handler(lorawan_event_t event) {
     switch (event) {