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