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 edge_sensor.cpp Source File

edge_sensor.cpp

00001 #include "mbed.h"
00002 #include "vt100.h"
00003 #include "edge_mgr.h"
00004 #include "edge_reset_mgr.h"
00005 #include "edge_time.h"
00006 #include "edge_sensor.h"
00007 
00008 extern vt100 *tty ;
00009 static uint16_t id = 0 ;
00010 
00011 edge_sensor::edge_sensor()
00012 {
00013     _interval = 0 ;
00014     _prev_status = EDGE_SENSOR_INACTIVE ;
00015     _status = EDGE_SENSOR_INACTIVE ;
00016     _id = id++ ;
00017     _enable = false ;
00018     _error_count = 0 ;
00019     _sample_error = 0 ;
00020 }
00021 
00022 edge_sensor::~edge_sensor()
00023 {
00024 }
00025 
00026 void edge_sensor::reset(void)
00027 {
00028     _status = EDGE_SENSOR_INACTIVE ;
00029 }
00030 
00031 void edge_sensor::enable(void) 
00032 {
00033     _enable = true ;
00034 }
00035 
00036 void edge_sensor::disable(void) 
00037 {
00038     _enable = false ;
00039 }
00040 
00041 bool edge_sensor::isEnabled(void)
00042 {
00043     return( _enable ) ;
00044 }
00045 
00046 void edge_sensor::prepare(void)
00047 {
00048 //    printf("Sensor %d prepare for sample\n", _id) ;
00049 }
00050 
00051 int edge_sensor::sample(void)
00052 {
00053     int result = EDGE_SAMPLE_SUCCESS ;
00054 //    printf("Sensor %d sample\n", _id) ;
00055     return( result ) ;
00056 }
00057 
00058 int edge_sensor::deliver(void) 
00059 {
00060 //  printf("Sensor %d data delivered\n", _id) ;
00061     /* usually return( result == afSUCCESS ) ; */
00062     return 1 ; /* return non zero for success */
00063 }
00064 
00065 void edge_sensor::show(void)
00066 {
00067     /* display value(s) to TFT */
00068 }
00069 
00070 void edge_sensor::displayTime(int32_t ts)
00071 {
00072     struct tm timestruct ;
00073     if (display) {
00074 reset_watch_dog() ;
00075         ts2tm(ts, &timestruct) ;
00076 reset_watch_dog() ;
00077         display->set_font((unsigned char*) Arial12x12);
00078         display->set_font_zoom(2, 2) ;
00079         display->foreground(White) ;
00080 //        display->locate(10, 5) ;
00081         display->printf("%d/%02d/%02d %02d:%02d:%02d",
00082             timestruct.tm_year,
00083             timestruct.tm_mon + 1,
00084             timestruct.tm_mday,
00085             timestruct.tm_hour,
00086             timestruct.tm_min,
00087             timestruct.tm_sec
00088         ) ;
00089 reset_watch_dog() ;
00090     }
00091 }
00092 
00093 void edge_sensor::toJson(char *buf)
00094 {
00095     sprintf(buf, "EDGE_SENSOR%d is not a real sensor", _id) ;
00096 }
00097 
00098 void edge_sensor::setInterval(uint16_t interval) 
00099 {
00100     _interval = interval ;
00101 }
00102 
00103 uint16_t edge_sensor::getInterval(void) 
00104 {
00105     return( _interval ) ;
00106 }
00107 
00108 int edge_sensor::getStatus(void)
00109 {
00110     return( _status ) ;
00111 }
00112 
00113 /*
00114 #define EDGE_SENSOR_INACTIVE    0
00115 #define EDGE_SENSOR_WAIT        1
00116 #define EDGE_SENSOR_READY       2
00117 #define EDGE_SENSOR_PREPARED    3
00118 #define EDGE_SENSOR_SAMPLED     4
00119 #define EDGE_SENSOR_DELIVERD    5
00120 */
00121 
00122 int edge_sensor::runStateMachine(void)
00123 {
00124     int result ;
00125     reset_watch_dog() ;
00126     switch(_status) {
00127     case EDGE_SENSOR_INACTIVE: /* inactive */
00128         if (isEnabled()) {
00129             _status = EDGE_SENSOR_WAIT ;
00130         }
00131         _prev_status = EDGE_SENSOR_INACTIVE ;
00132         break ;
00133     case EDGE_SENSOR_WAIT: /* wait for interval time expires */
00134         if (_prev_status == EDGE_SENSOR_INACTIVE) { // initial end_interval
00135             _end_interval = edge_time + _interval ;
00136         }
00137         _prev_status = EDGE_SENSOR_WAIT ;
00138         if (edge_time >= _end_interval) {
00139             _status = EDGE_SENSOR_READY ;
00140             _end_interval += _interval ;
00141         }
00142         break ;
00143     case EDGE_SENSOR_READY: /* prepare to sample */
00144         result = sample() ;
00145         if (result == EDGE_SAMPLE_SUCCESS) {
00146             _status = EDGE_SENSOR_SAMPLED ;
00147             _sample_error = 0 ;
00148         } else {
00149             reset_watch_dog() ;
00150             printf("Sampling error: ") ;
00151             switch(_id) {
00152             case SENSOR_ID_ACCEL:  printf("Accel (MMA8451Q) ") ;  break ;
00153             case SENSOR_ID_COLOR1: printf("Color1 (VEML6040) ") ; break ;
00154             case SENSOR_ID_COLOR2: printf("Color2 (VEML6040) ") ; break ;
00155             case SENSOR_ID_TEMP: printf("Temp (LM75B) ") ;      break ;
00156             case SENSOR_ID_PRESS: printf("Pressure (PSE530) ") ; break ;
00157             default: printf("Sensor[%d] ", _id) ;  break ;
00158             }
00159             switch(result) {
00160             case -2: printf("Bus Busy") ;         break ;
00161             case -1: printf("No Slave") ;         break ;
00162             case 0: printf("No Error") ;          break ;
00163             case 1: printf("Nack Received") ;     break ;
00164             case 2: printf("Time Out") ;          break ;
00165             default: printf("error[%d]",result) ; break ;
00166             }
00167             _sample_error++ ;
00168             printf("\n") ;
00169         }
00170         if (_sample_error > SAMPLE_ERROR_TOLERANCE) {
00171             printf("Sampling error of sensor[%d]\n", _id) ;
00172             wait(0.1) ;
00173             reset_watch_dog() ;
00174             reboot_edge() ;
00175         }
00176         _prev_status = EDGE_SENSOR_READY ;
00177         break ;
00178     case EDGE_SENSOR_SAMPLED: /* data is ready, wait for delivery */
00179         if (_prev_status != EDGE_SENSOR_SAMPLED) {
00180             _error_count = 0 ;
00181         }
00182         result = deliver() ;
00183         if (result) {
00184             _status = EDGE_SENSOR_DELIVERED ; // EDGE_SENSOR_INACTIVE ;
00185         } else {
00186             _error_count++ ;
00187         }
00188 
00189         _prev_status = EDGE_SENSOR_SAMPLED ;
00190         break ;
00191     case EDGE_SENSOR_DELIVERED:
00192         show() ;
00193         _status = EDGE_SENSOR_WAIT ;
00194         _prev_status = EDGE_SENSOR_DELIVERED ;
00195         break ;
00196     }
00197     reset_watch_dog() ;
00198     return(_status) ;
00199 }
00200