Color sensor reset at the end of calibration added. sensor id auto assignment was changed to be a fixed value assignment to avoid sensor id shift when some sensor is absent.

Dependencies:   UniGraphic mbed vt100

Committer:
Rhyme
Date:
Fri Feb 23 07:51:55 2018 +0000
Revision:
1:8818b793d147
Parent:
0:ce97f6d34336
Wrong behavior when one of color sensor is missing has been fixed.

Who changed what in which revision?

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