Nucleo-transfer
Dependencies: ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813
Fork of Nucleo-transfer by
Sensorplate/main.cpp@11:73c6def38fbd, 2017-09-28 (annotated)
- Committer:
- ricardo_95
- Date:
- Thu Sep 28 09:44:38 2017 +0000
- Revision:
- 11:73c6def38fbd
- Parent:
- 10:6b3034ec3c47
- Child:
- 12:7b3a5940f911
Pi to uC message fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DEldering | 0:c0e44c46c573 | 1 | #include "mbed.h" |
DEldering | 0:c0e44c46c573 | 2 | #include "Adafruit_ADS1015.h" |
DEldering | 0:c0e44c46c573 | 3 | #include "MPU6050.h" |
ricardo_95 | 7:dba5091c8b7d | 4 | #include "neopixel.h" |
ricardo_95 | 7:dba5091c8b7d | 5 | #define NLED (11) |
ricardo_95 | 7:dba5091c8b7d | 6 | #define ONE_COLOR |
ricardo_95 | 7:dba5091c8b7d | 7 | |
ricardo_95 | 7:dba5091c8b7d | 8 | InterruptIn lock(p16); // Interrupts for buttons. |
ricardo_95 | 7:dba5091c8b7d | 9 | InterruptIn reposition(p17); |
ricardo_95 | 7:dba5091c8b7d | 10 | InterruptIn mute(p15); |
ricardo_95 | 7:dba5091c8b7d | 11 | InterruptIn new_patient(p14); |
ricardo_95 | 8:bf0f7a6fb1fd | 12 | //AnalogIn LDR_val(p18); |
ricardo_95 | 8:bf0f7a6fb1fd | 13 | AnalogIn batteryvoltage(p18); // Analog input between 0 and 1 (0 and 100 %) for reading batteryvoltage from accupack. |
ricardo_95 | 8:bf0f7a6fb1fd | 14 | AnalogIn supplyvoltage(p20); // Analog input between 0 and 1 (0 and 100 %) for reading supplyvoltage from measuringpoint before power supply. |
ricardo_95 | 7:dba5091c8b7d | 15 | |
ricardo_95 | 7:dba5091c8b7d | 16 | PwmOut LED_intern1(LED1); |
ricardo_95 | 7:dba5091c8b7d | 17 | DigitalOut LED_intern2(LED2); |
ricardo_95 | 11:73c6def38fbd | 18 | DigitalOut LED_intern3(LED3); |
ricardo_95 | 11:73c6def38fbd | 19 | DigitalOut LED_intern4(LED4); |
ricardo_95 | 7:dba5091c8b7d | 20 | neopixel::PixelArray array(p11); |
ricardo_95 | 7:dba5091c8b7d | 21 | |
ricardo_95 | 7:dba5091c8b7d | 22 | Timer hold_timer; |
ricardo_95 | 7:dba5091c8b7d | 23 | Timer delay; |
ricardo_95 | 7:dba5091c8b7d | 24 | Timer speaker_timer; |
ricardo_95 | 7:dba5091c8b7d | 25 | |
ricardo_95 | 7:dba5091c8b7d | 26 | DigitalOut speaker1(p21); |
ricardo_95 | 7:dba5091c8b7d | 27 | DigitalOut speaker2(p22); |
DEldering | 0:c0e44c46c573 | 28 | |
DEldering | 1:a8e61f3910ad | 29 | I2C i2c(p28, p27); // I2C |
DEldering | 1:a8e61f3910ad | 30 | MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit |
DEldering | 1:a8e61f3910ad | 31 | Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 32 | Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 33 | Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC |
DEldering | 1:a8e61f3910ad | 34 | Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection |
ricardo_95 | 8:bf0f7a6fb1fd | 35 | Serial pi(p9, p10, 115200); // Setup serial communication for pi. |
DEldering | 1:a8e61f3910ad | 36 | Timer t; // Timer for equally time-spaced samples |
DEldering | 1:a8e61f3910ad | 37 | Ticker sample_cycle; // Polling cycle |
ricardo_95 | 8:bf0f7a6fb1fd | 38 | |
DEldering | 1:a8e61f3910ad | 39 | int cycle_time = 100000; // Cycle time in us |
DEldering | 1:a8e61f3910ad | 40 | int i2c_freq = 400000; // I2C Frequency |
DEldering | 1:a8e61f3910ad | 41 | int usb_baud = 115200; // USB Baud rate |
DEldering | 1:a8e61f3910ad | 42 | short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle |
DEldering | 1:a8e61f3910ad | 43 | short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle |
DEldering | 1:a8e61f3910ad | 44 | int angle = 0; // Accelerometer Z-axis |
DEldering | 1:a8e61f3910ad | 45 | int k = 0; |
DEldering | 1:a8e61f3910ad | 46 | float acce[3]; // Raw accelerometer data |
DEldering | 1:a8e61f3910ad | 47 | float gyro[3]; // Raw gyroscope data |
ricardo_95 | 7:dba5091c8b7d | 48 | char LED_colour; // Variable to set LED colour. |
ricardo_95 | 11:73c6def38fbd | 49 | bool lock_state, lock_flag, mute_state, alarm, calibration_flag, intensity_select; // Boolean variables for states logging. |
ricardo_95 | 8:bf0f7a6fb1fd | 50 | bool mute_flag, new_patient_flag, reposition_flag; |
ricardo_95 | 8:bf0f7a6fb1fd | 51 | bool speaker_state, LED_red_state, LED_yellow_state, LED_green_state, power_plug_state; |
ricardo_95 | 8:bf0f7a6fb1fd | 52 | bool speaker_logged, LED_red_logged, LED_yellow_logged, LED_green_logged, power_plug_logged; |
ricardo_95 | 11:73c6def38fbd | 53 | int locktime_ms = 2000; // Waittime for lock user interface in ms. |
ricardo_95 | 11:73c6def38fbd | 54 | int calibrationtime_ms = 5000; // Time to press new_patient button for calibration system. |
ricardo_95 | 11:73c6def38fbd | 55 | int calibration_flash; // Variable for flash LED's to indicate calibration. |
ricardo_95 | 7:dba5091c8b7d | 56 | int buttondelay_ms = 750; // Button delay in ms. |
ricardo_95 | 7:dba5091c8b7d | 57 | int delay_lock_interface = 3000*60; // Delay for non using interface locktime. |
ricardo_95 | 11:73c6def38fbd | 58 | int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs. |
ricardo_95 | 7:dba5091c8b7d | 59 | double alarm_voltage = 0.2; // Needed voltage for alarm expressed as a percentage (0 - 100 % => 0 - 3.3 V). |
ricardo_95 | 11:73c6def38fbd | 60 | int red_var, green_var, blue_var, intensity, current_intensity = 0; // Variables to set LED intensity. |
ricardo_95 | 8:bf0f7a6fb1fd | 61 | int batteryvoltage_current = 0, batteryvoltage_last = 0; |
ricardo_95 | 7:dba5091c8b7d | 62 | |
ricardo_95 | 11:73c6def38fbd | 63 | void set_intensity() // Function to set the intensity for the LED's. |
ricardo_95 | 7:dba5091c8b7d | 64 | { |
ricardo_95 | 11:73c6def38fbd | 65 | if (intensity_select == 1) { |
ricardo_95 | 7:dba5091c8b7d | 66 | intensity = 50; |
ricardo_95 | 7:dba5091c8b7d | 67 | } else { |
ricardo_95 | 7:dba5091c8b7d | 68 | intensity = 25; |
ricardo_95 | 7:dba5091c8b7d | 69 | } |
ricardo_95 | 11:73c6def38fbd | 70 | //intensity = (1-LDR_val)*100; // Calculate intensity (use right part of the graphic). |
ricardo_95 | 7:dba5091c8b7d | 71 | |
ricardo_95 | 7:dba5091c8b7d | 72 | //if (abs(intensity-current_intensity) > 5) { // If difference is greater then 5, change intensity dependent on range. |
ricardo_95 | 7:dba5091c8b7d | 73 | // if (intensity <= 20) { |
ricardo_95 | 7:dba5091c8b7d | 74 | // intensity = 20; |
ricardo_95 | 7:dba5091c8b7d | 75 | // } |
ricardo_95 | 7:dba5091c8b7d | 76 | |
ricardo_95 | 7:dba5091c8b7d | 77 | // if (40 >= intensity > 20) { |
ricardo_95 | 7:dba5091c8b7d | 78 | // intensity = 40; |
ricardo_95 | 7:dba5091c8b7d | 79 | // } |
ricardo_95 | 7:dba5091c8b7d | 80 | |
ricardo_95 | 7:dba5091c8b7d | 81 | // if (60 >= intensity > 40) { |
ricardo_95 | 7:dba5091c8b7d | 82 | // intensity = 60; |
ricardo_95 | 7:dba5091c8b7d | 83 | // } |
ricardo_95 | 7:dba5091c8b7d | 84 | |
ricardo_95 | 7:dba5091c8b7d | 85 | // if (80 >= intensity > 60) { |
ricardo_95 | 7:dba5091c8b7d | 86 | // intensity = 80; |
ricardo_95 | 7:dba5091c8b7d | 87 | // } |
ricardo_95 | 7:dba5091c8b7d | 88 | |
ricardo_95 | 7:dba5091c8b7d | 89 | // if (intensity > 80) { |
ricardo_95 | 7:dba5091c8b7d | 90 | // intensity = 100; |
ricardo_95 | 7:dba5091c8b7d | 91 | // } |
ricardo_95 | 7:dba5091c8b7d | 92 | //} |
ricardo_95 | 7:dba5091c8b7d | 93 | // current_intensity = intensity; // Save intensisty to compare in first if statement of this set_intensity function. |
ricardo_95 | 7:dba5091c8b7d | 94 | } |
ricardo_95 | 7:dba5091c8b7d | 95 | |
ricardo_95 | 11:73c6def38fbd | 96 | void serial_read() { // Serial read for colourselect |
ricardo_95 | 11:73c6def38fbd | 97 | if (pi.readable()) { |
ricardo_95 | 11:73c6def38fbd | 98 | char message[3]; |
ricardo_95 | 11:73c6def38fbd | 99 | pi.scanf("%s",message); |
ricardo_95 | 11:73c6def38fbd | 100 | message[strlen(message)] = '\n'; |
ricardo_95 | 11:73c6def38fbd | 101 | |
ricardo_95 | 11:73c6def38fbd | 102 | if (message[0] == '0') { |
ricardo_95 | 11:73c6def38fbd | 103 | intensity_select = 0; |
ricardo_95 | 11:73c6def38fbd | 104 | } |
ricardo_95 | 11:73c6def38fbd | 105 | |
ricardo_95 | 11:73c6def38fbd | 106 | if (message[0] == '1') { |
ricardo_95 | 11:73c6def38fbd | 107 | intensity_select = 1; |
ricardo_95 | 11:73c6def38fbd | 108 | } |
ricardo_95 | 11:73c6def38fbd | 109 | |
ricardo_95 | 11:73c6def38fbd | 110 | if (message[1] == 'g') { |
ricardo_95 | 11:73c6def38fbd | 111 | LED_colour = 'g'; |
ricardo_95 | 11:73c6def38fbd | 112 | } |
ricardo_95 | 11:73c6def38fbd | 113 | |
ricardo_95 | 11:73c6def38fbd | 114 | if (message[1] == 'y') { |
ricardo_95 | 11:73c6def38fbd | 115 | LED_colour = 'y'; |
ricardo_95 | 11:73c6def38fbd | 116 | } |
ricardo_95 | 11:73c6def38fbd | 117 | |
ricardo_95 | 11:73c6def38fbd | 118 | if (message[1] == 'r') { |
ricardo_95 | 11:73c6def38fbd | 119 | LED_colour = 'r'; |
ricardo_95 | 11:73c6def38fbd | 120 | } |
ricardo_95 | 11:73c6def38fbd | 121 | } |
ricardo_95 | 11:73c6def38fbd | 122 | } |
ricardo_95 | 11:73c6def38fbd | 123 | |
ricardo_95 | 7:dba5091c8b7d | 124 | void colour_select(char LED_colour) // Function to select the colour. |
ricardo_95 | 7:dba5091c8b7d | 125 | { |
ricardo_95 | 7:dba5091c8b7d | 126 | set_intensity(); // Call function set_intensity |
ricardo_95 | 7:dba5091c8b7d | 127 | |
ricardo_95 | 7:dba5091c8b7d | 128 | if (LED_colour == 'r') { |
ricardo_95 | 7:dba5091c8b7d | 129 | red_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 130 | green_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 131 | blue_var = 0; |
ricardo_95 | 8:bf0f7a6fb1fd | 132 | LED_red_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 133 | } else { |
ricardo_95 | 8:bf0f7a6fb1fd | 134 | LED_red_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 135 | } |
ricardo_95 | 7:dba5091c8b7d | 136 | |
ricardo_95 | 7:dba5091c8b7d | 137 | if (LED_colour == 'y') { |
ricardo_95 | 7:dba5091c8b7d | 138 | red_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 139 | green_var = (2.55*intensity); |
ricardo_95 | 8:bf0f7a6fb1fd | 140 | blue_var = 0; |
ricardo_95 | 8:bf0f7a6fb1fd | 141 | LED_yellow_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 142 | } else { |
ricardo_95 | 8:bf0f7a6fb1fd | 143 | LED_green_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 144 | } |
ricardo_95 | 7:dba5091c8b7d | 145 | |
ricardo_95 | 7:dba5091c8b7d | 146 | if (LED_colour == 'g') { |
ricardo_95 | 7:dba5091c8b7d | 147 | red_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 148 | green_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 149 | blue_var = 0; |
ricardo_95 | 8:bf0f7a6fb1fd | 150 | LED_green_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 151 | } else { |
ricardo_95 | 8:bf0f7a6fb1fd | 152 | LED_green_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 153 | } |
ricardo_95 | 7:dba5091c8b7d | 154 | |
ricardo_95 | 7:dba5091c8b7d | 155 | if (calibration_flash >= 1) { |
ricardo_95 | 7:dba5091c8b7d | 156 | if ((calibration_flash % 2) == 0) { |
ricardo_95 | 7:dba5091c8b7d | 157 | red_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 158 | green_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 159 | blue_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 160 | } else { |
ricardo_95 | 7:dba5091c8b7d | 161 | red_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 162 | green_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 163 | blue_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 164 | } |
ricardo_95 | 7:dba5091c8b7d | 165 | calibration_flash--; |
ricardo_95 | 7:dba5091c8b7d | 166 | } |
ricardo_95 | 7:dba5091c8b7d | 167 | } |
ricardo_95 | 7:dba5091c8b7d | 168 | |
ricardo_95 | 7:dba5091c8b7d | 169 | void trigger_lock() // If rising edge lock button is detected start locktimer. |
ricardo_95 | 7:dba5091c8b7d | 170 | { |
ricardo_95 | 7:dba5091c8b7d | 171 | hold_timer.start(); |
ricardo_95 | 7:dba5091c8b7d | 172 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 173 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 174 | } |
ricardo_95 | 7:dba5091c8b7d | 175 | |
ricardo_95 | 7:dba5091c8b7d | 176 | void timer_lock() // End timer lock. |
ricardo_95 | 7:dba5091c8b7d | 177 | { |
ricardo_95 | 7:dba5091c8b7d | 178 | lock_flag = 0; // Set lock_flag off. |
ricardo_95 | 7:dba5091c8b7d | 179 | hold_timer.stop(); // Stop and reset holdtimer |
ricardo_95 | 7:dba5091c8b7d | 180 | hold_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 181 | } |
ricardo_95 | 7:dba5091c8b7d | 182 | |
ricardo_95 | 7:dba5091c8b7d | 183 | void trigger_reposition() |
ricardo_95 | 7:dba5091c8b7d | 184 | { |
ricardo_95 | 7:dba5091c8b7d | 185 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 186 | } else { |
ricardo_95 | 7:dba5091c8b7d | 187 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 188 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 189 | |
ricardo_95 | 7:dba5091c8b7d | 190 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 191 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 192 | } else { |
ricardo_95 | 7:dba5091c8b7d | 193 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 194 | } |
ricardo_95 | 7:dba5091c8b7d | 195 | |
ricardo_95 | 8:bf0f7a6fb1fd | 196 | reposition_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 197 | } |
ricardo_95 | 7:dba5091c8b7d | 198 | } |
ricardo_95 | 7:dba5091c8b7d | 199 | |
ricardo_95 | 7:dba5091c8b7d | 200 | void trigger_mute() |
ricardo_95 | 7:dba5091c8b7d | 201 | { |
ricardo_95 | 7:dba5091c8b7d | 202 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 203 | mute_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 204 | } else { |
ricardo_95 | 7:dba5091c8b7d | 205 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 206 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 207 | pi.printf("01\n"); |
ricardo_95 | 7:dba5091c8b7d | 208 | mute_state = !mute_state; |
ricardo_95 | 7:dba5091c8b7d | 209 | |
ricardo_95 | 7:dba5091c8b7d | 210 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 211 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 212 | } else { |
ricardo_95 | 7:dba5091c8b7d | 213 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 214 | } |
ricardo_95 | 11:73c6def38fbd | 215 | |
ricardo_95 | 8:bf0f7a6fb1fd | 216 | mute_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 217 | } |
ricardo_95 | 7:dba5091c8b7d | 218 | } |
ricardo_95 | 7:dba5091c8b7d | 219 | |
ricardo_95 | 7:dba5091c8b7d | 220 | void trigger_new_patient() // Function to trigger hold timer for new patient calibration function. |
ricardo_95 | 7:dba5091c8b7d | 221 | { |
ricardo_95 | 7:dba5091c8b7d | 222 | if (lock_state == 1) { |
ricardo_95 | 7:dba5091c8b7d | 223 | } else { |
ricardo_95 | 7:dba5091c8b7d | 224 | hold_timer.start(); |
ricardo_95 | 8:bf0f7a6fb1fd | 225 | new_patient_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 226 | } |
ricardo_95 | 7:dba5091c8b7d | 227 | } |
ricardo_95 | 7:dba5091c8b7d | 228 | |
ricardo_95 | 7:dba5091c8b7d | 229 | void timer_calibration() // Timer calibration function. |
ricardo_95 | 7:dba5091c8b7d | 230 | { |
ricardo_95 | 7:dba5091c8b7d | 231 | hold_timer.stop(); |
ricardo_95 | 7:dba5091c8b7d | 232 | hold_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 233 | |
ricardo_95 | 7:dba5091c8b7d | 234 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 235 | } else { |
ricardo_95 | 7:dba5091c8b7d | 236 | if (calibration_flag == 0) { |
ricardo_95 | 8:bf0f7a6fb1fd | 237 | |
ricardo_95 | 7:dba5091c8b7d | 238 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 239 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 240 | } else { |
ricardo_95 | 7:dba5091c8b7d | 241 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 242 | } |
ricardo_95 | 11:73c6def38fbd | 243 | |
ricardo_95 | 7:dba5091c8b7d | 244 | } else { |
ricardo_95 | 7:dba5091c8b7d | 245 | calibration_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 246 | } |
ricardo_95 | 7:dba5091c8b7d | 247 | } |
ricardo_95 | 7:dba5091c8b7d | 248 | } |
ricardo_95 | 7:dba5091c8b7d | 249 | |
ricardo_95 | 7:dba5091c8b7d | 250 | void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour. |
ricardo_95 | 7:dba5091c8b7d | 251 | { |
ricardo_95 | 7:dba5091c8b7d | 252 | out->red = red_var; |
ricardo_95 | 7:dba5091c8b7d | 253 | out->green = green_var; |
ricardo_95 | 7:dba5091c8b7d | 254 | out->blue = blue_var; |
ricardo_95 | 7:dba5091c8b7d | 255 | } |
ricardo_95 | 7:dba5091c8b7d | 256 | |
ricardo_95 | 7:dba5091c8b7d | 257 | void read_voltage() |
ricardo_95 | 7:dba5091c8b7d | 258 | { |
ricardo_95 | 7:dba5091c8b7d | 259 | LED_intern3 = 0; |
ricardo_95 | 8:bf0f7a6fb1fd | 260 | |
ricardo_95 | 8:bf0f7a6fb1fd | 261 | if (batteryvoltage.read() > alarm_voltage) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage. |
ricardo_95 | 7:dba5091c8b7d | 262 | alarm = 0; // Alarm is off. |
ricardo_95 | 8:bf0f7a6fb1fd | 263 | speaker_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 264 | } else { |
ricardo_95 | 7:dba5091c8b7d | 265 | alarm = 1; // Else alarm is on. |
ricardo_95 | 8:bf0f7a6fb1fd | 266 | speaker_state = 1; |
ricardo_95 | 7:dba5091c8b7d | 267 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 268 | |
ricardo_95 | 7:dba5091c8b7d | 269 | if (alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. |
ricardo_95 | 7:dba5091c8b7d | 270 | speaker1 = 1; // Set speaker. |
ricardo_95 | 7:dba5091c8b7d | 271 | speaker2 = 1; |
ricardo_95 | 7:dba5091c8b7d | 272 | speaker_timer.start(); // Set timer for speaker to iterate on and off. |
ricardo_95 | 7:dba5091c8b7d | 273 | LED_intern3 = !LED_intern3; |
ricardo_95 | 7:dba5091c8b7d | 274 | } |
ricardo_95 | 7:dba5091c8b7d | 275 | |
ricardo_95 | 7:dba5091c8b7d | 276 | if (alarm == 1 && mute_state == 1 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. |
ricardo_95 | 7:dba5091c8b7d | 277 | speaker1 = 0; // Set speaker. |
ricardo_95 | 7:dba5091c8b7d | 278 | speaker2 = 0; |
ricardo_95 | 7:dba5091c8b7d | 279 | speaker_timer.start(); // Set timer for speaker to iterate on and off. |
ricardo_95 | 7:dba5091c8b7d | 280 | LED_intern3 = !LED_intern3; |
ricardo_95 | 7:dba5091c8b7d | 281 | } |
ricardo_95 | 7:dba5091c8b7d | 282 | |
ricardo_95 | 7:dba5091c8b7d | 283 | if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) { |
ricardo_95 | 7:dba5091c8b7d | 284 | speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one). |
ricardo_95 | 7:dba5091c8b7d | 285 | speaker2 = 0; |
ricardo_95 | 7:dba5091c8b7d | 286 | } |
ricardo_95 | 7:dba5091c8b7d | 287 | |
ricardo_95 | 7:dba5091c8b7d | 288 | if (speaker_timer.read_ms() > (speaker_active_ms*2)) { |
ricardo_95 | 7:dba5091c8b7d | 289 | speaker_timer.stop(); // Stop speaker timer. |
ricardo_95 | 7:dba5091c8b7d | 290 | speaker_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 291 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 292 | |
ricardo_95 | 8:bf0f7a6fb1fd | 293 | batteryvoltage_current = batteryvoltage.read(); |
ricardo_95 | 8:bf0f7a6fb1fd | 294 | |
ricardo_95 | 8:bf0f7a6fb1fd | 295 | if (supplyvoltage.read() == 0) { |
ricardo_95 | 8:bf0f7a6fb1fd | 296 | power_plug_state = 1; |
ricardo_95 | 8:bf0f7a6fb1fd | 297 | } else { |
ricardo_95 | 8:bf0f7a6fb1fd | 298 | power_plug_state = 0; |
ricardo_95 | 8:bf0f7a6fb1fd | 299 | } |
ricardo_95 | 7:dba5091c8b7d | 300 | } |
DEldering | 0:c0e44c46c573 | 301 | |
DEldering | 0:c0e44c46c573 | 302 | void read_adc() |
DEldering | 0:c0e44c46c573 | 303 | { |
DEldering | 1:a8e61f3910ad | 304 | t.reset(); |
DEldering | 1:a8e61f3910ad | 305 | t.start(); |
DEldering | 1:a8e61f3910ad | 306 | |
DEldering | 4:367af005956b | 307 | elec[0] = pel.readADC_SingleEnded(0); //First PE readout |
DEldering | 1:a8e61f3910ad | 308 | |
DEldering | 0:c0e44c46c573 | 309 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 4:367af005956b | 310 | res[k] = pr1.readADC_SingleEnded(k); //First 4 PR readout |
DEldering | 1:a8e61f3910ad | 311 | } |
DEldering | 4:367af005956b | 312 | while(t.read_us()<(1*(cycle_time/5))) {} //Wait untill 20% of cycle |
DEldering | 1:a8e61f3910ad | 313 | |
DEldering | 4:367af005956b | 314 | elec[1] = pel.readADC_SingleEnded(0); //Second PE readout |
DEldering | 1:a8e61f3910ad | 315 | |
DEldering | 1:a8e61f3910ad | 316 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 4:367af005956b | 317 | res[k+4] = pr2.readADC_SingleEnded(k); //Last 4 PR readout |
DEldering | 0:c0e44c46c573 | 318 | } |
ricardo_95 | 7:dba5091c8b7d | 319 | while(t.read_us()<(2*(cycle_time/5))) {} //Wait untill 40% of cycle |
DEldering | 1:a8e61f3910ad | 320 | |
DEldering | 4:367af005956b | 321 | elec[2] = pel.readADC_SingleEnded(0); //Third PE readout |
DEldering | 1:a8e61f3910ad | 322 | |
DEldering | 4:367af005956b | 323 | agu.getAccelero(acce); //Get accelerometer data |
DEldering | 1:a8e61f3910ad | 324 | angle = acce[2]*10; |
ricardo_95 | 7:dba5091c8b7d | 325 | agu.getGyro(gyro); //Get gyroscope data |
ricardo_95 | 7:dba5091c8b7d | 326 | |
DEldering | 4:367af005956b | 327 | while(t.read_us()<(3*(cycle_time/5))) {} //Wait untill 60% of cycle |
DEldering | 1:a8e61f3910ad | 328 | |
DEldering | 4:367af005956b | 329 | elec[3] = pel.readADC_SingleEnded(0); //Fourth PE readout |
DEldering | 1:a8e61f3910ad | 330 | |
ricardo_95 | 7:dba5091c8b7d | 331 | if ((hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 1) { // If statement for lock function. |
ricardo_95 | 7:dba5091c8b7d | 332 | lock_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 333 | LED_intern2 = !LED_intern2; |
ricardo_95 | 7:dba5091c8b7d | 334 | lock_state = !lock_state; |
ricardo_95 | 7:dba5091c8b7d | 335 | } |
ricardo_95 | 7:dba5091c8b7d | 336 | |
ricardo_95 | 7:dba5091c8b7d | 337 | if ((hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 1) { // If statement for calibration system. |
ricardo_95 | 7:dba5091c8b7d | 338 | calibration_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 339 | calibration_flash = 11; |
ricardo_95 | 7:dba5091c8b7d | 340 | pi.printf("Calibration button is pressed."); // Print statement for serial communication to inform algorithm to calibrate. |
ricardo_95 | 7:dba5091c8b7d | 341 | } |
DEldering | 1:a8e61f3910ad | 342 | |
ricardo_95 | 7:dba5091c8b7d | 343 | if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active. |
ricardo_95 | 7:dba5091c8b7d | 344 | lock_state = 1; |
ricardo_95 | 7:dba5091c8b7d | 345 | LED_intern2 = 1; |
ricardo_95 | 7:dba5091c8b7d | 346 | } |
ricardo_95 | 7:dba5091c8b7d | 347 | |
ricardo_95 | 8:bf0f7a6fb1fd | 348 | batteryvoltage_current = batteryvoltage_current*100; |
ricardo_95 | 8:bf0f7a6fb1fd | 349 | batteryvoltage_current = batteryvoltage_last; |
ricardo_95 | 7:dba5091c8b7d | 350 | read_voltage(); // Supplyvoltage control for alarm. |
ricardo_95 | 8:bf0f7a6fb1fd | 351 | |
ricardo_95 | 7:dba5091c8b7d | 352 | uint32_t val = 0; |
ricardo_95 | 7:dba5091c8b7d | 353 | colour_select(LED_colour); |
ricardo_95 | 7:dba5091c8b7d | 354 | array.update(generate, NLED, val); |
ricardo_95 | 7:dba5091c8b7d | 355 | |
DEldering | 4:367af005956b | 356 | while(t.read_us()<(4*(cycle_time/5))) {} //Wait untill 80% of cycle |
DEldering | 1:a8e61f3910ad | 357 | |
DEldering | 4:367af005956b | 358 | elec[4] = pel.readADC_SingleEnded(0); //Fifth PE readout |
DEldering | 1:a8e61f3910ad | 359 | |
DEldering | 4:367af005956b | 360 | while(t.read_us()<(4.5*(cycle_time/5))) {} //Wait untill 90% of cycle |
ricardo_95 | 11:73c6def38fbd | 361 | pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", res[4], res[7], res[6], res[5], res[1], res[0], res[2], res[3], elec[0], elec[1], elec[2], elec[3], elec[4], acce[0]*100, acce[1]*100, acce[2]*100, gyro[0]*100, gyro[1]*100, gyro[2]*100); // print all to serial port |
DEldering | 6:9c1944f3ebe5 | 362 | //receiving order: 8 resistive sensors, 5 electric readings, 3 accelerometer axes, 3 gyroscope axes |
ricardo_95 | 7:dba5091c8b7d | 363 | |
ricardo_95 | 7:dba5091c8b7d | 364 | if (mute_flag == 1) { |
ricardo_95 | 7:dba5091c8b7d | 365 | pi.printf(">01\n"); |
ricardo_95 | 7:dba5091c8b7d | 366 | mute_flag = 0; |
ricardo_95 | 11:73c6def38fbd | 367 | LED_intern4 = 1; |
ricardo_95 | 11:73c6def38fbd | 368 | } else { |
ricardo_95 | 11:73c6def38fbd | 369 | LED_intern4 = 0; |
ricardo_95 | 7:dba5091c8b7d | 370 | } |
ricardo_95 | 7:dba5091c8b7d | 371 | |
ricardo_95 | 7:dba5091c8b7d | 372 | if (new_patient_flag == 1) { |
ricardo_95 | 7:dba5091c8b7d | 373 | pi.printf(">02\n"); |
ricardo_95 | 7:dba5091c8b7d | 374 | new_patient_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 375 | } |
ricardo_95 | 7:dba5091c8b7d | 376 | |
ricardo_95 | 7:dba5091c8b7d | 377 | if (reposition_flag == 1) { |
ricardo_95 | 7:dba5091c8b7d | 378 | pi.printf(">03\n"); |
ricardo_95 | 7:dba5091c8b7d | 379 | reposition_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 380 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 381 | |
ricardo_95 | 8:bf0f7a6fb1fd | 382 | if (batteryvoltage_current != batteryvoltage_last) { |
ricardo_95 | 8:bf0f7a6fb1fd | 383 | pi.printf("%%d\n", batteryvoltage_current); |
ricardo_95 | 9:514a44bf510f | 384 | } |
ricardo_95 | 9:514a44bf510f | 385 | |
ricardo_95 | 9:514a44bf510f | 386 | if (LED_red_logged != LED_red_state) { |
ricardo_95 | 9:514a44bf510f | 387 | if (LED_red_state == 1) { |
ricardo_95 | 9:514a44bf510f | 388 | pi.printf("&04\n"); |
ricardo_95 | 9:514a44bf510f | 389 | LED_red_logged = LED_red_state; |
ricardo_95 | 9:514a44bf510f | 390 | } |
ricardo_95 | 9:514a44bf510f | 391 | if (LED_red_state == 0) { |
ricardo_95 | 9:514a44bf510f | 392 | pi.printf("&40\n"); |
ricardo_95 | 9:514a44bf510f | 393 | LED_red_logged = LED_red_state; |
ricardo_95 | 9:514a44bf510f | 394 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 395 | } |
ricardo_95 | 9:514a44bf510f | 396 | |
ricardo_95 | 9:514a44bf510f | 397 | if (LED_yellow_logged != LED_yellow_state) { |
ricardo_95 | 9:514a44bf510f | 398 | if (LED_yellow_state == 1) { |
ricardo_95 | 9:514a44bf510f | 399 | pi.printf("&06\n"); |
ricardo_95 | 9:514a44bf510f | 400 | LED_yellow_logged = LED_yellow_state; |
ricardo_95 | 9:514a44bf510f | 401 | } |
ricardo_95 | 9:514a44bf510f | 402 | if (LED_yellow_state == 0) { |
ricardo_95 | 9:514a44bf510f | 403 | pi.printf("&60\n"); |
ricardo_95 | 9:514a44bf510f | 404 | LED_yellow_logged = LED_yellow_state; |
ricardo_95 | 9:514a44bf510f | 405 | } |
ricardo_95 | 7:dba5091c8b7d | 406 | } |
ricardo_95 | 7:dba5091c8b7d | 407 | |
ricardo_95 | 9:514a44bf510f | 408 | if (LED_green_logged != LED_green_state) { |
ricardo_95 | 9:514a44bf510f | 409 | if (LED_green_state == 1) { |
ricardo_95 | 9:514a44bf510f | 410 | pi.printf("&05\n"); |
ricardo_95 | 9:514a44bf510f | 411 | LED_green_logged = LED_green_state; |
ricardo_95 | 9:514a44bf510f | 412 | } |
ricardo_95 | 9:514a44bf510f | 413 | if (LED_green_state == 0) { |
ricardo_95 | 9:514a44bf510f | 414 | pi.printf("&50\n"); |
ricardo_95 | 9:514a44bf510f | 415 | LED_green_logged = LED_green_state; |
ricardo_95 | 9:514a44bf510f | 416 | } |
ricardo_95 | 7:dba5091c8b7d | 417 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 418 | |
ricardo_95 | 9:514a44bf510f | 419 | if (speaker_logged != speaker_state) { |
ricardo_95 | 9:514a44bf510f | 420 | if (speaker_state == 1) { |
ricardo_95 | 9:514a44bf510f | 421 | pi.printf("&07\n"); |
ricardo_95 | 9:514a44bf510f | 422 | speaker_logged = speaker_state; |
ricardo_95 | 9:514a44bf510f | 423 | } |
ricardo_95 | 9:514a44bf510f | 424 | if (speaker_state == 0) { |
ricardo_95 | 9:514a44bf510f | 425 | pi.printf("&70\n"); |
ricardo_95 | 9:514a44bf510f | 426 | speaker_logged = speaker_state; |
ricardo_95 | 9:514a44bf510f | 427 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 428 | } |
ricardo_95 | 9:514a44bf510f | 429 | |
ricardo_95 | 9:514a44bf510f | 430 | if (power_plug_logged != power_plug_state) { |
ricardo_95 | 9:514a44bf510f | 431 | if (power_plug_state == 1) { |
ricardo_95 | 10:6b3034ec3c47 | 432 | pi.printf("#08\n"); |
ricardo_95 | 9:514a44bf510f | 433 | power_plug_logged = power_plug_state; |
ricardo_95 | 9:514a44bf510f | 434 | } |
ricardo_95 | 9:514a44bf510f | 435 | if (power_plug_state == 0) { |
ricardo_95 | 10:6b3034ec3c47 | 436 | pi.printf("#80\n"); |
ricardo_95 | 9:514a44bf510f | 437 | power_plug_logged = power_plug_state; |
ricardo_95 | 9:514a44bf510f | 438 | } |
ricardo_95 | 9:514a44bf510f | 439 | } |
DEldering | 0:c0e44c46c573 | 440 | } |
DEldering | 0:c0e44c46c573 | 441 | |
DEldering | 0:c0e44c46c573 | 442 | int main() |
DEldering | 0:c0e44c46c573 | 443 | { |
DEldering | 1:a8e61f3910ad | 444 | i2c.frequency(i2c_freq); |
DEldering | 1:a8e61f3910ad | 445 | pc.baud(usb_baud); |
DEldering | 1:a8e61f3910ad | 446 | pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 447 | pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 448 | pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
ricardo_95 | 7:dba5091c8b7d | 449 | pi.format(8, SerialBase::None, 1); |
ricardo_95 | 7:dba5091c8b7d | 450 | |
ricardo_95 | 7:dba5091c8b7d | 451 | lock.rise(&trigger_lock); // Interrupt for rising edge lock button. |
ricardo_95 | 7:dba5091c8b7d | 452 | lock.fall(&timer_lock); |
ricardo_95 | 7:dba5091c8b7d | 453 | reposition.rise(&trigger_reposition); |
ricardo_95 | 7:dba5091c8b7d | 454 | mute.rise(&trigger_mute); |
ricardo_95 | 7:dba5091c8b7d | 455 | new_patient.rise(&trigger_new_patient); // New patient/calibration button rising event. |
ricardo_95 | 7:dba5091c8b7d | 456 | new_patient.fall(&timer_calibration); // Falling edge for calibration algorithm option. |
ricardo_95 | 7:dba5091c8b7d | 457 | delay.reset(); // Delaytimer reset en start. |
ricardo_95 | 7:dba5091c8b7d | 458 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 459 | |
DEldering | 1:a8e61f3910ad | 460 | sample_cycle.attach_us(&read_adc, cycle_time); |
ricardo_95 | 7:dba5091c8b7d | 461 | |
DEldering | 0:c0e44c46c573 | 462 | while (1) { |
DEldering | 1:a8e61f3910ad | 463 | wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms |
DEldering | 0:c0e44c46c573 | 464 | } |
ricardo_95 | 7:dba5091c8b7d | 465 | } |
ricardo_95 | 7:dba5091c8b7d | 466 | |
ricardo_95 | 7:dba5091c8b7d | 467 | /* |
ricardo_95 | 7:dba5091c8b7d | 468 | Author : R. Molenaar |
ricardo_95 | 7:dba5091c8b7d | 469 | Company : Momo Medical |
ricardo_95 | 7:dba5091c8b7d | 470 | Source : developer.mbed.org |
ricardo_95 | 7:dba5091c8b7d | 471 | File : main.cpp |
ricardo_95 | 7:dba5091c8b7d | 472 | Version | -date : 0.1 | 18-9-2017 |
ricardo_95 | 7:dba5091c8b7d | 473 | |
ricardo_95 | 7:dba5091c8b7d | 474 | |
ricardo_95 | 7:dba5091c8b7d | 475 | #include "mbed.h" |
ricardo_95 | 7:dba5091c8b7d | 476 | #include "pwm_tone.h" |
ricardo_95 | 7:dba5091c8b7d | 477 | #include "neopixel.h" |
ricardo_95 | 7:dba5091c8b7d | 478 | #define NLED (11) |
ricardo_95 | 7:dba5091c8b7d | 479 | #define ONE_COLOR |
ricardo_95 | 7:dba5091c8b7d | 480 | |
ricardo_95 | 7:dba5091c8b7d | 481 | Serial pi(p9, p10, 9600); // Setup serial communication. |
ricardo_95 | 7:dba5091c8b7d | 482 | Serial pc(USBTX, USBRX, 9600); |
ricardo_95 | 7:dba5091c8b7d | 483 | |
ricardo_95 | 7:dba5091c8b7d | 484 | InterruptIn lock(p16); // Interrupts for buttons. |
ricardo_95 | 7:dba5091c8b7d | 485 | InterruptIn reposition(p17); |
ricardo_95 | 7:dba5091c8b7d | 486 | InterruptIn mute(p15); |
ricardo_95 | 7:dba5091c8b7d | 487 | InterruptIn new_patient(p14); |
ricardo_95 | 7:dba5091c8b7d | 488 | |
ricardo_95 | 7:dba5091c8b7d | 489 | AnalogIn LDR_val(p18); |
ricardo_95 | 7:dba5091c8b7d | 490 | AnalogIn supplyvoltage(p20); // Analog input between 0 and 1 (0 and 100 %) for reading supplyvoltage from accupack. |
ricardo_95 | 7:dba5091c8b7d | 491 | |
ricardo_95 | 7:dba5091c8b7d | 492 | PwmOut LED_intern1(LED1); |
ricardo_95 | 7:dba5091c8b7d | 493 | DigitalOut LED_intern2(LED2); |
ricardo_95 | 7:dba5091c8b7d | 494 | DigitalOut LED_intern3(LED4); |
ricardo_95 | 7:dba5091c8b7d | 495 | |
ricardo_95 | 7:dba5091c8b7d | 496 | Timer hold_timer; |
ricardo_95 | 7:dba5091c8b7d | 497 | Timer delay; |
ricardo_95 | 7:dba5091c8b7d | 498 | Timer speaker_timer; |
ricardo_95 | 7:dba5091c8b7d | 499 | |
ricardo_95 | 7:dba5091c8b7d | 500 | DigitalOut speaker1(p21); |
ricardo_95 | 7:dba5091c8b7d | 501 | DigitalOut speaker2(p22); |
ricardo_95 | 7:dba5091c8b7d | 502 | |
ricardo_95 | 7:dba5091c8b7d | 503 | char LED_colour; // Variable to set LED colour. |
ricardo_95 | 7:dba5091c8b7d | 504 | bool lock_state, lock_flag, mute_state, alarm, calibration_flag; // Boolean variables for states lock, mute and alarm. |
ricardo_95 | 7:dba5091c8b7d | 505 | int locktime_ms = 2000; // Waittime in ms. |
ricardo_95 | 7:dba5091c8b7d | 506 | int calibrationtime_ms = 5000; |
ricardo_95 | 7:dba5091c8b7d | 507 | int calibration_flash; |
ricardo_95 | 7:dba5091c8b7d | 508 | int buttondelay_ms = 750; // Button delay in ms. |
ricardo_95 | 7:dba5091c8b7d | 509 | int delay_lock_interface = 3000*60; // Delay for non using interface. |
ricardo_95 | 7:dba5091c8b7d | 510 | int speaker_active_ms = 750; |
ricardo_95 | 7:dba5091c8b7d | 511 | double alarm_voltage = 0.2; // Needed voltage for alarm expressed as a percentage (0 - 100 % => 0 - 3.3 V). |
ricardo_95 | 7:dba5091c8b7d | 512 | int red_var, green_var, blue_var, intensity, current_intensity = 0; // Variables to set LED intensity |
ricardo_95 | 7:dba5091c8b7d | 513 | |
ricardo_95 | 7:dba5091c8b7d | 514 | void set_intensity() // Function to set the intensity for the LED's |
ricardo_95 | 7:dba5091c8b7d | 515 | { |
ricardo_95 | 7:dba5091c8b7d | 516 | intensity = (1-LDR_val)*100; // Calculate intensity (use right part of the graphic) |
ricardo_95 | 7:dba5091c8b7d | 517 | |
ricardo_95 | 7:dba5091c8b7d | 518 | if (abs(intensity-current_intensity) > 5) { // If difference is greater then 5, change intensity dependent on range. |
ricardo_95 | 7:dba5091c8b7d | 519 | if (intensity <= 20) { |
ricardo_95 | 7:dba5091c8b7d | 520 | intensity = 20; |
ricardo_95 | 7:dba5091c8b7d | 521 | } |
ricardo_95 | 7:dba5091c8b7d | 522 | |
ricardo_95 | 7:dba5091c8b7d | 523 | if (40 >= intensity > 20) { |
ricardo_95 | 7:dba5091c8b7d | 524 | intensity = 40; |
ricardo_95 | 7:dba5091c8b7d | 525 | } |
ricardo_95 | 7:dba5091c8b7d | 526 | |
ricardo_95 | 7:dba5091c8b7d | 527 | if (60 >= intensity > 40) { |
ricardo_95 | 7:dba5091c8b7d | 528 | intensity = 60; |
ricardo_95 | 7:dba5091c8b7d | 529 | } |
ricardo_95 | 7:dba5091c8b7d | 530 | |
ricardo_95 | 7:dba5091c8b7d | 531 | if (80 >= intensity > 60) { |
ricardo_95 | 7:dba5091c8b7d | 532 | intensity = 80; |
ricardo_95 | 7:dba5091c8b7d | 533 | } |
ricardo_95 | 7:dba5091c8b7d | 534 | |
ricardo_95 | 7:dba5091c8b7d | 535 | if (intensity > 80) { |
ricardo_95 | 7:dba5091c8b7d | 536 | intensity = 100; |
ricardo_95 | 7:dba5091c8b7d | 537 | } |
ricardo_95 | 7:dba5091c8b7d | 538 | } |
ricardo_95 | 7:dba5091c8b7d | 539 | current_intensity = intensity; // Save intensisty to compare in first if statement of this set_intensity function. |
ricardo_95 | 7:dba5091c8b7d | 540 | } |
ricardo_95 | 7:dba5091c8b7d | 541 | |
ricardo_95 | 7:dba5091c8b7d | 542 | void colour_select(char LED_colour) // Function to select the colour. |
ricardo_95 | 7:dba5091c8b7d | 543 | { |
ricardo_95 | 7:dba5091c8b7d | 544 | set_intensity(); // Call function set_intensity |
ricardo_95 | 7:dba5091c8b7d | 545 | |
ricardo_95 | 7:dba5091c8b7d | 546 | if (LED_colour == 'r') { |
ricardo_95 | 7:dba5091c8b7d | 547 | red_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 548 | green_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 549 | blue_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 550 | } |
ricardo_95 | 7:dba5091c8b7d | 551 | |
ricardo_95 | 7:dba5091c8b7d | 552 | if (LED_colour == 'y') { |
ricardo_95 | 7:dba5091c8b7d | 553 | red_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 554 | green_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 555 | blue_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 556 | } |
ricardo_95 | 7:dba5091c8b7d | 557 | |
ricardo_95 | 7:dba5091c8b7d | 558 | if (LED_colour == 'g') { |
ricardo_95 | 7:dba5091c8b7d | 559 | red_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 560 | green_var = (2.55*intensity); |
ricardo_95 | 7:dba5091c8b7d | 561 | blue_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 562 | } |
ricardo_95 | 7:dba5091c8b7d | 563 | |
ricardo_95 | 7:dba5091c8b7d | 564 | if (calibration_flash >= 1) { |
ricardo_95 | 7:dba5091c8b7d | 565 | if((calibration_flash % 2) == 0) { |
ricardo_95 | 7:dba5091c8b7d | 566 | red_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 567 | green_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 568 | blue_var = 255; |
ricardo_95 | 7:dba5091c8b7d | 569 | } else { |
ricardo_95 | 7:dba5091c8b7d | 570 | red_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 571 | green_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 572 | blue_var = 0; |
ricardo_95 | 7:dba5091c8b7d | 573 | } |
ricardo_95 | 7:dba5091c8b7d | 574 | calibration_flash--; |
ricardo_95 | 7:dba5091c8b7d | 575 | } |
ricardo_95 | 7:dba5091c8b7d | 576 | } |
ricardo_95 | 7:dba5091c8b7d | 577 | |
ricardo_95 | 7:dba5091c8b7d | 578 | |
ricardo_95 | 7:dba5091c8b7d | 579 | void trigger_lock() // If rising edge lock button is detected start locktimer. |
ricardo_95 | 7:dba5091c8b7d | 580 | { |
ricardo_95 | 7:dba5091c8b7d | 581 | hold_timer.start(); |
ricardo_95 | 7:dba5091c8b7d | 582 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 583 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 584 | } |
ricardo_95 | 7:dba5091c8b7d | 585 | |
ricardo_95 | 7:dba5091c8b7d | 586 | void timer_lock() // End timer lock. |
ricardo_95 | 7:dba5091c8b7d | 587 | { |
ricardo_95 | 7:dba5091c8b7d | 588 | lock_flag = 0; // Set lock_flag off. |
ricardo_95 | 7:dba5091c8b7d | 589 | hold_timer.stop(); // Stop and reset holdtimer |
ricardo_95 | 7:dba5091c8b7d | 590 | hold_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 591 | } |
ricardo_95 | 7:dba5091c8b7d | 592 | |
ricardo_95 | 7:dba5091c8b7d | 593 | void trigger_reposition() |
ricardo_95 | 7:dba5091c8b7d | 594 | { |
ricardo_95 | 7:dba5091c8b7d | 595 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 596 | } else { |
ricardo_95 | 7:dba5091c8b7d | 597 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 598 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 599 | pi.printf("02\n"); // Seriele communicatie met PI. |
ricardo_95 | 7:dba5091c8b7d | 600 | |
ricardo_95 | 7:dba5091c8b7d | 601 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 602 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 603 | } else { |
ricardo_95 | 7:dba5091c8b7d | 604 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 605 | } |
ricardo_95 | 7:dba5091c8b7d | 606 | |
ricardo_95 | 7:dba5091c8b7d | 607 | LED_colour = 'r'; |
ricardo_95 | 7:dba5091c8b7d | 608 | } |
ricardo_95 | 7:dba5091c8b7d | 609 | } |
ricardo_95 | 7:dba5091c8b7d | 610 | |
ricardo_95 | 7:dba5091c8b7d | 611 | void trigger_mute() |
ricardo_95 | 7:dba5091c8b7d | 612 | { |
ricardo_95 | 7:dba5091c8b7d | 613 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 614 | mute_state = 0; |
ricardo_95 | 7:dba5091c8b7d | 615 | } else { |
ricardo_95 | 7:dba5091c8b7d | 616 | delay.reset(); |
ricardo_95 | 7:dba5091c8b7d | 617 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 618 | pi.printf("01\n"); |
ricardo_95 | 7:dba5091c8b7d | 619 | mute_state = !mute_state; |
ricardo_95 | 7:dba5091c8b7d | 620 | |
ricardo_95 | 7:dba5091c8b7d | 621 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 622 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 623 | } else { |
ricardo_95 | 7:dba5091c8b7d | 624 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 625 | } |
ricardo_95 | 7:dba5091c8b7d | 626 | |
ricardo_95 | 7:dba5091c8b7d | 627 | LED_colour = 'y'; |
ricardo_95 | 7:dba5091c8b7d | 628 | } |
ricardo_95 | 7:dba5091c8b7d | 629 | } |
ricardo_95 | 7:dba5091c8b7d | 630 | |
ricardo_95 | 7:dba5091c8b7d | 631 | void trigger_new_patient() // Function to trigger hold timer for new patient calibration function. |
ricardo_95 | 7:dba5091c8b7d | 632 | { |
ricardo_95 | 7:dba5091c8b7d | 633 | if (lock_state == 1) { |
ricardo_95 | 7:dba5091c8b7d | 634 | } else { |
ricardo_95 | 7:dba5091c8b7d | 635 | hold_timer.start(); |
ricardo_95 | 7:dba5091c8b7d | 636 | } |
ricardo_95 | 7:dba5091c8b7d | 637 | } |
ricardo_95 | 7:dba5091c8b7d | 638 | |
ricardo_95 | 7:dba5091c8b7d | 639 | void timer_calibration() // Timer calibration function. |
ricardo_95 | 7:dba5091c8b7d | 640 | { |
ricardo_95 | 7:dba5091c8b7d | 641 | hold_timer.stop(); |
ricardo_95 | 7:dba5091c8b7d | 642 | hold_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 643 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time. |
ricardo_95 | 7:dba5091c8b7d | 644 | } else { |
ricardo_95 | 7:dba5091c8b7d | 645 | if (calibration_flag == 0) { |
ricardo_95 | 7:dba5091c8b7d | 646 | pi.printf("03\n"); |
ricardo_95 | 7:dba5091c8b7d | 647 | |
ricardo_95 | 7:dba5091c8b7d | 648 | |
ricardo_95 | 7:dba5091c8b7d | 649 | if (LED_intern1 == 0) { |
ricardo_95 | 7:dba5091c8b7d | 650 | LED_intern1 = 1.0; |
ricardo_95 | 7:dba5091c8b7d | 651 | } else { |
ricardo_95 | 7:dba5091c8b7d | 652 | LED_intern1 = 0.0; |
ricardo_95 | 7:dba5091c8b7d | 653 | } |
ricardo_95 | 7:dba5091c8b7d | 654 | |
ricardo_95 | 7:dba5091c8b7d | 655 | LED_colour = 'g'; |
ricardo_95 | 7:dba5091c8b7d | 656 | } else { |
ricardo_95 | 7:dba5091c8b7d | 657 | calibration_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 658 | } |
ricardo_95 | 7:dba5091c8b7d | 659 | } |
ricardo_95 | 7:dba5091c8b7d | 660 | } |
ricardo_95 | 7:dba5091c8b7d | 661 | |
ricardo_95 | 7:dba5091c8b7d | 662 | void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour. |
ricardo_95 | 7:dba5091c8b7d | 663 | { |
ricardo_95 | 7:dba5091c8b7d | 664 | out->red = red_var; |
ricardo_95 | 7:dba5091c8b7d | 665 | out->green = green_var; |
ricardo_95 | 7:dba5091c8b7d | 666 | out->blue = blue_var; |
ricardo_95 | 7:dba5091c8b7d | 667 | } |
ricardo_95 | 7:dba5091c8b7d | 668 | |
ricardo_95 | 7:dba5091c8b7d | 669 | void read_voltage() |
ricardo_95 | 7:dba5091c8b7d | 670 | { |
ricardo_95 | 7:dba5091c8b7d | 671 | LED_intern3 = 0; |
ricardo_95 | 7:dba5091c8b7d | 672 | |
ricardo_95 | 7:dba5091c8b7d | 673 | if (supplyvoltage.read() > alarm_voltage) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage. |
ricardo_95 | 7:dba5091c8b7d | 674 | alarm = 0; // Alarm is off. |
ricardo_95 | 7:dba5091c8b7d | 675 | } else { |
ricardo_95 | 7:dba5091c8b7d | 676 | alarm = 1; // Else alarm is on. |
ricardo_95 | 7:dba5091c8b7d | 677 | } |
ricardo_95 | 7:dba5091c8b7d | 678 | |
ricardo_95 | 7:dba5091c8b7d | 679 | if (alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. |
ricardo_95 | 7:dba5091c8b7d | 680 | speaker1 = 1; // Set speaker. |
ricardo_95 | 7:dba5091c8b7d | 681 | speaker2 = 1; |
ricardo_95 | 7:dba5091c8b7d | 682 | speaker_timer.start(); // Set timer for speaker to iterate on and off. |
ricardo_95 | 7:dba5091c8b7d | 683 | LED_intern3 = !LED_intern3; |
ricardo_95 | 7:dba5091c8b7d | 684 | } |
ricardo_95 | 7:dba5091c8b7d | 685 | |
ricardo_95 | 7:dba5091c8b7d | 686 | if (alarm == 1 && mute_state == 1 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms. |
ricardo_95 | 7:dba5091c8b7d | 687 | speaker1 = 0; // Set speaker. |
ricardo_95 | 7:dba5091c8b7d | 688 | speaker2 = 0; |
ricardo_95 | 7:dba5091c8b7d | 689 | speaker_timer.start(); // Set timer for speaker to iterate on and off. |
ricardo_95 | 7:dba5091c8b7d | 690 | LED_intern3 = !LED_intern3; |
ricardo_95 | 7:dba5091c8b7d | 691 | } |
ricardo_95 | 7:dba5091c8b7d | 692 | |
ricardo_95 | 7:dba5091c8b7d | 693 | if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) { |
ricardo_95 | 7:dba5091c8b7d | 694 | speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one). |
ricardo_95 | 7:dba5091c8b7d | 695 | speaker2 = 0; |
ricardo_95 | 7:dba5091c8b7d | 696 | } |
ricardo_95 | 7:dba5091c8b7d | 697 | |
ricardo_95 | 7:dba5091c8b7d | 698 | if (speaker_timer.read_ms() > (speaker_active_ms*2)) { |
ricardo_95 | 7:dba5091c8b7d | 699 | speaker_timer.stop(); // Stop speaker timer. |
ricardo_95 | 7:dba5091c8b7d | 700 | speaker_timer.reset(); |
ricardo_95 | 7:dba5091c8b7d | 701 | } |
ricardo_95 | 7:dba5091c8b7d | 702 | } |
ricardo_95 | 7:dba5091c8b7d | 703 | |
ricardo_95 | 7:dba5091c8b7d | 704 | int main() |
ricardo_95 | 7:dba5091c8b7d | 705 | { |
ricardo_95 | 7:dba5091c8b7d | 706 | pi.format(8, SerialBase::None, 1); |
ricardo_95 | 7:dba5091c8b7d | 707 | |
ricardo_95 | 7:dba5091c8b7d | 708 | lock.rise(&trigger_lock); // Interrupt for rising edge lock button. |
ricardo_95 | 7:dba5091c8b7d | 709 | lock.fall(&timer_lock); |
ricardo_95 | 7:dba5091c8b7d | 710 | reposition.rise(&trigger_reposition); |
ricardo_95 | 7:dba5091c8b7d | 711 | mute.rise(&trigger_mute); |
ricardo_95 | 7:dba5091c8b7d | 712 | new_patient.rise(&trigger_new_patient); // New patient/calibration button rising event. |
ricardo_95 | 7:dba5091c8b7d | 713 | new_patient.fall(&timer_calibration); // Falling edge for calibration algorithm option. |
ricardo_95 | 7:dba5091c8b7d | 714 | delay.reset(); // Delaytimer reset en start. |
ricardo_95 | 7:dba5091c8b7d | 715 | delay.start(); |
ricardo_95 | 7:dba5091c8b7d | 716 | |
ricardo_95 | 7:dba5091c8b7d | 717 | neopixel::PixelArray array(p11); |
ricardo_95 | 7:dba5091c8b7d | 718 | |
ricardo_95 | 7:dba5091c8b7d | 719 | while(1) { |
ricardo_95 | 7:dba5091c8b7d | 720 | wait_ms(100); // Simulate 100 ms delay from sensorplate code. |
ricardo_95 | 7:dba5091c8b7d | 721 | |
ricardo_95 | 7:dba5091c8b7d | 722 | if ((hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 1) { // If statement for lock function. |
ricardo_95 | 7:dba5091c8b7d | 723 | lock_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 724 | LED_intern2 = !LED_intern2; |
ricardo_95 | 7:dba5091c8b7d | 725 | lock_state = !lock_state; |
ricardo_95 | 7:dba5091c8b7d | 726 | } |
ricardo_95 | 7:dba5091c8b7d | 727 | |
ricardo_95 | 7:dba5091c8b7d | 728 | if ((hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 1) { // If statement for calibration system. |
ricardo_95 | 7:dba5091c8b7d | 729 | calibration_flag = 1; |
ricardo_95 | 7:dba5091c8b7d | 730 | calibration_flash = 11; |
ricardo_95 | 7:dba5091c8b7d | 731 | pi.printf("Calibration button is pressed."); // Print statement for serial communication to inform algorithm to calibrate. |
ricardo_95 | 7:dba5091c8b7d | 732 | |
ricardo_95 | 7:dba5091c8b7d | 733 | if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active. |
ricardo_95 | 7:dba5091c8b7d | 734 | lock_state = 1; |
ricardo_95 | 7:dba5091c8b7d | 735 | LED_intern2 = 1; |
ricardo_95 | 7:dba5091c8b7d | 736 | } |
ricardo_95 | 7:dba5091c8b7d | 737 | |
ricardo_95 | 7:dba5091c8b7d | 738 | read_voltage(); // Supplyvoltage control for alarm. |
ricardo_95 | 7:dba5091c8b7d | 739 | |
ricardo_95 | 7:dba5091c8b7d | 740 | uint32_t val = 0; |
ricardo_95 | 7:dba5091c8b7d | 741 | colour_select(LED_colour); |
ricardo_95 | 7:dba5091c8b7d | 742 | array.update(generate, NLED, val); |
ricardo_95 | 7:dba5091c8b7d | 743 | } |
ricardo_95 | 7:dba5091c8b7d | 744 | |
ricardo_95 | 7:dba5091c8b7d | 745 | }*/ |