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