Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray-Nucleo mbed

Fork of Momo_Pilot_1 by Momo Medical

Committer:
ricardo_95
Date:
Mon Oct 09 13:56:54 2017 +0000
Revision:
31:24770fa8114b
Parent:
30:bc0bddc6f8d1
Child:
32:0944efc47e46
Intensity LED's on demand from Ide changed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ricardo_95 21:13e4824bc364 1 /********************* CODE INFORMATIE ******************************
ricardo_95 12:7b3a5940f911 2 Author : Danny Eldering & Ricardo Molenaar
ricardo_95 12:7b3a5940f911 3 Company : Momo Medical
ricardo_95 12:7b3a5940f911 4 Source : developer.mbed.org
ricardo_95 12:7b3a5940f911 5 File : main.cpp
ricardo_95 12:7b3a5940f911 6 Version | -date : 1.0 | 28-9-2017
ricardo_95 30:bc0bddc6f8d1 7 Flowchart : https://drive.google.com/open?id=0B_6KzOyPBBX-NExwRFc1VEtEdUk
ricardo_95 12:7b3a5940f911 8 */
ricardo_95 12:7b3a5940f911 9
ricardo_95 21:13e4824bc364 10 /************************ CONFIG ***********************************/
ricardo_95 21:13e4824bc364 11
DEldering 0:c0e44c46c573 12 #include "mbed.h"
DEldering 0:c0e44c46c573 13 #include "Adafruit_ADS1015.h"
DEldering 0:c0e44c46c573 14 #include "MPU6050.h"
ricardo_95 7:dba5091c8b7d 15 #include "neopixel.h"
ricardo_95 12:7b3a5940f911 16 #define NLED (3)
ricardo_95 7:dba5091c8b7d 17 #define ONE_COLOR
ricardo_95 7:dba5091c8b7d 18
ricardo_95 26:9e130f7ee829 19 InterruptIn lock(p15); // Interrupts for buttons.
ricardo_95 7:dba5091c8b7d 20 InterruptIn reposition(p17);
ricardo_95 26:9e130f7ee829 21 InterruptIn mute(p16);
ricardo_95 21:13e4824bc364 22 InterruptIn new_patient(p18);
ricardo_95 12:7b3a5940f911 23 DigitalIn supplyvoltage(p20); // Analog input between 0 and 1 for reading supplyvoltage from measuringpoint before power supply.
ricardo_95 7:dba5091c8b7d 24
ricardo_95 21:13e4824bc364 25 DigitalOut LED_intern1(LED1);
ricardo_95 12:7b3a5940f911 26 DigitalOut LED_intern2(LED2);
ricardo_95 12:7b3a5940f911 27 DigitalOut LED_intern3(LED3);
ricardo_95 26:9e130f7ee829 28 DigitalOut LED_intern4(LED4);
ricardo_95 7:dba5091c8b7d 29 neopixel::PixelArray array(p11);
ricardo_95 7:dba5091c8b7d 30
ricardo_95 19:3b5999fa7b7e 31 Timer lock_hold_timer;
ricardo_95 19:3b5999fa7b7e 32 Timer calibration_hold_timer;
ricardo_95 7:dba5091c8b7d 33 Timer delay;
ricardo_95 7:dba5091c8b7d 34 Timer speaker_timer;
ricardo_95 28:b4bee068780d 35 Timer test_timer;
ricardo_95 7:dba5091c8b7d 36
ricardo_95 7:dba5091c8b7d 37 DigitalOut speaker1(p21);
ricardo_95 7:dba5091c8b7d 38 DigitalOut speaker2(p22);
ricardo_95 21:13e4824bc364 39 PwmOut lock_LED(p23);
ricardo_95 26:9e130f7ee829 40 PwmOut reposition_LED(p25);
ricardo_95 26:9e130f7ee829 41 PwmOut mute_LED(p26);
ricardo_95 26:9e130f7ee829 42 PwmOut new_patient_LED(p24);
DEldering 0:c0e44c46c573 43
DEldering 1:a8e61f3910ad 44 I2C i2c(p28, p27); // I2C
ricardo_95 21:13e4824bc364 45 I2C i2cAccu(p9, p10); // I2C for accupack
DEldering 1:a8e61f3910ad 46 MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit
DEldering 1:a8e61f3910ad 47 Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC
DEldering 1:a8e61f3910ad 48 Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC
DEldering 1:a8e61f3910ad 49 Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC
ricardo_95 21:13e4824bc364 50 Adafruit_ADS1115 adsAccu(&i2cAccu, 0x48);
DEldering 1:a8e61f3910ad 51 Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection
ricardo_95 21:13e4824bc364 52 Serial pi(p13, p14); // tx, rx // Setup serial communication for pi.
DEldering 1:a8e61f3910ad 53 Timer t; // Timer for equally time-spaced samples
DEldering 1:a8e61f3910ad 54 Ticker sample_cycle; // Polling cycle
ricardo_95 8:bf0f7a6fb1fd 55
ricardo_95 12:7b3a5940f911 56 int boot_delay_ms = 500;
DEldering 1:a8e61f3910ad 57 int cycle_time = 100000; // Cycle time in us
DEldering 1:a8e61f3910ad 58 int i2c_freq = 400000; // I2C Frequency
ricardo_95 12:7b3a5940f911 59 int baud = 115200; // Baud rate
DEldering 1:a8e61f3910ad 60 short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle
DEldering 1:a8e61f3910ad 61 short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle
DEldering 1:a8e61f3910ad 62 int angle = 0; // Accelerometer Z-axis
DEldering 1:a8e61f3910ad 63 int k = 0;
DEldering 1:a8e61f3910ad 64 float acce[3]; // Raw accelerometer data
DEldering 1:a8e61f3910ad 65 float gyro[3]; // Raw gyroscope data
ricardo_95 22:a09775c25890 66 char LED_colour = 'g'; // Variable to set LED colour.
ricardo_95 28:b4bee068780d 67 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 68 bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0;
ricardo_95 21:13e4824bc364 69 bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0;
ricardo_95 21:13e4824bc364 70 bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0;
ricardo_95 11:73c6def38fbd 71 int locktime_ms = 2000; // Waittime for lock user interface in ms.
ricardo_95 12:7b3a5940f911 72 int calibrationtime_ms = 5000; // Time to press new_patient button for calibration system.
ricardo_95 12:7b3a5940f911 73 int calibration_flash; // Variable for flash LED's to indicate calibration.
ricardo_95 7:dba5091c8b7d 74 int buttondelay_ms = 750; // Button delay in ms.
ricardo_95 7:dba5091c8b7d 75 int delay_lock_interface = 3000*60; // Delay for non using interface locktime.
ricardo_95 12:7b3a5940f911 76 int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs.
ricardo_95 17:6ec7d594c1f1 77 int alarm_voltage = 5867; // Needed voltage for alarm expressed as a digital 15 bit value (=20% of max battery voltage)
ricardo_95 25:96c34634abda 78 int red_var, green_var, blue_var; // Variables to set LED intensity.
ricardo_95 21:13e4824bc364 79 short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage.
ricardo_95 31:24770fa8114b 80 int intensity_day = 40, intensity_night = 10; // Intensity settings for LED's to wall.
ricardo_95 31:24770fa8114b 81 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 82 int a; // Test
ricardo_95 21:13e4824bc364 83
ricardo_95 21:13e4824bc364 84 /*************************** CODE ********************************/
ricardo_95 7:dba5091c8b7d 85
ricardo_95 11:73c6def38fbd 86 void set_intensity() // Function to set the intensity for the LED's.
ricardo_95 7:dba5091c8b7d 87 {
ricardo_95 11:73c6def38fbd 88 if (intensity_select == 1) {
ricardo_95 12:7b3a5940f911 89 intensity = intensity_day;
ricardo_95 12:7b3a5940f911 90 } else {
ricardo_95 12:7b3a5940f911 91 intensity = intensity_night;
ricardo_95 7:dba5091c8b7d 92 }
ricardo_95 21:13e4824bc364 93 control_LED_intensity = (intensity/100);
ricardo_95 31:24770fa8114b 94 pc.printf("Intensity LED's shines to wall = %f\n", intensity);
ricardo_95 31:24770fa8114b 95 pc.printf("Intensity LED above buttons = %f\n", control_LED_intensity);
ricardo_95 7:dba5091c8b7d 96 }
ricardo_95 7:dba5091c8b7d 97
ricardo_95 12:7b3a5940f911 98 void serial_read() // Serial read for select LED intensity and colour.
ricardo_95 12:7b3a5940f911 99 {
ricardo_95 11:73c6def38fbd 100 if (pi.readable()) {
ricardo_95 16:adbbac0c79f9 101 char message[10];
ricardo_95 27:77065263c0ea 102 pi.scanf("%s", message);
ricardo_95 28:b4bee068780d 103
ricardo_95 11:73c6def38fbd 104 if (message[0] == '0') {
ricardo_95 11:73c6def38fbd 105 intensity_select = 0;
ricardo_95 12:7b3a5940f911 106 }
ricardo_95 12:7b3a5940f911 107
ricardo_95 12:7b3a5940f911 108 if (message[0] == '1') {
ricardo_95 12:7b3a5940f911 109 intensity_select = 1;
ricardo_95 12:7b3a5940f911 110 }
ricardo_95 12:7b3a5940f911 111
ricardo_95 11:73c6def38fbd 112 if (message[1] == 'g') {
ricardo_95 12:7b3a5940f911 113 LED_colour = 'g';
ricardo_95 11:73c6def38fbd 114 }
ricardo_95 11:73c6def38fbd 115
ricardo_95 11:73c6def38fbd 116 if (message[1] == 'y') {
ricardo_95 12:7b3a5940f911 117 LED_colour = 'y';
ricardo_95 11:73c6def38fbd 118 }
ricardo_95 12:7b3a5940f911 119
ricardo_95 11:73c6def38fbd 120 if (message[1] == 'r') {
ricardo_95 12:7b3a5940f911 121 LED_colour = 'r';
ricardo_95 11:73c6def38fbd 122 }
ricardo_95 11:73c6def38fbd 123 }
ricardo_95 11:73c6def38fbd 124 }
ricardo_95 11:73c6def38fbd 125
ricardo_95 21:13e4824bc364 126 void serial_log()
ricardo_95 7:dba5091c8b7d 127 {
ricardo_95 7:dba5091c8b7d 128 if (mute_flag == 1) {
ricardo_95 19:3b5999fa7b7e 129 pi.printf(">01\n");
ricardo_95 19:3b5999fa7b7e 130 pc.printf(">01\n");
ricardo_95 7:dba5091c8b7d 131 mute_flag = 0;
ricardo_95 19:3b5999fa7b7e 132 }
ricardo_95 12:7b3a5940f911 133
ricardo_95 19:3b5999fa7b7e 134 if (new_patient_flag == 1) {
ricardo_95 19:3b5999fa7b7e 135 pi.printf(">03\n");
ricardo_95 19:3b5999fa7b7e 136 pc.printf(">03\n");
ricardo_95 7:dba5091c8b7d 137 new_patient_flag = 0;
ricardo_95 7:dba5091c8b7d 138 }
ricardo_95 12:7b3a5940f911 139
ricardo_95 19:3b5999fa7b7e 140 if (reposition_flag == 1) {
ricardo_95 19:3b5999fa7b7e 141 pi.printf(">02\n");
ricardo_95 19:3b5999fa7b7e 142 pc.printf(">02\n");
ricardo_95 7:dba5091c8b7d 143 reposition_flag = 0;
ricardo_95 7:dba5091c8b7d 144 }
ricardo_95 12:7b3a5940f911 145
ricardo_95 8:bf0f7a6fb1fd 146 if (batteryvoltage_current != batteryvoltage_last) {
ricardo_95 8:bf0f7a6fb1fd 147 pi.printf("%%d\n", batteryvoltage_current);
ricardo_95 16:adbbac0c79f9 148 batteryvoltage_last = batteryvoltage_current;
ricardo_95 12:7b3a5940f911 149 }
ricardo_95 12:7b3a5940f911 150
ricardo_95 9:514a44bf510f 151 if (LED_red_logged != LED_red_state) {
ricardo_95 12:7b3a5940f911 152 if (LED_red_state == 1) {
ricardo_95 9:514a44bf510f 153 pi.printf("&04\n");
ricardo_95 9:514a44bf510f 154 LED_red_logged = LED_red_state;
ricardo_95 12:7b3a5940f911 155 }
ricardo_95 9:514a44bf510f 156 if (LED_red_state == 0) {
ricardo_95 9:514a44bf510f 157 pi.printf("&40\n");
ricardo_95 12:7b3a5940f911 158 LED_red_logged = LED_red_state;
ricardo_95 9:514a44bf510f 159 }
ricardo_95 8:bf0f7a6fb1fd 160 }
ricardo_95 12:7b3a5940f911 161
ricardo_95 9:514a44bf510f 162 if (LED_yellow_logged != LED_yellow_state) {
ricardo_95 12:7b3a5940f911 163 if (LED_yellow_state == 1) {
ricardo_95 9:514a44bf510f 164 pi.printf("&06\n");
ricardo_95 9:514a44bf510f 165 LED_yellow_logged = LED_yellow_state;
ricardo_95 12:7b3a5940f911 166 }
ricardo_95 9:514a44bf510f 167 if (LED_yellow_state == 0) {
ricardo_95 9:514a44bf510f 168 pi.printf("&60\n");
ricardo_95 12:7b3a5940f911 169 LED_yellow_logged = LED_yellow_state;
ricardo_95 9:514a44bf510f 170 }
ricardo_95 7:dba5091c8b7d 171 }
ricardo_95 12:7b3a5940f911 172
ricardo_95 9:514a44bf510f 173 if (LED_green_logged != LED_green_state) {
ricardo_95 12:7b3a5940f911 174 if (LED_green_state == 1) {
ricardo_95 9:514a44bf510f 175 pi.printf("&05\n");
ricardo_95 9:514a44bf510f 176 LED_green_logged = LED_green_state;
ricardo_95 12:7b3a5940f911 177 }
ricardo_95 9:514a44bf510f 178 if (LED_green_state == 0) {
ricardo_95 9:514a44bf510f 179 pi.printf("&50\n");
ricardo_95 12:7b3a5940f911 180 LED_green_logged = LED_green_state;
ricardo_95 9:514a44bf510f 181 }
ricardo_95 7:dba5091c8b7d 182 }
ricardo_95 12:7b3a5940f911 183
ricardo_95 9:514a44bf510f 184 if (speaker_logged != speaker_state) {
ricardo_95 12:7b3a5940f911 185 if (speaker_state == 1) {
ricardo_95 9:514a44bf510f 186 pi.printf("&07\n");
ricardo_95 9:514a44bf510f 187 speaker_logged = speaker_state;
ricardo_95 12:7b3a5940f911 188 }
ricardo_95 9:514a44bf510f 189 if (speaker_state == 0) {
ricardo_95 9:514a44bf510f 190 pi.printf("&70\n");
ricardo_95 12:7b3a5940f911 191 speaker_logged = speaker_state;
ricardo_95 9:514a44bf510f 192 }
ricardo_95 8:bf0f7a6fb1fd 193 }
ricardo_95 9:514a44bf510f 194
ricardo_95 9:514a44bf510f 195 if (power_plug_logged != power_plug_state) {
ricardo_95 12:7b3a5940f911 196 if (power_plug_state == 1) {
ricardo_95 10:6b3034ec3c47 197 pi.printf("#08\n");
ricardo_95 21:13e4824bc364 198 pc.printf("#08 power on\n");
ricardo_95 9:514a44bf510f 199 power_plug_logged = power_plug_state;
ricardo_95 12:7b3a5940f911 200 }
ricardo_95 9:514a44bf510f 201 if (power_plug_state == 0) {
ricardo_95 10:6b3034ec3c47 202 pi.printf("#80\n");
ricardo_95 21:13e4824bc364 203 pc.printf("#08 power off\n");
ricardo_95 12:7b3a5940f911 204 power_plug_logged = power_plug_state;
ricardo_95 9:514a44bf510f 205 }
ricardo_95 9:514a44bf510f 206 }
ricardo_95 28:b4bee068780d 207 if(a == 1) {
ricardo_95 29:45647b61f4cf 208 pi.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,\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
ricardo_95 28:b4bee068780d 209 }
ricardo_95 27:77065263c0ea 210 //receiving order: 8 resistive sensors, 5 electric readings, 3 accelerometer axes, 3 gyroscope axes
DEldering 0:c0e44c46c573 211 }
DEldering 0:c0e44c46c573 212
ricardo_95 21:13e4824bc364 213 void colour_select(char LED_colour) // Function to select the colour.
ricardo_95 21:13e4824bc364 214 {
ricardo_95 21:13e4824bc364 215 set_intensity(); // Call function set_intensity
ricardo_95 21:13e4824bc364 216
ricardo_95 28:b4bee068780d 217 red_var = 0;
ricardo_95 28:b4bee068780d 218 green_var = 0;
ricardo_95 28:b4bee068780d 219 blue_var = 0;
ricardo_95 28:b4bee068780d 220
ricardo_95 21:13e4824bc364 221 if (LED_colour == 'r') {
ricardo_95 21:13e4824bc364 222 red_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 223 green_var = 0;
ricardo_95 21:13e4824bc364 224 blue_var = 0;
ricardo_95 21:13e4824bc364 225 LED_red_state = 1;
ricardo_95 21:13e4824bc364 226 } else {
ricardo_95 21:13e4824bc364 227 LED_red_state = 0;
ricardo_95 21:13e4824bc364 228 }
ricardo_95 21:13e4824bc364 229
ricardo_95 21:13e4824bc364 230 if (LED_colour == 'y') {
ricardo_95 21:13e4824bc364 231 red_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 232 green_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 233 blue_var = 0;
ricardo_95 21:13e4824bc364 234 LED_yellow_state = 1;
ricardo_95 21:13e4824bc364 235 } else {
ricardo_95 21:13e4824bc364 236 LED_green_state = 0;
ricardo_95 21:13e4824bc364 237 }
ricardo_95 21:13e4824bc364 238
ricardo_95 21:13e4824bc364 239 if (LED_colour == 'g') {
ricardo_95 21:13e4824bc364 240 red_var = 0;
ricardo_95 21:13e4824bc364 241 green_var = (2.55*intensity);
ricardo_95 21:13e4824bc364 242 blue_var = 0;
ricardo_95 21:13e4824bc364 243 LED_green_state = 1;
ricardo_95 21:13e4824bc364 244 } else {
ricardo_95 21:13e4824bc364 245 LED_green_state = 0;
ricardo_95 21:13e4824bc364 246 }
ricardo_95 21:13e4824bc364 247
ricardo_95 28:b4bee068780d 248 if (LED_colour == 'b') {
ricardo_95 28:b4bee068780d 249 red_var = 0;
ricardo_95 28:b4bee068780d 250 green_var = 0;
ricardo_95 28:b4bee068780d 251 blue_var = (2.55*intensity);
ricardo_95 28:b4bee068780d 252 }
ricardo_95 28:b4bee068780d 253
ricardo_95 21:13e4824bc364 254 if (calibration_flash >= 1) {
ricardo_95 21:13e4824bc364 255 if ((calibration_flash % 2) == 0) {
ricardo_95 21:13e4824bc364 256 red_var = 255;
ricardo_95 21:13e4824bc364 257 green_var = 255;
ricardo_95 21:13e4824bc364 258 blue_var = 255;
ricardo_95 26:9e130f7ee829 259 LED_intern4 = 1;
ricardo_95 21:13e4824bc364 260 } else {
ricardo_95 21:13e4824bc364 261 red_var = 0;
ricardo_95 21:13e4824bc364 262 green_var = 0;
ricardo_95 21:13e4824bc364 263 blue_var = 0;
ricardo_95 26:9e130f7ee829 264 LED_intern4 = 0;
ricardo_95 21:13e4824bc364 265 }
ricardo_95 21:13e4824bc364 266 calibration_flash--;
ricardo_95 21:13e4824bc364 267 }
ricardo_95 21:13e4824bc364 268 }
ricardo_95 21:13e4824bc364 269
ricardo_95 21:13e4824bc364 270 void trigger_lock() // If rising edge lock button is detected start locktimer.
ricardo_95 21:13e4824bc364 271 {
ricardo_95 21:13e4824bc364 272 pc.printf("Lock triggered.\n");
ricardo_95 21:13e4824bc364 273 lock_hold_timer.reset();
ricardo_95 21:13e4824bc364 274 lock_hold_timer.start();
ricardo_95 21:13e4824bc364 275 delay.reset();
ricardo_95 21:13e4824bc364 276 delay.start();
ricardo_95 21:13e4824bc364 277 }
ricardo_95 21:13e4824bc364 278
ricardo_95 21:13e4824bc364 279 void timer_lock() // End timer lock.
ricardo_95 21:13e4824bc364 280 {
ricardo_95 21:13e4824bc364 281 lock_flag = 0; // Set lock_flag off.
ricardo_95 21:13e4824bc364 282 lock_hold_timer.stop(); // Stop and reset holdtimer
ricardo_95 21:13e4824bc364 283 lock_hold_timer.reset();
ricardo_95 21:13e4824bc364 284 }
ricardo_95 21:13e4824bc364 285
ricardo_95 21:13e4824bc364 286 void trigger_reposition()
ricardo_95 21:13e4824bc364 287 {
ricardo_95 21:13e4824bc364 288 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 289 } else {
ricardo_95 21:13e4824bc364 290 delay.reset();
ricardo_95 21:13e4824bc364 291 delay.start();
ricardo_95 21:13e4824bc364 292 pc.printf("Reposition triggered.\n");
ricardo_95 21:13e4824bc364 293 if (LED_intern1 == 0) {
ricardo_95 21:13e4824bc364 294 LED_intern1 = 1;
ricardo_95 21:13e4824bc364 295 } else {
ricardo_95 21:13e4824bc364 296 LED_intern1 = 0;
ricardo_95 21:13e4824bc364 297 }
ricardo_95 21:13e4824bc364 298
ricardo_95 21:13e4824bc364 299 reposition_flag = 1;
ricardo_95 21:13e4824bc364 300
ricardo_95 21:13e4824bc364 301 reposition_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 302 }
ricardo_95 21:13e4824bc364 303 }
ricardo_95 21:13e4824bc364 304
ricardo_95 21:13e4824bc364 305 void rise_reposition()
ricardo_95 21:13e4824bc364 306 {
ricardo_95 21:13e4824bc364 307 reposition_LED = 0;
ricardo_95 21:13e4824bc364 308 }
ricardo_95 21:13e4824bc364 309
ricardo_95 21:13e4824bc364 310 void trigger_mute()
ricardo_95 21:13e4824bc364 311 {
ricardo_95 21:13e4824bc364 312
ricardo_95 21:13e4824bc364 313 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 314 } else {
ricardo_95 21:13e4824bc364 315 delay.reset();
ricardo_95 21:13e4824bc364 316 delay.start();
ricardo_95 21:13e4824bc364 317 mute_state = !mute_state;
ricardo_95 21:13e4824bc364 318 if (mute_state == 1) {
ricardo_95 21:13e4824bc364 319 mute_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 320 } else {
ricardo_95 21:13e4824bc364 321 mute_LED = 0;
ricardo_95 21:13e4824bc364 322 }
ricardo_95 21:13e4824bc364 323 pc.printf("Mute triggered %d.\n",mute_state);
ricardo_95 21:13e4824bc364 324 if (LED_intern1 == 0) {
ricardo_95 21:13e4824bc364 325 LED_intern1 = 1;
ricardo_95 21:13e4824bc364 326 } else {
ricardo_95 21:13e4824bc364 327 LED_intern1 = 0;
ricardo_95 21:13e4824bc364 328 }
ricardo_95 21:13e4824bc364 329
ricardo_95 21:13e4824bc364 330 mute_flag = 1;
ricardo_95 21:13e4824bc364 331 }
ricardo_95 21:13e4824bc364 332 }
ricardo_95 21:13e4824bc364 333
ricardo_95 21:13e4824bc364 334 void trigger_new_patient() // Function to trigger hold timer for new patient calibration function.
ricardo_95 21:13e4824bc364 335 {
ricardo_95 21:13e4824bc364 336
ricardo_95 21:13e4824bc364 337 if (lock_state == 1) {
ricardo_95 21:13e4824bc364 338 } else {
ricardo_95 21:13e4824bc364 339 calibration_hold_timer.reset();
ricardo_95 21:13e4824bc364 340 calibration_hold_timer.start();
ricardo_95 21:13e4824bc364 341 new_patient_LED = control_LED_intensity;;
ricardo_95 21:13e4824bc364 342 pc.printf("New patient triggered.\n");
ricardo_95 21:13e4824bc364 343 }
ricardo_95 21:13e4824bc364 344 }
ricardo_95 21:13e4824bc364 345
ricardo_95 21:13e4824bc364 346 void timer_calibration() // Timer calibration function.
ricardo_95 21:13e4824bc364 347 {
ricardo_95 21:13e4824bc364 348 new_patient_LED = 0;
ricardo_95 21:13e4824bc364 349
ricardo_95 21:13e4824bc364 350 if (0 < calibration_hold_timer.read_ms() < calibrationtime_ms) {
ricardo_95 21:13e4824bc364 351 new_patient_flag = 1;
ricardo_95 21:13e4824bc364 352 }
ricardo_95 21:13e4824bc364 353
ricardo_95 21:13e4824bc364 354 calibration_hold_timer.stop();
ricardo_95 21:13e4824bc364 355 calibration_hold_timer.reset();
ricardo_95 21:13e4824bc364 356
ricardo_95 21:13e4824bc364 357 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 358 } else {
ricardo_95 21:13e4824bc364 359 if (calibration_flag == 0) {
ricardo_95 21:13e4824bc364 360
ricardo_95 21:13e4824bc364 361 if (LED_intern1 == 0) {
ricardo_95 21:13e4824bc364 362 LED_intern1 = 1;
ricardo_95 21:13e4824bc364 363 } else {
ricardo_95 21:13e4824bc364 364 LED_intern1 = 0;
ricardo_95 21:13e4824bc364 365 }
ricardo_95 21:13e4824bc364 366
ricardo_95 21:13e4824bc364 367 } else {
ricardo_95 21:13e4824bc364 368 calibration_flag = 0;
ricardo_95 21:13e4824bc364 369 }
ricardo_95 21:13e4824bc364 370 }
ricardo_95 21:13e4824bc364 371 }
ricardo_95 21:13e4824bc364 372
ricardo_95 21:13e4824bc364 373 void timer_functions()
ricardo_95 21:13e4824bc364 374 {
ricardo_95 28:b4bee068780d 375 pc.printf("locktime= %d\n",lock_hold_timer.read_ms());
ricardo_95 21:13e4824bc364 376 if ((lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && lock == 0) { // If statement for lock function.
ricardo_95 21:13e4824bc364 377 lock_flag = 1;
ricardo_95 21:13e4824bc364 378 LED_intern2 = !LED_intern2;
ricardo_95 21:13e4824bc364 379 lock_state = !lock_state;
ricardo_95 21:13e4824bc364 380 if (lock_state == 0) {
ricardo_95 21:13e4824bc364 381 lock_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 382 } else {
ricardo_95 21:13e4824bc364 383 lock_LED = 0;
ricardo_95 21:13e4824bc364 384 }
ricardo_95 21:13e4824bc364 385 }
ricardo_95 21:13e4824bc364 386
ricardo_95 23:4a09554bdc1a 387 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 388 calibration_flag = 1;
ricardo_95 21:13e4824bc364 389 calibration_flash = 11;
ricardo_95 21:13e4824bc364 390 pc.printf("Calibrate triggered.\n");
ricardo_95 21:13e4824bc364 391 pi.printf(">30\n"); // Print statement for serial communication to inform algorithm to calibrate.
ricardo_95 21:13e4824bc364 392 }
ricardo_95 21:13e4824bc364 393
ricardo_95 21:13e4824bc364 394 if (delay.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active.
ricardo_95 21:13e4824bc364 395 lock_state = 1;
ricardo_95 21:13e4824bc364 396 LED_intern2 = 1;
ricardo_95 21:13e4824bc364 397 lock_LED = 0;
ricardo_95 21:13e4824bc364 398 }
ricardo_95 21:13e4824bc364 399 }
ricardo_95 21:13e4824bc364 400
ricardo_95 21:13e4824bc364 401 void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) // Generate LED colour.
ricardo_95 21:13e4824bc364 402 {
ricardo_95 21:13e4824bc364 403 out->red = red_var;
ricardo_95 21:13e4824bc364 404 out->green = green_var;
ricardo_95 21:13e4824bc364 405 out->blue = blue_var;
ricardo_95 21:13e4824bc364 406 }
ricardo_95 21:13e4824bc364 407
ricardo_95 21:13e4824bc364 408 void set_ui_LED() // Control functions for LED above buttons (added because of failures).
ricardo_95 21:13e4824bc364 409 {
ricardo_95 21:13e4824bc364 410 if (lock_state == 1) {
ricardo_95 21:13e4824bc364 411 } else {
ricardo_95 21:13e4824bc364 412 if (reposition == 0) {
ricardo_95 22:a09775c25890 413 reposition_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 414 } else {
ricardo_95 21:13e4824bc364 415 reposition_LED = 0;
ricardo_95 21:13e4824bc364 416 }
ricardo_95 21:13e4824bc364 417
ricardo_95 21:13e4824bc364 418 if (new_patient == 0) {
ricardo_95 22:a09775c25890 419 new_patient_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 420 } else {
ricardo_95 21:13e4824bc364 421 new_patient_LED = 0;
ricardo_95 21:13e4824bc364 422 }
ricardo_95 21:13e4824bc364 423 }
ricardo_95 21:13e4824bc364 424 }
ricardo_95 21:13e4824bc364 425
ricardo_95 21:13e4824bc364 426 void read_voltage()
ricardo_95 21:13e4824bc364 427 {
ricardo_95 25:96c34634abda 428 if (power_plug_state == 1) { // If supplyvoltage (readed from input) is greater then the setted alarmvoltage.
ricardo_95 21:13e4824bc364 429 alarm = 0; // Alarm is off.
ricardo_95 21:13e4824bc364 430 speaker_state = 0;
ricardo_95 21:13e4824bc364 431 } else {
ricardo_95 21:13e4824bc364 432 alarm = 1; // Else alarm is on.
ricardo_95 21:13e4824bc364 433 speaker_state = 1;
ricardo_95 21:13e4824bc364 434 }
ricardo_95 26:9e130f7ee829 435
ricardo_95 26:9e130f7ee829 436
ricardo_95 21:13e4824bc364 437 if (alarm == 1 && mute_state == 1 && (batteryvoltage_current > alarm_voltage)) { // Set speaker on for 750 ms.
ricardo_95 21:13e4824bc364 438 speaker1 = 0; // Set speaker.
ricardo_95 21:13e4824bc364 439 speaker2 = 0;
ricardo_95 21:13e4824bc364 440 }
ricardo_95 21:13e4824bc364 441
ricardo_95 24:782c4dc4a3ff 442 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 443 speaker1 = 1; // Set speaker.
ricardo_95 21:13e4824bc364 444 speaker2 = 1;
ricardo_95 21:13e4824bc364 445 speaker_timer.start(); // Set timer for speaker to iterate on and off.
ricardo_95 21:13e4824bc364 446 }
ricardo_95 21:13e4824bc364 447
ricardo_95 21:13e4824bc364 448 if ((speaker_timer.read_ms() > speaker_active_ms) && (speaker_timer.read_ms() < (speaker_active_ms*2))) {
ricardo_95 21:13e4824bc364 449 speaker1 = 0; // Turn off speaker (use two outputs because of currentlimiting of one).
ricardo_95 21:13e4824bc364 450 speaker2 = 0;
ricardo_95 21:13e4824bc364 451 }
ricardo_95 21:13e4824bc364 452
ricardo_95 21:13e4824bc364 453 if (speaker_timer.read_ms() > (speaker_active_ms*2)) {
ricardo_95 21:13e4824bc364 454 speaker_timer.stop(); // Stop speaker timer.
ricardo_95 21:13e4824bc364 455 speaker_timer.reset();
ricardo_95 21:13e4824bc364 456 }
ricardo_95 21:13e4824bc364 457
ricardo_95 21:13e4824bc364 458 batteryvoltage_current = adsAccu.readADC_SingleEnded(0); // Read channel 0 from external ADC.
ricardo_95 25:96c34634abda 459 powervoltage_current = adsAccu.readADC_SingleEnded(1); // Read channel 1 from external ADC.
ricardo_95 26:9e130f7ee829 460
ricardo_95 21:13e4824bc364 461 if (powervoltage_current < 20000) {
ricardo_95 21:13e4824bc364 462 power_plug_state = 0;
ricardo_95 28:b4bee068780d 463 LED_colour = 'b';
ricardo_95 21:13e4824bc364 464 } else {
ricardo_95 21:13e4824bc364 465 power_plug_state = 1;
ricardo_95 21:13e4824bc364 466 }
ricardo_95 21:13e4824bc364 467 }
ricardo_95 21:13e4824bc364 468
ricardo_95 21:13e4824bc364 469 void read_adc()
ricardo_95 21:13e4824bc364 470 {
ricardo_95 21:13e4824bc364 471 t.reset();
ricardo_95 21:13e4824bc364 472 t.start();
ricardo_95 28:b4bee068780d 473 a = agu.testConnection();/*
ricardo_95 28:b4bee068780d 474 pc.printf("a= %d\n",a);
ricardo_95 28:b4bee068780d 475 if( a==0)
ricardo_95 28:b4bee068780d 476 {
ricardo_95 28:b4bee068780d 477 lock_state = 1;
ricardo_95 28:b4bee068780d 478 LED_intern2 = 1;
ricardo_95 28:b4bee068780d 479 lock_LED = 0;
ricardo_95 28:b4bee068780d 480 }*/
ricardo_95 28:b4bee068780d 481 if (a == 1) {
ricardo_95 21:13e4824bc364 482 elec[0] = pel.readADC_SingleEnded(0); // First PE readout
ricardo_95 21:13e4824bc364 483
ricardo_95 21:13e4824bc364 484 for (k = 0; k < 4; k = k + 1) {
ricardo_95 21:13e4824bc364 485 res[k] = pr1.readADC_SingleEnded(k); // First 4 PR readout
ricardo_95 21:13e4824bc364 486 }
ricardo_95 21:13e4824bc364 487 while(t.read_us()<(1*(cycle_time/5))) {} // Wait untill 20% of cycle
ricardo_95 21:13e4824bc364 488
ricardo_95 21:13e4824bc364 489 elec[1] = pel.readADC_SingleEnded(0); // Second PE readout
ricardo_95 21:13e4824bc364 490
ricardo_95 21:13e4824bc364 491 for (k = 0; k < 4; k = k + 1) {
ricardo_95 21:13e4824bc364 492 res[k+4] = pr2.readADC_SingleEnded(k); // Last 4 PR readout
ricardo_95 21:13e4824bc364 493 }
ricardo_95 21:13e4824bc364 494 while(t.read_us()<(2*(cycle_time/5))) {} // Wait untill 40% of cycle
ricardo_95 21:13e4824bc364 495
ricardo_95 21:13e4824bc364 496 elec[2] = pel.readADC_SingleEnded(0); // Third PE readout
ricardo_95 21:13e4824bc364 497
ricardo_95 21:13e4824bc364 498 agu.getAccelero(acce); // Get accelerometer data
ricardo_95 26:9e130f7ee829 499 angle = acce[2]*100;
ricardo_95 26:9e130f7ee829 500 if(angle == 0) {
ricardo_95 26:9e130f7ee829 501 MPU6050 agu(p28,p27);
ricardo_95 26:9e130f7ee829 502 agu.getAccelero(acce);
ricardo_95 26:9e130f7ee829 503 angle = acce[2]*100;
ricardo_95 26:9e130f7ee829 504 }
ricardo_95 21:13e4824bc364 505 agu.getGyro(gyro); // Get gyroscope data
ricardo_95 21:13e4824bc364 506
ricardo_95 21:13e4824bc364 507 while(t.read_us()<(3*(cycle_time/5))) {} // Wait untill 60% of cycle
ricardo_95 21:13e4824bc364 508
ricardo_95 21:13e4824bc364 509 elec[3] = pel.readADC_SingleEnded(0); // Fourth PE readout
ricardo_95 21:13e4824bc364 510 }
ricardo_95 21:13e4824bc364 511
ricardo_95 21:13e4824bc364 512 timer_functions();
ricardo_95 21:13e4824bc364 513
ricardo_95 21:13e4824bc364 514 batteryvoltage_current = batteryvoltage_last;
ricardo_95 21:13e4824bc364 515 powervoltage_current = powervoltage_last;
ricardo_95 21:13e4824bc364 516 read_voltage(); // Supplyvoltage control for alarm.
ricardo_95 21:13e4824bc364 517 pc.printf("Voltage = %d , %d\n", batteryvoltage_current, powervoltage_current);
ricardo_95 21:13e4824bc364 518
ricardo_95 21:13e4824bc364 519 uint32_t val = 0;
ricardo_95 21:13e4824bc364 520 colour_select(LED_colour);
ricardo_95 21:13e4824bc364 521 array.update(generate, NLED, val);
ricardo_95 21:13e4824bc364 522 set_ui_LED();
ricardo_95 21:13e4824bc364 523
ricardo_95 21:13e4824bc364 524 while(t.read_us()<(4*(cycle_time/5))) {} // Wait untill 80% of cycle
ricardo_95 21:13e4824bc364 525
ricardo_95 28:b4bee068780d 526 // pc.printf("2e = %d\n",agu.testConnection());
ricardo_95 28:b4bee068780d 527 if (a == 1) {
ricardo_95 21:13e4824bc364 528 elec[4] = pel.readADC_SingleEnded(0); // Fifth PE readout
ricardo_95 21:13e4824bc364 529 }
ricardo_95 21:13e4824bc364 530
ricardo_95 21:13e4824bc364 531 while(t.read_us()<(4.25*(cycle_time/5))) {} // Wait untill 85% of cycle
ricardo_95 28:b4bee068780d 532
ricardo_95 21:13e4824bc364 533 serial_read();
ricardo_95 21:13e4824bc364 534 serial_log();
ricardo_95 21:13e4824bc364 535 }
ricardo_95 21:13e4824bc364 536
DEldering 0:c0e44c46c573 537 int main()
DEldering 0:c0e44c46c573 538 {
ricardo_95 25:96c34634abda 539 wait_ms(boot_delay_ms); // Wait to boot sensorplate first
DEldering 1:a8e61f3910ad 540 i2c.frequency(i2c_freq);
ricardo_95 21:13e4824bc364 541 i2cAccu.frequency(i2c_freq);
ricardo_95 12:7b3a5940f911 542 pc.baud(baud);
ricardo_95 12:7b3a5940f911 543 pi.baud(baud);
DEldering 1:a8e61f3910ad 544 pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
ricardo_95 17:6ec7d594c1f1 545 pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
DEldering 1:a8e61f3910ad 546 pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
ricardo_95 17:6ec7d594c1f1 547 adsAccu.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V
ricardo_95 30:bc0bddc6f8d1 548 pi.format(8, SerialBase::Even, 1);
ricardo_95 7:dba5091c8b7d 549
ricardo_95 21:13e4824bc364 550 lock.fall(&trigger_lock); // Interrupt for rising edge lock button.
ricardo_95 21:13e4824bc364 551 lock.rise(&timer_lock);
ricardo_95 21:13e4824bc364 552 reposition.fall(&trigger_reposition);
ricardo_95 21:13e4824bc364 553 reposition.rise(&rise_reposition);
ricardo_95 21:13e4824bc364 554 mute.fall(&trigger_mute);
ricardo_95 21:13e4824bc364 555 new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event.
ricardo_95 21:13e4824bc364 556 new_patient.rise(&timer_calibration); // Falling edge for calibration algorithm option.
ricardo_95 7:dba5091c8b7d 557 delay.reset(); // Delaytimer reset en start.
ricardo_95 7:dba5091c8b7d 558 delay.start();
ricardo_95 26:9e130f7ee829 559
ricardo_95 22:a09775c25890 560 set_intensity();
ricardo_95 25:96c34634abda 561 lock_LED = control_LED_intensity; // Lock LED initialization.
ricardo_95 21:13e4824bc364 562
ricardo_95 19:3b5999fa7b7e 563 sample_cycle.attach_us(&read_adc, cycle_time);
ricardo_95 19:3b5999fa7b7e 564
DEldering 0:c0e44c46c573 565 while (1) {
DEldering 1:a8e61f3910ad 566 wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms
DEldering 0:c0e44c46c573 567 }
ricardo_95 12:7b3a5940f911 568 }