Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Committer:
Ishy
Date:
Thu Feb 15 16:32:24 2018 +0000
Revision:
55:b74e7076d7a0
Parent:
53:54c882995514
Child:
56:97dea631c5f2
Changes made regarding logging buttons and logging lights above buttons.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ricardo_95 36:d10f368d037b 1 /********************* CODE INFORMATON ******************************
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 45:7ebe860702b4 7 Current version name: 2.1.4
ricardo_95 45:7ebe860702b4 8 Date of modification: 8-1-2018
ricardo_95 36:d10f368d037b 9 Purpose of this file: Code for LPC1768 microcontroller for controlling buttons, LED's and communicate to PI
ricardo_95 45:7ebe860702b4 10 Update ‘what’s new in this version?’: Sensorplate connection test changed from software to hardware check.
deldering95 48:aca02ef5cd01 11 ADC readout for accu deleted.
ricardo_95 36:d10f368d037b 12 Todo: -> Fix LED issue (yellow and red flashes at random moments);
deldering95 43:d09814c177a0 13 -> Optimize functions / improve readability;
ricardo_95 36:d10f368d037b 14 -> Split functions in seperate files?;
ricardo_95 36:d10f368d037b 15 -> Fix when sensorplate is not connected;
ricardo_95 36:d10f368d037b 16 -> Rule 570: if statement change to turn off LED's when power is plugged out (also related to rule 106).
ricardo_95 36:d10f368d037b 17 -> For the speaker two outputs of the uC are used. Add MOSFET with external supply and control these by uC?
ricardo_95 36:d10f368d037b 18 Source file: http://mbed.com/
ricardo_95 33:df21cb8dc5c7 19
ricardo_95 36:d10f368d037b 20 Information files:
deldering95 43:d09814c177a0 21 (1) Flowchart:
ricardo_95 36:d10f368d037b 22 (2) Table serial communication: https://docs.google.com/spreadsheets/d/1kHlithHxtoMDGvbcdH8vwSw5W5ArxlwDPsyfra1dtQM/edit?usp=drive_web
deldering95 43:d09814c177a0 23 (3) Technical manual CU-/software:
ricardo_95 12:7b3a5940f911 24 */
ricardo_95 12:7b3a5940f911 25
ricardo_95 21:13e4824bc364 26 /************************ CONFIG ***********************************/
ricardo_95 21:13e4824bc364 27
ricardo_95 36:d10f368d037b 28 #include "mbed.h" // Include files and define parameters.
DEldering 0:c0e44c46c573 29 #include "Adafruit_ADS1015.h"
DEldering 0:c0e44c46c573 30 #include "MPU6050.h"
ricardo_95 33:df21cb8dc5c7 31 #include "MPU6050_belt.h"
deldering95 43:d09814c177a0 32 #include "PixelArray.h"
deldering95 43:d09814c177a0 33 #include "WS2812.h"
ricardo_95 37:d8f7b2b5719a 34
deldering95 43:d09814c177a0 35 #define WS2812_BUF 3
deldering95 43:d09814c177a0 36 #define NUM_COLORS 5
deldering95 43:d09814c177a0 37 #define NUM_LEDS_PER_COLOR 3
deldering95 48:aca02ef5cd01 38 #define NUMBER_LED_FRONT (3)
ricardo_95 7:dba5091c8b7d 39 #define ONE_COLOR
ricardo_95 7:dba5091c8b7d 40
ricardo_95 46:a0e6e088a50a 41 InterruptIn button_lock(PC_1); // Input on intterupt base decleration.
ricardo_95 46:a0e6e088a50a 42 InterruptIn button_reposition(PC_3);
Ishy 41:5380ada94ec6 43 InterruptIn button_mute(PC_2);
ricardo_95 46:a0e6e088a50a 44 InterruptIn button_new_patient(PC_0);
ricardo_95 7:dba5091c8b7d 45
deldering95 43:d09814c177a0 46 DigitalIn intensity_code(PA_12);
deldering95 43:d09814c177a0 47 DigitalIn colour_code_1(PA_11);
deldering95 43:d09814c177a0 48 DigitalIn colour_code_0(PB_12);
ricardo_95 46:a0e6e088a50a 49 DigitalIn testpin_sensorplate(PC_6);
deldering95 43:d09814c177a0 50
deldering95 43:d09814c177a0 51 DigitalOut LED_on_dev_board1(LED1); // Decleration of digital outputs.
ricardo_95 36:d10f368d037b 52 DigitalOut LED_on_dev_board2(LED2);
ricardo_95 36:d10f368d037b 53 DigitalOut LED_on_dev_board3(LED3);
ricardo_95 36:d10f368d037b 54 DigitalOut LED_on_dev_board4(LED4);
deldering95 43:d09814c177a0 55 DigitalOut speaker1(PC_12); // relatie aangeven!
deldering95 43:d09814c177a0 56 //neopixel::PixelArray indicator_LEDs(PA_7);
deldering95 43:d09814c177a0 57 PixelArray px(WS2812_BUF);
deldering95 51:69087c44e8ac 58 //WS2812 ws(PA_7, WS2812_BUF, 3, 9, 9, 6);
deldering95 53:54c882995514 59 WS2812 ws(PA_7, WS2812_BUF, 3, 9, 9, 6);
deldering95 43:d09814c177a0 60
deldering95 51:69087c44e8ac 61 PwmOut lock_feedback_LED(PB_13); //(PB_1); // Declaration of pulse with modulation outputs.
deldering95 51:69087c44e8ac 62 PwmOut mute_feedback_LED(PB_14); //(PB_15);
deldering95 51:69087c44e8ac 63 PwmOut new_patient_feedback_LED(PB_15); //(PB_14);
deldering95 51:69087c44e8ac 64 PwmOut reposition_feedback_LED(PB_1); //(PB_13);
ricardo_95 36:d10f368d037b 65
ricardo_95 37:d8f7b2b5719a 66 Timer button_lock_hold_timer; // Timer for time lock button should be pressed.
ricardo_95 36:d10f368d037b 67 Timer button_calibration_hold_timer; // Timer for calibration function (new patient holding 5 seconds).
deldering95 43:d09814c177a0 68 Timer delay_between_button_pressed; // Timer for time between two buttons (to prevent pressing buttons simultaneously).
ricardo_95 36:d10f368d037b 69 Timer speaker_timer; // Timer for speaker activation.
ricardo_95 36:d10f368d037b 70 Timer piezo_electric_sample_timer; // Timer for equally time-spaced samples.
deldering95 48:aca02ef5cd01 71 Timer Knight_Rider_Timer;
deldering95 43:d09814c177a0 72 /*
ricardo_95 36:d10f368d037b 73 The code underneath this commentbox has some fixed parameters for serial/ADC reading:
ricardo_95 36:d10f368d037b 74 -> The address for the angle_device_reference_belt is set to 0x68 in the file MPU6050_belt (rule number: 19);
ricardo_95 36:d10f368d037b 75 -> The adress for the angle_device_sensorplate is set to 0x69 in the file MPU6050.h (rule number: 19);
ricardo_95 36:d10f368d037b 76 -> This is because of using the same I2C line;
ricardo_95 36:d10f368d037b 77 -> For detailed information/questions about this item, please read the technical manual or contact: Ricardo Molenaar | ricardo.molenaar@gmail.com
ricardo_95 36:d10f368d037b 78 */
Ishy 41:5380ada94ec6 79 I2C i2c_sensorplate_adc(PB_9, PB_8); // I2C for sensorplate.
ricardo_95 46:a0e6e088a50a 80 MPU6050_belt angle_device_sensorplate(PB_9, PB_8); // i2c pins // i2c address hardcoded 0x68.
Ishy 41:5380ada94ec6 81 MPU6050_belt angle_device_reference_belt(PB_9, PB_8); // i2c pins // i2c address hardcoded 0x69.
ricardo_95 36:d10f368d037b 82 Adafruit_ADS1115 piezo_resistive_adc1(&i2c_sensorplate_adc, 0x48); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 83 Adafruit_ADS1115 piezo_resistive_adc2(&i2c_sensorplate_adc, 0x49); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 84 Adafruit_ADS1115 piezo_electric_adc(&i2c_sensorplate_adc, 0x4B); // i2c pins, i2c address.
deldering95 43:d09814c177a0 85 Serial usb_serial(SERIAL_TX, SERIAL_RX); // tx, rx
deldering95 43:d09814c177a0 86 Serial pi_serial(PC_10, PC_11); // tx, rx
ricardo_95 36:d10f368d037b 87 Ticker total_readout_cycle; // Polling cycle.
deldering95 43:d09814c177a0 88 // End of commentbox related to the serial configuration/ADC reading components.
ricardo_95 34:1614f4f2b841 89
ricardo_95 12:7b3a5940f911 90 int boot_delay_ms = 500;
Ishy 42:673ddef4cfa4 91 int total_readout_cycle_time_us = 100000; // Cycle time in us.
deldering95 48:aca02ef5cd01 92 int total_knight_rider_cycle_time_ms = 1000;
ricardo_95 39:cff99a9b7237 93 int i2c__frequency = 400000; // I2C Frequency.
ricardo_95 36:d10f368d037b 94 int baud_rate = 115200; // Baud rate.
ricardo_95 36:d10f368d037b 95 short piezo_resistive_array[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle.
ricardo_95 36:d10f368d037b 96 short piezo_electric_array[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle.
ricardo_95 36:d10f368d037b 97 int angle = 0; // Accelerometer Z-axis.
ricardo_95 37:d8f7b2b5719a 98 float accelerometer_sensorplate[3] = {0.0, 0.0, 0.0}; // Raw accelerometer data.
ricardo_95 36:d10f368d037b 99 float gyroscope_sensorplate[3]; // Raw gyroscope data.
ricardo_95 36:d10f368d037b 100 float accelerometer_reference_belt[3]; // Raw accelerometer data from belt.
ricardo_95 36:d10f368d037b 101 float gyroscope_reference_belt[3]; // Raw gyroscope data from belt.
deldering95 51:69087c44e8ac 102 int colourbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff,0xffa500,0xffffff}; // hex codes for the different colours
deldering95 44:dcbde3175a37 103 char LED_colour = 'w'; // Variable to set LED colour (standard set to green, untill PI sends other character). Other possible colours: red ('r') & yellow ('y').
ricardo_95 37:d8f7b2b5719a 104 bool lock_state = false, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1; // Boolean variables for logging states.
Ishy 55:b74e7076d7a0 105 bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0, new_patient_lock_flag = 0, reposition_lock_flag = 0, mute_lock_flag; // Flag variables.
ricardo_95 21:13e4824bc364 106 bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0;
Ishy 55:b74e7076d7a0 107 bool auto_lock_led_logged = 0, lock_is_logged = 0, speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0; // is toevoegen
ricardo_95 36:d10f368d037b 108 int locktime_ms = 2000; // Waittime for lock user interface in ms.
deldering95 48:aca02ef5cd01 109 int calibrationtime_ms = 2000; // Time to press new_patient button for calibration system.
ricardo_95 37:d8f7b2b5719a 110 int calibration_flash = 0; // Variable for flash LED's to indicate calibration.
deldering95 48:aca02ef5cd01 111 int lock_flash = 0;
Ishy 55:b74e7076d7a0 112 int buttondelay_ms = 250; // Button delay in ms. Default: 750
ricardo_95 36:d10f368d037b 113 int delay_lock_interface = 3000*60; // Delay for non using interface locktime.
ricardo_95 36:d10f368d037b 114 int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs.
ricardo_95 38:764847892afc 115 int alarm_voltage = 2400; // Needed voltage for alarm expressed as a digital 15 bit value (= 20% of max battery voltage).
ricardo_95 37:d8f7b2b5719a 116 int LED_red_intensity = 0, LED_blue_intensity = 0, LED_green_intensity = 0; // Variables to set LED intensity.
ricardo_95 45:7ebe860702b4 117 //short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage. Maybe change current to other?
ricardo_95 45:7ebe860702b4 118 //const int digital_value_ADC_powervoltage_unplugged = 15000; // Digital value to set the indicating LEDs to wall blue (should be set off later). const in hoofdletters
deldering95 51:69087c44e8ac 119 int intensity_day = 20, intensity_night = 5; // Intensity settings for LED's to wall.
ricardo_95 38:764847892afc 120 double intensity = 0.0, control_LED_intensity = 0.0; // Variable between 0 and 1 to set the intensity of the LED's above the buttons. Intensity change to smart name!
deldering95 44:dcbde3175a37 121 int colour_code = 0b00;
deldering95 44:dcbde3175a37 122 bool pi_active = false;
ricardo_95 33:df21cb8dc5c7 123 /*************************** TEST ********************************/
ricardo_95 36:d10f368d037b 124 // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V).
deldering95 43:d09814c177a0 125 Timer test_timer;
Ishy 41:5380ada94ec6 126 DigitalIn test_pin(PA_11, PullDown);
ricardo_95 33:df21cb8dc5c7 127
deldering95 43:d09814c177a0 128 // Variable to set if belt is used to test algorithm:
deldering95 43:d09814c177a0 129 bool test_belt = 0;
ricardo_95 33:df21cb8dc5c7 130
deldering95 43:d09814c177a0 131 // Set test mode on (log functions to pc serial: interrupts, LED intensity and serial messages):
Ishy 55:b74e7076d7a0 132 bool test_mode = 1;
ricardo_95 34:1614f4f2b841 133
ricardo_95 36:d10f368d037b 134 // Variable for connection test (should be changed):
ricardo_95 46:a0e6e088a50a 135 bool connection_test_sensorplate;
ricardo_95 33:df21cb8dc5c7 136
ricardo_95 21:13e4824bc364 137 /*************************** CODE ********************************/
ricardo_95 7:dba5091c8b7d 138
ricardo_95 36:d10f368d037b 139 void set_intensity_LEDs() // Function to set the intensity for the LED's.
ricardo_95 7:dba5091c8b7d 140 {
ricardo_95 11:73c6def38fbd 141 if (intensity_select == 1) {
ricardo_95 12:7b3a5940f911 142 intensity = intensity_day;
ricardo_95 12:7b3a5940f911 143 } else {
ricardo_95 12:7b3a5940f911 144 intensity = intensity_night;
ricardo_95 7:dba5091c8b7d 145 }
ricardo_95 21:13e4824bc364 146 control_LED_intensity = (intensity/100);
ricardo_95 34:1614f4f2b841 147
ricardo_95 37:d8f7b2b5719a 148 if (test_mode == 1) { // If statement for test purposal LED_intensity values. if def gebruiken voor testmode
Ishy 55:b74e7076d7a0 149 // usb_serial.printf("Intensity LED's shines to wall = %f\n", intensity);
Ishy 55:b74e7076d7a0 150 // usb_serial.printf("Intensity LED's above buttons = %f\n", control_LED_intensity);
ricardo_95 33:df21cb8dc5c7 151 }
ricardo_95 7:dba5091c8b7d 152 }
ricardo_95 7:dba5091c8b7d 153
ricardo_95 36:d10f368d037b 154 void serial_read() // Function for serial read for select LED intensity and colour.
ricardo_95 12:7b3a5940f911 155 {
deldering95 43:d09814c177a0 156 colour_code = (colour_code_1 << 1 | colour_code_0);
deldering95 48:aca02ef5cd01 157 if(colour_code != 0b00 && pi_active == false) {
deldering95 48:aca02ef5cd01 158 pi_active = true;
deldering95 48:aca02ef5cd01 159 }
deldering95 48:aca02ef5cd01 160 if(pi_active) {
deldering95 48:aca02ef5cd01 161 intensity_select = intensity_code;
deldering95 48:aca02ef5cd01 162 switch(colour_code) {
deldering95 48:aca02ef5cd01 163 case 0b11 :
deldering95 48:aca02ef5cd01 164 LED_colour = 'y';
deldering95 48:aca02ef5cd01 165 break;
deldering95 48:aca02ef5cd01 166 case 0b10 :
deldering95 48:aca02ef5cd01 167 LED_colour = 'b';
deldering95 48:aca02ef5cd01 168 break;
deldering95 48:aca02ef5cd01 169 case 0b01 :
deldering95 48:aca02ef5cd01 170 LED_colour = 'g';
deldering95 48:aca02ef5cd01 171 break;
deldering95 48:aca02ef5cd01 172 case 0b00 :
deldering95 48:aca02ef5cd01 173 LED_colour = 'r';
deldering95 48:aca02ef5cd01 174 break;
deldering95 48:aca02ef5cd01 175 default :
deldering95 48:aca02ef5cd01 176 LED_colour = 'w';
deldering95 48:aca02ef5cd01 177 break;
deldering95 48:aca02ef5cd01 178 }
deldering95 48:aca02ef5cd01 179 }
ricardo_95 34:1614f4f2b841 180
deldering95 43:d09814c177a0 181 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 182 // usb_serial.printf("Intensity_select = %d en LED_colour = %d\n", intensity_select, LED_colour);
deldering95 43:d09814c177a0 183 }
ricardo_95 34:1614f4f2b841 184
deldering95 43:d09814c177a0 185 if (test_mode == 0) {
deldering95 48:aca02ef5cd01 186 //usb_serial.printf("Message: %s\n", message);
deldering95 48:aca02ef5cd01 187 usb_serial.printf("Intensity_select = %d en LED_colour = %d\n", intensity_select, LED_colour);
deldering95 43:d09814c177a0 188
ricardo_95 11:73c6def38fbd 189 }
ricardo_95 11:73c6def38fbd 190 }
ricardo_95 11:73c6def38fbd 191
Ishy 55:b74e7076d7a0 192 void serial_log() { // Function for serial logging. See link to table with code declarations above in code.
Ishy 55:b74e7076d7a0 193
Ishy 55:b74e7076d7a0 194 if (reposition_flag == 1) { // If statement to control logging for reposition button.
ricardo_95 36:d10f368d037b 195 pi_serial.printf(">01\n");
ricardo_95 34:1614f4f2b841 196
deldering95 43:d09814c177a0 197 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 198 usb_serial.printf(">01\n");
ricardo_95 33:df21cb8dc5c7 199 }
ricardo_95 34:1614f4f2b841 200
Ishy 55:b74e7076d7a0 201 reposition_flag = 0;
Ishy 55:b74e7076d7a0 202 }
Ishy 55:b74e7076d7a0 203
Ishy 55:b74e7076d7a0 204 if (reposition_lock_flag == 1) { // If statement to control logging for reposition button.
Ishy 55:b74e7076d7a0 205 pi_serial.printf(">10\n");
Ishy 55:b74e7076d7a0 206
Ishy 55:b74e7076d7a0 207 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 208 usb_serial.printf(">10\n");
Ishy 55:b74e7076d7a0 209 }
Ishy 55:b74e7076d7a0 210
Ishy 55:b74e7076d7a0 211 reposition_lock_flag = 0;
ricardo_95 19:3b5999fa7b7e 212 }
ricardo_95 12:7b3a5940f911 213
ricardo_95 36:d10f368d037b 214 if (new_patient_flag == 1) { // If statement to control logging for new patient button.
Ishy 55:b74e7076d7a0 215 pi_serial.printf(">02\n");
Ishy 55:b74e7076d7a0 216
Ishy 55:b74e7076d7a0 217 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 218 usb_serial.printf(">02\n");
Ishy 55:b74e7076d7a0 219 }
Ishy 55:b74e7076d7a0 220
Ishy 55:b74e7076d7a0 221 new_patient_flag = 0;
Ishy 55:b74e7076d7a0 222 }
Ishy 55:b74e7076d7a0 223
Ishy 55:b74e7076d7a0 224 if (new_patient_lock_flag == 1) { // If statement to control logging for new patient button.
Ishy 55:b74e7076d7a0 225 pi_serial.printf(">20\n");
Ishy 55:b74e7076d7a0 226
Ishy 55:b74e7076d7a0 227 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 228 usb_serial.printf(">20\n");
Ishy 55:b74e7076d7a0 229 }
Ishy 55:b74e7076d7a0 230
Ishy 55:b74e7076d7a0 231 new_patient_lock_flag = 0;
Ishy 55:b74e7076d7a0 232 }
Ishy 55:b74e7076d7a0 233 // The new calibration button trigger
Ishy 55:b74e7076d7a0 234 if (mute_flag == 1) { // If statement to control logging for new patient button.
ricardo_95 36:d10f368d037b 235 pi_serial.printf(">03\n");
ricardo_95 34:1614f4f2b841 236
deldering95 43:d09814c177a0 237 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 238 usb_serial.printf(">03\n");
ricardo_95 33:df21cb8dc5c7 239 }
ricardo_95 34:1614f4f2b841 240
Ishy 55:b74e7076d7a0 241 mute_flag = 0;
ricardo_95 7:dba5091c8b7d 242 }
Ishy 55:b74e7076d7a0 243 // The new calibration button trigger
Ishy 55:b74e7076d7a0 244 if (mute_lock_flag == 1) { // If statement to control logging for new patient button.
Ishy 55:b74e7076d7a0 245 pi_serial.printf(">30\n");
ricardo_95 34:1614f4f2b841 246
deldering95 43:d09814c177a0 247 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 248 usb_serial.printf(">30\n");
ricardo_95 33:df21cb8dc5c7 249 }
ricardo_95 34:1614f4f2b841 250
Ishy 55:b74e7076d7a0 251 mute_lock_flag = 0;
Ishy 55:b74e7076d7a0 252 }
Ishy 55:b74e7076d7a0 253
Ishy 55:b74e7076d7a0 254 if (lock_flag == 1 && !lock_is_logged) {
Ishy 55:b74e7076d7a0 255 if (lock_state == 0) // If statement to control logging for lock button.
Ishy 55:b74e7076d7a0 256 pi_serial.printf(">04\n");
Ishy 55:b74e7076d7a0 257 else if(lock_state == 1)
Ishy 55:b74e7076d7a0 258 pi_serial.printf(">40\n");
Ishy 55:b74e7076d7a0 259
Ishy 55:b74e7076d7a0 260 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 261 if (lock_state == 0) // If statement to control logging for lock button.
Ishy 55:b74e7076d7a0 262 usb_serial.printf(">04\n");
Ishy 55:b74e7076d7a0 263 else if(lock_state == 1)
Ishy 55:b74e7076d7a0 264 usb_serial.printf(">40\n"); // If statement for test purposal.
Ishy 55:b74e7076d7a0 265 }
Ishy 55:b74e7076d7a0 266 lock_is_logged = 1;
ricardo_95 7:dba5091c8b7d 267 }
ricardo_95 12:7b3a5940f911 268
ricardo_95 36:d10f368d037b 269 if (LED_red_logged != LED_red_state) { // If statement to control logging for LED_red.
ricardo_95 12:7b3a5940f911 270 if (LED_red_state == 1) {
Ishy 55:b74e7076d7a0 271 pi_serial.printf("&01\n");
ricardo_95 9:514a44bf510f 272 LED_red_logged = LED_red_state;
ricardo_95 34:1614f4f2b841 273 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 274 usb_serial.printf("&01\n");
ricardo_95 34:1614f4f2b841 275 }
ricardo_95 12:7b3a5940f911 276 }
deldering95 43:d09814c177a0 277
ricardo_95 9:514a44bf510f 278 if (LED_red_state == 0) {
Ishy 55:b74e7076d7a0 279 pi_serial.printf("&10\n");
ricardo_95 12:7b3a5940f911 280 LED_red_logged = LED_red_state;
ricardo_95 34:1614f4f2b841 281 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 282 usb_serial.printf("&10\n");
ricardo_95 34:1614f4f2b841 283 }
ricardo_95 9:514a44bf510f 284 }
ricardo_95 8:bf0f7a6fb1fd 285 }
ricardo_95 12:7b3a5940f911 286
ricardo_95 36:d10f368d037b 287 if (LED_yellow_logged != LED_yellow_state) { // If statement to control logging for LED_yellow.
deldering95 43:d09814c177a0 288 if (LED_yellow_state == 1) {
Ishy 55:b74e7076d7a0 289 pi_serial.printf("&02\n");
ricardo_95 9:514a44bf510f 290 LED_yellow_logged = LED_yellow_state;
ricardo_95 34:1614f4f2b841 291 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 292 usb_serial.printf("&02\n");
ricardo_95 34:1614f4f2b841 293 }
ricardo_95 12:7b3a5940f911 294 }
ricardo_95 9:514a44bf510f 295 if (LED_yellow_state == 0) {
Ishy 55:b74e7076d7a0 296 pi_serial.printf("&20\n");
ricardo_95 12:7b3a5940f911 297 LED_yellow_logged = LED_yellow_state;
ricardo_95 34:1614f4f2b841 298 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 299 usb_serial.printf("&20\n");
ricardo_95 34:1614f4f2b841 300 }
ricardo_95 9:514a44bf510f 301 }
ricardo_95 7:dba5091c8b7d 302 }
ricardo_95 12:7b3a5940f911 303
ricardo_95 36:d10f368d037b 304 if (LED_green_logged != LED_green_state) { // If statement to control logging for LED_green.
ricardo_95 12:7b3a5940f911 305 if (LED_green_state == 1) {
Ishy 55:b74e7076d7a0 306 pi_serial.printf("&03\n");
ricardo_95 9:514a44bf510f 307 LED_green_logged = LED_green_state;
deldering95 43:d09814c177a0 308
ricardo_95 34:1614f4f2b841 309 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 310 usb_serial.printf("&03\n");
ricardo_95 34:1614f4f2b841 311 }
ricardo_95 12:7b3a5940f911 312 }
deldering95 43:d09814c177a0 313
ricardo_95 9:514a44bf510f 314 if (LED_green_state == 0) {
Ishy 55:b74e7076d7a0 315 pi_serial.printf("&30\n");
ricardo_95 12:7b3a5940f911 316 LED_green_logged = LED_green_state;
deldering95 43:d09814c177a0 317
ricardo_95 34:1614f4f2b841 318 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 319 usb_serial.printf("&30\n");
ricardo_95 34:1614f4f2b841 320 }
ricardo_95 9:514a44bf510f 321 }
ricardo_95 7:dba5091c8b7d 322 }
ricardo_95 12:7b3a5940f911 323
deldering95 43:d09814c177a0 324 if (speaker_logged != speaker_state) { // If statement to control logging for speaker.
ricardo_95 12:7b3a5940f911 325 if (speaker_state == 1) {
Ishy 55:b74e7076d7a0 326 pi_serial.printf("&04\n");
ricardo_95 9:514a44bf510f 327 speaker_logged = speaker_state;
deldering95 43:d09814c177a0 328
ricardo_95 36:d10f368d037b 329 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 330 usb_serial.printf("&04\n");
ricardo_95 34:1614f4f2b841 331 }
ricardo_95 12:7b3a5940f911 332 }
deldering95 43:d09814c177a0 333
ricardo_95 9:514a44bf510f 334 if (speaker_state == 0) {
Ishy 55:b74e7076d7a0 335 pi_serial.printf("&40\n");
ricardo_95 12:7b3a5940f911 336 speaker_logged = speaker_state;
deldering95 43:d09814c177a0 337
ricardo_95 36:d10f368d037b 338 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 339 usb_serial.printf("&40\n");
ricardo_95 34:1614f4f2b841 340 }
ricardo_95 9:514a44bf510f 341 }
ricardo_95 8:bf0f7a6fb1fd 342 }
ricardo_95 9:514a44bf510f 343
Ishy 55:b74e7076d7a0 344 // if (power_plug_logged != power_plug_state) { // If statement to control the logging for the state of the power plug.
Ishy 55:b74e7076d7a0 345 // if (power_plug_state == 1) {
Ishy 55:b74e7076d7a0 346 // pi_serial.printf("#08\n");
Ishy 55:b74e7076d7a0 347 //
Ishy 55:b74e7076d7a0 348 // if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 349 // usb_serial.printf("#08\n");
Ishy 55:b74e7076d7a0 350 // }
Ishy 55:b74e7076d7a0 351 // power_plug_logged = power_plug_state;
Ishy 55:b74e7076d7a0 352 // }
Ishy 55:b74e7076d7a0 353 //
Ishy 55:b74e7076d7a0 354 // if (power_plug_state == 0) {
Ishy 55:b74e7076d7a0 355 // pi_serial.printf("#80\n");
Ishy 55:b74e7076d7a0 356 //
Ishy 55:b74e7076d7a0 357 // if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 358 // usb_serial.printf("#80\n");
Ishy 55:b74e7076d7a0 359 // }
Ishy 55:b74e7076d7a0 360 // power_plug_logged = power_plug_state;
Ishy 55:b74e7076d7a0 361 // }
Ishy 55:b74e7076d7a0 362 // }
deldering95 43:d09814c177a0 363
deldering95 43:d09814c177a0 364 if (connection_test_sensorplate == 1) { // If statement for sending serial information sensorplate data when connection test is active.
deldering95 43:d09814c177a0 365 // Receiving order sensor information: 8 resistive sensors, 5 electric readings. Is splitted in two parts - part 1/2.
ricardo_95 39:cff99a9b7237 366 pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
deldering95 43:d09814c177a0 367
ricardo_95 34:1614f4f2b841 368 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 369 // usb_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[1], piezo_resistive_array[2], piezo_resistive_array[3], piezo_resistive_array[4], piezo_resistive_array[5], piezo_resistive_array[6], piezo_resistive_array[7], piezo_electric_array[0], piezo_electric_array[1], piezo_electric_array[2], piezo_electric_array[3], piezo_electric_array[4]); // print all to serial port
ricardo_95 34:1614f4f2b841 370 }
deldering95 53:54c882995514 371 } else {
deldering95 53:54c882995514 372 pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",0,0,0,0,0,0,0,0,0,0,0,0,0);
ricardo_95 28:b4bee068780d 373 }
ricardo_95 34:1614f4f2b841 374
DEldering 0:c0e44c46c573 375 }
DEldering 0:c0e44c46c573 376
deldering95 43:d09814c177a0 377 void colour_select_indicating_LED_wall(char nLED_colour) // Function to select the colour for LED's to wall (values comes from algorithm).
ricardo_95 21:13e4824bc364 378 {
ricardo_95 36:d10f368d037b 379 set_intensity_LEDs(); // Call function set_intensity_LEDs to set the intensity for LED's to wall and above buttons.
deldering95 43:d09814c177a0 380 ws.setII(2.55*intensity);
deldering95 43:d09814c177a0 381 switch(nLED_colour) {
deldering95 43:d09814c177a0 382 case 'r' :
deldering95 43:d09814c177a0 383 px.SetAll(colourbuf[0]);
Ishy 55:b74e7076d7a0 384 LED_red_state = 1;
Ishy 55:b74e7076d7a0 385 LED_yellow_state = 0;
deldering95 43:d09814c177a0 386 break;
deldering95 43:d09814c177a0 387 case 'g' :
deldering95 43:d09814c177a0 388 px.SetAll(colourbuf[1]);
Ishy 55:b74e7076d7a0 389 LED_green_state = 1;
Ishy 55:b74e7076d7a0 390 LED_red_state = 0;
deldering95 43:d09814c177a0 391 break;
deldering95 43:d09814c177a0 392 case 'b' :
deldering95 43:d09814c177a0 393 px.SetAll(colourbuf[2]);
deldering95 43:d09814c177a0 394 break;
deldering95 43:d09814c177a0 395 case 'y' :
deldering95 43:d09814c177a0 396 px.SetAll(colourbuf[3]);
Ishy 55:b74e7076d7a0 397 LED_yellow_state = 1;
Ishy 55:b74e7076d7a0 398 LED_green_state = 0;
deldering95 43:d09814c177a0 399 break;
deldering95 43:d09814c177a0 400 default :
deldering95 43:d09814c177a0 401 px.SetAll(colourbuf[4]);
deldering95 43:d09814c177a0 402 }
deldering95 43:d09814c177a0 403
deldering95 43:d09814c177a0 404 if (calibration_flash >= 1) {
deldering95 43:d09814c177a0 405 if ((calibration_flash % 2) == 0) {
deldering95 48:aca02ef5cd01 406 px.SetAll(colourbuf[2]);
deldering95 43:d09814c177a0 407 } else {
deldering95 43:d09814c177a0 408 ws.setII(0);
deldering95 43:d09814c177a0 409 }
deldering95 43:d09814c177a0 410 calibration_flash--;
deldering95 43:d09814c177a0 411 }
deldering95 51:69087c44e8ac 412 //for (int z=WS2812_BUF; z >= 0 ; z--) {
deldering95 53:54c882995514 413 ws.write_offsets(px.getBuf(),0,0,0);
deldering95 51:69087c44e8ac 414 //}
ricardo_95 21:13e4824bc364 415 }
ricardo_95 21:13e4824bc364 416
ricardo_95 36:d10f368d037b 417 void trigger_lock() // If rising edge lock button is detected start locktimer.
ricardo_95 21:13e4824bc364 418 {
ricardo_95 34:1614f4f2b841 419 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 420 usb_serial.printf("Lock triggered.\n");
ricardo_95 34:1614f4f2b841 421 }
Ishy 55:b74e7076d7a0 422 if (lock_state == 0) pi_serial.printf(">44\n");
Ishy 55:b74e7076d7a0 423 else if (lock_state == 1) pi_serial.printf(">00\n");
ricardo_95 36:d10f368d037b 424 button_lock_hold_timer.reset();
ricardo_95 36:d10f368d037b 425 button_lock_hold_timer.start();
ricardo_95 36:d10f368d037b 426 delay_between_button_pressed.reset();
ricardo_95 36:d10f368d037b 427 delay_between_button_pressed.start();
ricardo_95 21:13e4824bc364 428 }
ricardo_95 21:13e4824bc364 429
ricardo_95 36:d10f368d037b 430 void end_timer_lock_button() // End timer lock.
ricardo_95 21:13e4824bc364 431 {
deldering95 43:d09814c177a0 432 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 433 usb_serial.printf("Lock released.\n");
ricardo_95 36:d10f368d037b 434 }
ricardo_95 36:d10f368d037b 435 lock_flag = 0; // Set lock_flag off.
Ishy 55:b74e7076d7a0 436 lock_is_logged = 0;
ricardo_95 36:d10f368d037b 437 button_lock_hold_timer.stop(); // Stop and reset holdtimer
ricardo_95 36:d10f368d037b 438 button_lock_hold_timer.reset();
ricardo_95 36:d10f368d037b 439 }
ricardo_95 36:d10f368d037b 440
ricardo_95 36:d10f368d037b 441 void reposition_button_triggered()
ricardo_95 36:d10f368d037b 442 {
ricardo_95 36:d10f368d037b 443 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time.
deldering95 48:aca02ef5cd01 444 lock_flash = 10;
Ishy 55:b74e7076d7a0 445 }
Ishy 55:b74e7076d7a0 446
Ishy 55:b74e7076d7a0 447 delay_between_button_pressed.reset();
Ishy 55:b74e7076d7a0 448 delay_between_button_pressed.start();
Ishy 55:b74e7076d7a0 449 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 450 usb_serial.printf("Reposition triggered.\n");
Ishy 55:b74e7076d7a0 451 LED_on_dev_board1 = !LED_on_dev_board1;
Ishy 55:b74e7076d7a0 452 }
Ishy 55:b74e7076d7a0 453
Ishy 55:b74e7076d7a0 454 if (lock_state == 1) reposition_lock_flag = 1;
Ishy 55:b74e7076d7a0 455 else {
ricardo_95 21:13e4824bc364 456 reposition_flag = 1;
ricardo_95 36:d10f368d037b 457 reposition_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 458 pi_serial.printf("&05\n");
Ishy 55:b74e7076d7a0 459 if(test_mode == 1) usb_serial.printf("&05\n");
ricardo_95 21:13e4824bc364 460 }
ricardo_95 21:13e4824bc364 461 }
ricardo_95 21:13e4824bc364 462
deldering95 43:d09814c177a0 463 void rise_reposition() // Interrupt for rising edge reposition function (deactivation; active low).
ricardo_95 21:13e4824bc364 464 {
deldering95 43:d09814c177a0 465 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 466 usb_serial.printf("Reposition released.\n");
ricardo_95 34:1614f4f2b841 467 }
Ishy 55:b74e7076d7a0 468
Ishy 55:b74e7076d7a0 469 if (reposition_feedback_LED != 0) {
Ishy 55:b74e7076d7a0 470 pi_serial.printf("&50\n");
Ishy 55:b74e7076d7a0 471
Ishy 55:b74e7076d7a0 472 if(test_mode == 1) usb_serial.printf("&50\n");
Ishy 55:b74e7076d7a0 473 }
Ishy 55:b74e7076d7a0 474
ricardo_95 36:d10f368d037b 475 reposition_feedback_LED = 0;
ricardo_95 21:13e4824bc364 476 }
Ishy 55:b74e7076d7a0 477 //TODO rename to calibration
ricardo_95 36:d10f368d037b 478 void mute_button_triggered()
ricardo_95 21:13e4824bc364 479 {
ricardo_95 21:13e4824bc364 480
ricardo_95 36:d10f368d037b 481 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time.
deldering95 48:aca02ef5cd01 482 lock_flash = 10;
Ishy 55:b74e7076d7a0 483 }
Ishy 55:b74e7076d7a0 484
Ishy 55:b74e7076d7a0 485 delay_between_button_pressed.reset();
Ishy 55:b74e7076d7a0 486 delay_between_button_pressed.start();
Ishy 55:b74e7076d7a0 487 button_calibration_hold_timer.reset(); // inline ?
Ishy 55:b74e7076d7a0 488 button_calibration_hold_timer.start();
Ishy 55:b74e7076d7a0 489
Ishy 55:b74e7076d7a0 490 if (lock_state == 1) {
Ishy 55:b74e7076d7a0 491 mute_lock_flag = 1;
Ishy 55:b74e7076d7a0 492 }
Ishy 55:b74e7076d7a0 493
Ishy 55:b74e7076d7a0 494 else {
Ishy 55:b74e7076d7a0 495 mute_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 496 pi_serial.printf("&07\n");
Ishy 55:b74e7076d7a0 497 if (test_mode == 1) usb_serial.printf("&07\n");
Ishy 55:b74e7076d7a0 498 mute_flag = 1;
Ishy 55:b74e7076d7a0 499 }
Ishy 55:b74e7076d7a0 500
Ishy 55:b74e7076d7a0 501 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 502 usb_serial.printf("Calibration triggered\n");
Ishy 55:b74e7076d7a0 503 LED_on_dev_board1 = !LED_on_dev_board1;
Ishy 55:b74e7076d7a0 504 }
deldering95 43:d09814c177a0 505
Ishy 55:b74e7076d7a0 506
ricardo_95 21:13e4824bc364 507 }
ricardo_95 21:13e4824bc364 508
deldering95 51:69087c44e8ac 509 void rise_mute() // Interrupt for rising edge reposition function (deactivation; active low).
ricardo_95 21:13e4824bc364 510 {
deldering95 43:d09814c177a0 511 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 512 usb_serial.printf("Calibration released.\n");
ricardo_95 34:1614f4f2b841 513 }
Ishy 55:b74e7076d7a0 514 if(lock_state == 0 && mute_feedback_LED != 0) {
Ishy 55:b74e7076d7a0 515 pi_serial.printf("&70\n");
Ishy 55:b74e7076d7a0 516 if (test_mode == 1) usb_serial.printf("&70\n");
Ishy 55:b74e7076d7a0 517 }
Ishy 55:b74e7076d7a0 518
deldering95 51:69087c44e8ac 519 mute_feedback_LED = 0;
ricardo_95 21:13e4824bc364 520
deldering95 43:d09814c177a0 521 button_calibration_hold_timer.stop(); // Timer reset for calibration function of new patient button.
ricardo_95 36:d10f368d037b 522 button_calibration_hold_timer.reset();
ricardo_95 21:13e4824bc364 523
ricardo_95 36:d10f368d037b 524 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { // Control statement for lock interface and delay for non using buttons at the same time.
ricardo_95 21:13e4824bc364 525 } else {
ricardo_95 21:13e4824bc364 526 if (calibration_flag == 0) {
ricardo_95 21:13e4824bc364 527
deldering95 43:d09814c177a0 528 if (LED_on_dev_board1 == 0) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 529 LED_on_dev_board1 = 1;
ricardo_95 21:13e4824bc364 530 } else {
ricardo_95 36:d10f368d037b 531 LED_on_dev_board1 = 0;
ricardo_95 21:13e4824bc364 532 }
ricardo_95 21:13e4824bc364 533
ricardo_95 21:13e4824bc364 534 } else {
ricardo_95 21:13e4824bc364 535 calibration_flag = 0;
ricardo_95 21:13e4824bc364 536 }
ricardo_95 21:13e4824bc364 537 }
deldering95 51:69087c44e8ac 538
deldering95 51:69087c44e8ac 539 }
deldering95 51:69087c44e8ac 540
deldering95 51:69087c44e8ac 541 void trigger_new_patient() // Function to trigger hold timer for new patient and calibration function.
deldering95 51:69087c44e8ac 542 {
deldering95 51:69087c44e8ac 543
deldering95 51:69087c44e8ac 544 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {
deldering95 51:69087c44e8ac 545 lock_flash = 10;
Ishy 55:b74e7076d7a0 546 }
Ishy 55:b74e7076d7a0 547
Ishy 55:b74e7076d7a0 548 delay_between_button_pressed.reset();
Ishy 55:b74e7076d7a0 549 delay_between_button_pressed.start();
Ishy 55:b74e7076d7a0 550
Ishy 55:b74e7076d7a0 551 if (lock_state == 1) new_patient_lock_flag = 1;
Ishy 55:b74e7076d7a0 552 else {
Ishy 55:b74e7076d7a0 553 new_patient_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 554 pi_serial.printf("&06\n");
deldering95 51:69087c44e8ac 555 new_patient_flag = 1;
Ishy 55:b74e7076d7a0 556
Ishy 55:b74e7076d7a0 557 if(test_mode == 1){
Ishy 55:b74e7076d7a0 558 usb_serial.printf("&06\n");
deldering95 51:69087c44e8ac 559 }
deldering95 51:69087c44e8ac 560 }
Ishy 55:b74e7076d7a0 561
Ishy 55:b74e7076d7a0 562 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 563 usb_serial.printf("New patient triggered.\n");
Ishy 55:b74e7076d7a0 564 }
Ishy 55:b74e7076d7a0 565
deldering95 51:69087c44e8ac 566 }
deldering95 51:69087c44e8ac 567
deldering95 51:69087c44e8ac 568 void activate_new_patient_function() // Timer calibration function.
deldering95 51:69087c44e8ac 569 {
deldering95 51:69087c44e8ac 570 if (test_mode == 1) { // If statement for test purposal.
deldering95 51:69087c44e8ac 571 usb_serial.printf("New patient released.\n");
Ishy 55:b74e7076d7a0 572
Ishy 55:b74e7076d7a0 573 }
Ishy 55:b74e7076d7a0 574
Ishy 55:b74e7076d7a0 575 if (new_patient_feedback_LED != 0) {
Ishy 55:b74e7076d7a0 576 pi_serial.printf("&60\n");
Ishy 55:b74e7076d7a0 577 if(test_mode) usb_serial.printf("&60\n");
deldering95 51:69087c44e8ac 578 }
deldering95 51:69087c44e8ac 579 new_patient_feedback_LED = 0;
ricardo_95 21:13e4824bc364 580 }
ricardo_95 21:13e4824bc364 581
deldering95 43:d09814c177a0 582 void timer_functions() // Function which contains statements using timers.
ricardo_95 21:13e4824bc364 583 {
deldering95 48:aca02ef5cd01 584 if (button_lock == 1) {
deldering95 44:dcbde3175a37 585 button_lock_hold_timer.stop();
deldering95 44:dcbde3175a37 586 button_lock_hold_timer.reset();
deldering95 44:dcbde3175a37 587 }
ricardo_95 36:d10f368d037b 588 if ((button_lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && button_lock == 0) { // If statement for lock function.
ricardo_95 21:13e4824bc364 589 lock_flag = 1;
ricardo_95 36:d10f368d037b 590 LED_on_dev_board2 = !LED_on_dev_board2;
ricardo_95 21:13e4824bc364 591 lock_state = !lock_state;
deldering95 43:d09814c177a0 592
deldering95 43:d09814c177a0 593 if (lock_state == 0) { // If statement to control lock feedback LED above button.
ricardo_95 36:d10f368d037b 594 lock_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 595 pi_serial.printf("&08\n");
Ishy 55:b74e7076d7a0 596 if(test_mode == 1) usb_serial.printf("&08\n");
ricardo_95 21:13e4824bc364 597 } else {
ricardo_95 36:d10f368d037b 598 lock_feedback_LED = 0;
Ishy 55:b74e7076d7a0 599 pi_serial.printf("&80\n");
Ishy 55:b74e7076d7a0 600 if(test_mode == 1) usb_serial.printf("&80\n");
ricardo_95 21:13e4824bc364 601 }
ricardo_95 21:13e4824bc364 602 }
ricardo_95 21:13e4824bc364 603
deldering95 51:69087c44e8ac 604 if (button_mute == 1) {
deldering95 44:dcbde3175a37 605 button_calibration_hold_timer.stop();
deldering95 44:dcbde3175a37 606 button_calibration_hold_timer.reset();
deldering95 44:dcbde3175a37 607 }
deldering95 44:dcbde3175a37 608
deldering95 51:69087c44e8ac 609 if ((button_calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && button_mute == 0 && lock_state == 0) { // If statement for calibration algorithm.
ricardo_95 21:13e4824bc364 610 calibration_flag = 1;
ricardo_95 21:13e4824bc364 611 calibration_flash = 11;
Ishy 55:b74e7076d7a0 612
Ishy 55:b74e7076d7a0 613 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 614 usb_serial.printf("Calibrate triggered.\n");
ricardo_95 34:1614f4f2b841 615 }
Ishy 55:b74e7076d7a0 616 pi_serial.printf(">33\n");
Ishy 55:b74e7076d7a0 617
ricardo_95 21:13e4824bc364 618 }
ricardo_95 21:13e4824bc364 619
ricardo_95 36:d10f368d037b 620 if (delay_between_button_pressed.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active.
ricardo_95 21:13e4824bc364 621 lock_state = 1;
ricardo_95 36:d10f368d037b 622 LED_on_dev_board2 = 1;
ricardo_95 36:d10f368d037b 623 lock_feedback_LED = 0;
Ishy 55:b74e7076d7a0 624 if (!auto_lock_led_logged) {
Ishy 55:b74e7076d7a0 625 pi_serial.printf("&80\n");
Ishy 55:b74e7076d7a0 626 if(test_mode == 1) usb_serial.printf("&80\n");
Ishy 55:b74e7076d7a0 627 auto_lock_led_logged = 1;
Ishy 55:b74e7076d7a0 628 }
Ishy 55:b74e7076d7a0 629 } else {
Ishy 55:b74e7076d7a0 630 auto_lock_led_logged = 0;
ricardo_95 21:13e4824bc364 631 }
ricardo_95 21:13e4824bc364 632 }
ricardo_95 21:13e4824bc364 633
ricardo_95 36:d10f368d037b 634 void set_userinterface_LED() // Control functions for LED above buttons (added because of failures).
ricardo_95 21:13e4824bc364 635 {
ricardo_95 21:13e4824bc364 636 if (lock_state == 1) {
ricardo_95 21:13e4824bc364 637 } else {
ricardo_95 36:d10f368d037b 638 if (button_reposition == 0) {
ricardo_95 36:d10f368d037b 639 reposition_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 640 } else {
ricardo_95 36:d10f368d037b 641 reposition_feedback_LED = 0;
ricardo_95 21:13e4824bc364 642 }
ricardo_95 21:13e4824bc364 643
ricardo_95 36:d10f368d037b 644 if (button_new_patient == 0) {
ricardo_95 36:d10f368d037b 645 new_patient_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 646 } else {
ricardo_95 36:d10f368d037b 647 new_patient_feedback_LED = 0;
ricardo_95 21:13e4824bc364 648 }
ricardo_95 21:13e4824bc364 649 }
deldering95 48:aca02ef5cd01 650 if (lock_flash >= 1 && lock_state == 1) {
deldering95 48:aca02ef5cd01 651 if ((lock_flash % 2) == 0) {
deldering95 48:aca02ef5cd01 652 lock_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 653 pi_serial.printf("&08\n");
Ishy 55:b74e7076d7a0 654 if(test_mode == 1) usb_serial.printf("&08\n");
deldering95 48:aca02ef5cd01 655 } else {
deldering95 48:aca02ef5cd01 656 lock_feedback_LED = 0;
Ishy 55:b74e7076d7a0 657 pi_serial.printf("&80\n");
Ishy 55:b74e7076d7a0 658 if(test_mode == 1) usb_serial.printf("&80\n");
ricardo_95 21:13e4824bc364 659 }
deldering95 48:aca02ef5cd01 660 lock_flash--;
deldering95 48:aca02ef5cd01 661 } else {
deldering95 48:aca02ef5cd01 662 lock_flash = 0;
ricardo_95 21:13e4824bc364 663 }
ricardo_95 21:13e4824bc364 664 }
ricardo_95 21:13e4824bc364 665
deldering95 43:d09814c177a0 666 int main() // Main function. inline function "Momo Init" bijvoorbeeld
deldering95 43:d09814c177a0 667 {
deldering95 51:69087c44e8ac 668 ws.useII(WS2812::GLOBAL); // use global intensity scaling
deldering95 51:69087c44e8ac 669 set_intensity_LEDs(); // Initialize intensity for user interface LED's and LED's shines to wall.
deldering95 51:69087c44e8ac 670 colour_select_indicating_LED_wall(LED_colour);
deldering95 53:54c882995514 671
deldering95 48:aca02ef5cd01 672 speaker1 = 1;
ricardo_95 36:d10f368d037b 673 wait_ms(boot_delay_ms); // Wait to boot sensorplate first.
deldering95 48:aca02ef5cd01 674 speaker1 = 0;
ricardo_95 39:cff99a9b7237 675 i2c_sensorplate_adc.frequency(i2c__frequency); // Set frequency for i2c connection to sensorplate (variable is declared in config part).
ricardo_95 36:d10f368d037b 676 usb_serial.baud(baud_rate); // Set serial USB connection baud rate (variable is declared in config part).
ricardo_95 36:d10f368d037b 677 pi_serial.baud(baud_rate); // Same as line 697, but now for serial PI connection.
ricardo_95 36:d10f368d037b 678 piezo_resistive_adc1.setGain(GAIN_TWOTHIRDS); // Set ranges of ADC to +/-6.144V (end is marked with #):
deldering95 43:d09814c177a0 679 piezo_resistive_adc2.setGain(GAIN_TWOTHIRDS);
deldering95 51:69087c44e8ac 680 piezo_electric_adc.setGain(GAIN_TWO);
deldering95 43:d09814c177a0 681 pi_serial.format(8, SerialBase::None, 1); // Set serial communication line with PI.
ricardo_95 7:dba5091c8b7d 682
deldering95 43:d09814c177a0 683 button_lock.mode(PullUp);
deldering95 43:d09814c177a0 684 button_reposition.mode(PullUp);
deldering95 43:d09814c177a0 685 button_mute.mode(PullUp);
deldering95 43:d09814c177a0 686 button_new_patient.mode(PullUp);
deldering95 43:d09814c177a0 687
ricardo_95 36:d10f368d037b 688 button_lock.fall(&trigger_lock); // Interrupt for rising edge lock button.
ricardo_95 36:d10f368d037b 689 button_lock.rise(&end_timer_lock_button);
ricardo_95 36:d10f368d037b 690 button_reposition.fall(&reposition_button_triggered);
ricardo_95 36:d10f368d037b 691 button_reposition.rise(&rise_reposition);
ricardo_95 36:d10f368d037b 692 button_mute.fall(&mute_button_triggered);
deldering95 51:69087c44e8ac 693 button_mute.rise(&rise_mute);
ricardo_95 36:d10f368d037b 694 button_new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event.
deldering95 43:d09814c177a0 695 button_new_patient.rise(&activate_new_patient_function); // Falling edge for calibration algorithm option.
ricardo_95 21:13e4824bc364 696
deldering95 48:aca02ef5cd01 697 __disable_irq();
deldering95 48:aca02ef5cd01 698 while(!pi_active) {
deldering95 48:aca02ef5cd01 699 Knight_Rider_Timer.reset();
deldering95 48:aca02ef5cd01 700 Knight_Rider_Timer.start();
deldering95 48:aca02ef5cd01 701 reposition_feedback_LED = control_LED_intensity;
deldering95 48:aca02ef5cd01 702 new_patient_feedback_LED = 0;
deldering95 48:aca02ef5cd01 703 while(Knight_Rider_Timer.read_ms()<(1*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 704 new_patient_feedback_LED = control_LED_intensity;
deldering95 48:aca02ef5cd01 705 while(Knight_Rider_Timer.read_ms()<(2*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 706 mute_feedback_LED = control_LED_intensity;
deldering95 48:aca02ef5cd01 707 reposition_feedback_LED = 0;
deldering95 48:aca02ef5cd01 708 while(Knight_Rider_Timer.read_ms()<(3*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 709 lock_feedback_LED = control_LED_intensity;
deldering95 48:aca02ef5cd01 710 new_patient_feedback_LED = 0;
deldering95 48:aca02ef5cd01 711 while(Knight_Rider_Timer.read_ms()<(4*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 712 mute_feedback_LED = 0;
deldering95 48:aca02ef5cd01 713 while(Knight_Rider_Timer.read_ms()<(5*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 714 mute_feedback_LED = control_LED_intensity;
deldering95 48:aca02ef5cd01 715 while(Knight_Rider_Timer.read_ms()<(6*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 716 new_patient_feedback_LED = control_LED_intensity;
deldering95 48:aca02ef5cd01 717 lock_feedback_LED = 0;
deldering95 48:aca02ef5cd01 718 while(Knight_Rider_Timer.read_ms()<(7*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 719 reposition_feedback_LED = control_LED_intensity;
deldering95 48:aca02ef5cd01 720 mute_feedback_LED = 0;
ricardo_95 19:3b5999fa7b7e 721
deldering95 48:aca02ef5cd01 722 serial_read();
deldering95 48:aca02ef5cd01 723 colour_select_indicating_LED_wall(LED_colour);
deldering95 48:aca02ef5cd01 724 while(Knight_Rider_Timer.read_ms()<(8*(total_knight_rider_cycle_time_ms/8))) {}
deldering95 48:aca02ef5cd01 725
deldering95 48:aca02ef5cd01 726 }
deldering95 51:69087c44e8ac 727 __enable_irq();
deldering95 51:69087c44e8ac 728 delay_between_button_pressed.reset(); // Delaytimer reset en start.
deldering95 51:69087c44e8ac 729 delay_between_button_pressed.start();
deldering95 51:69087c44e8ac 730
deldering95 51:69087c44e8ac 731 reposition_feedback_LED = 0;
deldering95 51:69087c44e8ac 732 new_patient_feedback_LED = 0;
deldering95 51:69087c44e8ac 733 mute_feedback_LED = 0;
deldering95 51:69087c44e8ac 734 usb_serial.printf("Lock State: %d\n",lock_state);
deldering95 48:aca02ef5cd01 735 lock_feedback_LED = control_LED_intensity; // Lock LED initialization.
deldering95 51:69087c44e8ac 736
DEldering 0:c0e44c46c573 737 while (1) {
Ishy 42:673ddef4cfa4 738 piezo_electric_sample_timer.reset(); // Clock gebruiken o.i.d.?
Ishy 42:673ddef4cfa4 739 piezo_electric_sample_timer.start();
ricardo_95 46:a0e6e088a50a 740 connection_test_sensorplate = !testpin_sensorplate && pi_active;
deldering95 43:d09814c177a0 741
Ishy 42:673ddef4cfa4 742 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 743 // usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate);
Ishy 42:673ddef4cfa4 744 }
deldering95 43:d09814c177a0 745 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 746 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 747 }
deldering95 43:d09814c177a0 748
Ishy 42:673ddef4cfa4 749 if (connection_test_sensorplate == 1) {
deldering95 51:69087c44e8ac 750 piezo_electric_array[0] = piezo_electric_adc.readADC_Differential_0_1(); // First PE readout.
deldering95 43:d09814c177a0 751
deldering95 48:aca02ef5cd01 752 for (uint8_t k = 0; k < 4; ++k) {
deldering95 48:aca02ef5cd01 753 piezo_resistive_array[k] = piezo_resistive_adc1.readADC_SingleEnded(k); // First 4 PR readout.
deldering95 48:aca02ef5cd01 754 }
deldering95 43:d09814c177a0 755 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 756 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 757 }
Ishy 42:673ddef4cfa4 758 while(piezo_electric_sample_timer.read_us()<(1*(total_readout_cycle_time_us/5))) {} // Wait untill 20% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
deldering95 43:d09814c177a0 759
deldering95 51:69087c44e8ac 760 piezo_electric_array[1] = piezo_electric_adc.readADC_Differential_0_1(); // Second PE readout.
deldering95 43:d09814c177a0 761
deldering95 48:aca02ef5cd01 762 for (uint8_t k = 0; k < 4; ++k) {
deldering95 48:aca02ef5cd01 763 piezo_resistive_array[k+4] = piezo_resistive_adc2.readADC_SingleEnded(k); // Last 4 PR readout.
deldering95 48:aca02ef5cd01 764 }
deldering95 43:d09814c177a0 765 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 766 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 767 }
deldering95 43:d09814c177a0 768
Ishy 42:673ddef4cfa4 769 while(piezo_electric_sample_timer.read_us()<(2*(total_readout_cycle_time_us/5))) {} // Wait untill 40% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
deldering95 43:d09814c177a0 770
deldering95 51:69087c44e8ac 771 piezo_electric_array[2] = piezo_electric_adc.readADC_Differential_0_1(); // Third PE readout.
deldering95 48:aca02ef5cd01 772 angle_device_sensorplate.getAccelero(accelerometer_sensorplate); // Get accelerometer data.
deldering95 48:aca02ef5cd01 773 angle = accelerometer_sensorplate[2]*100;
deldering95 48:aca02ef5cd01 774 if(angle == 0) {
deldering95 51:69087c44e8ac 775 MPU6050_belt angle_device_sensorplate(PB_9, PB_8);
deldering95 53:54c882995514 776 angle_device_sensorplate.getAccelero(accelerometer_sensorplate);
deldering95 48:aca02ef5cd01 777 angle = accelerometer_sensorplate[2]*100;
deldering95 48:aca02ef5cd01 778 }
deldering95 53:54c882995514 779 if((abs(accelerometer_sensorplate[0])+abs(accelerometer_sensorplate[1])+abs(accelerometer_sensorplate[2]))<= 6) {
deldering95 53:54c882995514 780 for(uint8_t k = 0; k < 3; ++k) {
deldering95 53:54c882995514 781 accelerometer_sensorplate[k]=accelerometer_sensorplate[k]*2;
deldering95 53:54c882995514 782 }
deldering95 53:54c882995514 783 }
deldering95 48:aca02ef5cd01 784 angle_device_sensorplate.getGyro(gyroscope_sensorplate); // Get gyroscope data.
deldering95 43:d09814c177a0 785 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 786 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 787 }
deldering95 48:aca02ef5cd01 788 if (test_belt == 1) {
deldering95 48:aca02ef5cd01 789 angle_device_reference_belt.getGyro(gyroscope_reference_belt); // Get gyroscope data from Belt.
deldering95 48:aca02ef5cd01 790 angle_device_reference_belt.getAccelero(accelerometer_reference_belt); // Get accelerometer data from belt.
deldering95 48:aca02ef5cd01 791 }
deldering95 43:d09814c177a0 792
deldering95 48:aca02ef5cd01 793 if (connection_test_sensorplate == 1) {
deldering95 48:aca02ef5cd01 794 if (test_belt == 0) { // If statement for sending serial information sensorplate data when connection test is active.
deldering95 48:aca02ef5cd01 795 pi_serial.printf("?,%f,%f,%f,%f,%f,%f,0,0,0,0,0,0,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2]);
deldering95 53:54c882995514 796 } else {
deldering95 53:54c882995514 797 // Receiving order sensor information: 3 accelero sensors & 3 gyroscope sensors from sensorplate; 3 accelero sensors & 3 gyroscope sensors from belt. Is splitted in two parts - part 2/2.
deldering95 53:54c882995514 798 pi_serial.printf("?,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n", accelerometer_sensorplate[0], accelerometer_sensorplate[1], accelerometer_sensorplate[2], gyroscope_sensorplate[0], gyroscope_sensorplate[1], gyroscope_sensorplate[2], accelerometer_reference_belt[0], accelerometer_reference_belt[1], accelerometer_reference_belt[2], gyroscope_reference_belt[0], gyroscope_reference_belt[1], gyroscope_reference_belt[2]);
deldering95 48:aca02ef5cd01 799 }
deldering95 48:aca02ef5cd01 800 } // binair print and convert in pi
deldering95 53:54c882995514 801 else {
deldering95 53:54c882995514 802 pi_serial.printf("?,%f,%f,%f,%f,%f,%f,0,0,0,0,0,0,\n",0,0,0,0,0,0);
deldering95 53:54c882995514 803 }
deldering95 43:d09814c177a0 804
Ishy 42:673ddef4cfa4 805 while(piezo_electric_sample_timer.read_us()<(3*(total_readout_cycle_time_us/5))) {} // Wait untill 60% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
deldering95 43:d09814c177a0 806
deldering95 51:69087c44e8ac 807 piezo_electric_array[3] = piezo_electric_adc.readADC_Differential_0_1(); // Fourth PE readout.
Ishy 42:673ddef4cfa4 808 }
deldering95 43:d09814c177a0 809 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 810 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 811 }
Ishy 42:673ddef4cfa4 812 timer_functions();
deldering95 43:d09814c177a0 813 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 814 // usb_serial.printf("Loop time after timer_functions: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 815 }
deldering95 43:d09814c177a0 816
deldering95 43:d09814c177a0 817 colour_select_indicating_LED_wall(LED_colour); // Function to select colour.
Ishy 42:673ddef4cfa4 818 set_userinterface_LED(); // Set LED's of user interface (LED's above buttons).
deldering95 43:d09814c177a0 819
Ishy 42:673ddef4cfa4 820 while(piezo_electric_sample_timer.read_us()<(4*(total_readout_cycle_time_us/5))) {} // Wait untill 80% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
deldering95 43:d09814c177a0 821
deldering95 43:d09814c177a0 822 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 823 // usb_serial.printf("Angle device sensorplate = %d\n",angle_device_sensorplate.testConnection());
Ishy 42:673ddef4cfa4 824 }
deldering95 43:d09814c177a0 825
Ishy 42:673ddef4cfa4 826 if (connection_test_sensorplate == 1) {
deldering95 51:69087c44e8ac 827 piezo_electric_array[4] = piezo_electric_adc.readADC_Differential_0_1(); // Fifth PE readout.
Ishy 42:673ddef4cfa4 828 }
deldering95 43:d09814c177a0 829
Ishy 42:673ddef4cfa4 830 while(piezo_electric_sample_timer.read_us()<(4.25*(total_readout_cycle_time_us/5))) {} // Wait untill 85% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
Ishy 42:673ddef4cfa4 831
deldering95 43:d09814c177a0 832 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 833 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 834 }
deldering95 43:d09814c177a0 835
deldering95 43:d09814c177a0 836 if (test_mode == 0) { // If statements for test purposal (untill * mark).
Ishy 55:b74e7076d7a0 837 // usb_serial.printf("Loop time pre serial: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 838 }
deldering95 43:d09814c177a0 839 serial_read(); // Call function for reading information from PI by serial connection.
deldering95 43:d09814c177a0 840 serial_log(); // Call function for logging information to PI by serial connection.
deldering95 43:d09814c177a0 841
deldering95 43:d09814c177a0 842 if (test_mode == 0) { // If statements for test purposal (untill * mark).
Ishy 42:673ddef4cfa4 843 usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
Ishy 42:673ddef4cfa4 844 }
deldering95 48:aca02ef5cd01 845
deldering95 47:80cfc181f8b3 846 while(piezo_electric_sample_timer.read_us()<(total_readout_cycle_time_us)) {} // Wait untill 100% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
Ishy 42:673ddef4cfa4 847 //if (test_pin == 1) {
Ishy 42:673ddef4cfa4 848 // test_mode = 1;
Ishy 42:673ddef4cfa4 849 // usb_serial.printf("%d\n",test_mode);
Ishy 42:673ddef4cfa4 850 // }
Ishy 42:673ddef4cfa4 851 // if (test_pin == 0) {
Ishy 42:673ddef4cfa4 852 // test_mode = 0;
Ishy 42:673ddef4cfa4 853 // usb_serial.printf("%d\n",test_mode);
Ishy 42:673ddef4cfa4 854 // }
DEldering 0:c0e44c46c573 855 }
ricardo_95 12:7b3a5940f911 856 }