Jason Chang
/
9v1
Add I2CSlave to ov580 master.
main.cpp@12:9c9a532cd7d6, 2019-02-05 (annotated)
- Committer:
- claytonk
- Date:
- Tue Feb 05 19:20:57 2019 +0000
- Revision:
- 12:9c9a532cd7d6
- Parent:
- 11:236d223ba3bc
- Child:
- 13:2b83989d136c
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
claytonk | 7:9c77eaad5102 | 1 | #define TWIS1_ENABLED 1 |
claytonk | 7:9c77eaad5102 | 2 | #define TWIS0_ENABLED 0 |
claytonk | 7:9c77eaad5102 | 3 | #define TWIS_ENABLED 1 |
claytonk | 7:9c77eaad5102 | 4 | #define TWI0_ENABLED 1 |
claytonk | 7:9c77eaad5102 | 5 | #define TWI1_ENABLED 0 |
claytonk | 7:9c77eaad5102 | 6 | #define TWI0_USE_EASY_DMA 1 |
claytonk | 7:9c77eaad5102 | 7 | #define TWI1_USE_EASY_DMA 1 |
claytonk | 7:9c77eaad5102 | 8 | #define TWI_DEFAULT_CONFIG_FREQUENCY 104857600 |
claytonk | 7:9c77eaad5102 | 9 | #define ARDUINO_A4_PIN 14 |
claytonk | 7:9c77eaad5102 | 10 | #define ARDUINO_A5_PIN 15 |
claytonk | 7:9c77eaad5102 | 11 | |
claytonk | 7:9c77eaad5102 | 12 | |
claytonk | 0:605fe136b7ea | 13 | #include "mbed.h" |
claytonk | 7:9c77eaad5102 | 14 | //#include <nrfx_config.h> need SDK config instead |
claytonk | 7:9c77eaad5102 | 15 | //#include "sdk_config.h" not needed now |
claytonk | 0:605fe136b7ea | 16 | |
claytonk | 0:605fe136b7ea | 17 | /* |
claytonk | 0:605fe136b7ea | 18 | FEATURE REQUIREMENTS |
claytonk | 0:605fe136b7ea | 19 | |
claytonk | 3:6b14862f5f51 | 20 | - I2C write occurs on falling edge of strobe pulse to prevent unknown states |
claytonk | 3:6b14862f5f51 | 21 | |
claytonk | 3:6b14862f5f51 | 22 | - Stack check occurs on leading edge of strobe pulse to ensure strobes are |
claytonk | 3:6b14862f5f51 | 23 | set correctly |
claytonk | 0:605fe136b7ea | 24 | |
claytonk | 3:6b14862f5f51 | 25 | - Reoccuring enable from app that will prevent the emitters from remaining |
claytonk | 3:6b14862f5f51 | 26 | on if the app crashes and a turn off command isn't sent |
claytonk | 0:605fe136b7ea | 27 | - Can be a counter on the emitter enable > threshold |
claytonk | 3:6b14862f5f51 | 28 | - Better if it's a counter based on reads from the fault register |
claytonk | 0:605fe136b7ea | 29 | |
claytonk | 9:69e0bbfdb62b | 30 | -Flood/Dot tagging indicated on GPIO3 / PWM0 |
claytonk | 0:605fe136b7ea | 31 | - 0 Flood |
claytonk | 0:605fe136b7ea | 32 | - 1 DOT |
claytonk | 0:605fe136b7ea | 33 | |
claytonk | 9:69e0bbfdb62b | 34 | -Fault indication on GPIO4 / PWM1 to indicate to app to reboot |
claytonk | 0:605fe136b7ea | 35 | - 0 OK |
claytonk | 0:605fe136b7ea | 36 | - 1 Fault/Reboot |
claytonk | 0:605fe136b7ea | 37 | |
claytonk | 0:605fe136b7ea | 38 | I2C write commands |
claytonk | 0:605fe136b7ea | 39 | Reg: Information Condition |
claytonk | 4:fc0c93cb0197 | 40 | 0x0000 Emitter enable 0x00 Emitters off |
claytonk | 0:605fe136b7ea | 41 | 0x01 Dot on |
claytonk | 0:605fe136b7ea | 42 | 0x10 Flood on |
claytonk | 0:605fe136b7ea | 43 | 0x11 Flood / Dot on |
claytonk | 0:605fe136b7ea | 44 | |
claytonk | 4:fc0c93cb0197 | 45 | 0x0001 Dot fault Read from LM36011 |
claytonk | 0:605fe136b7ea | 46 | |
claytonk | 4:fc0c93cb0197 | 47 | 0x0002 Dot Current 0x00 - level_dot_max |
claytonk | 3:6b14862f5f51 | 48 | |
claytonk | 4:fc0c93cb0197 | 49 | 0x0003 Flood Fault Read from LM36011 |
claytonk | 3:6b14862f5f51 | 50 | |
claytonk | 4:fc0c93cb0197 | 51 | 0x0004 Flood Current 0x00 - level_flood_max |
claytonk | 0:605fe136b7ea | 52 | |
claytonk | 4:fc0c93cb0197 | 53 | 0x0005 Fault register bit[0] KILL_VCSEL fault |
claytonk | 3:6b14862f5f51 | 54 | bit[1] dot fault |
claytonk | 3:6b14862f5f51 | 55 | bit[2] flood fault |
claytonk | 3:6b14862f5f51 | 56 | bit[3] unstacked fault |
claytonk | 3:6b14862f5f51 | 57 | bit[4] ?not in app fault? |
claytonk | 3:6b14862f5f51 | 58 | bit[5-7] unused |
claytonk | 3:6b14862f5f51 | 59 | |
claytonk | 3:6b14862f5f51 | 60 | |
claytonk | 4:fc0c93cb0197 | 61 | 0x0006 Version number 0x70 = version 7.0 |
claytonk | 0:605fe136b7ea | 62 | |
claytonk | 9:69e0bbfdb62b | 63 | */ |
claytonk | 9:69e0bbfdb62b | 64 | /* ERROR CHECKING */ |
claytonk | 0:605fe136b7ea | 65 | |
claytonk | 7:9c77eaad5102 | 66 | #if (TWIS0_ENABLED == 1) |
claytonk | 7:9c77eaad5102 | 67 | #error TWIS0 is congigured wrong |
claytonk | 7:9c77eaad5102 | 68 | #endif |
claytonk | 7:9c77eaad5102 | 69 | |
claytonk | 7:9c77eaad5102 | 70 | #if (TWIS1_ENABLED == 0) |
claytonk | 7:9c77eaad5102 | 71 | #error TWIS1 is off |
claytonk | 7:9c77eaad5102 | 72 | #endif |
claytonk | 6:85d9ad912c14 | 73 | |
claytonk | 7:9c77eaad5102 | 74 | #if (TWIS_ENABLED == 0) |
claytonk | 7:9c77eaad5102 | 75 | #error TWIS is off |
claytonk | 7:9c77eaad5102 | 76 | #endif |
claytonk | 6:85d9ad912c14 | 77 | |
claytonk | 7:9c77eaad5102 | 78 | #if (TWI0_ENABLED == 0) |
claytonk | 7:9c77eaad5102 | 79 | #error TWI0 is off |
claytonk | 7:9c77eaad5102 | 80 | #endif |
claytonk | 7:9c77eaad5102 | 81 | |
claytonk | 7:9c77eaad5102 | 82 | #if (TWI1_ENABLED == 1) |
claytonk | 7:9c77eaad5102 | 83 | #error TWI1 is congigured wrong |
claytonk | 7:9c77eaad5102 | 84 | #endif |
claytonk | 6:85d9ad912c14 | 85 | |
claytonk | 3:6b14862f5f51 | 86 | /* DEFINES */ |
claytonk | 3:6b14862f5f51 | 87 | |
claytonk | 3:6b14862f5f51 | 88 | #define version_number 0x70 |
claytonk | 3:6b14862f5f51 | 89 | |
claytonk | 0:605fe136b7ea | 90 | // define pins for I2C |
claytonk | 0:605fe136b7ea | 91 | #define dot_sda p2 |
claytonk | 0:605fe136b7ea | 92 | #define dot_scl p4 |
claytonk | 0:605fe136b7ea | 93 | #define flood_sda p28 |
claytonk | 0:605fe136b7ea | 94 | #define flood_scl p3 |
claytonk | 0:605fe136b7ea | 95 | #define ov580_sda p30 |
claytonk | 0:605fe136b7ea | 96 | #define ov580_scl p31 |
claytonk | 3:6b14862f5f51 | 97 | |
claytonk | 3:6b14862f5f51 | 98 | // define strobe pins |
claytonk | 9:69e0bbfdb62b | 99 | #define strobe_flood p23 // strobe0 |
claytonk | 9:69e0bbfdb62b | 100 | #define strobe_dot p24 // strobe1 |
claytonk | 3:6b14862f5f51 | 101 | |
claytonk | 3:6b14862f5f51 | 102 | // define pmw pins |
claytonk | 0:605fe136b7ea | 103 | #define pwm_1_pin p29 |
claytonk | 0:605fe136b7ea | 104 | #define pwm_0_pin p5 |
claytonk | 0:605fe136b7ea | 105 | |
claytonk | 0:605fe136b7ea | 106 | // define LED pins |
claytonk | 0:605fe136b7ea | 107 | #define ledRed p12 |
claytonk | 0:605fe136b7ea | 108 | #define ledGreen p17 |
claytonk | 0:605fe136b7ea | 109 | #define ledBlue p13 |
claytonk | 0:605fe136b7ea | 110 | |
claytonk | 4:fc0c93cb0197 | 111 | /* THREAD */ |
claytonk | 4:fc0c93cb0197 | 112 | EventQueue queue; |
claytonk | 4:fc0c93cb0197 | 113 | |
claytonk | 6:85d9ad912c14 | 114 | /* TIMOUT */ |
claytonk | 8:10ffd42a3921 | 115 | Timeout dot_timeout; |
claytonk | 8:10ffd42a3921 | 116 | Timeout flood_timeout; |
claytonk | 9:69e0bbfdb62b | 117 | Timeout app_timeout; |
claytonk | 6:85d9ad912c14 | 118 | |
claytonk | 4:fc0c93cb0197 | 119 | /* INTERRUPTS */ |
claytonk | 4:fc0c93cb0197 | 120 | //create interupts |
claytonk | 4:fc0c93cb0197 | 121 | InterruptIn int_strobe_dot(strobe_dot); |
claytonk | 9:69e0bbfdb62b | 122 | InterruptIn int_strobe_flood(strobe_flood); |
claytonk | 3:6b14862f5f51 | 123 | |
claytonk | 3:6b14862f5f51 | 124 | /* I/O */ |
claytonk | 0:605fe136b7ea | 125 | // initialize LEDs |
claytonk | 0:605fe136b7ea | 126 | DigitalOut red(ledRed,1); |
claytonk | 0:605fe136b7ea | 127 | DigitalOut green(ledGreen,1); |
claytonk | 0:605fe136b7ea | 128 | DigitalOut blue(ledBlue,1); |
claytonk | 0:605fe136b7ea | 129 | |
claytonk | 0:605fe136b7ea | 130 | // Initialize outputs |
claytonk | 3:6b14862f5f51 | 131 | DigitalOut pwm_0_output(pwm_0_pin,0); // GPIO 3 |
claytonk | 3:6b14862f5f51 | 132 | DigitalOut pwm_1_output(pwm_1_pin,0); // GPIO 4 |
claytonk | 0:605fe136b7ea | 133 | |
claytonk | 0:605fe136b7ea | 134 | // Initialize inputs |
claytonk | 7:9c77eaad5102 | 135 | DigitalIn strobe0(strobe_flood,PullNone); |
claytonk | 7:9c77eaad5102 | 136 | DigitalIn strobe1(strobe_dot,PullNone); |
claytonk | 9:69e0bbfdb62b | 137 | DigitalIn vcselFault(p25,PullNone); // vcselFault is 1V8 instead of 3V3 |
claytonk | 9:69e0bbfdb62b | 138 | DigitalIn killVcsel(p26,PullNone); |
claytonk | 0:605fe136b7ea | 139 | |
claytonk | 7:9c77eaad5102 | 140 | |
claytonk | 3:6b14862f5f51 | 141 | /* REGISTERS */ |
claytonk | 0:605fe136b7ea | 142 | static uint8_t LM36011_addr = 0x64 << 1; //0xC8 |
claytonk | 0:605fe136b7ea | 143 | |
claytonk | 0:605fe136b7ea | 144 | // register names |
claytonk | 0:605fe136b7ea | 145 | static uint8_t enable_reg = 0x01; |
claytonk | 0:605fe136b7ea | 146 | static uint8_t configuration_reg = 0x02; |
claytonk | 0:605fe136b7ea | 147 | static uint8_t brightness_reg = 0x03; |
claytonk | 3:6b14862f5f51 | 148 | //static uint8_t torch_reg = 0x04; |
claytonk | 3:6b14862f5f51 | 149 | //static uint8_t flags_reg = 0x05; |
claytonk | 3:6b14862f5f51 | 150 | //static uint8_t device_id_reset_reg = 0x06; |
claytonk | 0:605fe136b7ea | 151 | |
claytonk | 0:605fe136b7ea | 152 | // register settings |
claytonk | 0:605fe136b7ea | 153 | static uint8_t enable_ir = 0x05; |
claytonk | 0:605fe136b7ea | 154 | static uint8_t disable_ir = 0x20; |
claytonk | 0:605fe136b7ea | 155 | static uint8_t enable_flash_timeout = 0x01; |
claytonk | 0:605fe136b7ea | 156 | |
claytonk | 0:605fe136b7ea | 157 | // level settings |
claytonk | 12:9c9a532cd7d6 | 158 | //static uint8_t level_flood_max = 0x66; // = 1.2 A |
claytonk | 12:9c9a532cd7d6 | 159 | static uint8_t level_flood_max = 0x3C; // = 0.70 A |
claytonk | 12:9c9a532cd7d6 | 160 | //static uint8_t level_dot_max = 0x5F; // = 1.03 A |
claytonk | 0:605fe136b7ea | 161 | //static uint8_t level_dot_max = 0x3F; // = 0.75 A |
claytonk | 0:605fe136b7ea | 162 | //static uint8_t level_dot_max = 0x3C; // = 0.70 A |
claytonk | 0:605fe136b7ea | 163 | //static uint8_t level_dot_max = 0x15 //0.257 A |
claytonk | 11:236d223ba3bc | 164 | //static uint8_t level_dot_max = 0x1F; // = 352mA |
claytonk | 12:9c9a532cd7d6 | 165 | static uint8_t level_dot_max = 0x60; // 1.1A |
claytonk | 0:605fe136b7ea | 166 | |
claytonk | 3:6b14862f5f51 | 167 | char lm_on[2] = {enable_reg,enable_ir}; |
claytonk | 3:6b14862f5f51 | 168 | char lm_off[2] = {enable_reg,disable_ir}; |
claytonk | 0:605fe136b7ea | 169 | char lmSafety[2] = {configuration_reg,enable_flash_timeout}; |
claytonk | 0:605fe136b7ea | 170 | |
claytonk | 0:605fe136b7ea | 171 | char flashBrightness_dot[2] = {brightness_reg,level_dot_max}; |
claytonk | 0:605fe136b7ea | 172 | char flashBrightness_flood[2] = {brightness_reg,level_flood_max}; |
claytonk | 0:605fe136b7ea | 173 | |
claytonk | 3:6b14862f5f51 | 174 | /* I2C */ |
claytonk | 9:69e0bbfdb62b | 175 | //I2CSlave ov_I2C(ov580_sda,ov580_scl); |
claytonk | 0:605fe136b7ea | 176 | I2C flood_I2C(flood_sda,flood_scl); |
claytonk | 0:605fe136b7ea | 177 | I2C dot_I2C(dot_sda,dot_scl); |
claytonk | 6:85d9ad912c14 | 178 | |
claytonk | 0:605fe136b7ea | 179 | |
claytonk | 3:6b14862f5f51 | 180 | /* VARIABLES */ |
claytonk | 3:6b14862f5f51 | 181 | bool stacked = false; |
claytonk | 3:6b14862f5f51 | 182 | bool emitter_status_dot = false; |
claytonk | 7:9c77eaad5102 | 183 | char rcv_buffer[3] = {0,0,0}; |
claytonk | 8:10ffd42a3921 | 184 | int dot_counter = 0; |
claytonk | 6:85d9ad912c14 | 185 | bool dot_on = false; |
claytonk | 8:10ffd42a3921 | 186 | int flood_counter = 0; |
claytonk | 6:85d9ad912c14 | 187 | bool flood_on = false; |
claytonk | 6:85d9ad912c14 | 188 | bool once = false; |
claytonk | 8:10ffd42a3921 | 189 | bool in_app = false; |
claytonk | 8:10ffd42a3921 | 190 | int app_counts_required = 10; |
claytonk | 8:10ffd42a3921 | 191 | int stacked_counter = 0; |
claytonk | 3:6b14862f5f51 | 192 | |
claytonk | 3:6b14862f5f51 | 193 | /* FUNCTIONS */ |
claytonk | 1:ffffa383ba7e | 194 | |
claytonk | 6:85d9ad912c14 | 195 | // WAI |
claytonk | 6:85d9ad912c14 | 196 | void write_off() |
claytonk | 6:85d9ad912c14 | 197 | { |
claytonk | 6:85d9ad912c14 | 198 | dot_I2C.write(LM36011_addr,lm_off,2,false); |
claytonk | 6:85d9ad912c14 | 199 | flood_I2C.write(LM36011_addr,lm_off,2,false); |
claytonk | 6:85d9ad912c14 | 200 | } |
claytonk | 6:85d9ad912c14 | 201 | |
claytonk | 8:10ffd42a3921 | 202 | // WAI |
claytonk | 6:85d9ad912c14 | 203 | void write_dot() |
claytonk | 1:ffffa383ba7e | 204 | { |
claytonk | 6:85d9ad912c14 | 205 | flood_I2C.write(LM36011_addr,lm_off,2,false); |
claytonk | 6:85d9ad912c14 | 206 | dot_I2C.write(LM36011_addr,lm_on,2,false); |
claytonk | 6:85d9ad912c14 | 207 | } |
claytonk | 6:85d9ad912c14 | 208 | |
claytonk | 8:10ffd42a3921 | 209 | // WAI |
claytonk | 6:85d9ad912c14 | 210 | void write_flood() |
claytonk | 6:85d9ad912c14 | 211 | { |
claytonk | 6:85d9ad912c14 | 212 | dot_I2C.write(LM36011_addr,lm_off,2,false); |
claytonk | 6:85d9ad912c14 | 213 | flood_I2C.write(LM36011_addr,lm_on,2,false); |
claytonk | 6:85d9ad912c14 | 214 | } |
claytonk | 6:85d9ad912c14 | 215 | |
claytonk | 8:10ffd42a3921 | 216 | // WAI |
claytonk | 6:85d9ad912c14 | 217 | void write_pulsed() |
claytonk | 6:85d9ad912c14 | 218 | { |
claytonk | 6:85d9ad912c14 | 219 | if(emitter_status_dot) { |
claytonk | 6:85d9ad912c14 | 220 | write_dot(); |
claytonk | 6:85d9ad912c14 | 221 | } else { |
claytonk | 6:85d9ad912c14 | 222 | write_flood(); |
claytonk | 6:85d9ad912c14 | 223 | } |
claytonk | 1:ffffa383ba7e | 224 | } |
claytonk | 1:ffffa383ba7e | 225 | |
claytonk | 8:10ffd42a3921 | 226 | // WAI |
claytonk | 4:fc0c93cb0197 | 227 | void write_once() |
claytonk | 4:fc0c93cb0197 | 228 | { |
claytonk | 8:10ffd42a3921 | 229 | if(in_app) { |
claytonk | 8:10ffd42a3921 | 230 | if(stacked_counter > app_counts_required) { |
claytonk | 8:10ffd42a3921 | 231 | write_pulsed(); |
claytonk | 8:10ffd42a3921 | 232 | write_pulsed(); |
claytonk | 4:fc0c93cb0197 | 233 | } else { |
claytonk | 8:10ffd42a3921 | 234 | if(dot_on) { |
claytonk | 8:10ffd42a3921 | 235 | write_dot(); |
claytonk | 8:10ffd42a3921 | 236 | write_dot(); |
claytonk | 8:10ffd42a3921 | 237 | } else if(flood_on) { |
claytonk | 8:10ffd42a3921 | 238 | write_flood(); |
claytonk | 8:10ffd42a3921 | 239 | write_flood(); |
claytonk | 8:10ffd42a3921 | 240 | } else { |
claytonk | 8:10ffd42a3921 | 241 | write_off(); |
claytonk | 8:10ffd42a3921 | 242 | write_off(); |
claytonk | 8:10ffd42a3921 | 243 | } |
claytonk | 4:fc0c93cb0197 | 244 | } |
claytonk | 8:10ffd42a3921 | 245 | } else { |
claytonk | 8:10ffd42a3921 | 246 | write_off(); |
claytonk | 8:10ffd42a3921 | 247 | write_off(); |
claytonk | 6:85d9ad912c14 | 248 | } |
claytonk | 6:85d9ad912c14 | 249 | } |
claytonk | 6:85d9ad912c14 | 250 | |
claytonk | 8:10ffd42a3921 | 251 | void dot_watchdog() |
claytonk | 6:85d9ad912c14 | 252 | { |
claytonk | 8:10ffd42a3921 | 253 | dot_on = false; |
claytonk | 8:10ffd42a3921 | 254 | dot_counter = 0; |
claytonk | 8:10ffd42a3921 | 255 | stacked_counter = 0; |
claytonk | 8:10ffd42a3921 | 256 | stacked = false; |
claytonk | 8:10ffd42a3921 | 257 | queue.call(&write_once); |
claytonk | 8:10ffd42a3921 | 258 | } |
claytonk | 8:10ffd42a3921 | 259 | |
claytonk | 8:10ffd42a3921 | 260 | void flood_watchdog() |
claytonk | 8:10ffd42a3921 | 261 | { |
claytonk | 8:10ffd42a3921 | 262 | flood_on = false; |
claytonk | 8:10ffd42a3921 | 263 | flood_counter = 0; |
claytonk | 8:10ffd42a3921 | 264 | stacked_counter = 0; |
claytonk | 8:10ffd42a3921 | 265 | stacked = false; |
claytonk | 8:10ffd42a3921 | 266 | queue.call(&write_once); |
claytonk | 8:10ffd42a3921 | 267 | } |
claytonk | 8:10ffd42a3921 | 268 | |
claytonk | 9:69e0bbfdb62b | 269 | void app_watchdog() |
claytonk | 9:69e0bbfdb62b | 270 | { |
claytonk | 9:69e0bbfdb62b | 271 | in_app = false; |
claytonk | 9:69e0bbfdb62b | 272 | dot_watchdog(); |
claytonk | 9:69e0bbfdb62b | 273 | flood_watchdog(); |
claytonk | 9:69e0bbfdb62b | 274 | } |
claytonk | 9:69e0bbfdb62b | 275 | |
claytonk | 8:10ffd42a3921 | 276 | void dot_falling_edge() |
claytonk | 8:10ffd42a3921 | 277 | { |
claytonk | 6:85d9ad912c14 | 278 | |
claytonk | 6:85d9ad912c14 | 279 | if(stacked) { |
claytonk | 6:85d9ad912c14 | 280 | emitter_status_dot = !emitter_status_dot; |
claytonk | 4:fc0c93cb0197 | 281 | } else { |
claytonk | 6:85d9ad912c14 | 282 | emitter_status_dot = dot_on; |
claytonk | 4:fc0c93cb0197 | 283 | } |
claytonk | 8:10ffd42a3921 | 284 | // indicate status dot |
claytonk | 8:10ffd42a3921 | 285 | pwm_0_output = emitter_status_dot; |
claytonk | 8:10ffd42a3921 | 286 | // indicate VCSEL fault if it exists |
claytonk | 8:10ffd42a3921 | 287 | pwm_1_output = !vcselFault.read(); |
claytonk | 8:10ffd42a3921 | 288 | |
claytonk | 8:10ffd42a3921 | 289 | // write once |
claytonk | 8:10ffd42a3921 | 290 | queue.call(&write_once); |
claytonk | 8:10ffd42a3921 | 291 | |
claytonk | 9:69e0bbfdb62b | 292 | // timeout for dot |
claytonk | 8:10ffd42a3921 | 293 | dot_timeout.attach(&dot_watchdog,.05); |
claytonk | 9:69e0bbfdb62b | 294 | // timout for app |
claytonk | 9:69e0bbfdb62b | 295 | app_timeout.detach(); |
claytonk | 9:69e0bbfdb62b | 296 | app_timeout.attach(&app_watchdog,1); |
claytonk | 4:fc0c93cb0197 | 297 | } |
claytonk | 4:fc0c93cb0197 | 298 | |
claytonk | 8:10ffd42a3921 | 299 | void flood_falling_edge() |
claytonk | 4:fc0c93cb0197 | 300 | { |
claytonk | 8:10ffd42a3921 | 301 | if (!stacked) { |
claytonk | 8:10ffd42a3921 | 302 | emitter_status_dot = dot_on; |
claytonk | 6:85d9ad912c14 | 303 | } |
claytonk | 8:10ffd42a3921 | 304 | // indicate status dot |
claytonk | 8:10ffd42a3921 | 305 | pwm_0_output = emitter_status_dot; |
claytonk | 8:10ffd42a3921 | 306 | // indicate VCSEL fault if it exists |
claytonk | 8:10ffd42a3921 | 307 | pwm_1_output = !vcselFault.read(); |
claytonk | 8:10ffd42a3921 | 308 | // timeout for app exit |
claytonk | 8:10ffd42a3921 | 309 | //timeout.attach(&write_off,.5); |
claytonk | 8:10ffd42a3921 | 310 | // write once |
claytonk | 8:10ffd42a3921 | 311 | queue.call(&write_once); |
claytonk | 4:fc0c93cb0197 | 312 | |
claytonk | 9:69e0bbfdb62b | 313 | // timeout for flood |
claytonk | 8:10ffd42a3921 | 314 | flood_timeout.attach(&flood_watchdog,.05); |
claytonk | 9:69e0bbfdb62b | 315 | // timout for app |
claytonk | 9:69e0bbfdb62b | 316 | app_timeout.detach(); |
claytonk | 9:69e0bbfdb62b | 317 | app_timeout.attach(&app_watchdog,1); |
claytonk | 8:10ffd42a3921 | 318 | } |
claytonk | 4:fc0c93cb0197 | 319 | |
claytonk | 6:85d9ad912c14 | 320 | void dot_check() |
claytonk | 6:85d9ad912c14 | 321 | { |
claytonk | 8:10ffd42a3921 | 322 | dot_timeout.detach(); |
claytonk | 6:85d9ad912c14 | 323 | |
claytonk | 8:10ffd42a3921 | 324 | dot_counter ++; |
claytonk | 8:10ffd42a3921 | 325 | if(dot_counter > app_counts_required) { |
claytonk | 8:10ffd42a3921 | 326 | dot_on = true; |
claytonk | 10:c757bd889ed1 | 327 | in_app = true; |
claytonk | 8:10ffd42a3921 | 328 | } else { |
claytonk | 8:10ffd42a3921 | 329 | dot_on = false; |
claytonk | 8:10ffd42a3921 | 330 | } |
claytonk | 8:10ffd42a3921 | 331 | stacked = strobe0.read(); |
claytonk | 8:10ffd42a3921 | 332 | if(stacked) { |
claytonk | 8:10ffd42a3921 | 333 | stacked_counter++; |
claytonk | 8:10ffd42a3921 | 334 | } else { |
claytonk | 8:10ffd42a3921 | 335 | stacked_counter = 0; |
claytonk | 8:10ffd42a3921 | 336 | flood_counter = 0; |
claytonk | 8:10ffd42a3921 | 337 | flood_on = false; |
claytonk | 8:10ffd42a3921 | 338 | } |
claytonk | 8:10ffd42a3921 | 339 | if(!in_app) { |
claytonk | 8:10ffd42a3921 | 340 | if (stacked_counter > app_counts_required) { |
claytonk | 8:10ffd42a3921 | 341 | in_app = true; |
claytonk | 8:10ffd42a3921 | 342 | } |
claytonk | 8:10ffd42a3921 | 343 | } |
claytonk | 6:85d9ad912c14 | 344 | } |
claytonk | 6:85d9ad912c14 | 345 | |
claytonk | 6:85d9ad912c14 | 346 | |
claytonk | 6:85d9ad912c14 | 347 | void flood_check() |
claytonk | 6:85d9ad912c14 | 348 | { |
claytonk | 8:10ffd42a3921 | 349 | flood_timeout.detach(); |
claytonk | 6:85d9ad912c14 | 350 | |
claytonk | 8:10ffd42a3921 | 351 | flood_counter ++; |
claytonk | 8:10ffd42a3921 | 352 | if(flood_counter > app_counts_required) { |
claytonk | 8:10ffd42a3921 | 353 | flood_on = true; |
claytonk | 10:c757bd889ed1 | 354 | in_app = true; |
claytonk | 8:10ffd42a3921 | 355 | } else { |
claytonk | 8:10ffd42a3921 | 356 | flood_on = false; |
claytonk | 8:10ffd42a3921 | 357 | } |
claytonk | 8:10ffd42a3921 | 358 | stacked = strobe1.read(); |
claytonk | 8:10ffd42a3921 | 359 | if(stacked) { |
claytonk | 8:10ffd42a3921 | 360 | stacked_counter++; |
claytonk | 8:10ffd42a3921 | 361 | } else { |
claytonk | 8:10ffd42a3921 | 362 | stacked_counter = 0; |
claytonk | 8:10ffd42a3921 | 363 | dot_counter = 0; |
claytonk | 8:10ffd42a3921 | 364 | dot_on = false; |
claytonk | 8:10ffd42a3921 | 365 | } |
claytonk | 8:10ffd42a3921 | 366 | if(!in_app) { |
claytonk | 8:10ffd42a3921 | 367 | if (stacked_counter > app_counts_required) { |
claytonk | 8:10ffd42a3921 | 368 | in_app = true; |
claytonk | 8:10ffd42a3921 | 369 | } |
claytonk | 8:10ffd42a3921 | 370 | } |
claytonk | 6:85d9ad912c14 | 371 | } |
claytonk | 6:85d9ad912c14 | 372 | |
claytonk | 6:85d9ad912c14 | 373 | // TODOS // |
claytonk | 7:9c77eaad5102 | 374 | // DONE P0 Get illumination working |
claytonk | 8:10ffd42a3921 | 375 | // P0 Get OV580 I2C to look correct. Suspect open drain culprit |
claytonk | 9:69e0bbfdb62b | 376 | // DONE P0 Ensure that current implementation meets the needs of data collection |
claytonk | 8:10ffd42a3921 | 377 | // DONE P0 Get in app working |
claytonk | 8:10ffd42a3921 | 378 | // DONE P0 Get watchdog time working |
claytonk | 8:10ffd42a3921 | 379 | // DONE P0 Check watchdog for working on dot and flood |
claytonk | 6:85d9ad912c14 | 380 | // P1 Bluetooth OTA updates |
claytonk | 6:85d9ad912c14 | 381 | // P2 Get writing working |
claytonk | 6:85d9ad912c14 | 382 | // P2 Get reading working |
claytonk | 6:85d9ad912c14 | 383 | // |
claytonk | 6:85d9ad912c14 | 384 | |
claytonk | 0:605fe136b7ea | 385 | // main() runs in its own thread in the OS |
claytonk | 1:ffffa383ba7e | 386 | int main() |
claytonk | 1:ffffa383ba7e | 387 | { |
claytonk | 4:fc0c93cb0197 | 388 | Thread eventThread(osPriorityHigh);; |
claytonk | 4:fc0c93cb0197 | 389 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
claytonk | 4:fc0c93cb0197 | 390 | |
claytonk | 4:fc0c93cb0197 | 391 | // set interrupts |
claytonk | 6:85d9ad912c14 | 392 | int_strobe_dot.rise(&dot_check); |
claytonk | 6:85d9ad912c14 | 393 | int_strobe_flood.rise(&flood_check); |
claytonk | 8:10ffd42a3921 | 394 | int_strobe_dot.fall(&dot_falling_edge); |
claytonk | 8:10ffd42a3921 | 395 | int_strobe_flood.fall(&flood_falling_edge); |
claytonk | 8:10ffd42a3921 | 396 | |
claytonk | 8:10ffd42a3921 | 397 | while(!in_app) { |
claytonk | 9:69e0bbfdb62b | 398 | wait(.1); |
claytonk | 8:10ffd42a3921 | 399 | } |
claytonk | 1:ffffa383ba7e | 400 | |
claytonk | 0:605fe136b7ea | 401 | // write safety |
claytonk | 0:605fe136b7ea | 402 | flood_I2C.write(LM36011_addr,lmSafety,2,false); |
claytonk | 0:605fe136b7ea | 403 | dot_I2C.write(LM36011_addr,lmSafety,2,false); |
claytonk | 3:6b14862f5f51 | 404 | |
claytonk | 1:ffffa383ba7e | 405 | // write brightness |
claytonk | 1:ffffa383ba7e | 406 | flood_I2C.write(LM36011_addr,flashBrightness_flood,2,false); |
claytonk | 3:6b14862f5f51 | 407 | dot_I2C.write(LM36011_addr,flashBrightness_dot,2,false); |
claytonk | 1:ffffa383ba7e | 408 | |
claytonk | 0:605fe136b7ea | 409 | while (true) { |
claytonk | 9:69e0bbfdb62b | 410 | wait(.1); |
claytonk | 0:605fe136b7ea | 411 | } |
claytonk | 0:605fe136b7ea | 412 | } |
claytonk | 0:605fe136b7ea | 413 |