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

Dependencies:   UniGraphic mbed vt100

18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。

Committer:
Rhyme
Date:
Fri Apr 13 04:19:23 2018 +0000
Revision:
0:846e2321c637
power to color sensor on/off test OK. Currently the function is disabled.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:846e2321c637 1 #include "mbed.h"
Rhyme 0:846e2321c637 2 #include "vt100.h"
Rhyme 0:846e2321c637 3 #include "afLib.h"
Rhyme 0:846e2321c637 4 #include "af_mgr.h"
Rhyme 0:846e2321c637 5 #include "edge_mgr.h"
Rhyme 0:846e2321c637 6 #include "edge_time.h"
Rhyme 0:846e2321c637 7 #include "edge_reset_mgr.h"
Rhyme 0:846e2321c637 8 /**
Rhyme 0:846e2321c637 9 * afero poc1.5 25-Dec-2017 version
Rhyme 0:846e2321c637 10 * from this version, watch dog timer joined again.
Rhyme 0:846e2321c637 11 */
Rhyme 0:846e2321c637 12
Rhyme 0:846e2321c637 13 vt100 *tty = 0 ;
Rhyme 0:846e2321c637 14 uint32_t wait_tolerance = 500 ; /* 5sec */
Rhyme 0:846e2321c637 15 uint32_t connect_tolerance = 60 ; /* after 60 trials, reboot */
Rhyme 0:846e2321c637 16 uint32_t wait_count = 0 ;
Rhyme 0:846e2321c637 17 uint32_t connect_trial_count = 0 ;
Rhyme 0:846e2321c637 18
Rhyme 0:846e2321c637 19 /**
Rhyme 0:846e2321c637 20 * wait_connection
Rhyme 0:846e2321c637 21 * When gConnected == false, which is connection is lost.
Rhyme 0:846e2321c637 22 * Each 5sec check attribute ATTR_WIFI_STDY_STATE to see
Rhyme 0:846e2321c637 23 * if the connection has recovered.
Rhyme 0:846e2321c637 24 * Meantime even if connection is established communicated
Rhyme 0:846e2321c637 25 * data is invalid, so AF_SYSTEM_ASR_STATE is also
Rhyme 0:846e2321c637 26 * checked for gLinked ;
Rhyme 0:846e2321c637 27 * And in case connect_tolerance trials failed
Rhyme 0:846e2321c637 28 * try to reboot the system if it can improve the situation.
Rhyme 0:846e2321c637 29 */
Rhyme 0:846e2321c637 30 void wait_connection(void)
Rhyme 0:846e2321c637 31 {
Rhyme 0:846e2321c637 32 int result ;
Rhyme 0:846e2321c637 33 wait_count++ ;
Rhyme 0:846e2321c637 34 if (wait_count > wait_tolerance) {
Rhyme 0:846e2321c637 35 reset_watch_dog() ;
Rhyme 0:846e2321c637 36 if (gConnected == false) {
Rhyme 0:846e2321c637 37 result = afero->getAttribute(ATTR_WIFI_STDY_STATE) ;
Rhyme 0:846e2321c637 38 if (result != afSUCCESS) {
Rhyme 0:846e2321c637 39 print_af_error(result) ;
Rhyme 0:846e2321c637 40 }
Rhyme 0:846e2321c637 41 }
Rhyme 0:846e2321c637 42 if (gLinked == false) {
Rhyme 0:846e2321c637 43 result = afero->getAttribute(AF_SYSTEM_ASR_STATE) ;
Rhyme 0:846e2321c637 44 if (result != afSUCCESS) {
Rhyme 0:846e2321c637 45 print_af_error(result) ;
Rhyme 0:846e2321c637 46 }
Rhyme 0:846e2321c637 47 }
Rhyme 0:846e2321c637 48 connect_trial_count++ ;
Rhyme 0:846e2321c637 49 if (connect_trial_count > connect_tolerance) {
Rhyme 0:846e2321c637 50 reboot_edge() ;
Rhyme 0:846e2321c637 51 }
Rhyme 0:846e2321c637 52 wait_count = 0 ;
Rhyme 0:846e2321c637 53 }
Rhyme 0:846e2321c637 54 }
Rhyme 0:846e2321c637 55
Rhyme 0:846e2321c637 56 void init_hardware(void)
Rhyme 0:846e2321c637 57 {
Rhyme 0:846e2321c637 58 int i ;
Rhyme 0:846e2321c637 59 int result ;
Rhyme 0:846e2321c637 60
Rhyme 0:846e2321c637 61 reset_watch_dog() ;
Rhyme 0:846e2321c637 62 init_display() ;
Rhyme 0:846e2321c637 63 reset_watch_dog() ;
Rhyme 0:846e2321c637 64 init_aflib() ;
Rhyme 0:846e2321c637 65 reset_watch_dog() ;
Rhyme 0:846e2321c637 66 init_sensors() ;
Rhyme 0:846e2321c637 67 reset_watch_dog() ;
Rhyme 0:846e2321c637 68 init_timer() ;
Rhyme 0:846e2321c637 69
Rhyme 0:846e2321c637 70 while(true) {
Rhyme 0:846e2321c637 71 reset_watch_dog() ;
Rhyme 0:846e2321c637 72 for (i = 0 ; i < 10 ; i++ ) {
Rhyme 0:846e2321c637 73 afero->loop() ;
Rhyme 0:846e2321c637 74 reset_watch_dog() ;
Rhyme 0:846e2321c637 75 }
Rhyme 0:846e2321c637 76 if ((gLinked == true)&&(gConnected == true)) {
Rhyme 0:846e2321c637 77 wait_count = 0 ;
Rhyme 0:846e2321c637 78 connect_trial_count = 0 ;
Rhyme 0:846e2321c637 79 if (afero->isIdle()) {
Rhyme 0:846e2321c637 80 result = init_edge_attribute() ;
Rhyme 0:846e2321c637 81 if (result == 0) {
Rhyme 0:846e2321c637 82 break ;
Rhyme 0:846e2321c637 83 }
Rhyme 0:846e2321c637 84 }
Rhyme 0:846e2321c637 85 } else { /* gLinked == false */
Rhyme 0:846e2321c637 86 wait_connection() ;
Rhyme 0:846e2321c637 87 }
Rhyme 0:846e2321c637 88 wait_ms(10) ;
Rhyme 0:846e2321c637 89 }
Rhyme 0:846e2321c637 90 do {
Rhyme 0:846e2321c637 91 // while(!afero->isIdle()) {
Rhyme 0:846e2321c637 92 reset_watch_dog() ;
Rhyme 0:846e2321c637 93 for (i = 0 ; i < 10 ; i++ ) {
Rhyme 0:846e2321c637 94 afero->loop() ;
Rhyme 0:846e2321c637 95 wait_ms(100) ;
Rhyme 0:846e2321c637 96 }
Rhyme 0:846e2321c637 97 } while(!afero->isIdle()) ;
Rhyme 0:846e2321c637 98 edge_mgr_status = EDGE_MGR_RUNNING ;
Rhyme 0:846e2321c637 99 }
Rhyme 0:846e2321c637 100
Rhyme 0:846e2321c637 101 // main() runs in its own thread in the OS
Rhyme 0:846e2321c637 102 int main() {
Rhyme 0:846e2321c637 103 static uint32_t count_robin = 0 ;
Rhyme 0:846e2321c637 104
Rhyme 0:846e2321c637 105 tty = new vt100() ;
Rhyme 0:846e2321c637 106 // tty->cls() ;
Rhyme 0:846e2321c637 107 printf("Afero test program (ver. %s) started\n", __DATE__) ;
Rhyme 0:846e2321c637 108 printf("=== Reset Reason ===\n") ;
Rhyme 0:846e2321c637 109 print_reset_reason() ;
Rhyme 0:846e2321c637 110 printf("====================\n") ;
Rhyme 0:846e2321c637 111
Rhyme 0:846e2321c637 112 init_hardware() ;
Rhyme 0:846e2321c637 113
Rhyme 0:846e2321c637 114 edge_splash() ;
Rhyme 0:846e2321c637 115
Rhyme 0:846e2321c637 116 while (true) {
Rhyme 0:846e2321c637 117 count_robin++ ;
Rhyme 0:846e2321c637 118 afero->loop() ;
Rhyme 0:846e2321c637 119 if ((gLinked == true)&&(gConnected == true)) {
Rhyme 0:846e2321c637 120 wait_count = 0 ;
Rhyme 0:846e2321c637 121 connect_trial_count = 0 ;
Rhyme 0:846e2321c637 122 if (afero->isIdle()) {
Rhyme 0:846e2321c637 123 edge_loop(count_robin) ;
Rhyme 0:846e2321c637 124 }
Rhyme 0:846e2321c637 125 } else { /* gLinked == false */
Rhyme 0:846e2321c637 126 wait_connection() ;
Rhyme 0:846e2321c637 127 }
Rhyme 0:846e2321c637 128 wait_ms(10) ;
Rhyme 0:846e2321c637 129 }
Rhyme 0:846e2321c637 130 }