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