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 Momo Medical

Committer:
ricardo_95
Date:
Wed Sep 27 15:55:32 2017 +0000
Revision:
10:6b3034ec3c47
Parent:
9:514a44bf510f
Child:
11:73c6def38fbd
Serial communication uC to pi works.

Who changed what in which revision?

UserRevisionLine numberNew 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 10:6b3034ec3c47 335 pi.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 8:bf0f7a6fb1fd 352
ricardo_95 8:bf0f7a6fb1fd 353 if (batteryvoltage_current != batteryvoltage_last) {
ricardo_95 8:bf0f7a6fb1fd 354 pi.printf("%%d\n", batteryvoltage_current);
ricardo_95 9:514a44bf510f 355 }
ricardo_95 9:514a44bf510f 356
ricardo_95 9:514a44bf510f 357 if (LED_red_logged != LED_red_state) {
ricardo_95 9:514a44bf510f 358 if (LED_red_state == 1) {
ricardo_95 9:514a44bf510f 359 pi.printf("&04\n");
ricardo_95 9:514a44bf510f 360 LED_red_logged = LED_red_state;
ricardo_95 9:514a44bf510f 361 }
ricardo_95 9:514a44bf510f 362 if (LED_red_state == 0) {
ricardo_95 9:514a44bf510f 363 pi.printf("&40\n");
ricardo_95 9:514a44bf510f 364 LED_red_logged = LED_red_state;
ricardo_95 9:514a44bf510f 365 }
ricardo_95 8:bf0f7a6fb1fd 366 }
ricardo_95 9:514a44bf510f 367
ricardo_95 9:514a44bf510f 368 if (LED_yellow_logged != LED_yellow_state) {
ricardo_95 9:514a44bf510f 369 if (LED_yellow_state == 1) {
ricardo_95 9:514a44bf510f 370 pi.printf("&06\n");
ricardo_95 9:514a44bf510f 371 LED_yellow_logged = LED_yellow_state;
ricardo_95 9:514a44bf510f 372 }
ricardo_95 9:514a44bf510f 373 if (LED_yellow_state == 0) {
ricardo_95 9:514a44bf510f 374 pi.printf("&60\n");
ricardo_95 9:514a44bf510f 375 LED_yellow_logged = LED_yellow_state;
ricardo_95 9:514a44bf510f 376 }
ricardo_95 7:dba5091c8b7d 377 }
ricardo_95 7:dba5091c8b7d 378
ricardo_95 9:514a44bf510f 379 if (LED_green_logged != LED_green_state) {
ricardo_95 9:514a44bf510f 380 if (LED_green_state == 1) {
ricardo_95 9:514a44bf510f 381 pi.printf("&05\n");
ricardo_95 9:514a44bf510f 382 LED_green_logged = LED_green_state;
ricardo_95 9:514a44bf510f 383 }
ricardo_95 9:514a44bf510f 384 if (LED_green_state == 0) {
ricardo_95 9:514a44bf510f 385 pi.printf("&50\n");
ricardo_95 9:514a44bf510f 386 LED_green_logged = LED_green_state;
ricardo_95 9:514a44bf510f 387 }
ricardo_95 7:dba5091c8b7d 388 }
ricardo_95 8:bf0f7a6fb1fd 389
ricardo_95 9:514a44bf510f 390 if (speaker_logged != speaker_state) {
ricardo_95 9:514a44bf510f 391 if (speaker_state == 1) {
ricardo_95 9:514a44bf510f 392 pi.printf("&07\n");
ricardo_95 9:514a44bf510f 393 speaker_logged = speaker_state;
ricardo_95 9:514a44bf510f 394 }
ricardo_95 9:514a44bf510f 395 if (speaker_state == 0) {
ricardo_95 9:514a44bf510f 396 pi.printf("&70\n");
ricardo_95 9:514a44bf510f 397 speaker_logged = speaker_state;
ricardo_95 9:514a44bf510f 398 }
ricardo_95 8:bf0f7a6fb1fd 399 }
ricardo_95 9:514a44bf510f 400
ricardo_95 9:514a44bf510f 401 if (power_plug_logged != power_plug_state) {
ricardo_95 9:514a44bf510f 402 if (power_plug_state == 1) {
ricardo_95 10:6b3034ec3c47 403 pi.printf("#08\n");
ricardo_95 9:514a44bf510f 404 power_plug_logged = power_plug_state;
ricardo_95 9:514a44bf510f 405 }
ricardo_95 9:514a44bf510f 406 if (power_plug_state == 0) {
ricardo_95 10:6b3034ec3c47 407 pi.printf("#80\n");
ricardo_95 9:514a44bf510f 408 power_plug_logged = power_plug_state;
ricardo_95 9:514a44bf510f 409 }
ricardo_95 9:514a44bf510f 410 }
DEldering 0:c0e44c46c573 411 }
DEldering 0:c0e44c46c573 412
DEldering 0:c0e44c46c573 413 int main()
DEldering 0:c0e44c46c573 414 {
DEldering 1:a8e61f3910ad 415 i2c.frequency(i2c_freq);
DEldering 1:a8e61f3910ad 416 pc.baud(usb_baud);
DEldering 1:a8e61f3910ad 417 pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
DEldering 1:a8e61f3910ad 418 pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
DEldering 1:a8e61f3910ad 419 pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
ricardo_95 7:dba5091c8b7d 420 pi.format(8, SerialBase::None, 1);
ricardo_95 7:dba5091c8b7d 421
ricardo_95 7:dba5091c8b7d 422 lock.rise(&trigger_lock); // Interrupt for rising edge lock button.
ricardo_95 7:dba5091c8b7d 423 lock.fall(&timer_lock);
ricardo_95 7:dba5091c8b7d 424 reposition.rise(&trigger_reposition);
ricardo_95 7:dba5091c8b7d 425 mute.rise(&trigger_mute);
ricardo_95 7:dba5091c8b7d 426 new_patient.rise(&trigger_new_patient); // New patient/calibration button rising event.
ricardo_95 7:dba5091c8b7d 427 new_patient.fall(&timer_calibration); // Falling edge for calibration algorithm option.
ricardo_95 7:dba5091c8b7d 428 delay.reset(); // Delaytimer reset en start.
ricardo_95 7:dba5091c8b7d 429 delay.start();
ricardo_95 7:dba5091c8b7d 430
DEldering 1:a8e61f3910ad 431 sample_cycle.attach_us(&read_adc, cycle_time);
ricardo_95 7:dba5091c8b7d 432
DEldering 0:c0e44c46c573 433 while (1) {
DEldering 1:a8e61f3910ad 434 wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms
DEldering 0:c0e44c46c573 435 }
ricardo_95 7:dba5091c8b7d 436 }
ricardo_95 7:dba5091c8b7d 437
ricardo_95 7:dba5091c8b7d 438 /*
ricardo_95 7:dba5091c8b7d 439 Author : R. Molenaar
ricardo_95 7:dba5091c8b7d 440 Company : Momo Medical
ricardo_95 7:dba5091c8b7d 441 Source : developer.mbed.org
ricardo_95 7:dba5091c8b7d 442 File : main.cpp
ricardo_95 7:dba5091c8b7d 443 Version | -date : 0.1 | 18-9-2017
ricardo_95 7:dba5091c8b7d 444
ricardo_95 7:dba5091c8b7d 445
ricardo_95 7:dba5091c8b7d 446 #include "mbed.h"
ricardo_95 7:dba5091c8b7d 447 #include "pwm_tone.h"
ricardo_95 7:dba5091c8b7d 448 #include "neopixel.h"
ricardo_95 7:dba5091c8b7d 449 #define NLED (11)
ricardo_95 7:dba5091c8b7d 450 #define ONE_COLOR
ricardo_95 7:dba5091c8b7d 451
ricardo_95 7:dba5091c8b7d 452 Serial pi(p9, p10, 9600); // Setup serial communication.
ricardo_95 7:dba5091c8b7d 453 Serial pc(USBTX, USBRX, 9600);
ricardo_95 7:dba5091c8b7d 454
ricardo_95 7:dba5091c8b7d 455 InterruptIn lock(p16); // Interrupts for buttons.
ricardo_95 7:dba5091c8b7d 456 InterruptIn reposition(p17);
ricardo_95 7:dba5091c8b7d 457 InterruptIn mute(p15);
ricardo_95 7:dba5091c8b7d 458 InterruptIn new_patient(p14);
ricardo_95 7:dba5091c8b7d 459
ricardo_95 7:dba5091c8b7d 460 AnalogIn LDR_val(p18);
ricardo_95 7:dba5091c8b7d 461 AnalogIn supplyvoltage(p20); // Analog input between 0 and 1 (0 and 100 %) for reading supplyvoltage from accupack.
ricardo_95 7:dba5091c8b7d 462
ricardo_95 7:dba5091c8b7d 463 PwmOut LED_intern1(LED1);
ricardo_95 7:dba5091c8b7d 464 DigitalOut LED_intern2(LED2);
ricardo_95 7:dba5091c8b7d 465 DigitalOut LED_intern3(LED4);
ricardo_95 7:dba5091c8b7d 466
ricardo_95 7:dba5091c8b7d 467 Timer hold_timer;
ricardo_95 7:dba5091c8b7d 468 Timer delay;
ricardo_95 7:dba5091c8b7d 469 Timer speaker_timer;
ricardo_95 7:dba5091c8b7d 470
ricardo_95 7:dba5091c8b7d 471 DigitalOut speaker1(p21);
ricardo_95 7:dba5091c8b7d 472 DigitalOut speaker2(p22);
ricardo_95 7:dba5091c8b7d 473
ricardo_95 7:dba5091c8b7d 474 char LED_colour; // Variable to set LED colour.
ricardo_95 7:dba5091c8b7d 475 bool lock_state, lock_flag, mute_state, alarm, calibration_flag; // Boolean variables for states lock, mute and alarm.
ricardo_95 7:dba5091c8b7d 476 int locktime_ms = 2000; // Waittime in ms.
ricardo_95 7:dba5091c8b7d 477 int calibrationtime_ms = 5000;
ricardo_95 7:dba5091c8b7d 478 int calibration_flash;
ricardo_95 7:dba5091c8b7d 479 int buttondelay_ms = 750; // Button delay in ms.
ricardo_95 7:dba5091c8b7d 480 int delay_lock_interface = 3000*60; // Delay for non using interface.
ricardo_95 7:dba5091c8b7d 481 int speaker_active_ms = 750;
ricardo_95 7:dba5091c8b7d 482 double alarm_voltage = 0.2; // Needed voltage for alarm expressed as a percentage (0 - 100 % => 0 - 3.3 V).
ricardo_95 7:dba5091c8b7d 483 int red_var, green_var, blue_var, intensity, current_intensity = 0; // Variables to set LED intensity
ricardo_95 7:dba5091c8b7d 484
ricardo_95 7:dba5091c8b7d 485 void set_intensity() // Function to set the intensity for the LED's
ricardo_95 7:dba5091c8b7d 486 {
ricardo_95 7:dba5091c8b7d 487 intensity = (1-LDR_val)*100; // Calculate intensity (use right part of the graphic)
ricardo_95 7:dba5091c8b7d 488
ricardo_95 7:dba5091c8b7d 489 if (abs(intensity-current_intensity) > 5) { // If difference is greater then 5, change intensity dependent on range.
ricardo_95 7:dba5091c8b7d 490 if (intensity <= 20) {
ricardo_95 7:dba5091c8b7d 491 intensity = 20;
ricardo_95 7:dba5091c8b7d 492 }
ricardo_95 7:dba5091c8b7d 493
ricardo_95 7:dba5091c8b7d 494 if (40 >= intensity > 20) {
ricardo_95 7:dba5091c8b7d 495 intensity = 40;
ricardo_95 7:dba5091c8b7d 496 }
ricardo_95 7:dba5091c8b7d 497
ricardo_95 7:dba5091c8b7d 498 if (60 >= intensity > 40) {
ricardo_95 7:dba5091c8b7d 499 intensity = 60;
ricardo_95 7:dba5091c8b7d 500 }
ricardo_95 7:dba5091c8b7d 501
ricardo_95 7:dba5091c8b7d 502 if (80 >= intensity > 60) {
ricardo_95 7:dba5091c8b7d 503 intensity = 80;
ricardo_95 7:dba5091c8b7d 504 }
ricardo_95 7:dba5091c8b7d 505
ricardo_95 7:dba5091c8b7d 506 if (intensity > 80) {
ricardo_95 7:dba5091c8b7d 507 intensity = 100;
ricardo_95 7:dba5091c8b7d 508 }
ricardo_95 7:dba5091c8b7d 509 }
ricardo_95 7:dba5091c8b7d 510 current_intensity = intensity; // Save intensisty to compare in first if statement of this set_intensity function.
ricardo_95 7:dba5091c8b7d 511 }
ricardo_95 7:dba5091c8b7d 512
ricardo_95 7:dba5091c8b7d 513 void colour_select(char LED_colour) // Function to select the colour.
ricardo_95 7:dba5091c8b7d 514 {
ricardo_95 7:dba5091c8b7d 515 set_intensity(); // Call function set_intensity
ricardo_95 7:dba5091c8b7d 516
ricardo_95 7:dba5091c8b7d 517 if (LED_colour == 'r') {
ricardo_95 7:dba5091c8b7d 518 red_var = (2.55*intensity);
ricardo_95 7:dba5091c8b7d 519 green_var = 0;
ricardo_95 7:dba5091c8b7d 520 blue_var = 0;
ricardo_95 7:dba5091c8b7d 521 }
ricardo_95 7:dba5091c8b7d 522
ricardo_95 7:dba5091c8b7d 523 if (LED_colour == 'y') {
ricardo_95 7:dba5091c8b7d 524 red_var = (2.55*intensity);
ricardo_95 7:dba5091c8b7d 525 green_var = (2.55*intensity);
ricardo_95 7:dba5091c8b7d 526 blue_var = 0;
ricardo_95 7:dba5091c8b7d 527 }
ricardo_95 7:dba5091c8b7d 528
ricardo_95 7:dba5091c8b7d 529 if (LED_colour == 'g') {
ricardo_95 7:dba5091c8b7d 530 red_var = 0;
ricardo_95 7:dba5091c8b7d 531 green_var = (2.55*intensity);
ricardo_95 7:dba5091c8b7d 532 blue_var = 0;
ricardo_95 7:dba5091c8b7d 533 }
ricardo_95 7:dba5091c8b7d 534
ricardo_95 7:dba5091c8b7d 535 if (calibration_flash >= 1) {
ricardo_95 7:dba5091c8b7d 536 if((calibration_flash % 2) == 0) {
ricardo_95 7:dba5091c8b7d 537 red_var = 255;
ricardo_95 7:dba5091c8b7d 538 green_var = 255;
ricardo_95 7:dba5091c8b7d 539 blue_var = 255;
ricardo_95 7:dba5091c8b7d 540 } else {
ricardo_95 7:dba5091c8b7d 541 red_var = 0;
ricardo_95 7:dba5091c8b7d 542 green_var = 0;
ricardo_95 7:dba5091c8b7d 543 blue_var = 0;
ricardo_95 7:dba5091c8b7d 544 }
ricardo_95 7:dba5091c8b7d 545 calibration_flash--;
ricardo_95 7:dba5091c8b7d 546 }
ricardo_95 7:dba5091c8b7d 547 }
ricardo_95 7:dba5091c8b7d 548
ricardo_95 7:dba5091c8b7d 549
ricardo_95 7:dba5091c8b7d 550 void trigger_lock() // If rising edge lock button is detected start locktimer.
ricardo_95 7:dba5091c8b7d 551 {
ricardo_95 7:dba5091c8b7d 552 hold_timer.start();
ricardo_95 7:dba5091c8b7d 553 delay.reset();
ricardo_95 7:dba5091c8b7d 554 delay.start();
ricardo_95 7:dba5091c8b7d 555 }
ricardo_95 7:dba5091c8b7d 556
ricardo_95 7:dba5091c8b7d 557 void timer_lock() // End timer lock.
ricardo_95 7:dba5091c8b7d 558 {
ricardo_95 7:dba5091c8b7d 559 lock_flag = 0; // Set lock_flag off.
ricardo_95 7:dba5091c8b7d 560 hold_timer.stop(); // Stop and reset holdtimer
ricardo_95 7:dba5091c8b7d 561 hold_timer.reset();
ricardo_95 7:dba5091c8b7d 562 }
ricardo_95 7:dba5091c8b7d 563
ricardo_95 7:dba5091c8b7d 564 void trigger_reposition()
ricardo_95 7:dba5091c8b7d 565 {
ricardo_95 7:dba5091c8b7d 566 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 567 } else {
ricardo_95 7:dba5091c8b7d 568 delay.reset();
ricardo_95 7:dba5091c8b7d 569 delay.start();
ricardo_95 7:dba5091c8b7d 570 pi.printf("02\n"); // Seriele communicatie met PI.
ricardo_95 7:dba5091c8b7d 571
ricardo_95 7:dba5091c8b7d 572 if (LED_intern1 == 0) {
ricardo_95 7:dba5091c8b7d 573 LED_intern1 = 1.0;
ricardo_95 7:dba5091c8b7d 574 } else {
ricardo_95 7:dba5091c8b7d 575 LED_intern1 = 0.0;
ricardo_95 7:dba5091c8b7d 576 }
ricardo_95 7:dba5091c8b7d 577
ricardo_95 7:dba5091c8b7d 578 LED_colour = 'r';
ricardo_95 7:dba5091c8b7d 579 }
ricardo_95 7:dba5091c8b7d 580 }
ricardo_95 7:dba5091c8b7d 581
ricardo_95 7:dba5091c8b7d 582 void trigger_mute()
ricardo_95 7:dba5091c8b7d 583 {
ricardo_95 7:dba5091c8b7d 584 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 585 mute_state = 0;
ricardo_95 7:dba5091c8b7d 586 } else {
ricardo_95 7:dba5091c8b7d 587 delay.reset();
ricardo_95 7:dba5091c8b7d 588 delay.start();
ricardo_95 7:dba5091c8b7d 589 pi.printf("01\n");
ricardo_95 7:dba5091c8b7d 590 mute_state = !mute_state;
ricardo_95 7:dba5091c8b7d 591
ricardo_95 7:dba5091c8b7d 592 if (LED_intern1 == 0) {
ricardo_95 7:dba5091c8b7d 593 LED_intern1 = 1.0;
ricardo_95 7:dba5091c8b7d 594 } else {
ricardo_95 7:dba5091c8b7d 595 LED_intern1 = 0.0;
ricardo_95 7:dba5091c8b7d 596 }
ricardo_95 7:dba5091c8b7d 597
ricardo_95 7:dba5091c8b7d 598 LED_colour = 'y';
ricardo_95 7:dba5091c8b7d 599 }
ricardo_95 7:dba5091c8b7d 600 }
ricardo_95 7:dba5091c8b7d 601
ricardo_95 7:dba5091c8b7d 602 void trigger_new_patient() // Function to trigger hold timer for new patient calibration function.
ricardo_95 7:dba5091c8b7d 603 {
ricardo_95 7:dba5091c8b7d 604 if (lock_state == 1) {
ricardo_95 7:dba5091c8b7d 605 } else {
ricardo_95 7:dba5091c8b7d 606 hold_timer.start();
ricardo_95 7:dba5091c8b7d 607 }
ricardo_95 7:dba5091c8b7d 608 }
ricardo_95 7:dba5091c8b7d 609
ricardo_95 7:dba5091c8b7d 610 void timer_calibration() // Timer calibration function.
ricardo_95 7:dba5091c8b7d 611 {
ricardo_95 7:dba5091c8b7d 612 hold_timer.stop();
ricardo_95 7:dba5091c8b7d 613 hold_timer.reset();
ricardo_95 7:dba5091c8b7d 614 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 615 } else {
ricardo_95 7:dba5091c8b7d 616 if (calibration_flag == 0) {
ricardo_95 7:dba5091c8b7d 617 pi.printf("03\n");
ricardo_95 7:dba5091c8b7d 618
ricardo_95 7:dba5091c8b7d 619
ricardo_95 7:dba5091c8b7d 620 if (LED_intern1 == 0) {
ricardo_95 7:dba5091c8b7d 621 LED_intern1 = 1.0;
ricardo_95 7:dba5091c8b7d 622 } else {
ricardo_95 7:dba5091c8b7d 623 LED_intern1 = 0.0;
ricardo_95 7:dba5091c8b7d 624 }
ricardo_95 7:dba5091c8b7d 625
ricardo_95 7:dba5091c8b7d 626 LED_colour = 'g';
ricardo_95 7:dba5091c8b7d 627 } else {
ricardo_95 7:dba5091c8b7d 628 calibration_flag = 0;
ricardo_95 7:dba5091c8b7d 629 }
ricardo_95 7:dba5091c8b7d 630 }
ricardo_95 7:dba5091c8b7d 631 }
ricardo_95 7:dba5091c8b7d 632
ricardo_95 7:dba5091c8b7d 633 void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour.
ricardo_95 7:dba5091c8b7d 634 {
ricardo_95 7:dba5091c8b7d 635 out->red = red_var;
ricardo_95 7:dba5091c8b7d 636 out->green = green_var;
ricardo_95 7:dba5091c8b7d 637 out->blue = blue_var;
ricardo_95 7:dba5091c8b7d 638 }
ricardo_95 7:dba5091c8b7d 639
ricardo_95 7:dba5091c8b7d 640 void read_voltage()
ricardo_95 7:dba5091c8b7d 641 {
ricardo_95 7:dba5091c8b7d 642 LED_intern3 = 0;
ricardo_95 7:dba5091c8b7d 643
ricardo_95 7:dba5091c8b7d 644 if (supplyvoltage.read() > alarm_voltage) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage.
ricardo_95 7:dba5091c8b7d 645 alarm = 0; // Alarm is off.
ricardo_95 7:dba5091c8b7d 646 } else {
ricardo_95 7:dba5091c8b7d 647 alarm = 1; // Else alarm is on.
ricardo_95 7:dba5091c8b7d 648 }
ricardo_95 7:dba5091c8b7d 649
ricardo_95 7:dba5091c8b7d 650 if (alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms.
ricardo_95 7:dba5091c8b7d 651 speaker1 = 1; // Set speaker.
ricardo_95 7:dba5091c8b7d 652 speaker2 = 1;
ricardo_95 7:dba5091c8b7d 653 speaker_timer.start(); // Set timer for speaker to iterate on and off.
ricardo_95 7:dba5091c8b7d 654 LED_intern3 = !LED_intern3;
ricardo_95 7:dba5091c8b7d 655 }
ricardo_95 7:dba5091c8b7d 656
ricardo_95 7:dba5091c8b7d 657 if (alarm == 1 && mute_state == 1 && (speaker_timer.read_ms() < speaker_active_ms)) { // Set speaker on for 750 ms.
ricardo_95 7:dba5091c8b7d 658 speaker1 = 0; // Set speaker.
ricardo_95 7:dba5091c8b7d 659 speaker2 = 0;
ricardo_95 7:dba5091c8b7d 660 speaker_timer.start(); // Set timer for speaker to iterate on and off.
ricardo_95 7:dba5091c8b7d 661 LED_intern3 = !LED_intern3;
ricardo_95 7:dba5091c8b7d 662 }
ricardo_95 7:dba5091c8b7d 663
ricardo_95 7:dba5091c8b7d 664 if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) {
ricardo_95 7:dba5091c8b7d 665 speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one).
ricardo_95 7:dba5091c8b7d 666 speaker2 = 0;
ricardo_95 7:dba5091c8b7d 667 }
ricardo_95 7:dba5091c8b7d 668
ricardo_95 7:dba5091c8b7d 669 if (speaker_timer.read_ms() > (speaker_active_ms*2)) {
ricardo_95 7:dba5091c8b7d 670 speaker_timer.stop(); // Stop speaker timer.
ricardo_95 7:dba5091c8b7d 671 speaker_timer.reset();
ricardo_95 7:dba5091c8b7d 672 }
ricardo_95 7:dba5091c8b7d 673 }
ricardo_95 7:dba5091c8b7d 674
ricardo_95 7:dba5091c8b7d 675 int main()
ricardo_95 7:dba5091c8b7d 676 {
ricardo_95 7:dba5091c8b7d 677 pi.format(8, SerialBase::None, 1);
ricardo_95 7:dba5091c8b7d 678
ricardo_95 7:dba5091c8b7d 679 lock.rise(&trigger_lock); // Interrupt for rising edge lock button.
ricardo_95 7:dba5091c8b7d 680 lock.fall(&timer_lock);
ricardo_95 7:dba5091c8b7d 681 reposition.rise(&trigger_reposition);
ricardo_95 7:dba5091c8b7d 682 mute.rise(&trigger_mute);
ricardo_95 7:dba5091c8b7d 683 new_patient.rise(&trigger_new_patient); // New patient/calibration button rising event.
ricardo_95 7:dba5091c8b7d 684 new_patient.fall(&timer_calibration); // Falling edge for calibration algorithm option.
ricardo_95 7:dba5091c8b7d 685 delay.reset(); // Delaytimer reset en start.
ricardo_95 7:dba5091c8b7d 686 delay.start();
ricardo_95 7:dba5091c8b7d 687
ricardo_95 7:dba5091c8b7d 688 neopixel::PixelArray array(p11);
ricardo_95 7:dba5091c8b7d 689
ricardo_95 7:dba5091c8b7d 690 while(1) {
ricardo_95 7:dba5091c8b7d 691 wait_ms(100); // Simulate 100 ms delay from sensorplate code.
ricardo_95 7:dba5091c8b7d 692
ricardo_95 7:dba5091c8b7d 693 if ((hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 1) { // If statement for lock function.
ricardo_95 7:dba5091c8b7d 694 lock_flag = 1;
ricardo_95 7:dba5091c8b7d 695 LED_intern2 = !LED_intern2;
ricardo_95 7:dba5091c8b7d 696 lock_state = !lock_state;
ricardo_95 7:dba5091c8b7d 697 }
ricardo_95 7:dba5091c8b7d 698
ricardo_95 7:dba5091c8b7d 699 if ((hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 1) { // If statement for calibration system.
ricardo_95 7:dba5091c8b7d 700 calibration_flag = 1;
ricardo_95 7:dba5091c8b7d 701 calibration_flash = 11;
ricardo_95 7:dba5091c8b7d 702 pi.printf("Calibration button is pressed."); // Print statement for serial communication to inform algorithm to calibrate.
ricardo_95 7:dba5091c8b7d 703
ricardo_95 7:dba5091c8b7d 704 if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active.
ricardo_95 7:dba5091c8b7d 705 lock_state = 1;
ricardo_95 7:dba5091c8b7d 706 LED_intern2 = 1;
ricardo_95 7:dba5091c8b7d 707 }
ricardo_95 7:dba5091c8b7d 708
ricardo_95 7:dba5091c8b7d 709 read_voltage(); // Supplyvoltage control for alarm.
ricardo_95 7:dba5091c8b7d 710
ricardo_95 7:dba5091c8b7d 711 uint32_t val = 0;
ricardo_95 7:dba5091c8b7d 712 colour_select(LED_colour);
ricardo_95 7:dba5091c8b7d 713 array.update(generate, NLED, val);
ricardo_95 7:dba5091c8b7d 714 }
ricardo_95 7:dba5091c8b7d 715
ricardo_95 7:dba5091c8b7d 716 }*/