La Suno / Mbed 2 deprecated afero_poc15_180223

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 bool       reboot_requested  = false ;
00110 
00111 void init_display(void)
00112 {
00113 reset_watch_dog() ;
00114     printf("TFT Initializing\n") ;
00115     tft_reset = new DigitalOut(PIN_RESET_TFT, 1) ;
00116     tft_backlight = new DigitalOut(PIN_BL_TFT, 0) ;
00117     tft_cs = new DigitalOut(PIN_CS_TFT, 1) ;
00118 
00119 reset_watch_dog() ;
00120     display = new ILI9341(SPI_8, 10000000,
00121                 PIN_MOSI, PIN_MISO, PIN_SCK,
00122                 PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "LaSuno") ;
00123 
00124 reset_watch_dog() ;
00125     display->BusEnable(true) ;
00126     display->set_orientation(1) ;
00127     
00128 reset_watch_dog() ;
00129     display->cls() ;
00130     *tft_backlight = 1 ;
00131     display->BusEnable(false) ;
00132     printf("TFT Initialized\n") ;
00133 }
00134 
00135 void   edge_splash(void) 
00136 {
00137     printf("Sensor loop started!\n") ;
00138     if (display) {
00139         reset_watch_dog() ;
00140         display->BusEnable(true) ;
00141         display->cls() ;
00142         display->foreground(Green) ;
00143         display->locate(40, 20) ;
00144         display->printf("Sensor Loop") ;
00145         display->locate(40, 60) ;
00146         display->printf("  Started!") ;
00147         display->BusEnable(false) ;
00148         reset_watch_dog() ;
00149     }
00150 }
00151 
00152 int init_edge_attribute(void)
00153 {
00154     static int sensor_index = 0 ;
00155     static int attr_index = 0 ;
00156     static int error_count = 0 ;
00157     int return_value = 1 ;
00158     int result ;
00159     
00160     reset_watch_dog() ;
00161     
00162     if (reset_reason_str) { 
00163         result = afero->setAttribute(ATTR_MCU_RESET_REASON, reset_reason_str) ;
00164         if (result == afSUCCESS) {
00165             error_count = 0 ;
00166             reset_reason_str = 0 ;
00167         } else {
00168             error_count++ ;
00169         }
00170         reset_watch_dog() ;
00171     }
00172     if (sensor_index < NUM_MAX_SENSOR) {// for each sensor send presence
00173 // printf("Setting sensor[%d] presence\n", sensor_index) ;
00174         if (sensor_index == 3) { /* for temp lm75b0 is used */
00175             result = afero->setAttributeBool(attr_to_set[sensor_index], lm75b0) ;
00176         } else {
00177             result = afero->setAttributeBool(attr_to_set[sensor_index], sensor[sensor_index]) ;
00178         }
00179         if (result == afSUCCESS) {
00180             error_count = 0 ;
00181             sensor_index++ ;
00182         } else {
00183             error_count++ ;
00184         }
00185         reset_watch_dog() ;
00186     } else { // all sensor presence sent, now get attributes
00187         if (attr_to_get[attr_index] != 0) {
00188 // printf("getting attribute [%d]\n", attr_index) ;
00189             result = afero->getAttribute(attr_to_get[attr_index]) ;
00190             if (result == afSUCCESS) {
00191                 error_count = 0 ;
00192                 attr_index++ ;
00193             } else {
00194                 error_count++ ;
00195             }
00196         }
00197         reset_watch_dog() ;
00198     }
00199     
00200     if (error_count > error_tolerance) { // too many fails, trying reset
00201         reset_watch_dog() ;
00202         reboot_edge() ;
00203     }
00204  
00205     if ((sensor_index >= NUM_MAX_SENSOR)&&(attr_to_get[attr_index] == 0)) { /* all sensors attributes done */
00206         sensor_index = 0 ;
00207         attr_index = 0 ;
00208         return_value = 0 ;
00209     }
00210     return(return_value) ;
00211 }
00212 
00213 void edge_loop(uint32_t count_robin)
00214 {
00215     static int sensor_index = 0 ;
00216     int result ;
00217     
00218     reset_watch_dog() ;
00219     
00220     if ((count_robin % accel_interval) == 0) {
00221         if (accel) {
00222             accel->accum() ; /* get and accum accel data */
00223         }
00224         reset_watch_dog() ;
00225     }
00226 
00227     if ((count_robin % loop_interval) == 0) {
00228         reset_watch_dog() ;
00229         loop_interval = 10 ;
00230         if ((sensor[sensor_index])&&(sensor[sensor_index]->isEnabled())) {
00231             switch(sensor_index) {
00232             case 1: /* color0 */
00233                 if (((edge_color*)sensor[1])->calibration_requested()) {
00234                     ((edge_color*)sensor[1])->calibrate(color0_target, color0_pwm, 10) ;
00235                     reset_watch_dog() ;
00236                     while((result = afero->setAttribute32(ATTR_COLOR0_PWM_R, color0_pwm[0])) != afSUCCESS) { 
00237                         reset_watch_dog() ;
00238                         print_af_error(result) ;
00239                         wait_ms(10) ;
00240                     }
00241                     while((result = afero->setAttribute32(ATTR_COLOR0_PWM_G, color0_pwm[1])) != afSUCCESS) {
00242                         reset_watch_dog() ;
00243                         print_af_error(result) ;
00244                         wait_ms(10) ;
00245                     } 
00246                     while((result = afero->setAttribute32(ATTR_COLOR0_PWM_B, color0_pwm[2])) != afSUCCESS) {
00247                         reset_watch_dog() ;
00248                         print_af_error(result) ;
00249                         wait_ms(10) ;
00250                     }
00251                     while((afero->setAttributeBool(ATTR_COLOR0_CALIBRATE, false)) != afSUCCESS) {
00252                         reset_watch_dog() ;     
00253                         print_af_error(result) ;
00254                         wait_ms(10) ;
00255                     }
00256                 } else { 
00257                     sensor[sensor_index]->runStateMachine() ;
00258                 }
00259                 break ;
00260             case 2: /* color1 */
00261                 if (((edge_color*)sensor[2])->calibration_requested()) {
00262                     ((edge_color*)sensor[2])->calibrate(color1_target, color1_pwm, 10) ;
00263                     reset_watch_dog() ;
00264                     if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_R, color1_pwm[0])) != afSUCCESS) {
00265                         reset_watch_dog() ;
00266                         print_af_error(result) ;
00267                         wait_ms(10) ;
00268                     }
00269                     if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_G, color1_pwm[1])) != afSUCCESS) {
00270                         reset_watch_dog() ;
00271                         print_af_error(result) ;
00272                         wait_ms(10) ;
00273                     }
00274                     reset_watch_dog() ;
00275                     if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_B, color1_pwm[2])) != afSUCCESS) {
00276                         reset_watch_dog() ;       
00277                         print_af_error(result) ;
00278                         wait_ms(10) ;
00279                     }
00280                     while((afero->setAttributeBool(ATTR_COLOR1_CALIBRATE, false)) != afSUCCESS) {
00281                         reset_watch_dog() ;     
00282                         print_af_error(result) ;
00283                         wait_ms(10) ;
00284                     }
00285                 } else { 
00286                     sensor[sensor_index]->runStateMachine() ;
00287                 }
00288                 break ;
00289             default:
00290                 sensor[sensor_index]->runStateMachine() ;
00291                 break ;
00292             }
00293         }
00294         sensor_index = (sensor_index + 1) % NUM_MAX_SENSOR ;
00295     }
00296     reset_watch_dog() ;
00297 }
00298 
00299 int is_present(I2C *i2c, int address)
00300 {
00301     char t[1] = { 0 } ;
00302     char data[2] = { 0, 0 } ;
00303     int result ;
00304     address <<= 1 ;
00305     result = i2c->write(address, t, 1, true) ;
00306     if (result == 0) {
00307         result = i2c->read(address, data, 2) ;
00308     }
00309     return((result == 0)) ;    
00310 }
00311 
00312 void init_sensors(void)
00313 {
00314     printf("=== Initializing Sensor(s) ===\n") ;    
00315     edge_i2c0 = new I2C(PIN_I2C0_SDA, PIN_I2C0_SCL) ;
00316     edge_i2c1 = new I2C(PIN_I2C1_SDA, PIN_I2C1_SCL) ;
00317                 
00318     if (display) {
00319 reset_watch_dog() ;
00320 printf("printing inital string to TFT\n") ;
00321         display->BusEnable(true) ;
00322 
00323 //        display->cls() ;
00324     display->background(Black) ;
00325     display->foreground(White) ;
00326 reset_watch_dog() ;
00327     display->cls() ;
00328 reset_watch_dog() ;
00329         display->set_font((unsigned char*) Arial24x23);
00330         display->foreground(Green) ;
00331         display->locate(70, 5) ;
00332         display->printf("Suntory") ;
00333         display->locate(30, 30) ;
00334         display->printf("Server Monitor") ;
00335         display->set_font((unsigned char*) Arial28x28);
00336         display->foreground(White) ;
00337         display->locate(30, 60) ;
00338         display->printf("La Suno") ;
00339 //        display->set_font_zoom(3, 3) ;
00340         display->locate(30, 100) ;
00341         display->foreground(Red) ;
00342         display->printf("Preparing...") ;
00343     display->BusEnable(true) ;
00344 //    display->fillrect(200,50,250, 150, Green) ;
00345     printf("Done\n") ;
00346     wait(0.1) ;
00347 reset_watch_dog() ;
00348     display->cls() ;
00349     display->foreground(Yellow) ;
00350     display->locate(40, 5) ;
00351     display->printf("Probing sensors...") ;
00352     display->foreground(Green) ;
00353     display->BusEnable(false) ;
00354 //        display->BusEnable(false) ;
00355     }
00356 reset_watch_dog() ;
00357     if (is_present(edge_i2c1, MMA8451Q_I2C_ADDRESS)) {
00358         printf("MMA8451Q on I2C1 is present\n") ;
00359         if (display) {
00360             display->BusEnable(true) ;
00361             display->locate(30, num_sensor * 30 + 40) ;
00362             display->printf("ACCEL is present") ;
00363             display->BusEnable(false) ;
00364         }
00365         mma8451q = new MMA8451Q(edge_i2c1, MMA8451Q_I2C_ADDRESS) ;
00366         accel    = new edge_accel(mma8451q) ;
00367         sensor[0] = accel ;
00368         sensor[0]->setId( 0 ) ;
00369         num_sensor++ ;
00370     } else {
00371         sensor[0] = 0 ;
00372         printf("MMA8451Q is absent\n") ;
00373     }
00374 reset_watch_dog() ;   
00375     if (is_present(edge_i2c1, VEML6040_I2C_ADDRESS)) {
00376         printf("VEML6040 on I2C1 is present\n") ;  
00377         if (display) {
00378             display->BusEnable(true) ;
00379             display->locate(30, num_sensor * 30 + 40) ;
00380             display->printf("COLOR1 is present") ;
00381             display->BusEnable(false) ;
00382         }  
00383         veml6040[0] = new VEML6040(edge_i2c1, VEML6040_I2C_ADDRESS) ;
00384         led[0] = new PwmOut(PIN_LED_R) ; 
00385         led[1] = new PwmOut(PIN_LED_G) ; 
00386         led[2] = new PwmOut(PIN_LED_B) ; 
00387         color[0] = new edge_color(veml6040[0], led, pwm) ;
00388         sensor[1] = color[0] ;
00389         sensor[1]->setId( 1 ) ;
00390         num_sensor++ ;
00391     } else {
00392         sensor[1] = 0 ;
00393         printf("VEML6040 on I2C1 is absent\n") ;
00394     }
00395 reset_watch_dog() ;    
00396     if (is_present(edge_i2c0, VEML6040_I2C_ADDRESS)) {
00397         printf("VEML6040 on I2C0 is present\n") ;  
00398         if (display) {
00399             display->BusEnable(true) ;
00400             display->locate(30, num_sensor * 30 + 40) ;
00401             display->printf("COLOR2 is present") ;
00402             display->BusEnable(false) ;
00403         }   
00404         veml6040[1] = new VEML6040(edge_i2c0, VEML6040_I2C_ADDRESS) ;
00405         if (led[0] == 0) {
00406             led[0] = new PwmOut(PIN_LED_R) ;
00407             led[1] = new PwmOut(PIN_LED_G) ;
00408             led[2] = new PwmOut(PIN_LED_B) ;
00409         }
00410         color[1] = new edge_color(veml6040[1], led, pwm) ;
00411         sensor[2] = color[1] ;
00412         sensor[2]->setId(2) ;
00413         num_sensor++ ;
00414     } else {
00415         sensor[2] = 0 ;
00416         printf("VEML6040 on I2C0 is absent\n") ;
00417     }
00418 reset_watch_dog() ;    
00419     if (is_present(edge_i2c1, LM75B_I2C_ADDRESS)) {
00420         printf("LM75B on I2C1 is present\n") ;
00421         if (display) {
00422             display->BusEnable(true) ;
00423             display->locate(30, num_sensor * 30 + 40) ;
00424             display->printf("TEMP1 is present") ;
00425             display->BusEnable(false) ;
00426         }   
00427         lm75b0 = new LM75B(edge_i2c1, LM75B_I2C_ADDRESS) ;
00428     } else {
00429         printf("LM75B on I2C1 is absent\n") ;
00430     }
00431 #if 0    
00432     if (is_present(edge_i2c0, LM75B_I2C_ADDRESS)) {
00433         printf("LM75B on I2C0 is present\n") ;
00434         lm75b1 = new LM75B(edge_i2c0, LM75B_I2C_ADDRESS) ;
00435     } else {
00436         printf("LM75B on I2C0 is absent\n") ;
00437     }
00438 #endif  
00439      if (display) { /* press is present anyway */
00440         display->BusEnable(true) ;
00441         if (lm75b0) {
00442             display->locate(30, (num_sensor+1) * 30 + 40) ;
00443         } else {
00444             display->locate(30, num_sensor * 30 + 40) ;
00445         }
00446         display->printf("PRESS is present") ;
00447         display->BusEnable(false) ;
00448     }  
00449 reset_watch_dog() ;    
00450     an0        = new AnalogIn(PIN_AN0) ;
00451     smtc502at0 = new SMTC502AT(an0) ;
00452     an1        = new AnalogIn(PIN_AN1) ;
00453     smtc502at1 = new SMTC502AT(an1) ;
00454     temp       = new edge_temp(lm75b0, smtc502at0, smtc502at1, lm75b1) ;
00455     sensor[3]  = temp ;
00456     sensor[3]->setId(3) ;
00457     num_sensor++ ;
00458     
00459 
00460 reset_watch_dog() ;    
00461     an2        = new AnalogIn(PIN_AN2) ;
00462     pse530_en  = new DigitalOut(PIN_PRESS_EN, 0) ;
00463     pse530     = new PSE530(an2) ;
00464     pressure   = new edge_pressure(pse530, pse530_en) ;
00465     sensor[4]  = pressure ;
00466     sensor[4]->setId(4) ;
00467     num_sensor++ ;
00468  
00469 reset_watch_dog() ;
00470     if (num_sensor > 0) {
00471         printf("%d edge_sensor(s) registered\n", num_sensor) ;
00472         printf("Edge is waiting for ASR to link\n") ;
00473         if (display) {
00474             display->BusEnable(true) ;
00475             display->foreground(White) ;
00476             display->locate(40, 200) ;
00477             display->printf("Waiting for ASR") ;
00478             display->BusEnable(false) ;
00479         }
00480     }
00481 reset_watch_dog() ;
00482 }
00483 
00484 void enable_sensors(void) 
00485 {
00486     int i ;
00487     for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
00488         if (sensor[i]) {
00489             sensor[i]->enable() ;
00490         }
00491     }
00492 }
00493 
00494 void disable_sensors(void)
00495 {
00496     int i ;
00497     for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
00498         if (sensor[i]) {
00499             sensor[i]->disable() ;
00500         }
00501     }
00502 }
00503 
00504 void   reboot_edge(void) 
00505 {
00506     int i ;
00507     reset_watch_dog() ;
00508     disable_sensors() ;
00509     reset_watch_dog() ;
00510     if (display) {
00511         delete display ;
00512         display = 0 ;
00513     }
00514     for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
00515         if (sensor[i]) {
00516             reset_watch_dog() ;
00517             delete sensor[i] ;
00518             sensor[i] = 0 ;
00519         }
00520     }
00521     reset_watch_dog() ;
00522     software_reset() ;
00523 }