La Suno / Mbed 2 deprecated afero_poc15_180216

Dependencies:   UniGraphic mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers edge_mgr.cpp Source File

edge_mgr.cpp

00001 #include "mbed.h"
00002 #include "edge_mgr.h"
00003 #include "af_attributes.h"
00004 
00005 #include "edge_time.h"
00006 #include "edge_pin.h"
00007 #include "MMA8451Q.h"
00008 #include "VEML6040.h"
00009 #include "LM75B.h"
00010 #include "SMTC502AT.h"
00011 #include "PSE530.h"
00012 #include <ILI9341.h>
00013 #include "Arial12x12.h"
00014 #include "Arial24x23.h"
00015 #include "Arial28x28.h"
00016 
00017 #include "edge_sensor.h"
00018 #include "edge_accel.h"
00019 #include "edge_color.h"
00020 #include "edge_temp.h"
00021 #include "edge_pressure.h"
00022 #include "edge_reset_mgr.h"
00023 #include "edge_chart.h"
00024 
00025 #define MMA8451Q_I2C_ADDRESS 0x1C
00026 #define VEML6040_I2C_ADDRESS 0x10
00027 #define LM75B_I2C_ADDRESS    0x48
00028 #define SO1602A_I2C_ADDRESS  0x3C
00029 
00030 #define NUM_MAX_SENSOR 5
00031 
00032 uint16_t attr_to_set[] = {
00033 ATTR_ACCEL_PRESENT,
00034 ATTR_COLOR0_PRESENT,
00035 ATTR_COLOR1_PRESENT,
00036 ATTR_TEMP0_PRESENT,
00037 ATTR_GAS_PRESENT,
00038 } ;
00039 
00040 uint16_t attr_to_get[] = {
00041 // accel
00042 ATTR_ACCEL_ENABLE,
00043 ATTR_ACCEL_INTERVAL,
00044 // Color0
00045 ATTR_COLOR0_ENABLE,
00046 ATTR_COLOR0_INTERVAL,
00047 ATTR_COLOR0_ITIME,
00048 ATTR_COLOR0_PWM_PERIOD,
00049 ATTR_COLOR0_PWM_TARGET,
00050 ATTR_COLOR0_PWM_R,
00051 ATTR_COLOR0_PWM_G,
00052 ATTR_COLOR0_PWM_B,
00053 // Color1
00054 ATTR_COLOR1_ENABLE,
00055 ATTR_COLOR1_INTERVAL,
00056 ATTR_COLOR1_ITIME,
00057 ATTR_COLOR1_PWM_PERIOD,
00058 ATTR_COLOR1_PWM_TARGET,
00059 ATTR_COLOR1_PWM_R,
00060 ATTR_COLOR1_PWM_G,
00061 ATTR_COLOR1_PWM_B,
00062 // Temp
00063 ATTR_TEMP0_INTERVAL,
00064 ATTR_TEMP0_ENABLE,
00065 // Gas Pressure
00066 ATTR_GAS_ENABLE,
00067 ATTR_GAS_INTERVAL,
00068 ATTR_GAS_THR_MODE,
00069 ATTR_GAS_THR_HIGH,
00070 ATTR_GAS_THR_LOW,
00071 0 } ;
00072 
00073 bool            verbos = true ;
00074 edge_sensor     *sensor[NUM_MAX_SENSOR] ;
00075 int             num_sensor   = 0 ;
00076 
00077 edge_accel      *accel       = 0 ;
00078 edge_color      *color[2]    = {0, 0} ;
00079 edge_temp       *temp        = 0 ;
00080 edge_pressure   *pressure    = 0 ;
00081 
00082 PwmOut          *led[3]      = {0, 0, 0} ;
00083 uint16_t        pwm[3]       = { 0x5FA2, 0xB09B, 0x83DF } ;
00084 I2C             *edge_i2c0   = 0 ;
00085 I2C             *edge_i2c1   = 0 ;
00086 ILI9341         *display     = 0 ;
00087 MMA8451Q        *mma8451q    = 0 ;
00088 VEML6040        *veml6040[2] = { 0, 0 } ;
00089 LM75B           *lm75b0      = 0 ; /* for temp1 */
00090 AnalogIn        *an0         = 0 ; /* for temp2 */
00091 SMTC502AT       *smtc502at0  = 0 ;
00092 AnalogIn        *an1         = 0 ; /* for temp3 */
00093 SMTC502AT       *smtc502at1  = 0 ;
00094 LM75B           *lm75b1      = 0 ; /* for temp4 */
00095 AnalogIn        *an2         = 0 ; /* for gas pressure */
00096 PSE530          *pse530      = 0 ; /* gas pressure sensor */
00097 
00098 DigitalOut      *tft_reset   = 0 ;
00099 DigitalOut      *tft_backlight = 0 ;
00100 DigitalOut      *tft_cs       = 0 ;
00101 DigitalOut      *pse530_en    = 0 ;
00102 
00103 static int error_tolerance   = 100 ;
00104 static int loop_interval     = 100 ; // 1000 ; 
00105 static int accel_interval    = 10 ;
00106 int        edge_mgr_status   = EDGE_MGR_INIT ;
00107 char       *reset_reason_str = 0 ;
00108 int        display_mode      = 1 ;
00109 
00110 void init_display(void)
00111 {
00112 reset_watch_dog() ;
00113     printf("TFT Initializing\n") ;
00114     tft_reset = new DigitalOut(PIN_RESET_TFT, 1) ;
00115     tft_backlight = new DigitalOut(PIN_BL_TFT, 0) ;
00116     tft_cs = new DigitalOut(PIN_CS_TFT, 1) ;
00117 
00118 reset_watch_dog() ;
00119     display = new ILI9341(SPI_8, 10000000,
00120                 PIN_MOSI, PIN_MISO, PIN_SCK,
00121                 PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "LaSuno") ;
00122 
00123 reset_watch_dog() ;
00124     display->BusEnable(true) ;
00125     display->set_orientation(1) ;
00126     
00127 reset_watch_dog() ;
00128     display->cls() ;
00129     *tft_backlight = 1 ;
00130     display->BusEnable(false) ;
00131     printf("TFT Initialized\n") ;
00132 }
00133 
00134 void   edge_splash(void) 
00135 {
00136     printf("Sensor loop started!\n") ;
00137     if (display) {
00138         reset_watch_dog() ;
00139         display->BusEnable(true) ;
00140         display->cls() ;
00141         display->foreground(Green) ;
00142         display->locate(40, 20) ;
00143         display->printf("Sensor Loop") ;
00144         display->locate(40, 60) ;
00145         display->printf("  Started!") ;
00146         display->BusEnable(false) ;
00147         reset_watch_dog() ;
00148     }
00149 }
00150 
00151 int init_edge_attribute(void)
00152 {
00153     static int sensor_index = 0 ;
00154     static int attr_index = 0 ;
00155     static int error_count = 0 ;
00156     int return_value = 1 ;
00157     int result ;
00158     
00159     reset_watch_dog() ;
00160     
00161     if (reset_reason_str) { 
00162         result = afero->setAttribute(ATTR_MCU_RESET_REASON, reset_reason_str) ;
00163         if (result == afSUCCESS) {
00164             error_count = 0 ;
00165             reset_reason_str = 0 ;
00166         } else {
00167             error_count++ ;
00168         }
00169         reset_watch_dog() ;
00170     }
00171     if (sensor_index < NUM_MAX_SENSOR) {// for each sensor send presence
00172 // printf("Setting sensor[%d] presence\n", sensor_index) ;
00173         if (sensor_index == 3) { /* for temp lm75b0 is used */
00174             result = afero->setAttributeBool(attr_to_set[sensor_index], lm75b0) ;
00175         } else {
00176             result = afero->setAttributeBool(attr_to_set[sensor_index], sensor[sensor_index]) ;
00177         }
00178         if (result == afSUCCESS) {
00179             error_count = 0 ;
00180             sensor_index++ ;
00181         } else {
00182             error_count++ ;
00183         }
00184         reset_watch_dog() ;
00185     } else { // all sensor presence sent, now get attributes
00186         if (attr_to_get[attr_index] != 0) {
00187 // printf("getting attribute [%d]\n", attr_index) ;
00188             result = afero->getAttribute(attr_to_get[attr_index]) ;
00189             if (result == afSUCCESS) {
00190                 error_count = 0 ;
00191                 attr_index++ ;
00192             } else {
00193                 error_count++ ;
00194             }
00195         }
00196         reset_watch_dog() ;
00197     }
00198     
00199     if (error_count > error_tolerance) { // too many fails, trying reset
00200         reset_watch_dog() ;
00201         reboot_edge() ;
00202     }
00203  
00204     if ((sensor_index >= NUM_MAX_SENSOR)&&(attr_to_get[attr_index] == 0)) { /* all sensors attributes done */
00205         sensor_index = 0 ;
00206         attr_index = 0 ;
00207         return_value = 0 ;
00208     }
00209     return(return_value) ;
00210 }
00211 
00212 void edge_loop(uint32_t count_robin)
00213 {
00214     static int sensor_index = 0 ;
00215     int result ;
00216     
00217     reset_watch_dog() ;
00218     
00219     if ((count_robin % accel_interval) == 0) {
00220         if (accel) {
00221             accel->accum() ; /* get and accum accel data */
00222         }
00223         reset_watch_dog() ;
00224     }
00225 
00226     if ((count_robin % loop_interval) == 0) {
00227         reset_watch_dog() ;
00228         loop_interval = 1 ;
00229         if ((sensor[sensor_index])&&(sensor[sensor_index]->isEnabled())) {
00230             switch(sensor_index) {
00231             case 1: /* color0 */
00232                 if (((edge_color*)sensor[1])->calibration_requested()) {
00233                     ((edge_color*)sensor[1])->calibrate(color0_target, color0_pwm, 10) ;
00234                     reset_watch_dog() ;
00235                     while((result = afero->setAttribute32(ATTR_COLOR0_PWM_R, color0_pwm[0])) != afSUCCESS) { 
00236                         reset_watch_dog() ;
00237                         print_af_error(result) ;
00238                         wait_ms(10) ;
00239                     }
00240                     while((result = afero->setAttribute32(ATTR_COLOR0_PWM_G, color0_pwm[1])) != afSUCCESS) {
00241                         reset_watch_dog() ;
00242                         print_af_error(result) ;
00243                         wait_ms(10) ;
00244                     } 
00245                     while((result = afero->setAttribute32(ATTR_COLOR0_PWM_B, color0_pwm[2])) != afSUCCESS) {
00246                         reset_watch_dog() ;
00247                         print_af_error(result) ;
00248                         wait_ms(10) ;
00249                     }
00250                     while((afero->setAttributeBool(ATTR_COLOR0_CALIBRATE, false)) != afSUCCESS) {
00251                         reset_watch_dog() ;     
00252                         print_af_error(result) ;
00253                         wait_ms(10) ;
00254                     }
00255                 } else { 
00256                     sensor[sensor_index]->runStateMachine() ;
00257                 }
00258                 break ;
00259             case 2: /* color1 */
00260                 if (((edge_color*)sensor[2])->calibration_requested()) {
00261                     ((edge_color*)sensor[2])->calibrate(color1_target, color1_pwm, 10) ;
00262                     reset_watch_dog() ;
00263                     if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_R, color1_pwm[0])) != afSUCCESS) {
00264                         reset_watch_dog() ;
00265                         print_af_error(result) ;
00266                         wait_ms(10) ;
00267                     }
00268                     if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_G, color1_pwm[1])) != afSUCCESS) {
00269                         reset_watch_dog() ;
00270                         print_af_error(result) ;
00271                         wait_ms(10) ;
00272                     }
00273                     reset_watch_dog() ;
00274                     if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_B, color1_pwm[2])) != afSUCCESS) {
00275                         reset_watch_dog() ;       
00276                         print_af_error(result) ;
00277                         wait_ms(10) ;
00278                     }
00279                     while((afero->setAttributeBool(ATTR_COLOR1_CALIBRATE, false)) != afSUCCESS) {
00280                         reset_watch_dog() ;     
00281                         print_af_error(result) ;
00282                         wait_ms(10) ;
00283                     }
00284                 } else { 
00285                     sensor[sensor_index]->runStateMachine() ;
00286                 }
00287                 break ;
00288             default:
00289                 sensor[sensor_index]->runStateMachine() ;
00290                 break ;
00291             }
00292         }
00293         sensor_index = (sensor_index + 1) % NUM_MAX_SENSOR ;
00294     }
00295     reset_watch_dog() ;
00296 }
00297 
00298 int is_present(I2C *i2c, int address)
00299 {
00300     char t[1] = { 0 } ;
00301     char data[2] = { 0, 0 } ;
00302     int result ;
00303     address <<= 1 ;
00304     result = i2c->write(address, t, 1, true) ;
00305     if (result == 0) {
00306         result = i2c->read(address, data, 2) ;
00307     }
00308     return((result == 0)) ;    
00309 }
00310 
00311 void init_sensors(void)
00312 {
00313     printf("=== Initializing Sensor(s) ===\n") ;    
00314     edge_i2c0 = new I2C(PIN_I2C0_SDA, PIN_I2C0_SCL) ;
00315     edge_i2c1 = new I2C(PIN_I2C1_SDA, PIN_I2C1_SCL) ;
00316                 
00317     if (display) {
00318 reset_watch_dog() ;
00319 printf("printing inital string to TFT\n") ;
00320         display->BusEnable(true) ;
00321 
00322 //        display->cls() ;
00323     display->background(Black) ;
00324     display->foreground(White) ;
00325 reset_watch_dog() ;
00326     display->cls() ;
00327 reset_watch_dog() ;
00328         display->set_font((unsigned char*) Arial24x23);
00329         display->foreground(Green) ;
00330         display->locate(70, 5) ;
00331         display->printf("Suntory") ;
00332         display->locate(30, 30) ;
00333         display->printf("Server Monitor") ;
00334         display->set_font((unsigned char*) Arial28x28);
00335         display->foreground(White) ;
00336         display->locate(30, 60) ;
00337         display->printf("La Suno") ;
00338 //        display->set_font_zoom(3, 3) ;
00339         display->locate(30, 100) ;
00340         display->foreground(Red) ;
00341         display->printf("Preparing...") ;
00342     display->BusEnable(true) ;
00343 //    display->fillrect(200,50,250, 150, Green) ;
00344     printf("Done\n") ;
00345     wait(0.1) ;
00346 reset_watch_dog() ;
00347     display->cls() ;
00348     display->foreground(Yellow) ;
00349     display->locate(40, 5) ;
00350     display->printf("Probing sensors...") ;
00351     display->foreground(Green) ;
00352     display->BusEnable(false) ;
00353 //        display->BusEnable(false) ;
00354     }
00355 reset_watch_dog() ;
00356     if (is_present(edge_i2c1, MMA8451Q_I2C_ADDRESS)) {
00357         printf("MMA8451Q on I2C1 is present\n") ;
00358         if (display) {
00359             display->BusEnable(true) ;
00360             display->locate(30, num_sensor * 30 + 40) ;
00361             display->printf("ACCEL is present") ;
00362             display->BusEnable(false) ;
00363         }
00364         mma8451q = new MMA8451Q(edge_i2c1, MMA8451Q_I2C_ADDRESS) ;
00365         accel    = new edge_accel(mma8451q) ;
00366         sensor[0] = accel ;
00367         num_sensor++ ;
00368     } else {
00369         sensor[0] = 0 ;
00370         printf("MMA8451Q is absent\n") ;
00371     }
00372 reset_watch_dog() ;   
00373     if (is_present(edge_i2c1, VEML6040_I2C_ADDRESS)) {
00374         printf("VEML6040 on I2C1 is present\n") ;  
00375         if (display) {
00376             display->BusEnable(true) ;
00377             display->locate(30, num_sensor * 30 + 40) ;
00378             display->printf("COLOR1 is present") ;
00379             display->BusEnable(false) ;
00380         }  
00381         veml6040[0] = new VEML6040(edge_i2c1, VEML6040_I2C_ADDRESS) ;
00382         led[0] = new PwmOut(PIN_LED_R) ; 
00383         led[1] = new PwmOut(PIN_LED_G) ; 
00384         led[2] = new PwmOut(PIN_LED_B) ; 
00385         color[0] = new edge_color(veml6040[0], led, pwm) ;
00386         sensor[1] = color[0] ;
00387         num_sensor++ ;
00388     } else {
00389         sensor[1] = 0 ;
00390         printf("VEML6040 on I2C1 is absent\n") ;
00391     }
00392 reset_watch_dog() ;    
00393     if (is_present(edge_i2c0, VEML6040_I2C_ADDRESS)) {
00394         printf("VEML6040 on I2C0 is present\n") ;  
00395         if (display) {
00396             display->BusEnable(true) ;
00397             display->locate(30, num_sensor * 30 + 40) ;
00398             display->printf("COLOR2 is present") ;
00399             display->BusEnable(false) ;
00400         }   
00401         veml6040[1] = new VEML6040(edge_i2c0, VEML6040_I2C_ADDRESS) ;
00402         if (led[0] == 0) {
00403             led[0] = new PwmOut(PIN_LED_R) ;
00404             led[1] = new PwmOut(PIN_LED_G) ;
00405             led[2] = new PwmOut(PIN_LED_B) ;
00406         }
00407         color[1] = new edge_color(veml6040[1], led, pwm) ;
00408         sensor[2] = color[1] ;
00409         num_sensor++ ;
00410     } else {
00411         sensor[2] = 0 ;
00412         printf("VEML6040 on I2C0 is absent\n") ;
00413     }
00414 reset_watch_dog() ;    
00415     if (is_present(edge_i2c1, LM75B_I2C_ADDRESS)) {
00416         printf("LM75B on I2C1 is present\n") ;
00417         if (display) {
00418             display->BusEnable(true) ;
00419             display->locate(30, num_sensor * 30 + 40) ;
00420             display->printf("TEMP1 is present") ;
00421             display->BusEnable(false) ;
00422         }   
00423         lm75b0 = new LM75B(edge_i2c1, LM75B_I2C_ADDRESS) ;
00424     } else {
00425         printf("LM75B on I2C1 is absent\n") ;
00426     }
00427 #if 0    
00428     if (is_present(edge_i2c0, LM75B_I2C_ADDRESS)) {
00429         printf("LM75B on I2C0 is present\n") ;
00430         lm75b1 = new LM75B(edge_i2c0, LM75B_I2C_ADDRESS) ;
00431     } else {
00432         printf("LM75B on I2C0 is absent\n") ;
00433     }
00434 #endif  
00435      if (display) { /* press is present anyway */
00436         display->BusEnable(true) ;
00437         if (lm75b0) {
00438             display->locate(30, (num_sensor+1) * 30 + 40) ;
00439         } else {
00440             display->locate(30, num_sensor * 30 + 40) ;
00441         }
00442         display->printf("PRESS is present") ;
00443         display->BusEnable(false) ;
00444     }  
00445 reset_watch_dog() ;    
00446     an0        = new AnalogIn(PIN_AN0) ;
00447     smtc502at0 = new SMTC502AT(an0) ;
00448     an1        = new AnalogIn(PIN_AN1) ;
00449     smtc502at1 = new SMTC502AT(an1) ;
00450     temp       = new edge_temp(lm75b0, smtc502at0, smtc502at1, lm75b1) ;
00451     sensor[3]  = temp ;
00452     num_sensor++ ;
00453     
00454 
00455 reset_watch_dog() ;    
00456     an2        = new AnalogIn(PIN_AN2) ;
00457     pse530_en  = new DigitalOut(PIN_PRESS_EN, 0) ;
00458     pse530     = new PSE530(an2) ;
00459     pressure   = new edge_pressure(pse530, pse530_en) ;
00460     sensor[4]  = pressure ;
00461     num_sensor++ ;
00462  
00463 reset_watch_dog() ;
00464     if (num_sensor > 0) {
00465         printf("%d edge_sensor(s) registered\n", num_sensor) ;
00466         printf("Edge is waiting for ASR to link\n") ;
00467         if (display) {
00468             display->BusEnable(true) ;
00469             display->foreground(White) ;
00470             display->locate(40, 200) ;
00471             display->printf("Waiting for ASR") ;
00472             display->BusEnable(false) ;
00473         }
00474     }
00475 reset_watch_dog() ;
00476 }
00477 
00478 void enable_sensors(void) 
00479 {
00480     int i ;
00481     for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
00482         if (sensor[i]) {
00483             sensor[i]->enable() ;
00484         }
00485     }
00486 }
00487 
00488 void disable_sensors(void)
00489 {
00490     int i ;
00491     for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
00492         if (sensor[i]) {
00493             sensor[i]->disable() ;
00494         }
00495     }
00496 }
00497 
00498 void   reboot_edge(void) 
00499 {
00500     int i ;
00501     reset_watch_dog() ;
00502     disable_sensors() ;
00503     reset_watch_dog() ;
00504     if (display) {
00505         delete display ;
00506         display = 0 ;
00507     }
00508     for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
00509         if (sensor[i]) {
00510             reset_watch_dog() ;
00511             delete sensor[i] ;
00512             sensor[i] = 0 ;
00513         }
00514     }
00515     reset_watch_dog() ;
00516     software_reset() ;
00517 }