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
Child:
1:8d65cfc3a2e2
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 "edge_mgr.h"
Rhyme 0:846e2321c637 3 #include "af_attributes.h"
Rhyme 0:846e2321c637 4
Rhyme 0:846e2321c637 5 #include "edge_time.h"
Rhyme 0:846e2321c637 6 #include "edge_pin.h"
Rhyme 0:846e2321c637 7 #include "MMA8451Q.h"
Rhyme 0:846e2321c637 8 #include "VEML6040.h"
Rhyme 0:846e2321c637 9 #include "LM75B.h"
Rhyme 0:846e2321c637 10 #include "SMTC502AT.h"
Rhyme 0:846e2321c637 11 #include "PSE530.h"
Rhyme 0:846e2321c637 12 #include <ILI9341.h>
Rhyme 0:846e2321c637 13 #include "Arial12x12.h"
Rhyme 0:846e2321c637 14 #include "Arial24x23.h"
Rhyme 0:846e2321c637 15 #include "Arial28x28.h"
Rhyme 0:846e2321c637 16
Rhyme 0:846e2321c637 17 #include "edge_sensor.h"
Rhyme 0:846e2321c637 18 #include "edge_accel.h"
Rhyme 0:846e2321c637 19 #include "edge_color.h"
Rhyme 0:846e2321c637 20 #include "edge_temp.h"
Rhyme 0:846e2321c637 21 #include "edge_pressure.h"
Rhyme 0:846e2321c637 22 #include "edge_reset_mgr.h"
Rhyme 0:846e2321c637 23 #include "edge_chart.h"
Rhyme 0:846e2321c637 24
Rhyme 0:846e2321c637 25 #define MMA8451Q_I2C_ADDRESS 0x1C
Rhyme 0:846e2321c637 26 #define VEML6040_I2C_ADDRESS 0x10
Rhyme 0:846e2321c637 27 #define LM75B_I2C_ADDRESS 0x48
Rhyme 0:846e2321c637 28 #define SO1602A_I2C_ADDRESS 0x3C
Rhyme 0:846e2321c637 29
Rhyme 0:846e2321c637 30 #define NUM_MAX_SENSOR 5
Rhyme 0:846e2321c637 31
Rhyme 0:846e2321c637 32 uint16_t attr_to_set[] = {
Rhyme 0:846e2321c637 33 ATTR_ACCEL_PRESENT,
Rhyme 0:846e2321c637 34 ATTR_COLOR0_PRESENT,
Rhyme 0:846e2321c637 35 ATTR_COLOR1_PRESENT,
Rhyme 0:846e2321c637 36 ATTR_TEMP0_PRESENT,
Rhyme 0:846e2321c637 37 ATTR_GAS_PRESENT,
Rhyme 0:846e2321c637 38 } ;
Rhyme 0:846e2321c637 39
Rhyme 0:846e2321c637 40 uint16_t attr_to_get[] = {
Rhyme 0:846e2321c637 41 // accel
Rhyme 0:846e2321c637 42 ATTR_ACCEL_ENABLE,
Rhyme 0:846e2321c637 43 ATTR_ACCEL_INTERVAL,
Rhyme 0:846e2321c637 44 // Color0
Rhyme 0:846e2321c637 45 ATTR_COLOR0_ENABLE,
Rhyme 0:846e2321c637 46 ATTR_COLOR0_INTERVAL,
Rhyme 0:846e2321c637 47 ATTR_COLOR0_ITIME,
Rhyme 0:846e2321c637 48 ATTR_COLOR0_PWM_PERIOD,
Rhyme 0:846e2321c637 49 ATTR_COLOR0_PWM_TARGET,
Rhyme 0:846e2321c637 50 ATTR_COLOR0_PWM_R,
Rhyme 0:846e2321c637 51 ATTR_COLOR0_PWM_G,
Rhyme 0:846e2321c637 52 ATTR_COLOR0_PWM_B,
Rhyme 0:846e2321c637 53 // Color1
Rhyme 0:846e2321c637 54 ATTR_COLOR1_ENABLE,
Rhyme 0:846e2321c637 55 ATTR_COLOR1_INTERVAL,
Rhyme 0:846e2321c637 56 ATTR_COLOR1_ITIME,
Rhyme 0:846e2321c637 57 ATTR_COLOR1_PWM_PERIOD,
Rhyme 0:846e2321c637 58 ATTR_COLOR1_PWM_TARGET,
Rhyme 0:846e2321c637 59 ATTR_COLOR1_PWM_R,
Rhyme 0:846e2321c637 60 ATTR_COLOR1_PWM_G,
Rhyme 0:846e2321c637 61 ATTR_COLOR1_PWM_B,
Rhyme 0:846e2321c637 62 // Temp
Rhyme 0:846e2321c637 63 ATTR_TEMP0_INTERVAL,
Rhyme 0:846e2321c637 64 ATTR_TEMP0_ENABLE,
Rhyme 0:846e2321c637 65 // Gas Pressure
Rhyme 0:846e2321c637 66 ATTR_GAS_ENABLE,
Rhyme 0:846e2321c637 67 ATTR_GAS_INTERVAL,
Rhyme 0:846e2321c637 68 ATTR_GAS_THR_MODE,
Rhyme 0:846e2321c637 69 ATTR_GAS_THR_HIGH,
Rhyme 0:846e2321c637 70 ATTR_GAS_THR_LOW,
Rhyme 0:846e2321c637 71 0 } ;
Rhyme 0:846e2321c637 72
Rhyme 0:846e2321c637 73 bool verbos = true ;
Rhyme 0:846e2321c637 74 edge_sensor *sensor[NUM_MAX_SENSOR] ;
Rhyme 0:846e2321c637 75 int num_sensor = 0 ;
Rhyme 0:846e2321c637 76
Rhyme 0:846e2321c637 77 edge_accel *accel = 0 ;
Rhyme 0:846e2321c637 78 edge_color *color[2] = {0, 0} ;
Rhyme 0:846e2321c637 79 edge_temp *temp = 0 ;
Rhyme 0:846e2321c637 80 edge_pressure *pressure = 0 ;
Rhyme 0:846e2321c637 81
Rhyme 0:846e2321c637 82 PwmOut *led[3] = {0, 0, 0} ;
Rhyme 0:846e2321c637 83 uint16_t pwm[3] = { 0x5FA2, 0xB09B, 0x83DF } ;
Rhyme 0:846e2321c637 84 I2C *edge_i2c0 = 0 ;
Rhyme 0:846e2321c637 85 I2C *edge_i2c1 = 0 ;
Rhyme 0:846e2321c637 86 ILI9341 *display = 0 ;
Rhyme 0:846e2321c637 87 MMA8451Q *mma8451q = 0 ;
Rhyme 0:846e2321c637 88 VEML6040 *veml6040[2] = { 0, 0 } ;
Rhyme 0:846e2321c637 89 LM75B *lm75b0 = 0 ; /* for temp1 */
Rhyme 0:846e2321c637 90 AnalogIn *an0 = 0 ; /* for temp2 */
Rhyme 0:846e2321c637 91 SMTC502AT *smtc502at0 = 0 ;
Rhyme 0:846e2321c637 92 AnalogIn *an1 = 0 ; /* for temp3 */
Rhyme 0:846e2321c637 93 SMTC502AT *smtc502at1 = 0 ;
Rhyme 0:846e2321c637 94 LM75B *lm75b1 = 0 ; /* for temp4 */
Rhyme 0:846e2321c637 95 AnalogIn *an2 = 0 ; /* for gas pressure */
Rhyme 0:846e2321c637 96 PSE530 *pse530 = 0 ; /* gas pressure sensor */
Rhyme 0:846e2321c637 97
Rhyme 0:846e2321c637 98 DigitalOut *tft_reset = 0 ;
Rhyme 0:846e2321c637 99 DigitalOut *tft_backlight = 0 ;
Rhyme 0:846e2321c637 100 DigitalOut *tft_cs = 0 ;
Rhyme 0:846e2321c637 101 DigitalOut *pse530_en = 0 ;
Rhyme 0:846e2321c637 102 DigitalOut *color_en = 0 ;
Rhyme 0:846e2321c637 103
Rhyme 0:846e2321c637 104 static int error_tolerance = 100 ;
Rhyme 0:846e2321c637 105 static int loop_interval = 100 ; // 1000 ;
Rhyme 0:846e2321c637 106 static int accel_interval = 10 ;
Rhyme 0:846e2321c637 107 int edge_mgr_status = EDGE_MGR_INIT ;
Rhyme 0:846e2321c637 108 char *reset_reason_str = 0 ;
Rhyme 0:846e2321c637 109 int display_mode = 1 ;
Rhyme 0:846e2321c637 110 bool reboot_requested = false ;
Rhyme 0:846e2321c637 111
Rhyme 0:846e2321c637 112 /* following two functions are for test power on/off of color sensor */
Rhyme 0:846e2321c637 113 void enable_color_sensor(void)
Rhyme 0:846e2321c637 114 {
Rhyme 0:846e2321c637 115 if (color_en == 0) {
Rhyme 0:846e2321c637 116 printf("Color Enable Pin is not initiated\n") ;
Rhyme 0:846e2321c637 117 } else {
Rhyme 0:846e2321c637 118 *color_en = 0 ;
Rhyme 0:846e2321c637 119 }
Rhyme 0:846e2321c637 120 }
Rhyme 0:846e2321c637 121
Rhyme 0:846e2321c637 122 void disable_color_sensor(void)
Rhyme 0:846e2321c637 123 {
Rhyme 0:846e2321c637 124 if (color_en == 0) {
Rhyme 0:846e2321c637 125 printf("Color Enable Pin is not initiated\n") ;
Rhyme 0:846e2321c637 126 } else {
Rhyme 0:846e2321c637 127 *color_en = 1 ;
Rhyme 0:846e2321c637 128 }
Rhyme 0:846e2321c637 129 }
Rhyme 0:846e2321c637 130
Rhyme 0:846e2321c637 131 void init_display(void)
Rhyme 0:846e2321c637 132 {
Rhyme 0:846e2321c637 133 reset_watch_dog() ;
Rhyme 0:846e2321c637 134 printf("TFT Initializing\n") ;
Rhyme 0:846e2321c637 135 tft_reset = new DigitalOut(PIN_RESET_TFT, 1) ;
Rhyme 0:846e2321c637 136 tft_backlight = new DigitalOut(PIN_BL_TFT, 0) ;
Rhyme 0:846e2321c637 137 tft_cs = new DigitalOut(PIN_CS_TFT, 1) ;
Rhyme 0:846e2321c637 138
Rhyme 0:846e2321c637 139 reset_watch_dog() ;
Rhyme 0:846e2321c637 140 display = new ILI9341(SPI_8, 10000000,
Rhyme 0:846e2321c637 141 PIN_MOSI, PIN_MISO, PIN_SCK,
Rhyme 0:846e2321c637 142 PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "LaSuno") ;
Rhyme 0:846e2321c637 143
Rhyme 0:846e2321c637 144 reset_watch_dog() ;
Rhyme 0:846e2321c637 145 display->BusEnable(true) ;
Rhyme 0:846e2321c637 146 display->set_orientation(1) ;
Rhyme 0:846e2321c637 147
Rhyme 0:846e2321c637 148 reset_watch_dog() ;
Rhyme 0:846e2321c637 149 display->cls() ;
Rhyme 0:846e2321c637 150 *tft_backlight = 1 ;
Rhyme 0:846e2321c637 151 display->BusEnable(false) ;
Rhyme 0:846e2321c637 152 printf("TFT Initialized\n") ;
Rhyme 0:846e2321c637 153 }
Rhyme 0:846e2321c637 154
Rhyme 0:846e2321c637 155 void edge_splash(void)
Rhyme 0:846e2321c637 156 {
Rhyme 0:846e2321c637 157 printf("Sensor loop started!\n") ;
Rhyme 0:846e2321c637 158 if (display) {
Rhyme 0:846e2321c637 159 reset_watch_dog() ;
Rhyme 0:846e2321c637 160 display->BusEnable(true) ;
Rhyme 0:846e2321c637 161 display->cls() ;
Rhyme 0:846e2321c637 162 display->foreground(Green) ;
Rhyme 0:846e2321c637 163 display->locate(40, 20) ;
Rhyme 0:846e2321c637 164 display->printf("Sensor Loop") ;
Rhyme 0:846e2321c637 165 display->locate(40, 60) ;
Rhyme 0:846e2321c637 166 display->printf(" Started!") ;
Rhyme 0:846e2321c637 167 display->BusEnable(false) ;
Rhyme 0:846e2321c637 168 reset_watch_dog() ;
Rhyme 0:846e2321c637 169 }
Rhyme 0:846e2321c637 170 }
Rhyme 0:846e2321c637 171
Rhyme 0:846e2321c637 172 int init_edge_attribute(void)
Rhyme 0:846e2321c637 173 {
Rhyme 0:846e2321c637 174 static int sensor_index = 0 ;
Rhyme 0:846e2321c637 175 static int attr_index = 0 ;
Rhyme 0:846e2321c637 176 static int error_count = 0 ;
Rhyme 0:846e2321c637 177 int return_value = 1 ;
Rhyme 0:846e2321c637 178 int result ;
Rhyme 0:846e2321c637 179
Rhyme 0:846e2321c637 180 reset_watch_dog() ;
Rhyme 0:846e2321c637 181
Rhyme 0:846e2321c637 182 if (reset_reason_str) {
Rhyme 0:846e2321c637 183 result = afero->setAttribute(ATTR_MCU_RESET_REASON, reset_reason_str) ;
Rhyme 0:846e2321c637 184 if (result == afSUCCESS) {
Rhyme 0:846e2321c637 185 error_count = 0 ;
Rhyme 0:846e2321c637 186 reset_reason_str = 0 ;
Rhyme 0:846e2321c637 187 } else {
Rhyme 0:846e2321c637 188 error_count++ ;
Rhyme 0:846e2321c637 189 }
Rhyme 0:846e2321c637 190 reset_watch_dog() ;
Rhyme 0:846e2321c637 191 }
Rhyme 0:846e2321c637 192 if (sensor_index < NUM_MAX_SENSOR) {// for each sensor send presence
Rhyme 0:846e2321c637 193 // printf("Setting sensor[%d] presence\n", sensor_index) ;
Rhyme 0:846e2321c637 194 if (sensor_index == 3) { /* for temp lm75b0 is used */
Rhyme 0:846e2321c637 195 result = afero->setAttributeBool(attr_to_set[sensor_index], lm75b0) ;
Rhyme 0:846e2321c637 196 } else {
Rhyme 0:846e2321c637 197 result = afero->setAttributeBool(attr_to_set[sensor_index], sensor[sensor_index]) ;
Rhyme 0:846e2321c637 198 }
Rhyme 0:846e2321c637 199 if (result == afSUCCESS) {
Rhyme 0:846e2321c637 200 error_count = 0 ;
Rhyme 0:846e2321c637 201 sensor_index++ ;
Rhyme 0:846e2321c637 202 } else {
Rhyme 0:846e2321c637 203 error_count++ ;
Rhyme 0:846e2321c637 204 }
Rhyme 0:846e2321c637 205 reset_watch_dog() ;
Rhyme 0:846e2321c637 206 } else { // all sensor presence sent, now get attributes
Rhyme 0:846e2321c637 207 if (attr_to_get[attr_index] != 0) {
Rhyme 0:846e2321c637 208 // printf("getting attribute [%d]\n", attr_index) ;
Rhyme 0:846e2321c637 209 result = afero->getAttribute(attr_to_get[attr_index]) ;
Rhyme 0:846e2321c637 210 if (result == afSUCCESS) {
Rhyme 0:846e2321c637 211 error_count = 0 ;
Rhyme 0:846e2321c637 212 attr_index++ ;
Rhyme 0:846e2321c637 213 } else {
Rhyme 0:846e2321c637 214 error_count++ ;
Rhyme 0:846e2321c637 215 }
Rhyme 0:846e2321c637 216 }
Rhyme 0:846e2321c637 217 reset_watch_dog() ;
Rhyme 0:846e2321c637 218 }
Rhyme 0:846e2321c637 219
Rhyme 0:846e2321c637 220 if (error_count > error_tolerance) { // too many fails, trying reset
Rhyme 0:846e2321c637 221 reset_watch_dog() ;
Rhyme 0:846e2321c637 222 reboot_edge() ;
Rhyme 0:846e2321c637 223 }
Rhyme 0:846e2321c637 224
Rhyme 0:846e2321c637 225 if ((sensor_index >= NUM_MAX_SENSOR)&&(attr_to_get[attr_index] == 0)) { /* all sensors attributes done */
Rhyme 0:846e2321c637 226 sensor_index = 0 ;
Rhyme 0:846e2321c637 227 attr_index = 0 ;
Rhyme 0:846e2321c637 228 return_value = 0 ;
Rhyme 0:846e2321c637 229 }
Rhyme 0:846e2321c637 230 return(return_value) ;
Rhyme 0:846e2321c637 231 }
Rhyme 0:846e2321c637 232
Rhyme 0:846e2321c637 233 void edge_loop(uint32_t count_robin)
Rhyme 0:846e2321c637 234 {
Rhyme 0:846e2321c637 235 static int sensor_index = 0 ;
Rhyme 0:846e2321c637 236 int result ;
Rhyme 0:846e2321c637 237
Rhyme 0:846e2321c637 238 reset_watch_dog() ;
Rhyme 0:846e2321c637 239
Rhyme 0:846e2321c637 240 if ((count_robin % accel_interval) == 0) {
Rhyme 0:846e2321c637 241 if (accel) {
Rhyme 0:846e2321c637 242 accel->accum() ; /* get and accum accel data */
Rhyme 0:846e2321c637 243 }
Rhyme 0:846e2321c637 244 reset_watch_dog() ;
Rhyme 0:846e2321c637 245 }
Rhyme 0:846e2321c637 246
Rhyme 0:846e2321c637 247 if ((count_robin % loop_interval) == 0) {
Rhyme 0:846e2321c637 248 reset_watch_dog() ;
Rhyme 0:846e2321c637 249 loop_interval = 10 ;
Rhyme 0:846e2321c637 250 if ((sensor[sensor_index])&&(sensor[sensor_index]->isEnabled())) {
Rhyme 0:846e2321c637 251 switch(sensor_index) {
Rhyme 0:846e2321c637 252 case SENSOR_ID_COLOR1: /* color0 */
Rhyme 0:846e2321c637 253 if (((edge_color*)sensor[sensor_index])->calibration_requested()) {
Rhyme 0:846e2321c637 254 ((edge_color*)sensor[sensor_index])->calibrate(color0_target, color0_pwm, 10) ;
Rhyme 0:846e2321c637 255 reset_watch_dog() ;
Rhyme 0:846e2321c637 256 while((result = afero->setAttribute32(ATTR_COLOR0_PWM_R, color0_pwm[0])) != afSUCCESS) {
Rhyme 0:846e2321c637 257 reset_watch_dog() ;
Rhyme 0:846e2321c637 258 print_af_error(result) ;
Rhyme 0:846e2321c637 259 wait_ms(10) ;
Rhyme 0:846e2321c637 260 }
Rhyme 0:846e2321c637 261 while((result = afero->setAttribute32(ATTR_COLOR0_PWM_G, color0_pwm[1])) != afSUCCESS) {
Rhyme 0:846e2321c637 262 reset_watch_dog() ;
Rhyme 0:846e2321c637 263 print_af_error(result) ;
Rhyme 0:846e2321c637 264 wait_ms(10) ;
Rhyme 0:846e2321c637 265 }
Rhyme 0:846e2321c637 266 while((result = afero->setAttribute32(ATTR_COLOR0_PWM_B, color0_pwm[2])) != afSUCCESS) {
Rhyme 0:846e2321c637 267 reset_watch_dog() ;
Rhyme 0:846e2321c637 268 print_af_error(result) ;
Rhyme 0:846e2321c637 269 wait_ms(10) ;
Rhyme 0:846e2321c637 270 }
Rhyme 0:846e2321c637 271 while((afero->setAttributeBool(ATTR_COLOR0_CALIBRATE, false)) != afSUCCESS) {
Rhyme 0:846e2321c637 272 reset_watch_dog() ;
Rhyme 0:846e2321c637 273 print_af_error(result) ;
Rhyme 0:846e2321c637 274 wait_ms(10) ;
Rhyme 0:846e2321c637 275 }
Rhyme 0:846e2321c637 276 } else {
Rhyme 0:846e2321c637 277 sensor[sensor_index]->runStateMachine() ;
Rhyme 0:846e2321c637 278 }
Rhyme 0:846e2321c637 279 break ;
Rhyme 0:846e2321c637 280 case SENSOR_ID_COLOR2: /* color1 */
Rhyme 0:846e2321c637 281 if (((edge_color*)sensor[sensor_index])->calibration_requested()) {
Rhyme 0:846e2321c637 282 ((edge_color*)sensor[sensor_index])->calibrate(color1_target, color1_pwm, 10) ;
Rhyme 0:846e2321c637 283 reset_watch_dog() ;
Rhyme 0:846e2321c637 284 if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_R, color1_pwm[0])) != afSUCCESS) {
Rhyme 0:846e2321c637 285 reset_watch_dog() ;
Rhyme 0:846e2321c637 286 print_af_error(result) ;
Rhyme 0:846e2321c637 287 wait_ms(10) ;
Rhyme 0:846e2321c637 288 }
Rhyme 0:846e2321c637 289 if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_G, color1_pwm[1])) != afSUCCESS) {
Rhyme 0:846e2321c637 290 reset_watch_dog() ;
Rhyme 0:846e2321c637 291 print_af_error(result) ;
Rhyme 0:846e2321c637 292 wait_ms(10) ;
Rhyme 0:846e2321c637 293 }
Rhyme 0:846e2321c637 294 reset_watch_dog() ;
Rhyme 0:846e2321c637 295 if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_B, color1_pwm[2])) != afSUCCESS) {
Rhyme 0:846e2321c637 296 reset_watch_dog() ;
Rhyme 0:846e2321c637 297 print_af_error(result) ;
Rhyme 0:846e2321c637 298 wait_ms(10) ;
Rhyme 0:846e2321c637 299 }
Rhyme 0:846e2321c637 300 while((afero->setAttributeBool(ATTR_COLOR1_CALIBRATE, false)) != afSUCCESS) {
Rhyme 0:846e2321c637 301 reset_watch_dog() ;
Rhyme 0:846e2321c637 302 print_af_error(result) ;
Rhyme 0:846e2321c637 303 wait_ms(10) ;
Rhyme 0:846e2321c637 304 }
Rhyme 0:846e2321c637 305 } else {
Rhyme 0:846e2321c637 306 sensor[sensor_index]->runStateMachine() ;
Rhyme 0:846e2321c637 307 }
Rhyme 0:846e2321c637 308 break ;
Rhyme 0:846e2321c637 309 default:
Rhyme 0:846e2321c637 310 sensor[sensor_index]->runStateMachine() ;
Rhyme 0:846e2321c637 311 break ;
Rhyme 0:846e2321c637 312 }
Rhyme 0:846e2321c637 313 }
Rhyme 0:846e2321c637 314 sensor_index = (sensor_index + 1) % NUM_MAX_SENSOR ;
Rhyme 0:846e2321c637 315 }
Rhyme 0:846e2321c637 316 reset_watch_dog() ;
Rhyme 0:846e2321c637 317 }
Rhyme 0:846e2321c637 318
Rhyme 0:846e2321c637 319 int is_present(I2C *i2c, int address)
Rhyme 0:846e2321c637 320 {
Rhyme 0:846e2321c637 321 char t[1] = { 0 } ;
Rhyme 0:846e2321c637 322 char data[2] = { 0, 0 } ;
Rhyme 0:846e2321c637 323 int result ;
Rhyme 0:846e2321c637 324 address <<= 1 ;
Rhyme 0:846e2321c637 325 result = i2c->write(address, t, 1, true) ;
Rhyme 0:846e2321c637 326 if (result == 0) {
Rhyme 0:846e2321c637 327 result = i2c->read(address, data, 2) ;
Rhyme 0:846e2321c637 328 }
Rhyme 0:846e2321c637 329 return((result == 0)) ;
Rhyme 0:846e2321c637 330 }
Rhyme 0:846e2321c637 331
Rhyme 0:846e2321c637 332 /**
Rhyme 0:846e2321c637 333 * check_i2c_pins
Rhyme 0:846e2321c637 334 * To avoid I2C dead-lock condition,
Rhyme 0:846e2321c637 335 * check status of SDA and SCL.
Rhyme 0:846e2321c637 336 * As they are supposed to be HIGH
Rhyme 0:846e2321c637 337 * in case one of them is/are LOW,
Rhyme 0:846e2321c637 338 * change SCL pin to a digital out pin and
Rhyme 0:846e2321c637 339 * generate forced clock for a several cycles.
Rhyme 0:846e2321c637 340 * and when SDA come back to High returns
Rhyme 0:846e2321c637 341 * or I2C_UNLOCK_TRIAL_CYCLE exceeds, give up.
Rhyme 0:846e2321c637 342 */
Rhyme 0:846e2321c637 343 #define I2C_UNLOCK_TRIAL_CYCLE 50
Rhyme 0:846e2321c637 344
Rhyme 0:846e2321c637 345 void check_i2c_pins(PinName sda_pin, PinName scl_pin, int number)
Rhyme 0:846e2321c637 346 {
Rhyme 0:846e2321c637 347 DigitalIn *sda_in = 0 ;
Rhyme 0:846e2321c637 348 DigitalIn *scl_in = 0 ;
Rhyme 0:846e2321c637 349 DigitalOut *scl_out = 0 ;
Rhyme 0:846e2321c637 350 int count = 0 ;
Rhyme 0:846e2321c637 351 sda_in = new DigitalIn(sda_pin, PullUp) ;
Rhyme 0:846e2321c637 352 scl_in = new DigitalIn(scl_pin, PullUp) ;
Rhyme 0:846e2321c637 353 printf("I2C%d pin ", number) ;
Rhyme 0:846e2321c637 354 if ((*sda_in == 0) || (*scl_in == 0)) { /* bus hang! */
Rhyme 0:846e2321c637 355 printf("hang detected, trying to clear ... ") ;
Rhyme 0:846e2321c637 356 delete scl_in ;
Rhyme 0:846e2321c637 357 scl_in = 0 ;
Rhyme 0:846e2321c637 358 scl_out = new DigitalOut(scl_pin) ;
Rhyme 0:846e2321c637 359 while((*sda_in == 0)&&(count++ > I2C_UNLOCK_TRIAL_CYCLE)) {
Rhyme 0:846e2321c637 360 *scl_out = 0 ;
Rhyme 0:846e2321c637 361 wait(0.01) ;
Rhyme 0:846e2321c637 362 *scl_out = 1 ;
Rhyme 0:846e2321c637 363 wait(0.01) ;
Rhyme 0:846e2321c637 364 }
Rhyme 0:846e2321c637 365 if (*sda_in != 0) {
Rhyme 0:846e2321c637 366 printf("Cleared!\n") ;
Rhyme 0:846e2321c637 367 } else {
Rhyme 0:846e2321c637 368 printf("Failed to Clear, proceeding\n") ;
Rhyme 0:846e2321c637 369 }
Rhyme 0:846e2321c637 370 } else {
Rhyme 0:846e2321c637 371 printf("condition OK\n") ;
Rhyme 0:846e2321c637 372 }
Rhyme 0:846e2321c637 373 if (sda_in) { delete sda_in ; }
Rhyme 0:846e2321c637 374 if (scl_in) { delete scl_in ; }
Rhyme 0:846e2321c637 375 if (scl_out) { delete scl_out ; }
Rhyme 0:846e2321c637 376 }
Rhyme 0:846e2321c637 377
Rhyme 0:846e2321c637 378 void init_sensors(void)
Rhyme 0:846e2321c637 379 {
Rhyme 0:846e2321c637 380 printf("=== Initializing Sensor(s) ===\n") ;
Rhyme 0:846e2321c637 381 #if 0
Rhyme 0:846e2321c637 382 color_en = new DigitalOut(PIN_COLOR_EN, 0) ;
Rhyme 0:846e2321c637 383 *color_en = 0 ; /* enable */
Rhyme 0:846e2321c637 384 *color_en = 1 ; /* disable */
Rhyme 0:846e2321c637 385 wait_ms(100) ;
Rhyme 0:846e2321c637 386 *color_en = 0 ; /* enable */
Rhyme 0:846e2321c637 387 wait_ms(10) ;
Rhyme 0:846e2321c637 388 #endif
Rhyme 0:846e2321c637 389
Rhyme 0:846e2321c637 390 check_i2c_pins(PIN_I2C0_SDA, PIN_I2C0_SCL, 0) ;
Rhyme 0:846e2321c637 391 edge_i2c0 = new I2C(PIN_I2C0_SDA, PIN_I2C0_SCL) ;
Rhyme 0:846e2321c637 392
Rhyme 0:846e2321c637 393 check_i2c_pins(PIN_I2C1_SDA, PIN_I2C1_SCL, 1) ;
Rhyme 0:846e2321c637 394 edge_i2c1 = new I2C(PIN_I2C1_SDA, PIN_I2C1_SCL) ;
Rhyme 0:846e2321c637 395
Rhyme 0:846e2321c637 396 if (display) {
Rhyme 0:846e2321c637 397 reset_watch_dog() ;
Rhyme 0:846e2321c637 398 printf("printing inital string to TFT\n") ;
Rhyme 0:846e2321c637 399 display->BusEnable(true) ;
Rhyme 0:846e2321c637 400
Rhyme 0:846e2321c637 401
Rhyme 0:846e2321c637 402 display->background(Black) ;
Rhyme 0:846e2321c637 403 display->foreground(White) ;
Rhyme 0:846e2321c637 404 reset_watch_dog() ;
Rhyme 0:846e2321c637 405 display->cls() ;
Rhyme 0:846e2321c637 406 reset_watch_dog() ;
Rhyme 0:846e2321c637 407 display->set_font((unsigned char*) Arial24x23);
Rhyme 0:846e2321c637 408 display->foreground(Green) ;
Rhyme 0:846e2321c637 409 display->locate(70, 5) ;
Rhyme 0:846e2321c637 410 display->printf("Suntory") ;
Rhyme 0:846e2321c637 411 display->locate(30, 30) ;
Rhyme 0:846e2321c637 412 display->printf("Server Monitor") ;
Rhyme 0:846e2321c637 413 display->set_font((unsigned char*) Arial28x28);
Rhyme 0:846e2321c637 414 display->foreground(White) ;
Rhyme 0:846e2321c637 415 display->locate(30, 60) ;
Rhyme 0:846e2321c637 416 display->printf("La Suno") ;
Rhyme 0:846e2321c637 417 display->locate(30, 100) ;
Rhyme 0:846e2321c637 418 display->foreground(Red) ;
Rhyme 0:846e2321c637 419 display->printf("Preparing...") ;
Rhyme 0:846e2321c637 420 display->BusEnable(true) ;
Rhyme 0:846e2321c637 421 printf("Done\n") ;
Rhyme 0:846e2321c637 422 wait(0.1) ;
Rhyme 0:846e2321c637 423 reset_watch_dog() ;
Rhyme 0:846e2321c637 424 display->cls() ;
Rhyme 0:846e2321c637 425 display->foreground(Yellow) ;
Rhyme 0:846e2321c637 426 display->locate(40, 5) ;
Rhyme 0:846e2321c637 427 display->printf("Probing sensors...") ;
Rhyme 0:846e2321c637 428 display->foreground(Green) ;
Rhyme 0:846e2321c637 429 display->BusEnable(false) ;
Rhyme 0:846e2321c637 430 }
Rhyme 0:846e2321c637 431 reset_watch_dog() ;
Rhyme 0:846e2321c637 432 if (is_present(edge_i2c1, MMA8451Q_I2C_ADDRESS)) {
Rhyme 0:846e2321c637 433 printf("MMA8451Q on I2C1 is present\n") ;
Rhyme 0:846e2321c637 434 if (display) {
Rhyme 0:846e2321c637 435 display->BusEnable(true) ;
Rhyme 0:846e2321c637 436 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:846e2321c637 437 display->printf("ACCEL is present") ;
Rhyme 0:846e2321c637 438 display->BusEnable(false) ;
Rhyme 0:846e2321c637 439 }
Rhyme 0:846e2321c637 440 mma8451q = new MMA8451Q(edge_i2c1, MMA8451Q_I2C_ADDRESS) ;
Rhyme 0:846e2321c637 441 accel = new edge_accel(mma8451q) ;
Rhyme 0:846e2321c637 442 sensor[SENSOR_ID_ACCEL] = accel ;
Rhyme 0:846e2321c637 443 sensor[SENSOR_ID_ACCEL]->setId(SENSOR_ID_ACCEL) ;
Rhyme 0:846e2321c637 444 num_sensor++ ;
Rhyme 0:846e2321c637 445 } else {
Rhyme 0:846e2321c637 446 sensor[SENSOR_ID_ACCEL] = 0 ;
Rhyme 0:846e2321c637 447 printf("MMA8451Q is absent\n") ;
Rhyme 0:846e2321c637 448 }
Rhyme 0:846e2321c637 449 reset_watch_dog() ;
Rhyme 0:846e2321c637 450 if (is_present(edge_i2c1, VEML6040_I2C_ADDRESS)) {
Rhyme 0:846e2321c637 451 printf("VEML6040 on I2C1 is present\n") ;
Rhyme 0:846e2321c637 452 if (display) {
Rhyme 0:846e2321c637 453 display->BusEnable(true) ;
Rhyme 0:846e2321c637 454 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:846e2321c637 455 display->printf("COLOR1 is present") ;
Rhyme 0:846e2321c637 456 display->BusEnable(false) ;
Rhyme 0:846e2321c637 457 }
Rhyme 0:846e2321c637 458 veml6040[0] = new VEML6040(edge_i2c1, VEML6040_I2C_ADDRESS) ;
Rhyme 0:846e2321c637 459 led[0] = new PwmOut(PIN_LED_R) ;
Rhyme 0:846e2321c637 460 led[1] = new PwmOut(PIN_LED_G) ;
Rhyme 0:846e2321c637 461 led[2] = new PwmOut(PIN_LED_B) ;
Rhyme 0:846e2321c637 462 color[0] = new edge_color(veml6040[0], led, pwm) ;
Rhyme 0:846e2321c637 463 sensor[SENSOR_ID_COLOR1] = color[0] ;
Rhyme 0:846e2321c637 464 sensor[SENSOR_ID_COLOR1]->setId(SENSOR_ID_COLOR1) ;
Rhyme 0:846e2321c637 465 num_sensor++ ;
Rhyme 0:846e2321c637 466 } else {
Rhyme 0:846e2321c637 467 sensor[SENSOR_ID_COLOR1] = 0 ;
Rhyme 0:846e2321c637 468 printf("VEML6040 on I2C1 is absent\n") ;
Rhyme 0:846e2321c637 469 }
Rhyme 0:846e2321c637 470 reset_watch_dog() ;
Rhyme 0:846e2321c637 471 if (is_present(edge_i2c0, VEML6040_I2C_ADDRESS)) {
Rhyme 0:846e2321c637 472 printf("VEML6040 on I2C0 is present\n") ;
Rhyme 0:846e2321c637 473 if (display) {
Rhyme 0:846e2321c637 474 display->BusEnable(true) ;
Rhyme 0:846e2321c637 475 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:846e2321c637 476 display->printf("COLOR2 is present") ;
Rhyme 0:846e2321c637 477 display->BusEnable(false) ;
Rhyme 0:846e2321c637 478 }
Rhyme 0:846e2321c637 479 veml6040[1] = new VEML6040(edge_i2c0, VEML6040_I2C_ADDRESS) ;
Rhyme 0:846e2321c637 480 if (led[0] == 0) {
Rhyme 0:846e2321c637 481 led[0] = new PwmOut(PIN_LED_R) ;
Rhyme 0:846e2321c637 482 led[1] = new PwmOut(PIN_LED_G) ;
Rhyme 0:846e2321c637 483 led[2] = new PwmOut(PIN_LED_B) ;
Rhyme 0:846e2321c637 484 }
Rhyme 0:846e2321c637 485 color[1] = new edge_color(veml6040[1], led, pwm) ;
Rhyme 0:846e2321c637 486 sensor[SENSOR_ID_COLOR2] = color[1] ;
Rhyme 0:846e2321c637 487 sensor[SENSOR_ID_COLOR2]->setId(SENSOR_ID_COLOR2) ;
Rhyme 0:846e2321c637 488 num_sensor++ ;
Rhyme 0:846e2321c637 489 } else {
Rhyme 0:846e2321c637 490 sensor[SENSOR_ID_COLOR2] = 0 ;
Rhyme 0:846e2321c637 491 printf("VEML6040 on I2C0 is absent\n") ;
Rhyme 0:846e2321c637 492 }
Rhyme 0:846e2321c637 493 reset_watch_dog() ;
Rhyme 0:846e2321c637 494 if (is_present(edge_i2c1, LM75B_I2C_ADDRESS)) {
Rhyme 0:846e2321c637 495 printf("LM75B on I2C1 is present\n") ;
Rhyme 0:846e2321c637 496 if (display) {
Rhyme 0:846e2321c637 497 display->BusEnable(true) ;
Rhyme 0:846e2321c637 498 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:846e2321c637 499 display->printf("TEMP1 is present") ;
Rhyme 0:846e2321c637 500 display->BusEnable(false) ;
Rhyme 0:846e2321c637 501 }
Rhyme 0:846e2321c637 502 lm75b0 = new LM75B(edge_i2c1, LM75B_I2C_ADDRESS) ;
Rhyme 0:846e2321c637 503 } else {
Rhyme 0:846e2321c637 504 printf("LM75B on I2C1 is absent\n") ;
Rhyme 0:846e2321c637 505 }
Rhyme 0:846e2321c637 506 #if 0
Rhyme 0:846e2321c637 507 if (is_present(edge_i2c0, LM75B_I2C_ADDRESS)) {
Rhyme 0:846e2321c637 508 printf("LM75B on I2C0 is present\n") ;
Rhyme 0:846e2321c637 509 lm75b1 = new LM75B(edge_i2c0, LM75B_I2C_ADDRESS) ;
Rhyme 0:846e2321c637 510 } else {
Rhyme 0:846e2321c637 511 printf("LM75B on I2C0 is absent\n") ;
Rhyme 0:846e2321c637 512 }
Rhyme 0:846e2321c637 513 #endif
Rhyme 0:846e2321c637 514 if (display) { /* press is present anyway */
Rhyme 0:846e2321c637 515 display->BusEnable(true) ;
Rhyme 0:846e2321c637 516 if (lm75b0) {
Rhyme 0:846e2321c637 517 display->locate(30, (num_sensor+1) * 30 + 40) ;
Rhyme 0:846e2321c637 518 } else {
Rhyme 0:846e2321c637 519 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:846e2321c637 520 }
Rhyme 0:846e2321c637 521 display->printf("PRESS is present") ;
Rhyme 0:846e2321c637 522 display->BusEnable(false) ;
Rhyme 0:846e2321c637 523 }
Rhyme 0:846e2321c637 524 reset_watch_dog() ;
Rhyme 0:846e2321c637 525 an0 = new AnalogIn(PIN_AN0) ;
Rhyme 0:846e2321c637 526 smtc502at0 = new SMTC502AT(an0) ;
Rhyme 0:846e2321c637 527 an1 = new AnalogIn(PIN_AN1) ;
Rhyme 0:846e2321c637 528 smtc502at1 = new SMTC502AT(an1) ;
Rhyme 0:846e2321c637 529 temp = new edge_temp(lm75b0, smtc502at0, smtc502at1, lm75b1) ;
Rhyme 0:846e2321c637 530 sensor[SENSOR_ID_TEMP] = temp ;
Rhyme 0:846e2321c637 531 sensor[SENSOR_ID_TEMP]->setId(SENSOR_ID_TEMP) ;
Rhyme 0:846e2321c637 532 num_sensor++ ;
Rhyme 0:846e2321c637 533
Rhyme 0:846e2321c637 534
Rhyme 0:846e2321c637 535 reset_watch_dog() ;
Rhyme 0:846e2321c637 536 an2 = new AnalogIn(PIN_AN2) ;
Rhyme 0:846e2321c637 537 pse530_en = new DigitalOut(PIN_PRESS_EN, 0) ;
Rhyme 0:846e2321c637 538 pse530 = new PSE530(an2) ;
Rhyme 0:846e2321c637 539 pressure = new edge_pressure(pse530, pse530_en) ;
Rhyme 0:846e2321c637 540 sensor[SENSOR_ID_PRESS] = pressure ;
Rhyme 0:846e2321c637 541 sensor[SENSOR_ID_PRESS]->setId(SENSOR_ID_PRESS) ;
Rhyme 0:846e2321c637 542 num_sensor++ ;
Rhyme 0:846e2321c637 543
Rhyme 0:846e2321c637 544 reset_watch_dog() ;
Rhyme 0:846e2321c637 545 if (num_sensor > 0) {
Rhyme 0:846e2321c637 546 printf("%d edge_sensor(s) registered\n", num_sensor) ;
Rhyme 0:846e2321c637 547 printf("Edge is waiting for ASR to link\n") ;
Rhyme 0:846e2321c637 548 if (display) {
Rhyme 0:846e2321c637 549 display->BusEnable(true) ;
Rhyme 0:846e2321c637 550 display->foreground(White) ;
Rhyme 0:846e2321c637 551 display->locate(40, 200) ;
Rhyme 0:846e2321c637 552 display->printf("Waiting for ASR") ;
Rhyme 0:846e2321c637 553 display->BusEnable(false) ;
Rhyme 0:846e2321c637 554 }
Rhyme 0:846e2321c637 555 }
Rhyme 0:846e2321c637 556 reset_watch_dog() ;
Rhyme 0:846e2321c637 557 }
Rhyme 0:846e2321c637 558
Rhyme 0:846e2321c637 559 void enable_sensors(void)
Rhyme 0:846e2321c637 560 {
Rhyme 0:846e2321c637 561 int i ;
Rhyme 0:846e2321c637 562 for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
Rhyme 0:846e2321c637 563 if (sensor[i]) {
Rhyme 0:846e2321c637 564 sensor[i]->enable() ;
Rhyme 0:846e2321c637 565 }
Rhyme 0:846e2321c637 566 }
Rhyme 0:846e2321c637 567 }
Rhyme 0:846e2321c637 568
Rhyme 0:846e2321c637 569 void disable_sensors(void)
Rhyme 0:846e2321c637 570 {
Rhyme 0:846e2321c637 571 int i ;
Rhyme 0:846e2321c637 572 for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
Rhyme 0:846e2321c637 573 if (sensor[i]) {
Rhyme 0:846e2321c637 574 sensor[i]->disable() ;
Rhyme 0:846e2321c637 575 }
Rhyme 0:846e2321c637 576 }
Rhyme 0:846e2321c637 577 }
Rhyme 0:846e2321c637 578
Rhyme 0:846e2321c637 579 void reboot_edge(void)
Rhyme 0:846e2321c637 580 {
Rhyme 0:846e2321c637 581 int i ;
Rhyme 0:846e2321c637 582 reset_watch_dog() ;
Rhyme 0:846e2321c637 583 disable_sensors() ;
Rhyme 0:846e2321c637 584 reset_watch_dog() ;
Rhyme 0:846e2321c637 585 if (display) {
Rhyme 0:846e2321c637 586 delete display ;
Rhyme 0:846e2321c637 587 display = 0 ;
Rhyme 0:846e2321c637 588 }
Rhyme 0:846e2321c637 589 for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
Rhyme 0:846e2321c637 590 if (sensor[i]) {
Rhyme 0:846e2321c637 591 reset_watch_dog() ;
Rhyme 0:846e2321c637 592 delete sensor[i] ;
Rhyme 0:846e2321c637 593 sensor[i] = 0 ;
Rhyme 0:846e2321c637 594 }
Rhyme 0:846e2321c637 595 }
Rhyme 0:846e2321c637 596 reset_watch_dog() ;
Rhyme 0:846e2321c637 597 software_reset() ;
Rhyme 0:846e2321c637 598 }