Nucleo-transfer
Dependencies: ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813
Fork of Nucleo-transfer by
Sensorplate/main.cpp@35:e9026c40726e, 2017-10-12 (annotated)
- Committer:
- ricardo_95
- Date:
- Thu Oct 12 09:30:58 2017 +0000
- Revision:
- 35:e9026c40726e
- Parent:
- 34:1614f4f2b841
- Child:
- 36:d10f368d037b
Rubbish deleted.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ricardo_95 | 21:13e4824bc364 | 1 | /********************* CODE INFORMATIE ****************************** |
ricardo_95 | 34:1614f4f2b841 | 2 | Date of creation: 30-09-2017 |
ricardo_95 | 34:1614f4f2b841 | 3 | Authors: Danny Eldering & Ricardo Molenaar |
ricardo_95 | 33:df21cb8dc5c7 | 4 | co-authors: Menno Gravemaker |
ricardo_95 | 34:1614f4f2b841 | 5 | (c) Copyright by Momo Medical BV. |
ricardo_95 | 34:1614f4f2b841 | 6 | |
ricardo_95 | 33:df21cb8dc5c7 | 7 | Current version name: 2.1.2 |
ricardo_95 | 34:1614f4f2b841 | 8 | Date of modification: 11-10-2017 |
ricardo_95 | 34:1614f4f2b841 | 9 | Purpose of this file: Oneliner of purpose |
ricardo_95 | 34:1614f4f2b841 | 10 | Update ‘what’s new in this version?’: short meaningful description (no more than 3 lines) |
ricardo_95 | 34:1614f4f2b841 | 11 | Todo: |
ricardo_95 | 34:1614f4f2b841 | 12 | Source file: http://mbed.com |
ricardo_95 | 33:df21cb8dc5c7 | 13 | |
ricardo_95 | 12:7b3a5940f911 | 14 | */ |
ricardo_95 | 12:7b3a5940f911 | 15 | |
ricardo_95 | 21:13e4824bc364 | 16 | /************************ CONFIG ***********************************/ |
ricardo_95 | 21:13e4824bc364 | 17 | |
DEldering | 0:c0e44c46c573 | 18 | #include "mbed.h" |
DEldering | 0:c0e44c46c573 | 19 | #include "Adafruit_ADS1015.h" |
DEldering | 0:c0e44c46c573 | 20 | #include "MPU6050.h" |
ricardo_95 | 33:df21cb8dc5c7 | 21 | #include "MPU6050_belt.h" |
ricardo_95 | 7:dba5091c8b7d | 22 | #include "neopixel.h" |
ricardo_95 | 34:1614f4f2b841 | 23 | #define NLED (3) // to do: nled uitschrijven |
ricardo_95 | 7:dba5091c8b7d | 24 | #define ONE_COLOR |
ricardo_95 | 7:dba5091c8b7d | 25 | |
ricardo_95 | 34:1614f4f2b841 | 26 | InterruptIn lock(p15); // Interrupts for buttons. todo: button toevoegen |
ricardo_95 | 7:dba5091c8b7d | 27 | InterruptIn reposition(p17); |
ricardo_95 | 26:9e130f7ee829 | 28 | InterruptIn mute(p16); |
ricardo_95 | 21:13e4824bc364 | 29 | InterruptIn new_patient(p18); |
ricardo_95 | 7:dba5091c8b7d | 30 | |
ricardo_95 | 34:1614f4f2b841 | 31 | DigitalOut LED_intern1(LED1); // todo: intern veranderen in on_dev_board oid |
ricardo_95 | 12:7b3a5940f911 | 32 | DigitalOut LED_intern2(LED2); |
ricardo_95 | 12:7b3a5940f911 | 33 | DigitalOut LED_intern3(LED3); |
ricardo_95 | 26:9e130f7ee829 | 34 | DigitalOut LED_intern4(LED4); |
ricardo_95 | 34:1614f4f2b841 | 35 | neopixel::PixelArray array(p11); // todo: array -> wat ermee gedaan wordt |
ricardo_95 | 7:dba5091c8b7d | 36 | |
ricardo_95 | 19:3b5999fa7b7e | 37 | Timer lock_hold_timer; |
ricardo_95 | 19:3b5999fa7b7e | 38 | Timer calibration_hold_timer; |
ricardo_95 | 34:1614f4f2b841 | 39 | Timer delay; // todo: delay -> delay_between_2_buttons_pressed |
ricardo_95 | 7:dba5091c8b7d | 40 | Timer speaker_timer; |
ricardo_95 | 34:1614f4f2b841 | 41 | Timer test_timer; // todo: naar testsectie |
ricardo_95 | 7:dba5091c8b7d | 42 | |
ricardo_95 | 7:dba5091c8b7d | 43 | DigitalOut speaker1(p21); |
ricardo_95 | 7:dba5091c8b7d | 44 | DigitalOut speaker2(p22); |
ricardo_95 | 34:1614f4f2b841 | 45 | PwmOut lock_LED(p23); // todo: button toevoegen bijv. feedback_LED_lock |
ricardo_95 | 26:9e130f7ee829 | 46 | PwmOut reposition_LED(p25); |
ricardo_95 | 26:9e130f7ee829 | 47 | PwmOut mute_LED(p26); |
ricardo_95 | 26:9e130f7ee829 | 48 | PwmOut new_patient_LED(p24); |
ricardo_95 | 34:1614f4f2b841 | 49 | // todo: comments for library hardcoding etc. |
ricardo_95 | 34:1614f4f2b841 | 50 | I2C i2c(p28, p27); // I2C todo: i2c_sensorplaat |
ricardo_95 | 34:1614f4f2b841 | 51 | I2C i2cAccu(p9, p10); // I2C for accupack todo: i2c_voltage_measurement |
DEldering | 1:a8e61f3910ad | 52 | MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit |
ricardo_95 | 33:df21cb8dc5c7 | 53 | MPU6050_belt agu_belt(p28,p27); // Accelerometer/Gyroscope Unit Belt |
DEldering | 1:a8e61f3910ad | 54 | Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 55 | Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 56 | Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC |
ricardo_95 | 21:13e4824bc364 | 57 | Adafruit_ADS1115 adsAccu(&i2cAccu, 0x48); |
ricardo_95 | 34:1614f4f2b841 | 58 | Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection todo -> test en uart toevoegen ook bij pi |
ricardo_95 | 21:13e4824bc364 | 59 | Serial pi(p13, p14); // tx, rx // Setup serial communication for pi. |
DEldering | 1:a8e61f3910ad | 60 | Timer t; // Timer for equally time-spaced samples |
DEldering | 1:a8e61f3910ad | 61 | Ticker sample_cycle; // Polling cycle |
ricardo_95 | 8:bf0f7a6fb1fd | 62 | |
ricardo_95 | 34:1614f4f2b841 | 63 | // todo: uitschrijven namen |
ricardo_95 | 34:1614f4f2b841 | 64 | |
ricardo_95 | 12:7b3a5940f911 | 65 | int boot_delay_ms = 500; |
DEldering | 1:a8e61f3910ad | 66 | int cycle_time = 100000; // Cycle time in us |
DEldering | 1:a8e61f3910ad | 67 | int i2c_freq = 400000; // I2C Frequency |
ricardo_95 | 12:7b3a5940f911 | 68 | int baud = 115200; // Baud rate |
DEldering | 1:a8e61f3910ad | 69 | short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle |
DEldering | 1:a8e61f3910ad | 70 | short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle |
DEldering | 1:a8e61f3910ad | 71 | int angle = 0; // Accelerometer Z-axis |
DEldering | 1:a8e61f3910ad | 72 | int k = 0; |
DEldering | 1:a8e61f3910ad | 73 | float acce[3]; // Raw accelerometer data |
DEldering | 1:a8e61f3910ad | 74 | float gyro[3]; // Raw gyroscope data |
ricardo_95 | 33:df21cb8dc5c7 | 75 | float acce_belt[3]; // Raw accelerometer data from belt |
ricardo_95 | 33:df21cb8dc5c7 | 76 | float gyro_belt[3]; // Raw gyroscope data from belt |
ricardo_95 | 22:a09775c25890 | 77 | char LED_colour = 'g'; // Variable to set LED colour. |
ricardo_95 | 28:b4bee068780d | 78 | bool lock_state = 0, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1; // Boolean variables for states logging. |
ricardo_95 | 25:96c34634abda | 79 | bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0; |
ricardo_95 | 21:13e4824bc364 | 80 | bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0; |
ricardo_95 | 21:13e4824bc364 | 81 | bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0; |
ricardo_95 | 11:73c6def38fbd | 82 | int locktime_ms = 2000; // Waittime for lock user interface in ms. |
ricardo_95 | 12:7b3a5940f911 | 83 | int calibrationtime_ms = 5000; // Time to press new_patient button for calibration system. |
ricardo_95 | 12:7b3a5940f911 | 84 | int calibration_flash; // Variable for flash LED's to indicate calibration. |
ricardo_95 | 7:dba5091c8b7d | 85 | int buttondelay_ms = 750; // Button delay in ms. |
ricardo_95 | 7:dba5091c8b7d | 86 | int delay_lock_interface = 3000*60; // Delay for non using interface locktime. |
ricardo_95 | 12:7b3a5940f911 | 87 | int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs. |
ricardo_95 | 17:6ec7d594c1f1 | 88 | int alarm_voltage = 5867; // Needed voltage for alarm expressed as a digital 15 bit value (=20% of max battery voltage) |
ricardo_95 | 25:96c34634abda | 89 | int red_var, green_var, blue_var; // Variables to set LED intensity. |
ricardo_95 | 21:13e4824bc364 | 90 | short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage. |
ricardo_95 | 31:24770fa8114b | 91 | int intensity_day = 40, intensity_night = 10; // Intensity settings for LED's to wall. |
ricardo_95 | 31:24770fa8114b | 92 | double intensity, control_LED_intensity = 0; // Variable between 0 and 1 to set the intensity of the LED's above the buttons. |
ricardo_95 | 30:bc0bddc6f8d1 | 93 | int a; // Test |
ricardo_95 | 21:13e4824bc364 | 94 | |
ricardo_95 | 33:df21cb8dc5c7 | 95 | /*************************** TEST ********************************/ |
ricardo_95 | 33:df21cb8dc5c7 | 96 | // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V) |
ricardo_95 | 34:1614f4f2b841 | 97 | DigitalIn test_pin(p30, PullDown); |
ricardo_95 | 33:df21cb8dc5c7 | 98 | bool test_belt = 1; //test_pin; |
ricardo_95 | 33:df21cb8dc5c7 | 99 | |
ricardo_95 | 33:df21cb8dc5c7 | 100 | // Verify if interrupts are working properly |
ricardo_95 | 33:df21cb8dc5c7 | 101 | |
ricardo_95 | 34:1614f4f2b841 | 102 | |
ricardo_95 | 34:1614f4f2b841 | 103 | // Verify if parameters are |
ricardo_95 | 34:1614f4f2b841 | 104 | bool test_mode = 0; |
ricardo_95 | 34:1614f4f2b841 | 105 | |
ricardo_95 | 33:df21cb8dc5c7 | 106 | |
ricardo_95 | 21:13e4824bc364 | 107 | /*************************** CODE ********************************/ |
ricardo_95 | 7:dba5091c8b7d | 108 | |
ricardo_95 | 11:73c6def38fbd | 109 | void set_intensity() // Function to set the intensity for the LED's. |
ricardo_95 | 7:dba5091c8b7d | 110 | { |
ricardo_95 | 11:73c6def38fbd | 111 | if (intensity_select == 1) { |
ricardo_95 | 12:7b3a5940f911 | 112 | intensity = intensity_day; |
ricardo_95 | 12:7b3a5940f911 | 113 | } else { |
ricardo_95 | 12:7b3a5940f911 | 114 | intensity = intensity_night; |
ricardo_95 | 7:dba5091c8b7d | 115 | } |
ricardo_95 | 21:13e4824bc364 | 116 | control_LED_intensity = (intensity/100); |
ricardo_95 | 34:1614f4f2b841 | 117 | |
ricardo_95 | 33:df21cb8dc5c7 | 118 | if (test_mode == 1) { |
ricardo_95 | 33:df21cb8dc5c7 | 119 | pc.printf("Intensity LED's shines to wall = %f\n", intensity); |
ricardo_95 | 33:df21cb8dc5c7 | 120 | pc.printf("Intensity LED's above buttons = %f\n", control_LED_intensity); |
ricardo_95 | 33:df21cb8dc5c7 | 121 | } |
ricardo_95 | 7:dba5091c8b7d | 122 | } |
ricardo_95 | 7:dba5091c8b7d | 123 | |
ricardo_95 | 12:7b3a5940f911 | 124 | void serial_read() // Serial read for select LED intensity and colour. |
ricardo_95 | 12:7b3a5940f911 | 125 | { |
ricardo_95 | 11:73c6def38fbd | 126 | if (pi.readable()) { |
ricardo_95 | 34:1614f4f2b841 | 127 | char message[10]; |
ricardo_95 | 27:77065263c0ea | 128 | pi.scanf("%s", message); |
ricardo_95 | 34:1614f4f2b841 | 129 | pc.printf("Message = %s, Intensity_select = %d en LED_colour = %c\n", message, intensity_select, LED_colour); |
ricardo_95 | 33:df21cb8dc5c7 | 130 | |
ricardo_95 | 34:1614f4f2b841 | 131 | if (intensity_select != (message[0]-'0')) { |
ricardo_95 | 34:1614f4f2b841 | 132 | intensity_select = (message[0]-'0'); |
ricardo_95 | 34:1614f4f2b841 | 133 | } |
ricardo_95 | 34:1614f4f2b841 | 134 | |
ricardo_95 | 34:1614f4f2b841 | 135 | if (LED_colour != message[1]) { |
ricardo_95 | 34:1614f4f2b841 | 136 | LED_colour = message[1]; |
ricardo_95 | 34:1614f4f2b841 | 137 | } |
ricardo_95 | 34:1614f4f2b841 | 138 | |
ricardo_95 | 34:1614f4f2b841 | 139 | pc.printf("Intensity_select = %d en LED_colour = %c\n", intensity_select, LED_colour); |
ricardo_95 | 34:1614f4f2b841 | 140 | |
ricardo_95 | 33:df21cb8dc5c7 | 141 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 142 | pc.printf("message: %s\n", message); |
ricardo_95 | 11:73c6def38fbd | 143 | } |
ricardo_95 | 11:73c6def38fbd | 144 | } |
ricardo_95 | 11:73c6def38fbd | 145 | } |
ricardo_95 | 11:73c6def38fbd | 146 | |
ricardo_95 | 21:13e4824bc364 | 147 | void serial_log() |
ricardo_95 | 7:dba5091c8b7d | 148 | { |
ricardo_95 | 7:dba5091c8b7d | 149 | if (mute_flag == 1) { |
ricardo_95 | 19:3b5999fa7b7e | 150 | pi.printf(">01\n"); |
ricardo_95 | 34:1614f4f2b841 | 151 | |
ricardo_95 | 34:1614f4f2b841 | 152 | if (test_mode == 1) { |
ricardo_95 | 33:df21cb8dc5c7 | 153 | pc.printf(">01\n"); |
ricardo_95 | 33:df21cb8dc5c7 | 154 | } |
ricardo_95 | 34:1614f4f2b841 | 155 | |
ricardo_95 | 7:dba5091c8b7d | 156 | mute_flag = 0; |
ricardo_95 | 19:3b5999fa7b7e | 157 | } |
ricardo_95 | 12:7b3a5940f911 | 158 | |
ricardo_95 | 19:3b5999fa7b7e | 159 | if (new_patient_flag == 1) { |
ricardo_95 | 19:3b5999fa7b7e | 160 | pi.printf(">03\n"); |
ricardo_95 | 34:1614f4f2b841 | 161 | |
ricardo_95 | 34:1614f4f2b841 | 162 | if (test_mode == 1) { |
ricardo_95 | 33:df21cb8dc5c7 | 163 | pc.printf(">03\n"); |
ricardo_95 | 33:df21cb8dc5c7 | 164 | } |
ricardo_95 | 34:1614f4f2b841 | 165 | |
ricardo_95 | 7:dba5091c8b7d | 166 | new_patient_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 167 | } |
ricardo_95 | 12:7b3a5940f911 | 168 | |
ricardo_95 | 19:3b5999fa7b7e | 169 | if (reposition_flag == 1) { |
ricardo_95 | 19:3b5999fa7b7e | 170 | pi.printf(">02\n"); |
ricardo_95 | 34:1614f4f2b841 | 171 | |
ricardo_95 | 34:1614f4f2b841 | 172 | if (test_mode == 1) { |
ricardo_95 | 33:df21cb8dc5c7 | 173 | pc.printf(">02\n"); |
ricardo_95 | 33:df21cb8dc5c7 | 174 | } |
ricardo_95 | 34:1614f4f2b841 | 175 | |
ricardo_95 | 7:dba5091c8b7d | 176 | reposition_flag = 0; |
ricardo_95 | 7:dba5091c8b7d | 177 | } |
ricardo_95 | 12:7b3a5940f911 | 178 | |
ricardo_95 | 8:bf0f7a6fb1fd | 179 | if (batteryvoltage_current != batteryvoltage_last) { |
ricardo_95 | 34:1614f4f2b841 | 180 | pi.printf("%%" "%d\n", batteryvoltage_current); |
ricardo_95 | 34:1614f4f2b841 | 181 | |
ricardo_95 | 34:1614f4f2b841 | 182 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 183 | pc.printf("%%" "%d\n", batteryvoltage_current); |
ricardo_95 | 33:df21cb8dc5c7 | 184 | } |
ricardo_95 | 34:1614f4f2b841 | 185 | |
ricardo_95 | 16:adbbac0c79f9 | 186 | batteryvoltage_last = batteryvoltage_current; |
ricardo_95 | 12:7b3a5940f911 | 187 | } |
ricardo_95 | 12:7b3a5940f911 | 188 | |
ricardo_95 | 9:514a44bf510f | 189 | if (LED_red_logged != LED_red_state) { |
ricardo_95 | 12:7b3a5940f911 | 190 | if (LED_red_state == 1) { |
ricardo_95 | 9:514a44bf510f | 191 | pi.printf("&04\n"); |
ricardo_95 | 9:514a44bf510f | 192 | LED_red_logged = LED_red_state; |
ricardo_95 | 34:1614f4f2b841 | 193 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 194 | pc.printf("&04\n"); |
ricardo_95 | 34:1614f4f2b841 | 195 | } |
ricardo_95 | 12:7b3a5940f911 | 196 | } |
ricardo_95 | 9:514a44bf510f | 197 | if (LED_red_state == 0) { |
ricardo_95 | 9:514a44bf510f | 198 | pi.printf("&40\n"); |
ricardo_95 | 12:7b3a5940f911 | 199 | LED_red_logged = LED_red_state; |
ricardo_95 | 34:1614f4f2b841 | 200 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 201 | pc.printf("&40\n"); |
ricardo_95 | 34:1614f4f2b841 | 202 | } |
ricardo_95 | 9:514a44bf510f | 203 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 204 | } |
ricardo_95 | 12:7b3a5940f911 | 205 | |
ricardo_95 | 9:514a44bf510f | 206 | if (LED_yellow_logged != LED_yellow_state) { |
ricardo_95 | 12:7b3a5940f911 | 207 | if (LED_yellow_state == 1) { |
ricardo_95 | 9:514a44bf510f | 208 | pi.printf("&06\n"); |
ricardo_95 | 9:514a44bf510f | 209 | LED_yellow_logged = LED_yellow_state; |
ricardo_95 | 34:1614f4f2b841 | 210 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 211 | pc.printf("&06\n"); |
ricardo_95 | 34:1614f4f2b841 | 212 | } |
ricardo_95 | 12:7b3a5940f911 | 213 | } |
ricardo_95 | 9:514a44bf510f | 214 | if (LED_yellow_state == 0) { |
ricardo_95 | 9:514a44bf510f | 215 | pi.printf("&60\n"); |
ricardo_95 | 12:7b3a5940f911 | 216 | LED_yellow_logged = LED_yellow_state; |
ricardo_95 | 34:1614f4f2b841 | 217 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 218 | pc.printf("&60\n"); |
ricardo_95 | 34:1614f4f2b841 | 219 | } |
ricardo_95 | 9:514a44bf510f | 220 | } |
ricardo_95 | 7:dba5091c8b7d | 221 | } |
ricardo_95 | 12:7b3a5940f911 | 222 | |
ricardo_95 | 9:514a44bf510f | 223 | if (LED_green_logged != LED_green_state) { |
ricardo_95 | 12:7b3a5940f911 | 224 | if (LED_green_state == 1) { |
ricardo_95 | 9:514a44bf510f | 225 | pi.printf("&05\n"); |
ricardo_95 | 9:514a44bf510f | 226 | LED_green_logged = LED_green_state; |
ricardo_95 | 34:1614f4f2b841 | 227 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 228 | pc.printf("&05\n"); |
ricardo_95 | 34:1614f4f2b841 | 229 | } |
ricardo_95 | 12:7b3a5940f911 | 230 | } |
ricardo_95 | 9:514a44bf510f | 231 | if (LED_green_state == 0) { |
ricardo_95 | 9:514a44bf510f | 232 | pi.printf("&50\n"); |
ricardo_95 | 12:7b3a5940f911 | 233 | LED_green_logged = LED_green_state; |
ricardo_95 | 34:1614f4f2b841 | 234 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 235 | pc.printf("&50\n"); |
ricardo_95 | 34:1614f4f2b841 | 236 | } |
ricardo_95 | 9:514a44bf510f | 237 | } |
ricardo_95 | 7:dba5091c8b7d | 238 | } |
ricardo_95 | 12:7b3a5940f911 | 239 | |
ricardo_95 | 9:514a44bf510f | 240 | if (speaker_logged != speaker_state) { |
ricardo_95 | 12:7b3a5940f911 | 241 | if (speaker_state == 1) { |
ricardo_95 | 9:514a44bf510f | 242 | pi.printf("&07\n"); |
ricardo_95 | 9:514a44bf510f | 243 | speaker_logged = speaker_state; |
ricardo_95 | 34:1614f4f2b841 | 244 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 245 | pc.printf("&07\n"); |
ricardo_95 | 34:1614f4f2b841 | 246 | } |
ricardo_95 | 12:7b3a5940f911 | 247 | } |
ricardo_95 | 9:514a44bf510f | 248 | if (speaker_state == 0) { |
ricardo_95 | 9:514a44bf510f | 249 | pi.printf("&70\n"); |
ricardo_95 | 12:7b3a5940f911 | 250 | speaker_logged = speaker_state; |
ricardo_95 | 34:1614f4f2b841 | 251 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 252 | pc.printf("&70\n"); |
ricardo_95 | 34:1614f4f2b841 | 253 | } |
ricardo_95 | 9:514a44bf510f | 254 | } |
ricardo_95 | 8:bf0f7a6fb1fd | 255 | } |
ricardo_95 | 9:514a44bf510f | 256 | |
ricardo_95 | 9:514a44bf510f | 257 | if (power_plug_logged != power_plug_state) { |
ricardo_95 | 12:7b3a5940f911 | 258 | if (power_plug_state == 1) { |
ricardo_95 | 10:6b3034ec3c47 | 259 | pi.printf("#08\n"); |
ricardo_95 | 34:1614f4f2b841 | 260 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 261 | pc.printf("#08\n"); |
ricardo_95 | 34:1614f4f2b841 | 262 | } |
ricardo_95 | 9:514a44bf510f | 263 | power_plug_logged = power_plug_state; |
ricardo_95 | 12:7b3a5940f911 | 264 | } |
ricardo_95 | 9:514a44bf510f | 265 | if (power_plug_state == 0) { |
ricardo_95 | 10:6b3034ec3c47 | 266 | pi.printf("#80\n"); |
ricardo_95 | 34:1614f4f2b841 | 267 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 268 | pc.printf("#80\n"); |
ricardo_95 | 34:1614f4f2b841 | 269 | } |
ricardo_95 | 12:7b3a5940f911 | 270 | power_plug_logged = power_plug_state; |
ricardo_95 | 9:514a44bf510f | 271 | } |
ricardo_95 | 9:514a44bf510f | 272 | } |
ricardo_95 | 33:df21cb8dc5c7 | 273 | if (a == 1) { |
ricardo_95 | 34:1614f4f2b841 | 274 | //receiving order: 8 resistive sensors, 5 electric readings |
ricardo_95 | 34:1614f4f2b841 | 275 | pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", res[4], res[7], res[6], res[5], res[1], res[0], res[2], res[3], elec[0], elec[1], elec[2], elec[3], elec[4]); // print all to serial port |
ricardo_95 | 34:1614f4f2b841 | 276 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 277 | pc.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", res[4], res[7], res[6], res[5], res[1], res[0], res[2], res[3], elec[0], elec[1], elec[2], elec[3], elec[4]); // print all to serial port |
ricardo_95 | 34:1614f4f2b841 | 278 | } |
ricardo_95 | 28:b4bee068780d | 279 | } |
ricardo_95 | 34:1614f4f2b841 | 280 | |
DEldering | 0:c0e44c46c573 | 281 | } |
DEldering | 0:c0e44c46c573 | 282 | |
ricardo_95 | 21:13e4824bc364 | 283 | void colour_select(char LED_colour) // Function to select the colour. |
ricardo_95 | 21:13e4824bc364 | 284 | { |
ricardo_95 | 21:13e4824bc364 | 285 | set_intensity(); // Call function set_intensity |
ricardo_95 | 34:1614f4f2b841 | 286 | |
ricardo_95 | 34:1614f4f2b841 | 287 | if ((LED_colour == 'r') || (LED_colour == 'g') || (LED_colour == 'b') || (LED_colour == 'y')) { |
ricardo_95 | 34:1614f4f2b841 | 288 | red_var = 0; |
ricardo_95 | 21:13e4824bc364 | 289 | green_var = 0; |
ricardo_95 | 21:13e4824bc364 | 290 | blue_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 291 | |
ricardo_95 | 34:1614f4f2b841 | 292 | if (LED_colour == 'r') { |
ricardo_95 | 34:1614f4f2b841 | 293 | red_var = (2.55*intensity); |
ricardo_95 | 34:1614f4f2b841 | 294 | green_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 295 | blue_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 296 | LED_red_state = 1; |
ricardo_95 | 34:1614f4f2b841 | 297 | } else { |
ricardo_95 | 34:1614f4f2b841 | 298 | LED_red_state = 0; |
ricardo_95 | 34:1614f4f2b841 | 299 | } |
ricardo_95 | 34:1614f4f2b841 | 300 | |
ricardo_95 | 34:1614f4f2b841 | 301 | if (LED_colour == 'y') { |
ricardo_95 | 34:1614f4f2b841 | 302 | red_var = (2.55*intensity); |
ricardo_95 | 34:1614f4f2b841 | 303 | green_var = (2.55*intensity); |
ricardo_95 | 34:1614f4f2b841 | 304 | blue_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 305 | LED_yellow_state = 1; |
ricardo_95 | 34:1614f4f2b841 | 306 | } else { |
ricardo_95 | 34:1614f4f2b841 | 307 | LED_green_state = 0; |
ricardo_95 | 34:1614f4f2b841 | 308 | } |
ricardo_95 | 34:1614f4f2b841 | 309 | |
ricardo_95 | 34:1614f4f2b841 | 310 | if (LED_colour == 'g') { |
ricardo_95 | 34:1614f4f2b841 | 311 | red_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 312 | green_var = (2.55*intensity); |
ricardo_95 | 34:1614f4f2b841 | 313 | blue_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 314 | LED_green_state = 1; |
ricardo_95 | 34:1614f4f2b841 | 315 | } else { |
ricardo_95 | 34:1614f4f2b841 | 316 | LED_green_state = 0; |
ricardo_95 | 34:1614f4f2b841 | 317 | } |
ricardo_95 | 34:1614f4f2b841 | 318 | |
ricardo_95 | 34:1614f4f2b841 | 319 | if (LED_colour == 'b') { |
ricardo_95 | 34:1614f4f2b841 | 320 | red_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 321 | green_var = 0; |
ricardo_95 | 34:1614f4f2b841 | 322 | blue_var = (2.55*intensity); |
ricardo_95 | 34:1614f4f2b841 | 323 | } |
ricardo_95 | 28:b4bee068780d | 324 | } |
ricardo_95 | 28:b4bee068780d | 325 | |
ricardo_95 | 21:13e4824bc364 | 326 | if (calibration_flash >= 1) { |
ricardo_95 | 21:13e4824bc364 | 327 | if ((calibration_flash % 2) == 0) { |
ricardo_95 | 21:13e4824bc364 | 328 | red_var = 255; |
ricardo_95 | 21:13e4824bc364 | 329 | green_var = 255; |
ricardo_95 | 21:13e4824bc364 | 330 | blue_var = 255; |
ricardo_95 | 26:9e130f7ee829 | 331 | LED_intern4 = 1; |
ricardo_95 | 21:13e4824bc364 | 332 | } else { |
ricardo_95 | 21:13e4824bc364 | 333 | red_var = 0; |
ricardo_95 | 21:13e4824bc364 | 334 | green_var = 0; |
ricardo_95 | 21:13e4824bc364 | 335 | blue_var = 0; |
ricardo_95 | 26:9e130f7ee829 | 336 | LED_intern4 = 0; |
ricardo_95 | 21:13e4824bc364 | 337 | } |
ricardo_95 | 21:13e4824bc364 | 338 | calibration_flash--; |
ricardo_95 | 21:13e4824bc364 | 339 | } |
ricardo_95 | 21:13e4824bc364 | 340 | } |
ricardo_95 | 21:13e4824bc364 | 341 | |
ricardo_95 | 21:13e4824bc364 | 342 | void trigger_lock() // If rising edge lock button is detected start locktimer. |
ricardo_95 | 21:13e4824bc364 | 343 | { |
ricardo_95 | 34:1614f4f2b841 | 344 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 345 | pc.printf("Lock triggered.\n"); |
ricardo_95 | 34:1614f4f2b841 | 346 | } |
ricardo_95 | 21:13e4824bc364 | 347 | lock_hold_timer.reset(); |
ricardo_95 | 21:13e4824bc364 | 348 | lock_hold_timer.start(); |
ricardo_95 | 21:13e4824bc364 | 349 | delay.reset(); |
ricardo_95 | 21:13e4824bc364 | 350 | delay.start(); |
ricardo_95 | 21:13e4824bc364 | 351 | } |
ricardo_95 | 21:13e4824bc364 | 352 | |
ricardo_95 | 21:13e4824bc364 | 353 | void timer_lock() // End timer lock. |
ricardo_95 | 21:13e4824bc364 | 354 | { |
ricardo_95 | 34:1614f4f2b841 | 355 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 356 | pc.printf("Lock released.\n"); |
ricardo_95 | 34:1614f4f2b841 | 357 | } |
ricardo_95 | 21:13e4824bc364 | 358 | lock_flag = 0; // Set lock_flag off. |
ricardo_95 | 21:13e4824bc364 | 359 | lock_hold_timer.stop(); // Stop and reset holdtimer |
ricardo_95 | 21:13e4824bc364 | 360 | lock_hold_timer.reset(); |
ricardo_95 | 21:13e4824bc364 | 361 | } |
ricardo_95 | 21:13e4824bc364 | 362 | |
ricardo_95 | 21:13e4824bc364 | 363 | void trigger_reposition() |
ricardo_95 | 21:13e4824bc364 | 364 | { |
ricardo_95 | 21:13e4824bc364 | 365 | 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 | 21:13e4824bc364 | 366 | } else { |
ricardo_95 | 21:13e4824bc364 | 367 | delay.reset(); |
ricardo_95 | 21:13e4824bc364 | 368 | delay.start(); |
ricardo_95 | 34:1614f4f2b841 | 369 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 370 | pc.printf("Reposition triggered.\n"); |
ricardo_95 | 34:1614f4f2b841 | 371 | LED_intern1 = !LED_intern1; |
ricardo_95 | 21:13e4824bc364 | 372 | } |
ricardo_95 | 21:13e4824bc364 | 373 | reposition_flag = 1; |
ricardo_95 | 21:13e4824bc364 | 374 | |
ricardo_95 | 21:13e4824bc364 | 375 | reposition_LED = control_LED_intensity; |
ricardo_95 | 21:13e4824bc364 | 376 | } |
ricardo_95 | 21:13e4824bc364 | 377 | } |
ricardo_95 | 21:13e4824bc364 | 378 | |
ricardo_95 | 21:13e4824bc364 | 379 | void rise_reposition() |
ricardo_95 | 21:13e4824bc364 | 380 | { |
ricardo_95 | 34:1614f4f2b841 | 381 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 382 | pc.printf("Reposition released.\n"); |
ricardo_95 | 34:1614f4f2b841 | 383 | } |
ricardo_95 | 21:13e4824bc364 | 384 | reposition_LED = 0; |
ricardo_95 | 34:1614f4f2b841 | 385 | |
ricardo_95 | 21:13e4824bc364 | 386 | } |
ricardo_95 | 21:13e4824bc364 | 387 | |
ricardo_95 | 21:13e4824bc364 | 388 | void trigger_mute() |
ricardo_95 | 21:13e4824bc364 | 389 | { |
ricardo_95 | 21:13e4824bc364 | 390 | |
ricardo_95 | 21:13e4824bc364 | 391 | 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 | 21:13e4824bc364 | 392 | } else { |
ricardo_95 | 21:13e4824bc364 | 393 | delay.reset(); |
ricardo_95 | 21:13e4824bc364 | 394 | delay.start(); |
ricardo_95 | 21:13e4824bc364 | 395 | mute_state = !mute_state; |
ricardo_95 | 21:13e4824bc364 | 396 | if (mute_state == 1) { |
ricardo_95 | 21:13e4824bc364 | 397 | mute_LED = control_LED_intensity; |
ricardo_95 | 21:13e4824bc364 | 398 | } else { |
ricardo_95 | 21:13e4824bc364 | 399 | mute_LED = 0; |
ricardo_95 | 21:13e4824bc364 | 400 | } |
ricardo_95 | 34:1614f4f2b841 | 401 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 402 | pc.printf("Mute triggered %d.\n",mute_state); |
ricardo_95 | 34:1614f4f2b841 | 403 | LED_intern1 = !LED_intern1; |
ricardo_95 | 21:13e4824bc364 | 404 | } |
ricardo_95 | 21:13e4824bc364 | 405 | |
ricardo_95 | 21:13e4824bc364 | 406 | mute_flag = 1; |
ricardo_95 | 21:13e4824bc364 | 407 | } |
ricardo_95 | 21:13e4824bc364 | 408 | } |
ricardo_95 | 21:13e4824bc364 | 409 | |
ricardo_95 | 21:13e4824bc364 | 410 | void trigger_new_patient() // Function to trigger hold timer for new patient calibration function. |
ricardo_95 | 21:13e4824bc364 | 411 | { |
ricardo_95 | 21:13e4824bc364 | 412 | |
ricardo_95 | 34:1614f4f2b841 | 413 | if (lock_state == 1 | (delay.read_ms() < buttondelay_ms)) { |
ricardo_95 | 21:13e4824bc364 | 414 | } else { |
ricardo_95 | 21:13e4824bc364 | 415 | calibration_hold_timer.reset(); |
ricardo_95 | 21:13e4824bc364 | 416 | calibration_hold_timer.start(); |
ricardo_95 | 21:13e4824bc364 | 417 | new_patient_LED = control_LED_intensity;; |
ricardo_95 | 34:1614f4f2b841 | 418 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 419 | pc.printf("New patient triggered.\n"); |
ricardo_95 | 34:1614f4f2b841 | 420 | } |
ricardo_95 | 21:13e4824bc364 | 421 | } |
ricardo_95 | 21:13e4824bc364 | 422 | } |
ricardo_95 | 21:13e4824bc364 | 423 | |
ricardo_95 | 21:13e4824bc364 | 424 | void timer_calibration() // Timer calibration function. |
ricardo_95 | 21:13e4824bc364 | 425 | { |
ricardo_95 | 34:1614f4f2b841 | 426 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 427 | pc.printf("New patient released.\n"); |
ricardo_95 | 34:1614f4f2b841 | 428 | } |
ricardo_95 | 21:13e4824bc364 | 429 | new_patient_LED = 0; |
ricardo_95 | 21:13e4824bc364 | 430 | |
ricardo_95 | 21:13e4824bc364 | 431 | if (0 < calibration_hold_timer.read_ms() < calibrationtime_ms) { |
ricardo_95 | 21:13e4824bc364 | 432 | new_patient_flag = 1; |
ricardo_95 | 21:13e4824bc364 | 433 | } |
ricardo_95 | 21:13e4824bc364 | 434 | |
ricardo_95 | 21:13e4824bc364 | 435 | calibration_hold_timer.stop(); |
ricardo_95 | 21:13e4824bc364 | 436 | calibration_hold_timer.reset(); |
ricardo_95 | 21:13e4824bc364 | 437 | |
ricardo_95 | 21:13e4824bc364 | 438 | 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 | 21:13e4824bc364 | 439 | } else { |
ricardo_95 | 21:13e4824bc364 | 440 | if (calibration_flag == 0) { |
ricardo_95 | 21:13e4824bc364 | 441 | |
ricardo_95 | 21:13e4824bc364 | 442 | if (LED_intern1 == 0) { |
ricardo_95 | 21:13e4824bc364 | 443 | LED_intern1 = 1; |
ricardo_95 | 21:13e4824bc364 | 444 | } else { |
ricardo_95 | 21:13e4824bc364 | 445 | LED_intern1 = 0; |
ricardo_95 | 21:13e4824bc364 | 446 | } |
ricardo_95 | 21:13e4824bc364 | 447 | |
ricardo_95 | 21:13e4824bc364 | 448 | } else { |
ricardo_95 | 21:13e4824bc364 | 449 | calibration_flag = 0; |
ricardo_95 | 21:13e4824bc364 | 450 | } |
ricardo_95 | 21:13e4824bc364 | 451 | } |
ricardo_95 | 21:13e4824bc364 | 452 | } |
ricardo_95 | 21:13e4824bc364 | 453 | |
ricardo_95 | 21:13e4824bc364 | 454 | void timer_functions() |
ricardo_95 | 21:13e4824bc364 | 455 | { |
ricardo_95 | 21:13e4824bc364 | 456 | if ((lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 0) { // If statement for lock function. |
ricardo_95 | 21:13e4824bc364 | 457 | lock_flag = 1; |
ricardo_95 | 21:13e4824bc364 | 458 | LED_intern2 = !LED_intern2; |
ricardo_95 | 21:13e4824bc364 | 459 | lock_state = !lock_state; |
ricardo_95 | 21:13e4824bc364 | 460 | if (lock_state == 0) { |
ricardo_95 | 21:13e4824bc364 | 461 | lock_LED = control_LED_intensity; |
ricardo_95 | 21:13e4824bc364 | 462 | } else { |
ricardo_95 | 21:13e4824bc364 | 463 | lock_LED = 0; |
ricardo_95 | 21:13e4824bc364 | 464 | } |
ricardo_95 | 21:13e4824bc364 | 465 | } |
ricardo_95 | 21:13e4824bc364 | 466 | |
ricardo_95 | 23:4a09554bdc1a | 467 | if ((calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && new_patient == 0 && lock_state == 0) { // If statement for calibration system. |
ricardo_95 | 21:13e4824bc364 | 468 | calibration_flag = 1; |
ricardo_95 | 21:13e4824bc364 | 469 | calibration_flash = 11; |
ricardo_95 | 34:1614f4f2b841 | 470 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 471 | pc.printf("Calibrate triggered.\n"); |
ricardo_95 | 34:1614f4f2b841 | 472 | } |
ricardo_95 | 21:13e4824bc364 | 473 | pi.printf(">30\n"); // Print statement for serial communication to inform algorithm to calibrate. |
ricardo_95 | 21:13e4824bc364 | 474 | } |
ricardo_95 | 21:13e4824bc364 | 475 | |
ricardo_95 | 21:13e4824bc364 | 476 | if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active. |
ricardo_95 | 21:13e4824bc364 | 477 | lock_state = 1; |
ricardo_95 | 21:13e4824bc364 | 478 | LED_intern2 = 1; |
ricardo_95 | 21:13e4824bc364 | 479 | lock_LED = 0; |
ricardo_95 | 21:13e4824bc364 | 480 | } |
ricardo_95 | 21:13e4824bc364 | 481 | } |
ricardo_95 | 21:13e4824bc364 | 482 | |
ricardo_95 | 21:13e4824bc364 | 483 | void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour. |
ricardo_95 | 21:13e4824bc364 | 484 | { |
ricardo_95 | 21:13e4824bc364 | 485 | out->red = red_var; |
ricardo_95 | 21:13e4824bc364 | 486 | out->green = green_var; |
ricardo_95 | 21:13e4824bc364 | 487 | out->blue = blue_var; |
ricardo_95 | 21:13e4824bc364 | 488 | } |
ricardo_95 | 21:13e4824bc364 | 489 | |
ricardo_95 | 21:13e4824bc364 | 490 | void set_ui_LED() // Control functions for LED above buttons (added because of failures). |
ricardo_95 | 21:13e4824bc364 | 491 | { |
ricardo_95 | 21:13e4824bc364 | 492 | if (lock_state == 1) { |
ricardo_95 | 21:13e4824bc364 | 493 | } else { |
ricardo_95 | 21:13e4824bc364 | 494 | if (reposition == 0) { |
ricardo_95 | 22:a09775c25890 | 495 | reposition_LED = control_LED_intensity; |
ricardo_95 | 21:13e4824bc364 | 496 | } else { |
ricardo_95 | 21:13e4824bc364 | 497 | reposition_LED = 0; |
ricardo_95 | 21:13e4824bc364 | 498 | } |
ricardo_95 | 21:13e4824bc364 | 499 | |
ricardo_95 | 21:13e4824bc364 | 500 | if (new_patient == 0) { |
ricardo_95 | 22:a09775c25890 | 501 | new_patient_LED = control_LED_intensity; |
ricardo_95 | 21:13e4824bc364 | 502 | } else { |
ricardo_95 | 21:13e4824bc364 | 503 | new_patient_LED = 0; |
ricardo_95 | 21:13e4824bc364 | 504 | } |
ricardo_95 | 21:13e4824bc364 | 505 | } |
ricardo_95 | 21:13e4824bc364 | 506 | } |
ricardo_95 | 21:13e4824bc364 | 507 | |
ricardo_95 | 21:13e4824bc364 | 508 | void read_voltage() |
ricardo_95 | 21:13e4824bc364 | 509 | { |
ricardo_95 | 25:96c34634abda | 510 | if (power_plug_state == 1) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage. |
ricardo_95 | 21:13e4824bc364 | 511 | alarm = 0; // Alarm is off. |
ricardo_95 | 21:13e4824bc364 | 512 | speaker_state = 0; |
ricardo_95 | 21:13e4824bc364 | 513 | } else { |
ricardo_95 | 21:13e4824bc364 | 514 | alarm = 1; // Else alarm is on. |
ricardo_95 | 21:13e4824bc364 | 515 | speaker_state = 1; |
ricardo_95 | 21:13e4824bc364 | 516 | } |
ricardo_95 | 26:9e130f7ee829 | 517 | |
ricardo_95 | 26:9e130f7ee829 | 518 | |
ricardo_95 | 21:13e4824bc364 | 519 | if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) { // Set speaker on for 750 ms. |
ricardo_95 | 21:13e4824bc364 | 520 | speaker1 = 0; // Set speaker. |
ricardo_95 | 21:13e4824bc364 | 521 | speaker2 = 0; |
ricardo_95 | 21:13e4824bc364 | 522 | } |
ricardo_95 | 21:13e4824bc364 | 523 | |
ricardo_95 | 24:782c4dc4a3ff | 524 | if ((alarm == 1 && mute_state == 0 && (speaker_timer.read_ms() < speaker_active_ms)) || ((batteryvoltage_current < alarm_voltage) && (speaker_timer.read_ms() < speaker_active_ms) && power_plug_state == 0)) { // Set speaker on for 750 ms. |
ricardo_95 | 21:13e4824bc364 | 525 | speaker1 = 1; // Set speaker. |
ricardo_95 | 21:13e4824bc364 | 526 | speaker2 = 1; |
ricardo_95 | 21:13e4824bc364 | 527 | speaker_timer.start(); // Set timer for speaker to iterate on and off. |
ricardo_95 | 21:13e4824bc364 | 528 | } |
ricardo_95 | 21:13e4824bc364 | 529 | |
ricardo_95 | 21:13e4824bc364 | 530 | if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) { |
ricardo_95 | 21:13e4824bc364 | 531 | speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one). |
ricardo_95 | 21:13e4824bc364 | 532 | speaker2 = 0; |
ricardo_95 | 21:13e4824bc364 | 533 | } |
ricardo_95 | 21:13e4824bc364 | 534 | |
ricardo_95 | 21:13e4824bc364 | 535 | if (speaker_timer.read_ms() > (speaker_active_ms*2)) { |
ricardo_95 | 21:13e4824bc364 | 536 | speaker_timer.stop(); // Stop speaker timer. |
ricardo_95 | 21:13e4824bc364 | 537 | speaker_timer.reset(); |
ricardo_95 | 21:13e4824bc364 | 538 | } |
ricardo_95 | 21:13e4824bc364 | 539 | |
ricardo_95 | 21:13e4824bc364 | 540 | batteryvoltage_current = adsAccu.readADC_SingleEnded(0); // Read channel 0 from external ADC. |
ricardo_95 | 25:96c34634abda | 541 | powervoltage_current = adsAccu.readADC_SingleEnded(1); // Read channel 1 from external ADC. |
ricardo_95 | 26:9e130f7ee829 | 542 | |
ricardo_95 | 21:13e4824bc364 | 543 | if (powervoltage_current < 20000) { |
ricardo_95 | 21:13e4824bc364 | 544 | power_plug_state = 0; |
ricardo_95 | 28:b4bee068780d | 545 | LED_colour = 'b'; |
ricardo_95 | 21:13e4824bc364 | 546 | } else { |
ricardo_95 | 21:13e4824bc364 | 547 | power_plug_state = 1; |
ricardo_95 | 21:13e4824bc364 | 548 | } |
ricardo_95 | 21:13e4824bc364 | 549 | } |
ricardo_95 | 21:13e4824bc364 | 550 | |
ricardo_95 | 21:13e4824bc364 | 551 | void read_adc() |
ricardo_95 | 21:13e4824bc364 | 552 | { |
ricardo_95 | 21:13e4824bc364 | 553 | t.reset(); |
ricardo_95 | 21:13e4824bc364 | 554 | t.start(); |
ricardo_95 | 28:b4bee068780d | 555 | a = agu.testConnection();/* |
ricardo_95 | 28:b4bee068780d | 556 | pc.printf("a= %d\n",a); |
ricardo_95 | 28:b4bee068780d | 557 | if( a==0) |
ricardo_95 | 28:b4bee068780d | 558 | { |
ricardo_95 | 28:b4bee068780d | 559 | lock_state = 1; |
ricardo_95 | 28:b4bee068780d | 560 | LED_intern2 = 1; |
ricardo_95 | 28:b4bee068780d | 561 | lock_LED = 0; |
ricardo_95 | 28:b4bee068780d | 562 | }*/ |
ricardo_95 | 28:b4bee068780d | 563 | if (a == 1) { |
ricardo_95 | 21:13e4824bc364 | 564 | elec[0] = pel.readADC_SingleEnded(0); // First PE readout |
ricardo_95 | 21:13e4824bc364 | 565 | |
ricardo_95 | 21:13e4824bc364 | 566 | for (k = 0; k < 4; k = k + 1) { |
ricardo_95 | 21:13e4824bc364 | 567 | res[k] = pr1.readADC_SingleEnded(k); // First 4 PR readout |
ricardo_95 | 21:13e4824bc364 | 568 | } |
ricardo_95 | 21:13e4824bc364 | 569 | while(t.read_us()<(1*(cycle_time/5))) {} // Wait untill 20% of cycle |
ricardo_95 | 21:13e4824bc364 | 570 | |
ricardo_95 | 21:13e4824bc364 | 571 | elec[1] = pel.readADC_SingleEnded(0); // Second PE readout |
ricardo_95 | 21:13e4824bc364 | 572 | |
ricardo_95 | 21:13e4824bc364 | 573 | for (k = 0; k < 4; k = k + 1) { |
ricardo_95 | 21:13e4824bc364 | 574 | res[k+4] = pr2.readADC_SingleEnded(k); // Last 4 PR readout |
ricardo_95 | 21:13e4824bc364 | 575 | } |
ricardo_95 | 21:13e4824bc364 | 576 | while(t.read_us()<(2*(cycle_time/5))) {} // Wait untill 40% of cycle |
ricardo_95 | 21:13e4824bc364 | 577 | |
ricardo_95 | 21:13e4824bc364 | 578 | elec[2] = pel.readADC_SingleEnded(0); // Third PE readout |
ricardo_95 | 21:13e4824bc364 | 579 | |
ricardo_95 | 21:13e4824bc364 | 580 | agu.getAccelero(acce); // Get accelerometer data |
ricardo_95 | 26:9e130f7ee829 | 581 | angle = acce[2]*100; |
ricardo_95 | 26:9e130f7ee829 | 582 | if(angle == 0) { |
ricardo_95 | 26:9e130f7ee829 | 583 | MPU6050 agu(p28,p27); |
ricardo_95 | 26:9e130f7ee829 | 584 | agu.getAccelero(acce); |
ricardo_95 | 26:9e130f7ee829 | 585 | angle = acce[2]*100; |
ricardo_95 | 26:9e130f7ee829 | 586 | } |
ricardo_95 | 21:13e4824bc364 | 587 | agu.getGyro(gyro); // Get gyroscope data |
ricardo_95 | 34:1614f4f2b841 | 588 | |
ricardo_95 | 33:df21cb8dc5c7 | 589 | if (test_belt == 1) { |
ricardo_95 | 33:df21cb8dc5c7 | 590 | agu_belt.getGyro(gyro_belt); // Get gyroscope data from Belt |
ricardo_95 | 33:df21cb8dc5c7 | 591 | agu_belt.getAccelero(acce_belt); // Get accelerometer data from belt |
ricardo_95 | 33:df21cb8dc5c7 | 592 | } |
ricardo_95 | 34:1614f4f2b841 | 593 | pi.printf("?,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n", acce[0], acce[1], acce[2], gyro[0], gyro[1], gyro[2],acce_belt[0], acce_belt[1], acce_belt[2], gyro_belt[0], gyro_belt[1], gyro_belt[2]); |
ricardo_95 | 34:1614f4f2b841 | 594 | |
ricardo_95 | 21:13e4824bc364 | 595 | while(t.read_us()<(3*(cycle_time/5))) {} // Wait untill 60% of cycle |
ricardo_95 | 21:13e4824bc364 | 596 | |
ricardo_95 | 21:13e4824bc364 | 597 | elec[3] = pel.readADC_SingleEnded(0); // Fourth PE readout |
ricardo_95 | 21:13e4824bc364 | 598 | } |
ricardo_95 | 21:13e4824bc364 | 599 | |
ricardo_95 | 21:13e4824bc364 | 600 | timer_functions(); |
ricardo_95 | 21:13e4824bc364 | 601 | |
ricardo_95 | 21:13e4824bc364 | 602 | batteryvoltage_current = batteryvoltage_last; |
ricardo_95 | 21:13e4824bc364 | 603 | powervoltage_current = powervoltage_last; |
ricardo_95 | 21:13e4824bc364 | 604 | read_voltage(); // Supplyvoltage control for alarm. |
ricardo_95 | 34:1614f4f2b841 | 605 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 606 | pc.printf("Voltage = %d , %d\n", batteryvoltage_current, powervoltage_current); |
ricardo_95 | 34:1614f4f2b841 | 607 | } |
ricardo_95 | 21:13e4824bc364 | 608 | uint32_t val = 0; |
ricardo_95 | 21:13e4824bc364 | 609 | colour_select(LED_colour); |
ricardo_95 | 21:13e4824bc364 | 610 | array.update(generate, NLED, val); |
ricardo_95 | 21:13e4824bc364 | 611 | set_ui_LED(); |
ricardo_95 | 21:13e4824bc364 | 612 | |
ricardo_95 | 21:13e4824bc364 | 613 | while(t.read_us()<(4*(cycle_time/5))) {} // Wait untill 80% of cycle |
ricardo_95 | 21:13e4824bc364 | 614 | |
ricardo_95 | 28:b4bee068780d | 615 | // pc.printf("2e = %d\n",agu.testConnection()); |
ricardo_95 | 28:b4bee068780d | 616 | if (a == 1) { |
ricardo_95 | 21:13e4824bc364 | 617 | elec[4] = pel.readADC_SingleEnded(0); // Fifth PE readout |
ricardo_95 | 21:13e4824bc364 | 618 | } |
ricardo_95 | 21:13e4824bc364 | 619 | |
ricardo_95 | 21:13e4824bc364 | 620 | while(t.read_us()<(4.25*(cycle_time/5))) {} // Wait untill 85% of cycle |
ricardo_95 | 28:b4bee068780d | 621 | |
ricardo_95 | 21:13e4824bc364 | 622 | serial_read(); |
ricardo_95 | 21:13e4824bc364 | 623 | serial_log(); |
ricardo_95 | 34:1614f4f2b841 | 624 | if (test_mode ==1) { |
ricardo_95 | 34:1614f4f2b841 | 625 | pc.printf("Loop time: %d ms\n",t.read_ms()); |
ricardo_95 | 34:1614f4f2b841 | 626 | } |
ricardo_95 | 34:1614f4f2b841 | 627 | if (test_pin == 1) { |
ricardo_95 | 34:1614f4f2b841 | 628 | test_mode = 1; |
ricardo_95 | 34:1614f4f2b841 | 629 | pc.printf("%d\n",test_mode); |
ricardo_95 | 34:1614f4f2b841 | 630 | } |
ricardo_95 | 34:1614f4f2b841 | 631 | if (test_pin == 0) { |
ricardo_95 | 34:1614f4f2b841 | 632 | test_mode = 0; |
ricardo_95 | 34:1614f4f2b841 | 633 | pc.printf("%d\n",test_mode); |
ricardo_95 | 34:1614f4f2b841 | 634 | } |
ricardo_95 | 34:1614f4f2b841 | 635 | if (test_mode == 1) { |
ricardo_95 | 34:1614f4f2b841 | 636 | //cycle_time = 500000; |
ricardo_95 | 34:1614f4f2b841 | 637 | } else { |
ricardo_95 | 34:1614f4f2b841 | 638 | cycle_time = 100000; |
ricardo_95 | 34:1614f4f2b841 | 639 | } |
ricardo_95 | 34:1614f4f2b841 | 640 | pc.printf("Loop time: %d ms\n",t.read_ms()); |
ricardo_95 | 21:13e4824bc364 | 641 | } |
ricardo_95 | 21:13e4824bc364 | 642 | |
DEldering | 0:c0e44c46c573 | 643 | int main() |
DEldering | 0:c0e44c46c573 | 644 | { |
ricardo_95 | 25:96c34634abda | 645 | wait_ms(boot_delay_ms); // Wait to boot sensorplate first |
DEldering | 1:a8e61f3910ad | 646 | i2c.frequency(i2c_freq); |
ricardo_95 | 21:13e4824bc364 | 647 | i2cAccu.frequency(i2c_freq); |
ricardo_95 | 12:7b3a5940f911 | 648 | pc.baud(baud); |
ricardo_95 | 12:7b3a5940f911 | 649 | pi.baud(baud); |
DEldering | 1:a8e61f3910ad | 650 | pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
ricardo_95 | 17:6ec7d594c1f1 | 651 | pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 652 | pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
ricardo_95 | 17:6ec7d594c1f1 | 653 | adsAccu.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
ricardo_95 | 32:0944efc47e46 | 654 | pi.format(8, SerialBase::None, 1); |
ricardo_95 | 7:dba5091c8b7d | 655 | |
ricardo_95 | 21:13e4824bc364 | 656 | lock.fall(&trigger_lock); // Interrupt for rising edge lock button. |
ricardo_95 | 21:13e4824bc364 | 657 | lock.rise(&timer_lock); |
ricardo_95 | 21:13e4824bc364 | 658 | reposition.fall(&trigger_reposition); |
ricardo_95 | 21:13e4824bc364 | 659 | reposition.rise(&rise_reposition); |
ricardo_95 | 21:13e4824bc364 | 660 | mute.fall(&trigger_mute); |
ricardo_95 | 21:13e4824bc364 | 661 | new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event. |
ricardo_95 | 21:13e4824bc364 | 662 | new_patient.rise(&timer_calibration); // Falling edge for calibration algorithm option. |
ricardo_95 | 7:dba5091c8b7d | 663 | delay.reset(); // Delaytimer reset en start. |
ricardo_95 | 7:dba5091c8b7d | 664 | delay.start(); |
ricardo_95 | 26:9e130f7ee829 | 665 | |
ricardo_95 | 22:a09775c25890 | 666 | set_intensity(); |
ricardo_95 | 25:96c34634abda | 667 | lock_LED = control_LED_intensity; // Lock LED initialization. |
ricardo_95 | 21:13e4824bc364 | 668 | |
ricardo_95 | 34:1614f4f2b841 | 669 | |
ricardo_95 | 19:3b5999fa7b7e | 670 | sample_cycle.attach_us(&read_adc, cycle_time); |
ricardo_95 | 19:3b5999fa7b7e | 671 | |
DEldering | 0:c0e44c46c573 | 672 | while (1) { |
DEldering | 1:a8e61f3910ad | 673 | wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms |
DEldering | 0:c0e44c46c573 | 674 | } |
ricardo_95 | 12:7b3a5940f911 | 675 | } |