Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)

Dependencies:   UniGraphic mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "vt100.h"
00003 #include "afLib.h"
00004 #include "af_mgr.h"
00005 #include "edge_mgr.h"
00006 #include "edge_time.h"
00007 #include "edge_reset_mgr.h"
00008 /**
00009  * afero poc1.5 25-Dec-2017 version
00010  * from this version, watch dog timer joined again.
00011  */
00012 
00013 vt100   *tty = 0 ;
00014 uint32_t wait_tolerance = 500 ; /* 5sec */
00015 uint32_t connect_tolerance = 60 ; /* after 60 trials, reboot */
00016 uint32_t wait_count = 0 ;
00017 uint32_t connect_trial_count = 0 ;
00018 
00019 /**
00020  * wait_connection
00021  * When gConnected == false, which is connection is lost.
00022  * Each 5sec check attribute ATTR_WIFI_STDY_STATE to see
00023  * if the connection has recovered.
00024  * Meantime even if connection is established communicated
00025  * data is invalid, so AF_SYSTEM_ASR_STATE is also
00026  * checked for gLinked ;
00027  * And in case connect_tolerance trials failed
00028  * try to reboot the system if it can improve the situation.
00029  */
00030 void wait_connection(void)
00031 {
00032     int result ;
00033     wait_count++ ;
00034     if (wait_count > wait_tolerance) {
00035         reset_watch_dog() ;
00036         if (gConnected == false) {
00037             result = afero->getAttribute(ATTR_WIFI_STDY_STATE) ;
00038             if (result != afSUCCESS) {
00039                 print_af_error(result) ;
00040             }
00041         }
00042         if (gLinked == false) {
00043             result = afero->getAttribute(AF_SYSTEM_ASR_STATE) ;
00044             if (result != afSUCCESS) {
00045                 print_af_error(result) ;
00046             }
00047         }
00048         connect_trial_count++ ;
00049         if (connect_trial_count > connect_tolerance) {
00050             reboot_edge() ;
00051         }
00052         wait_count = 0 ;
00053     }
00054 }
00055 
00056 void init_hardware(void)
00057 {
00058     int i ;
00059     int result ;
00060     
00061         reset_watch_dog() ;
00062     init_display() ;
00063         reset_watch_dog() ;
00064     init_aflib() ;
00065         reset_watch_dog() ;
00066     init_sensors() ;
00067         reset_watch_dog() ;
00068     init_timer() ;
00069 
00070     while(true) {
00071         reset_watch_dog() ;
00072         for (i = 0 ; i < 10 ; i++ ) {
00073             afero->loop() ;
00074             reset_watch_dog() ;
00075         }
00076         if ((gLinked == true)&&(gConnected == true)) {
00077             wait_count = 0 ;
00078             connect_trial_count = 0 ;
00079             if (afero->isIdle()) {
00080                 result = init_edge_attribute() ;
00081                 if (result == 0) {
00082                     break ;
00083                 }
00084             }
00085         } else { /* gLinked == false */
00086             wait_connection() ;
00087         }
00088         wait_ms(10) ;
00089     }
00090     do {
00091 //    while(!afero->isIdle()) {
00092         reset_watch_dog() ;
00093         for (i = 0 ; i < 10 ; i++ ) { 
00094             afero->loop() ;
00095             wait_ms(100) ; 
00096         }
00097     } while(!afero->isIdle()) ;
00098     edge_mgr_status = EDGE_MGR_RUNNING ;
00099 }
00100 
00101 // main() runs in its own thread in the OS
00102 int main() {
00103     static uint32_t count_robin = 0 ;
00104 
00105     tty = new vt100() ;
00106 //    tty->cls() ;
00107     printf("Afero test program (ver. %s) started\n", __DATE__) ;   
00108     printf("=== Reset Reason ===\n") ;
00109     print_reset_reason() ;
00110     printf("====================\n") ;
00111 
00112     init_hardware() ;
00113     
00114     edge_splash() ;
00115                     
00116     while (true) {
00117         count_robin++ ;
00118         afero->loop() ;
00119         if ((gLinked == true)&&(gConnected == true)) {
00120             wait_count = 0 ;
00121             connect_trial_count = 0 ;
00122             if (afero->isIdle()) {
00123                 edge_loop(count_robin) ;
00124             }
00125         } else { /* gLinked == false */
00126             wait_connection() ;      
00127         }
00128         wait_ms(10) ;
00129     }
00130 }