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 "edge_mgr.h"
Rhyme 0:846e2321c637 4 #include "edge_reset_mgr.h"
Rhyme 0:846e2321c637 5 #include "edge_time.h"
Rhyme 0:846e2321c637 6 #include "edge_sensor.h"
Rhyme 0:846e2321c637 7
Rhyme 0:846e2321c637 8 extern vt100 *tty ;
Rhyme 0:846e2321c637 9 static uint16_t id = 0 ;
Rhyme 0:846e2321c637 10
Rhyme 0:846e2321c637 11 edge_sensor::edge_sensor()
Rhyme 0:846e2321c637 12 {
Rhyme 0:846e2321c637 13 _interval = 0 ;
Rhyme 0:846e2321c637 14 _prev_status = EDGE_SENSOR_INACTIVE ;
Rhyme 0:846e2321c637 15 _status = EDGE_SENSOR_INACTIVE ;
Rhyme 0:846e2321c637 16 _id = id++ ;
Rhyme 0:846e2321c637 17 _enable = false ;
Rhyme 0:846e2321c637 18 _error_count = 0 ;
Rhyme 0:846e2321c637 19 _sample_error = 0 ;
Rhyme 0:846e2321c637 20 }
Rhyme 0:846e2321c637 21
Rhyme 0:846e2321c637 22 edge_sensor::~edge_sensor()
Rhyme 0:846e2321c637 23 {
Rhyme 0:846e2321c637 24 }
Rhyme 0:846e2321c637 25
Rhyme 0:846e2321c637 26 void edge_sensor::reset(void)
Rhyme 0:846e2321c637 27 {
Rhyme 0:846e2321c637 28 _status = EDGE_SENSOR_INACTIVE ;
Rhyme 0:846e2321c637 29 }
Rhyme 0:846e2321c637 30
Rhyme 0:846e2321c637 31 void edge_sensor::enable(void)
Rhyme 0:846e2321c637 32 {
Rhyme 0:846e2321c637 33 _enable = true ;
Rhyme 0:846e2321c637 34 }
Rhyme 0:846e2321c637 35
Rhyme 0:846e2321c637 36 void edge_sensor::disable(void)
Rhyme 0:846e2321c637 37 {
Rhyme 0:846e2321c637 38 _enable = false ;
Rhyme 0:846e2321c637 39 }
Rhyme 0:846e2321c637 40
Rhyme 0:846e2321c637 41 bool edge_sensor::isEnabled(void)
Rhyme 0:846e2321c637 42 {
Rhyme 0:846e2321c637 43 return( _enable ) ;
Rhyme 0:846e2321c637 44 }
Rhyme 0:846e2321c637 45
Rhyme 0:846e2321c637 46 void edge_sensor::prepare(void)
Rhyme 0:846e2321c637 47 {
Rhyme 0:846e2321c637 48 // printf("Sensor %d prepare for sample\n", _id) ;
Rhyme 0:846e2321c637 49 }
Rhyme 0:846e2321c637 50
Rhyme 0:846e2321c637 51 int edge_sensor::sample(void)
Rhyme 0:846e2321c637 52 {
Rhyme 0:846e2321c637 53 int result = EDGE_SAMPLE_SUCCESS ;
Rhyme 0:846e2321c637 54 // printf("Sensor %d sample\n", _id) ;
Rhyme 0:846e2321c637 55 return( result ) ;
Rhyme 0:846e2321c637 56 }
Rhyme 0:846e2321c637 57
Rhyme 0:846e2321c637 58 int edge_sensor::deliver(void)
Rhyme 0:846e2321c637 59 {
Rhyme 0:846e2321c637 60 // printf("Sensor %d data delivered\n", _id) ;
Rhyme 0:846e2321c637 61 /* usually return( result == afSUCCESS ) ; */
Rhyme 0:846e2321c637 62 return 1 ; /* return non zero for success */
Rhyme 0:846e2321c637 63 }
Rhyme 0:846e2321c637 64
Rhyme 0:846e2321c637 65 void edge_sensor::show(void)
Rhyme 0:846e2321c637 66 {
Rhyme 0:846e2321c637 67 /* display value(s) to TFT */
Rhyme 0:846e2321c637 68 }
Rhyme 0:846e2321c637 69
Rhyme 0:846e2321c637 70 void edge_sensor::displayTime(int32_t ts)
Rhyme 0:846e2321c637 71 {
Rhyme 0:846e2321c637 72 struct tm timestruct ;
Rhyme 0:846e2321c637 73 if (display) {
Rhyme 0:846e2321c637 74 reset_watch_dog() ;
Rhyme 0:846e2321c637 75 ts2tm(ts, &timestruct) ;
Rhyme 0:846e2321c637 76 reset_watch_dog() ;
Rhyme 0:846e2321c637 77 display->set_font((unsigned char*) Arial12x12);
Rhyme 0:846e2321c637 78 display->set_font_zoom(2, 2) ;
Rhyme 0:846e2321c637 79 display->foreground(White) ;
Rhyme 0:846e2321c637 80 // display->locate(10, 5) ;
Rhyme 0:846e2321c637 81 display->printf("%d/%02d/%02d %02d:%02d:%02d",
Rhyme 0:846e2321c637 82 timestruct.tm_year,
Rhyme 0:846e2321c637 83 timestruct.tm_mon + 1,
Rhyme 0:846e2321c637 84 timestruct.tm_mday,
Rhyme 0:846e2321c637 85 timestruct.tm_hour,
Rhyme 0:846e2321c637 86 timestruct.tm_min,
Rhyme 0:846e2321c637 87 timestruct.tm_sec
Rhyme 0:846e2321c637 88 ) ;
Rhyme 0:846e2321c637 89 reset_watch_dog() ;
Rhyme 0:846e2321c637 90 }
Rhyme 0:846e2321c637 91 }
Rhyme 0:846e2321c637 92
Rhyme 0:846e2321c637 93 void edge_sensor::toJson(char *buf)
Rhyme 0:846e2321c637 94 {
Rhyme 0:846e2321c637 95 sprintf(buf, "EDGE_SENSOR%d is not a real sensor", _id) ;
Rhyme 0:846e2321c637 96 }
Rhyme 0:846e2321c637 97
Rhyme 0:846e2321c637 98 void edge_sensor::setInterval(uint16_t interval)
Rhyme 0:846e2321c637 99 {
Rhyme 0:846e2321c637 100 _interval = interval ;
Rhyme 0:846e2321c637 101 }
Rhyme 0:846e2321c637 102
Rhyme 0:846e2321c637 103 uint16_t edge_sensor::getInterval(void)
Rhyme 0:846e2321c637 104 {
Rhyme 0:846e2321c637 105 return( _interval ) ;
Rhyme 0:846e2321c637 106 }
Rhyme 0:846e2321c637 107
Rhyme 0:846e2321c637 108 int edge_sensor::getStatus(void)
Rhyme 0:846e2321c637 109 {
Rhyme 0:846e2321c637 110 return( _status ) ;
Rhyme 0:846e2321c637 111 }
Rhyme 0:846e2321c637 112
Rhyme 0:846e2321c637 113 /*
Rhyme 0:846e2321c637 114 #define EDGE_SENSOR_INACTIVE 0
Rhyme 0:846e2321c637 115 #define EDGE_SENSOR_WAIT 1
Rhyme 0:846e2321c637 116 #define EDGE_SENSOR_READY 2
Rhyme 0:846e2321c637 117 #define EDGE_SENSOR_PREPARED 3
Rhyme 0:846e2321c637 118 #define EDGE_SENSOR_SAMPLED 4
Rhyme 0:846e2321c637 119 #define EDGE_SENSOR_DELIVERD 5
Rhyme 0:846e2321c637 120 */
Rhyme 0:846e2321c637 121
Rhyme 0:846e2321c637 122 int edge_sensor::runStateMachine(void)
Rhyme 0:846e2321c637 123 {
Rhyme 0:846e2321c637 124 int result ;
Rhyme 0:846e2321c637 125 reset_watch_dog() ;
Rhyme 0:846e2321c637 126 switch(_status) {
Rhyme 0:846e2321c637 127 case EDGE_SENSOR_INACTIVE: /* inactive */
Rhyme 0:846e2321c637 128 if (isEnabled()) {
Rhyme 0:846e2321c637 129 _status = EDGE_SENSOR_WAIT ;
Rhyme 0:846e2321c637 130 }
Rhyme 0:846e2321c637 131 _prev_status = EDGE_SENSOR_INACTIVE ;
Rhyme 0:846e2321c637 132 break ;
Rhyme 0:846e2321c637 133 case EDGE_SENSOR_WAIT: /* wait for interval time expires */
Rhyme 0:846e2321c637 134 if (_prev_status == EDGE_SENSOR_INACTIVE) { // initial end_interval
Rhyme 0:846e2321c637 135 _end_interval = edge_time + _interval ;
Rhyme 0:846e2321c637 136 }
Rhyme 0:846e2321c637 137 _prev_status = EDGE_SENSOR_WAIT ;
Rhyme 0:846e2321c637 138 if (edge_time >= _end_interval) {
Rhyme 0:846e2321c637 139 _status = EDGE_SENSOR_READY ;
Rhyme 0:846e2321c637 140 _end_interval += _interval ;
Rhyme 0:846e2321c637 141 }
Rhyme 0:846e2321c637 142 break ;
Rhyme 0:846e2321c637 143 case EDGE_SENSOR_READY: /* prepare to sample */
Rhyme 0:846e2321c637 144 result = sample() ;
Rhyme 0:846e2321c637 145 if (result == EDGE_SAMPLE_SUCCESS) {
Rhyme 0:846e2321c637 146 _status = EDGE_SENSOR_SAMPLED ;
Rhyme 0:846e2321c637 147 _sample_error = 0 ;
Rhyme 0:846e2321c637 148 } else {
Rhyme 0:846e2321c637 149 reset_watch_dog() ;
Rhyme 0:846e2321c637 150 printf("Sampling error: ") ;
Rhyme 0:846e2321c637 151 switch(_id) {
Rhyme 0:846e2321c637 152 case SENSOR_ID_ACCEL: printf("Accel (MMA8451Q) ") ; break ;
Rhyme 0:846e2321c637 153 case SENSOR_ID_COLOR1: printf("Color1 (VEML6040) ") ; break ;
Rhyme 0:846e2321c637 154 case SENSOR_ID_COLOR2: printf("Color2 (VEML6040) ") ; break ;
Rhyme 0:846e2321c637 155 case SENSOR_ID_TEMP: printf("Temp (LM75B) ") ; break ;
Rhyme 0:846e2321c637 156 case SENSOR_ID_PRESS: printf("Pressure (PSE530) ") ; break ;
Rhyme 0:846e2321c637 157 default: printf("Sensor[%d] ", _id) ; break ;
Rhyme 0:846e2321c637 158 }
Rhyme 0:846e2321c637 159 switch(result) {
Rhyme 0:846e2321c637 160 case -2: printf("Bus Busy") ; break ;
Rhyme 0:846e2321c637 161 case -1: printf("No Slave") ; break ;
Rhyme 0:846e2321c637 162 case 0: printf("No Error") ; break ;
Rhyme 0:846e2321c637 163 case 1: printf("Nack Received") ; break ;
Rhyme 0:846e2321c637 164 case 2: printf("Time Out") ; break ;
Rhyme 0:846e2321c637 165 default: printf("error[%d]",result) ; break ;
Rhyme 0:846e2321c637 166 }
Rhyme 0:846e2321c637 167 _sample_error++ ;
Rhyme 0:846e2321c637 168 printf("\n") ;
Rhyme 0:846e2321c637 169 }
Rhyme 0:846e2321c637 170 if (_sample_error > SAMPLE_ERROR_TOLERANCE) {
Rhyme 0:846e2321c637 171 printf("Sampling error of sensor[%d]\n", _id) ;
Rhyme 0:846e2321c637 172 wait(0.1) ;
Rhyme 0:846e2321c637 173 reset_watch_dog() ;
Rhyme 0:846e2321c637 174 reboot_edge() ;
Rhyme 0:846e2321c637 175 }
Rhyme 0:846e2321c637 176 _prev_status = EDGE_SENSOR_READY ;
Rhyme 0:846e2321c637 177 break ;
Rhyme 0:846e2321c637 178 case EDGE_SENSOR_SAMPLED: /* data is ready, wait for delivery */
Rhyme 0:846e2321c637 179 if (_prev_status != EDGE_SENSOR_SAMPLED) {
Rhyme 0:846e2321c637 180 _error_count = 0 ;
Rhyme 0:846e2321c637 181 }
Rhyme 0:846e2321c637 182 result = deliver() ;
Rhyme 0:846e2321c637 183 if (result) {
Rhyme 0:846e2321c637 184 _status = EDGE_SENSOR_DELIVERED ; // EDGE_SENSOR_INACTIVE ;
Rhyme 0:846e2321c637 185 } else {
Rhyme 0:846e2321c637 186 _error_count++ ;
Rhyme 0:846e2321c637 187 }
Rhyme 0:846e2321c637 188
Rhyme 0:846e2321c637 189 _prev_status = EDGE_SENSOR_SAMPLED ;
Rhyme 0:846e2321c637 190 break ;
Rhyme 0:846e2321c637 191 case EDGE_SENSOR_DELIVERED:
Rhyme 0:846e2321c637 192 show() ;
Rhyme 0:846e2321c637 193 _status = EDGE_SENSOR_WAIT ;
Rhyme 0:846e2321c637 194 _prev_status = EDGE_SENSOR_DELIVERED ;
Rhyme 0:846e2321c637 195 break ;
Rhyme 0:846e2321c637 196 }
Rhyme 0:846e2321c637 197 reset_watch_dog() ;
Rhyme 0:846e2321c637 198 return(_status) ;
Rhyme 0:846e2321c637 199 }
Rhyme 0:846e2321c637 200