Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Committer:
deldering95
Date:
Tue Jun 05 12:17:57 2018 +0000
Revision:
68:1663f305ac33
Parent:
67:1b300aa30923
Child:
69:98db4df7278f
LED circle with final functions

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
Ishy 60:2f7e82c6f916 4 co-authors: Menno Gravemaker & Ishvara Lalta
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 66:88c910cd4d9e 35 #define ALARMBUF 16
deldering95 66:88c910cd4d9e 36 #define NUM_COLORS 8
deldering95 68:1663f305ac33 37 #define YELLOW_TRANSITION 13
deldering95 68:1663f305ac33 38 #define RED_TRANSITION 16
deldering95 43:d09814c177a0 39 #define NUM_LEDS_PER_COLOR 3
deldering95 48:aca02ef5cd01 40 #define NUMBER_LED_FRONT (3)
ricardo_95 7:dba5091c8b7d 41 #define ONE_COLOR
deldering95 66:88c910cd4d9e 42 #define TAIL_LENGTH 6
deldering95 66:88c910cd4d9e 43 #define COMET_TAIL_END_INTENSITY 90
deldering95 66:88c910cd4d9e 44 #define FADE_STEPS 20
ricardo_95 7:dba5091c8b7d 45
deldering95 66:88c910cd4d9e 46 InterruptIn button_lock(PC_0); // Input on intterupt base decleration.
deldering95 68:1663f305ac33 47 InterruptIn button_reposition(PC_1);
Ishy 41:5380ada94ec6 48 InterruptIn button_mute(PC_2);
deldering95 68:1663f305ac33 49 InterruptIn button_new_patient(PC_3);
ricardo_95 7:dba5091c8b7d 50
deldering95 43:d09814c177a0 51 DigitalIn intensity_code(PA_12);
deldering95 43:d09814c177a0 52 DigitalIn colour_code_1(PA_11);
deldering95 43:d09814c177a0 53 DigitalIn colour_code_0(PB_12);
deldering95 57:fac732476810 54 InterruptIn testpin_sensorplate(PC_6);
deldering95 43:d09814c177a0 55
deldering95 43:d09814c177a0 56 DigitalOut LED_on_dev_board1(LED1); // Decleration of digital outputs.
ricardo_95 36:d10f368d037b 57 DigitalOut LED_on_dev_board2(LED2);
ricardo_95 36:d10f368d037b 58 DigitalOut LED_on_dev_board3(LED3);
ricardo_95 36:d10f368d037b 59 DigitalOut LED_on_dev_board4(LED4);
deldering95 43:d09814c177a0 60 DigitalOut speaker1(PC_12); // relatie aangeven!
deldering95 43:d09814c177a0 61 //neopixel::PixelArray indicator_LEDs(PA_7);
deldering95 66:88c910cd4d9e 62 PixelArray px(ALARMBUF);
deldering95 66:88c910cd4d9e 63 //WS2812 ws(PA_7, ALARMBUF, 3, 9, 9, 6);
deldering95 66:88c910cd4d9e 64 WS2812 ws(PA_7, ALARMBUF, 3, 12, 8, 12);
deldering95 43:d09814c177a0 65
deldering95 51:69087c44e8ac 66 PwmOut lock_feedback_LED(PB_13); //(PB_1); // Declaration of pulse with modulation outputs.
deldering95 51:69087c44e8ac 67 PwmOut mute_feedback_LED(PB_14); //(PB_15);
deldering95 51:69087c44e8ac 68 PwmOut new_patient_feedback_LED(PB_15); //(PB_14);
deldering95 51:69087c44e8ac 69 PwmOut reposition_feedback_LED(PB_1); //(PB_13);
ricardo_95 36:d10f368d037b 70
ricardo_95 37:d8f7b2b5719a 71 Timer button_lock_hold_timer; // Timer for time lock button should be pressed.
ricardo_95 36:d10f368d037b 72 Timer button_calibration_hold_timer; // Timer for calibration function (new patient holding 5 seconds).
deldering95 43:d09814c177a0 73 Timer delay_between_button_pressed; // Timer for time between two buttons (to prevent pressing buttons simultaneously).
ricardo_95 36:d10f368d037b 74 Timer speaker_timer; // Timer for speaker activation.
ricardo_95 36:d10f368d037b 75 Timer piezo_electric_sample_timer; // Timer for equally time-spaced samples.
deldering95 66:88c910cd4d9e 76 Timer comet_timer;
deldering95 66:88c910cd4d9e 77 Timer reposition_button_hold_timer;
deldering95 66:88c910cd4d9e 78 Timer new_patient_button_hold_timer;
deldering95 43:d09814c177a0 79 /*
ricardo_95 36:d10f368d037b 80 The code underneath this commentbox has some fixed parameters for serial/ADC reading:
ricardo_95 36:d10f368d037b 81 -> The address for the angle_device_reference_belt is set to 0x68 in the file MPU6050_belt (rule number: 19);
ricardo_95 36:d10f368d037b 82 -> The adress for the angle_device_sensorplate is set to 0x69 in the file MPU6050.h (rule number: 19);
ricardo_95 36:d10f368d037b 83 -> This is because of using the same I2C line;
ricardo_95 36:d10f368d037b 84 -> For detailed information/questions about this item, please read the technical manual or contact: Ricardo Molenaar | ricardo.molenaar@gmail.com
ricardo_95 36:d10f368d037b 85 */
Ishy 41:5380ada94ec6 86 I2C i2c_sensorplate_adc(PB_9, PB_8); // I2C for sensorplate.
ricardo_95 46:a0e6e088a50a 87 MPU6050_belt angle_device_sensorplate(PB_9, PB_8); // i2c pins // i2c address hardcoded 0x68.
deldering95 68:1663f305ac33 88 MPU6050 angle_device_reference_belt(PB_9, PB_8); // i2c pins // i2c address hardcoded 0x69.
ricardo_95 36:d10f368d037b 89 Adafruit_ADS1115 piezo_resistive_adc1(&i2c_sensorplate_adc, 0x48); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 90 Adafruit_ADS1115 piezo_resistive_adc2(&i2c_sensorplate_adc, 0x49); // i2c pins, i2c address.
ricardo_95 36:d10f368d037b 91 Adafruit_ADS1115 piezo_electric_adc(&i2c_sensorplate_adc, 0x4B); // i2c pins, i2c address.
deldering95 66:88c910cd4d9e 92 RawSerial usb_serial(SERIAL_TX, SERIAL_RX); // tx, rx
deldering95 66:88c910cd4d9e 93 RawSerial pi_serial(PC_10, PC_11); // tx, rx
ricardo_95 36:d10f368d037b 94 Ticker total_readout_cycle; // Polling cycle.
deldering95 43:d09814c177a0 95 // End of commentbox related to the serial configuration/ADC reading components.
ricardo_95 34:1614f4f2b841 96
ricardo_95 12:7b3a5940f911 97 int boot_delay_ms = 500;
Ishy 42:673ddef4cfa4 98 int total_readout_cycle_time_us = 100000; // Cycle time in us.
deldering95 66:88c910cd4d9e 99 int total_comet_cycle_time_ms = 750/16;
ricardo_95 39:cff99a9b7237 100 int i2c__frequency = 400000; // I2C Frequency.
ricardo_95 36:d10f368d037b 101 int baud_rate = 115200; // Baud rate.
deldering95 68:1663f305ac33 102 int uart_input_buffer[120];
deldering95 68:1663f305ac33 103 int buffer_counter = 0;
ricardo_95 36:d10f368d037b 104 short piezo_resistive_array[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle.
deldering95 58:8cfa736d8553 105 short piezo_electric_array[6] = {0,0,0,0,0,0}; // 1 PE sensor 5 times per cycle.
ricardo_95 36:d10f368d037b 106 int angle = 0; // Accelerometer Z-axis.
ricardo_95 37:d8f7b2b5719a 107 float accelerometer_sensorplate[3] = {0.0, 0.0, 0.0}; // Raw accelerometer data.
ricardo_95 36:d10f368d037b 108 float gyroscope_sensorplate[3]; // Raw gyroscope data.
ricardo_95 36:d10f368d037b 109 float accelerometer_reference_belt[3]; // Raw accelerometer data from belt.
ricardo_95 36:d10f368d037b 110 float gyroscope_reference_belt[3]; // Raw gyroscope data from belt.
deldering95 66:88c910cd4d9e 111 int colourbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff,0xffa500,0x555555,0x800080,0x000000,0x8B4513}; // hex codes for the different colours
deldering95 44:dcbde3175a37 112 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 113 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 114 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 115 bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0;
Ishy 55:b74e7076d7a0 116 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 117 int locktime_ms = 2000; // Waittime for lock user interface in ms.
deldering95 48:aca02ef5cd01 118 int calibrationtime_ms = 2000; // Time to press new_patient button for calibration system.
ricardo_95 37:d8f7b2b5719a 119 int calibration_flash = 0; // Variable for flash LED's to indicate calibration.
deldering95 48:aca02ef5cd01 120 int lock_flash = 0;
Ishy 55:b74e7076d7a0 121 int buttondelay_ms = 250; // Button delay in ms. Default: 750
ricardo_95 36:d10f368d037b 122 int delay_lock_interface = 3000*60; // Delay for non using interface locktime.
ricardo_95 36:d10f368d037b 123 int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs.
ricardo_95 38:764847892afc 124 int alarm_voltage = 2400; // Needed voltage for alarm expressed as a digital 15 bit value (= 20% of max battery voltage).
ricardo_95 37:d8f7b2b5719a 125 int LED_red_intensity = 0, LED_blue_intensity = 0, LED_green_intensity = 0; // Variables to set LED intensity.
deldering95 66:88c910cd4d9e 126 int LED_colour_wheel_percentage=0;
deldering95 66:88c910cd4d9e 127 int ring_colour_old,mixer=0;
deldering95 66:88c910cd4d9e 128 int colour_wheel_filler = 5;
deldering95 68:1663f305ac33 129 int patient_present=4;
deldering95 68:1663f305ac33 130 int patient_present_old=4;
deldering95 66:88c910cd4d9e 131 bool colour_wheel_drain_reposition = false;
deldering95 66:88c910cd4d9e 132 bool colour_wheel_drain_new_patient = false;
deldering95 66:88c910cd4d9e 133 bool circle_filling_reposition = false;
deldering95 66:88c910cd4d9e 134 bool circle_filling_new_patient = false;
deldering95 66:88c910cd4d9e 135 int circle_filled_reposition = 0;
deldering95 66:88c910cd4d9e 136 int circle_filled_new_patient = 0;
deldering95 66:88c910cd4d9e 137 int comet=0;
deldering95 66:88c910cd4d9e 138 int tail,tail_step;
ricardo_95 45:7ebe860702b4 139 //short batteryvoltage_current = 0, batteryvoltage_last = 0, powervoltage_current, powervoltage_last; // Variables to manage batteryvoltage. Maybe change current to other?
ricardo_95 45:7ebe860702b4 140 //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 68:1663f305ac33 141 int intensity_day = 30, intensity_night = 15; // Intensity settings for LED's to wall.
ricardo_95 38:764847892afc 142 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 143 int colour_code = 0b00;
deldering95 44:dcbde3175a37 144 bool pi_active = false;
deldering95 68:1663f305ac33 145 bool i2c_error=false;
ricardo_95 33:df21cb8dc5c7 146 /*************************** TEST ********************************/
ricardo_95 36:d10f368d037b 147 // Verify algoritm function: for belt activation, set test_belt 1 (connect pin p20 to 3.3V).
deldering95 43:d09814c177a0 148 Timer test_timer;
Ishy 41:5380ada94ec6 149 DigitalIn test_pin(PA_11, PullDown);
ricardo_95 33:df21cb8dc5c7 150
deldering95 67:1b300aa30923 151 float percentage_tester=0;
deldering95 67:1b300aa30923 152
deldering95 43:d09814c177a0 153 // Variable to set if belt is used to test algorithm:
deldering95 43:d09814c177a0 154 bool test_belt = 0;
ricardo_95 33:df21cb8dc5c7 155
deldering95 43:d09814c177a0 156 // Set test mode on (log functions to pc serial: interrupts, LED intensity and serial messages):
deldering95 68:1663f305ac33 157 bool test_mode = 0;
ricardo_95 34:1614f4f2b841 158
ricardo_95 36:d10f368d037b 159 // Variable for connection test (should be changed):
ricardo_95 46:a0e6e088a50a 160 bool connection_test_sensorplate;
ricardo_95 33:df21cb8dc5c7 161
ricardo_95 21:13e4824bc364 162 /*************************** CODE ********************************/
ricardo_95 7:dba5091c8b7d 163
ricardo_95 36:d10f368d037b 164 void set_intensity_LEDs() // Function to set the intensity for the LED's.
ricardo_95 7:dba5091c8b7d 165 {
ricardo_95 11:73c6def38fbd 166 if (intensity_select == 1) {
ricardo_95 12:7b3a5940f911 167 intensity = intensity_day;
ricardo_95 12:7b3a5940f911 168 } else {
ricardo_95 12:7b3a5940f911 169 intensity = intensity_night;
ricardo_95 7:dba5091c8b7d 170 }
ricardo_95 21:13e4824bc364 171 control_LED_intensity = (intensity/100);
ricardo_95 34:1614f4f2b841 172
ricardo_95 37:d8f7b2b5719a 173 if (test_mode == 1) { // If statement for test purposal LED_intensity values. if def gebruiken voor testmode
Ishy 55:b74e7076d7a0 174 // usb_serial.printf("Intensity LED's shines to wall = %f\n", intensity);
Ishy 55:b74e7076d7a0 175 // usb_serial.printf("Intensity LED's above buttons = %f\n", control_LED_intensity);
ricardo_95 33:df21cb8dc5c7 176 }
ricardo_95 7:dba5091c8b7d 177 }
ricardo_95 7:dba5091c8b7d 178
ricardo_95 36:d10f368d037b 179 void serial_read() // Function for serial read for select LED intensity and colour.
ricardo_95 12:7b3a5940f911 180 {
deldering95 68:1663f305ac33 181 mute_feedback_LED=1;
deldering95 68:1663f305ac33 182 if(pi_serial.readable()) {
deldering95 68:1663f305ac33 183 uart_input_buffer[buffer_counter] = pi_serial.getc();
deldering95 68:1663f305ac33 184 buffer_counter++;
deldering95 68:1663f305ac33 185 }
deldering95 68:1663f305ac33 186 mute_feedback_LED=0;
ricardo_95 11:73c6def38fbd 187 }
ricardo_95 11:73c6def38fbd 188
deldering95 58:8cfa736d8553 189 void serial_log() // Function for serial logging. See link to table with code declarations above in code.
deldering95 58:8cfa736d8553 190 {
Ishy 55:b74e7076d7a0 191
Ishy 55:b74e7076d7a0 192 if (reposition_flag == 1) { // If statement to control logging for reposition button.
ricardo_95 36:d10f368d037b 193 pi_serial.printf(">01\n");
ricardo_95 34:1614f4f2b841 194
deldering95 43:d09814c177a0 195 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 196 usb_serial.printf(">01\n");
ricardo_95 33:df21cb8dc5c7 197 }
ricardo_95 34:1614f4f2b841 198
Ishy 55:b74e7076d7a0 199 reposition_flag = 0;
Ishy 55:b74e7076d7a0 200 }
deldering95 58:8cfa736d8553 201
Ishy 55:b74e7076d7a0 202 if (reposition_lock_flag == 1) { // If statement to control logging for reposition button.
Ishy 55:b74e7076d7a0 203 pi_serial.printf(">10\n");
Ishy 55:b74e7076d7a0 204
Ishy 55:b74e7076d7a0 205 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 206 usb_serial.printf(">10\n");
Ishy 55:b74e7076d7a0 207 }
Ishy 55:b74e7076d7a0 208
Ishy 55:b74e7076d7a0 209 reposition_lock_flag = 0;
ricardo_95 19:3b5999fa7b7e 210 }
ricardo_95 12:7b3a5940f911 211
ricardo_95 36:d10f368d037b 212 if (new_patient_flag == 1) { // If statement to control logging for new patient button.
Ishy 55:b74e7076d7a0 213 pi_serial.printf(">02\n");
Ishy 55:b74e7076d7a0 214
Ishy 55:b74e7076d7a0 215 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 216 usb_serial.printf(">02\n");
Ishy 55:b74e7076d7a0 217 }
Ishy 55:b74e7076d7a0 218
Ishy 55:b74e7076d7a0 219 new_patient_flag = 0;
Ishy 55:b74e7076d7a0 220 }
deldering95 58:8cfa736d8553 221
Ishy 55:b74e7076d7a0 222 if (new_patient_lock_flag == 1) { // If statement to control logging for new patient button.
Ishy 55:b74e7076d7a0 223 pi_serial.printf(">20\n");
Ishy 55:b74e7076d7a0 224
Ishy 55:b74e7076d7a0 225 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 226 usb_serial.printf(">20\n");
Ishy 55:b74e7076d7a0 227 }
Ishy 55:b74e7076d7a0 228
Ishy 55:b74e7076d7a0 229 new_patient_lock_flag = 0;
Ishy 55:b74e7076d7a0 230 }
Ishy 55:b74e7076d7a0 231 // The new calibration button trigger
Ishy 55:b74e7076d7a0 232 if (mute_flag == 1) { // If statement to control logging for new patient button.
ricardo_95 36:d10f368d037b 233 pi_serial.printf(">03\n");
ricardo_95 34:1614f4f2b841 234
deldering95 43:d09814c177a0 235 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 236 usb_serial.printf(">03\n");
ricardo_95 33:df21cb8dc5c7 237 }
ricardo_95 34:1614f4f2b841 238
Ishy 55:b74e7076d7a0 239 mute_flag = 0;
ricardo_95 7:dba5091c8b7d 240 }
Ishy 55:b74e7076d7a0 241 // The new calibration button trigger
Ishy 55:b74e7076d7a0 242 if (mute_lock_flag == 1) { // If statement to control logging for new patient button.
Ishy 55:b74e7076d7a0 243 pi_serial.printf(">30\n");
ricardo_95 34:1614f4f2b841 244
deldering95 43:d09814c177a0 245 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 246 usb_serial.printf(">30\n");
ricardo_95 33:df21cb8dc5c7 247 }
ricardo_95 34:1614f4f2b841 248
Ishy 55:b74e7076d7a0 249 mute_lock_flag = 0;
Ishy 55:b74e7076d7a0 250 }
deldering95 58:8cfa736d8553 251
Ishy 55:b74e7076d7a0 252 if (lock_flag == 1 && !lock_is_logged) {
Ishy 55:b74e7076d7a0 253 if (lock_state == 0) // If statement to control logging for lock button.
Ishy 55:b74e7076d7a0 254 pi_serial.printf(">04\n");
Ishy 55:b74e7076d7a0 255 else if(lock_state == 1)
Ishy 55:b74e7076d7a0 256 pi_serial.printf(">40\n");
Ishy 55:b74e7076d7a0 257
Ishy 55:b74e7076d7a0 258 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 259 if (lock_state == 0) // If statement to control logging for lock button.
Ishy 55:b74e7076d7a0 260 usb_serial.printf(">04\n");
Ishy 55:b74e7076d7a0 261 else if(lock_state == 1)
Ishy 55:b74e7076d7a0 262 usb_serial.printf(">40\n"); // If statement for test purposal.
Ishy 55:b74e7076d7a0 263 }
Ishy 55:b74e7076d7a0 264 lock_is_logged = 1;
ricardo_95 7:dba5091c8b7d 265 }
ricardo_95 12:7b3a5940f911 266
ricardo_95 36:d10f368d037b 267 if (LED_red_logged != LED_red_state) { // If statement to control logging for LED_red.
ricardo_95 12:7b3a5940f911 268 if (LED_red_state == 1) {
Ishy 55:b74e7076d7a0 269 pi_serial.printf("&01\n");
ricardo_95 9:514a44bf510f 270 LED_red_logged = LED_red_state;
ricardo_95 34:1614f4f2b841 271 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 272 usb_serial.printf("&01\n");
ricardo_95 34:1614f4f2b841 273 }
ricardo_95 12:7b3a5940f911 274 }
deldering95 43:d09814c177a0 275
ricardo_95 9:514a44bf510f 276 if (LED_red_state == 0) {
Ishy 55:b74e7076d7a0 277 pi_serial.printf("&10\n");
ricardo_95 12:7b3a5940f911 278 LED_red_logged = LED_red_state;
ricardo_95 34:1614f4f2b841 279 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 280 usb_serial.printf("&10\n");
ricardo_95 34:1614f4f2b841 281 }
ricardo_95 9:514a44bf510f 282 }
ricardo_95 8:bf0f7a6fb1fd 283 }
ricardo_95 12:7b3a5940f911 284
ricardo_95 36:d10f368d037b 285 if (LED_yellow_logged != LED_yellow_state) { // If statement to control logging for LED_yellow.
deldering95 43:d09814c177a0 286 if (LED_yellow_state == 1) {
Ishy 55:b74e7076d7a0 287 pi_serial.printf("&02\n");
ricardo_95 9:514a44bf510f 288 LED_yellow_logged = LED_yellow_state;
ricardo_95 34:1614f4f2b841 289 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 290 usb_serial.printf("&02\n");
ricardo_95 34:1614f4f2b841 291 }
ricardo_95 12:7b3a5940f911 292 }
ricardo_95 9:514a44bf510f 293 if (LED_yellow_state == 0) {
Ishy 55:b74e7076d7a0 294 pi_serial.printf("&20\n");
ricardo_95 12:7b3a5940f911 295 LED_yellow_logged = LED_yellow_state;
ricardo_95 34:1614f4f2b841 296 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 297 usb_serial.printf("&20\n");
ricardo_95 34:1614f4f2b841 298 }
ricardo_95 9:514a44bf510f 299 }
ricardo_95 7:dba5091c8b7d 300 }
ricardo_95 12:7b3a5940f911 301
ricardo_95 36:d10f368d037b 302 if (LED_green_logged != LED_green_state) { // If statement to control logging for LED_green.
ricardo_95 12:7b3a5940f911 303 if (LED_green_state == 1) {
Ishy 55:b74e7076d7a0 304 pi_serial.printf("&03\n");
ricardo_95 9:514a44bf510f 305 LED_green_logged = LED_green_state;
deldering95 43:d09814c177a0 306
ricardo_95 34:1614f4f2b841 307 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 308 usb_serial.printf("&03\n");
ricardo_95 34:1614f4f2b841 309 }
ricardo_95 12:7b3a5940f911 310 }
deldering95 43:d09814c177a0 311
ricardo_95 9:514a44bf510f 312 if (LED_green_state == 0) {
Ishy 55:b74e7076d7a0 313 pi_serial.printf("&30\n");
ricardo_95 12:7b3a5940f911 314 LED_green_logged = LED_green_state;
deldering95 43:d09814c177a0 315
ricardo_95 34:1614f4f2b841 316 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 317 usb_serial.printf("&30\n");
ricardo_95 34:1614f4f2b841 318 }
ricardo_95 9:514a44bf510f 319 }
ricardo_95 7:dba5091c8b7d 320 }
ricardo_95 12:7b3a5940f911 321
deldering95 43:d09814c177a0 322 if (speaker_logged != speaker_state) { // If statement to control logging for speaker.
ricardo_95 12:7b3a5940f911 323 if (speaker_state == 1) {
Ishy 55:b74e7076d7a0 324 pi_serial.printf("&04\n");
ricardo_95 9:514a44bf510f 325 speaker_logged = speaker_state;
deldering95 43:d09814c177a0 326
ricardo_95 36:d10f368d037b 327 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 328 usb_serial.printf("&04\n");
ricardo_95 34:1614f4f2b841 329 }
ricardo_95 12:7b3a5940f911 330 }
deldering95 43:d09814c177a0 331
ricardo_95 9:514a44bf510f 332 if (speaker_state == 0) {
Ishy 55:b74e7076d7a0 333 pi_serial.printf("&40\n");
ricardo_95 12:7b3a5940f911 334 speaker_logged = speaker_state;
deldering95 43:d09814c177a0 335
ricardo_95 36:d10f368d037b 336 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 337 usb_serial.printf("&40\n");
ricardo_95 34:1614f4f2b841 338 }
ricardo_95 9:514a44bf510f 339 }
ricardo_95 8:bf0f7a6fb1fd 340 }
ricardo_95 9:514a44bf510f 341
Ishy 55:b74e7076d7a0 342 // if (power_plug_logged != power_plug_state) { // If statement to control the logging for the state of the power plug.
Ishy 55:b74e7076d7a0 343 // if (power_plug_state == 1) {
Ishy 55:b74e7076d7a0 344 // pi_serial.printf("#08\n");
Ishy 55:b74e7076d7a0 345 //
Ishy 55:b74e7076d7a0 346 // if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 347 // usb_serial.printf("#08\n");
Ishy 55:b74e7076d7a0 348 // }
Ishy 55:b74e7076d7a0 349 // power_plug_logged = power_plug_state;
Ishy 55:b74e7076d7a0 350 // }
Ishy 55:b74e7076d7a0 351 //
Ishy 55:b74e7076d7a0 352 // if (power_plug_state == 0) {
Ishy 55:b74e7076d7a0 353 // pi_serial.printf("#80\n");
Ishy 55:b74e7076d7a0 354 //
Ishy 55:b74e7076d7a0 355 // if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 356 // usb_serial.printf("#80\n");
Ishy 55:b74e7076d7a0 357 // }
Ishy 55:b74e7076d7a0 358 // power_plug_logged = power_plug_state;
Ishy 55:b74e7076d7a0 359 // }
Ishy 55:b74e7076d7a0 360 // }
deldering95 58:8cfa736d8553 361
deldering95 43:d09814c177a0 362 if (connection_test_sensorplate == 1) { // If statement for sending serial information sensorplate data when connection test is active.
deldering95 43:d09814c177a0 363 // Receiving order sensor information: 8 resistive sensors, 5 electric readings. Is splitted in two parts - part 1/2.
deldering95 68:1663f305ac33 364 pi_serial.printf("!,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n", piezo_resistive_array[0], piezo_resistive_array[3], piezo_resistive_array[1], piezo_resistive_array[4], piezo_resistive_array[2], 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], piezo_electric_array[5]); // print all to serial port
deldering95 43:d09814c177a0 365
ricardo_95 34:1614f4f2b841 366 if (test_mode == 1) {
deldering95 68:1663f305ac33 367 usb_serial.printf("%d,%d\n%d,%d\n%d,%d\n", piezo_electric_array[0], piezo_electric_array[3], piezo_electric_array[1], piezo_electric_array[4], piezo_electric_array[2],piezo_electric_array[5]); // print all to serial port
ricardo_95 34:1614f4f2b841 368 }
deldering95 53:54c882995514 369 } else {
deldering95 58:8cfa736d8553 370 pi_serial.printf("!,%d,%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,0);
Ishy 56:97dea631c5f2 371 if (test_mode == 1) {
deldering95 58:8cfa736d8553 372 usb_serial.printf("!,%d,%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,0); // print all to serial port
Ishy 56:97dea631c5f2 373 }
ricardo_95 28:b4bee068780d 374 }
ricardo_95 34:1614f4f2b841 375
DEldering 0:c0e44c46c573 376 }
DEldering 0:c0e44c46c573 377
deldering95 68:1663f305ac33 378 bool i2c_error_detect()
deldering95 68:1663f305ac33 379 {
deldering95 68:1663f305ac33 380 int error_counter=0;
deldering95 68:1663f305ac33 381 for(int i=0; i<4; i++) {
deldering95 68:1663f305ac33 382 if(piezo_resistive_array[i]==193+16*i)error_counter++;
deldering95 68:1663f305ac33 383 if(piezo_resistive_array[i+4]==193+16*i)error_counter++;
deldering95 68:1663f305ac33 384 }
deldering95 68:1663f305ac33 385 if(error_counter==8)return 1;
deldering95 68:1663f305ac33 386 error_counter=0;
deldering95 68:1663f305ac33 387 for(int i=0; i<3; i++) {
deldering95 68:1663f305ac33 388 if (piezo_electric_array[i]==149)error_counter++;
deldering95 68:1663f305ac33 389 if (piezo_electric_array[i+3]==165)error_counter++;
deldering95 68:1663f305ac33 390 }
deldering95 68:1663f305ac33 391 if (error_counter==6)return 1;
deldering95 68:1663f305ac33 392 return 0;
deldering95 68:1663f305ac33 393 }
deldering95 68:1663f305ac33 394
deldering95 68:1663f305ac33 395
deldering95 66:88c910cd4d9e 396 int minimum(int a,int b)
deldering95 66:88c910cd4d9e 397 {
deldering95 66:88c910cd4d9e 398 if(a>b)return b;
deldering95 66:88c910cd4d9e 399 else return a;
deldering95 66:88c910cd4d9e 400 }
deldering95 66:88c910cd4d9e 401
deldering95 66:88c910cd4d9e 402 int colour_fade(int colour_old,int colour_new,int colour_mix)
deldering95 66:88c910cd4d9e 403 {
deldering95 66:88c910cd4d9e 404 int old_r=(colour_old&0xff0000)>>16;
deldering95 66:88c910cd4d9e 405 int old_g=(colour_old&0x00ff00)>> 8;
deldering95 66:88c910cd4d9e 406 int old_b=(colour_old&0x0000ff)>> 0;
deldering95 66:88c910cd4d9e 407 int new_r=(colour_new&0xff0000)>>16;
deldering95 66:88c910cd4d9e 408 int new_g=(colour_new&0x00ff00)>> 8;
deldering95 66:88c910cd4d9e 409 int new_b=(colour_new&0x0000ff)>> 0;
deldering95 66:88c910cd4d9e 410 int mix = (100*colour_mix)/FADE_STEPS;
deldering95 66:88c910cd4d9e 411 int mix_r=((new_r-old_r)*mix/100)+old_r;
deldering95 66:88c910cd4d9e 412 int mix_g=((new_g-old_g)*mix/100)+old_g;
deldering95 66:88c910cd4d9e 413 int mix_b=((new_b-old_b)*mix/100)+old_b;
deldering95 66:88c910cd4d9e 414 return ((mix_r<<16)|(mix_g<<8)|(mix_b));
deldering95 66:88c910cd4d9e 415
deldering95 66:88c910cd4d9e 416 }
deldering95 66:88c910cd4d9e 417
deldering95 66:88c910cd4d9e 418
deldering95 66:88c910cd4d9e 419
deldering95 66:88c910cd4d9e 420 void colour_wheel(int percentage_in)
deldering95 66:88c910cd4d9e 421 {
deldering95 66:88c910cd4d9e 422 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 423 if(percentage_in>100)percentage_in=100;
deldering95 66:88c910cd4d9e 424 int ring_colour = colourbuf[2];
deldering95 66:88c910cd4d9e 425 px.SetAll(colourbuf[6]);
deldering95 68:1663f305ac33 426 int leds_on =((percentage_in*15)/100)+1;
deldering95 68:1663f305ac33 427 float led_partly = ((percentage_in*15)%100);
deldering95 68:1663f305ac33 428 if(leds_on>=YELLOW_TRANSITION&&leds_on<RED_TRANSITION) {
deldering95 66:88c910cd4d9e 429 if(mixer>20)mixer=20;
deldering95 66:88c910cd4d9e 430 ring_colour = colour_fade(colourbuf[2],colourbuf[3],mixer);
deldering95 66:88c910cd4d9e 431 mixer++;
deldering95 66:88c910cd4d9e 432 }
deldering95 68:1663f305ac33 433 if(leds_on>YELLOW_TRANSITION&&leds_on<RED_TRANSITION&&(ring_colour_old==colourbuf[3]||ring_colour_old==colourbuf[4])) {
deldering95 66:88c910cd4d9e 434 ring_colour=colourbuf[3];
deldering95 66:88c910cd4d9e 435 mixer=0;
deldering95 66:88c910cd4d9e 436 }
deldering95 68:1663f305ac33 437 if(leds_on>=RED_TRANSITION) {
deldering95 66:88c910cd4d9e 438 if(mixer>20)mixer=20;
deldering95 66:88c910cd4d9e 439 usb_serial.printf("mixer:%d\n",mixer);
deldering95 66:88c910cd4d9e 440 ring_colour = colour_fade(colourbuf[3],colourbuf[0],mixer);
deldering95 66:88c910cd4d9e 441 mixer++;
deldering95 66:88c910cd4d9e 442 }
deldering95 68:1663f305ac33 443 if(leds_on>RED_TRANSITION&&(ring_colour_old==colourbuf[0]||ring_colour_old==colourbuf[4])) {
deldering95 66:88c910cd4d9e 444 ring_colour = colourbuf[0];
deldering95 66:88c910cd4d9e 445 mixer=0;
deldering95 66:88c910cd4d9e 446 }
deldering95 68:1663f305ac33 447 //usb_serial.printf("on:%d,part:%f\n",leds_on,led_partly);
deldering95 68:1663f305ac33 448 for(int j = 0; j<(leds_on); j++)px.Set(((16-j)%16),ring_colour);
deldering95 66:88c910cd4d9e 449 px.SetAllI(int(intensity*2.55));
deldering95 68:1663f305ac33 450 if((16-leds_on)>0) {
deldering95 68:1663f305ac33 451 px.Set(((16-leds_on)%16),ring_colour);
deldering95 68:1663f305ac33 452 px.SetI(((16-leds_on)%16),int(0.01*led_partly*intensity*2.55));
deldering95 66:88c910cd4d9e 453 }
deldering95 66:88c910cd4d9e 454 usb_serial.printf("percentage in %d,%d\n",percentage_in,leds_on);
deldering95 66:88c910cd4d9e 455
deldering95 68:1663f305ac33 456 if(patient_present==4) {
deldering95 68:1663f305ac33 457 //if(mixer>20)mixer=20;
deldering95 68:1663f305ac33 458 //ring_colour = colour_fade(ring_colour,colourbuf[4],mixer);
deldering95 67:1b300aa30923 459 px.SetAll(colourbuf[4]);
deldering95 67:1b300aa30923 460 px.SetAllI(int(2.55*intensity));
deldering95 68:1663f305ac33 461 //mixer++;
deldering95 68:1663f305ac33 462 //usb_serial.putc(0xee);
deldering95 68:1663f305ac33 463 }
deldering95 68:1663f305ac33 464 /*else{
deldering95 68:1663f305ac33 465 if(mixer>20)mixer=20;
deldering95 68:1663f305ac33 466 ring_colour = colour_fade(colourbuf[4],ring_colour,mixer);
deldering95 68:1663f305ac33 467 px.SetAll(ring_colour);
deldering95 68:1663f305ac33 468 px.SetAllI(int(2.55*intensity));
deldering95 68:1663f305ac33 469 mixer++;
deldering95 68:1663f305ac33 470 usb_serial.putc(0xdd);
deldering95 68:1663f305ac33 471 }*/
deldering95 67:1b300aa30923 472
deldering95 67:1b300aa30923 473
deldering95 66:88c910cd4d9e 474 if(reposition_button_hold_timer.read_ms()) {
deldering95 68:1663f305ac33 475 //usb_serial.printf("filling circle\n");
deldering95 66:88c910cd4d9e 476 circle_filling_reposition = true;
deldering95 66:88c910cd4d9e 477 colour_wheel_filler = reposition_button_hold_timer.read_ms()/250;
deldering95 66:88c910cd4d9e 478 if(colour_wheel_filler>=8) {
deldering95 66:88c910cd4d9e 479 colour_wheel_filler=8;
deldering95 66:88c910cd4d9e 480 circle_filled_reposition = 15;
deldering95 66:88c910cd4d9e 481 circle_filling_reposition = false;
deldering95 66:88c910cd4d9e 482 colour_wheel_drain_reposition = false;
deldering95 66:88c910cd4d9e 483 reposition_flag = 1;
deldering95 66:88c910cd4d9e 484 }
deldering95 66:88c910cd4d9e 485 px.SetAll(colourbuf[6]);
deldering95 66:88c910cd4d9e 486 for(int k =0; k<=colour_wheel_filler; k++) {
deldering95 66:88c910cd4d9e 487 px.Set(k,colourbuf[5]);
deldering95 66:88c910cd4d9e 488 px.Set(16-k,colourbuf[5]);
deldering95 66:88c910cd4d9e 489 }
deldering95 66:88c910cd4d9e 490 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 491 }
deldering95 66:88c910cd4d9e 492
deldering95 66:88c910cd4d9e 493 if(new_patient_button_hold_timer.read_ms()) {
deldering95 68:1663f305ac33 494 //usb_serial.printf("filling circle\n");
deldering95 66:88c910cd4d9e 495 circle_filling_new_patient = true;
deldering95 66:88c910cd4d9e 496 colour_wheel_filler = new_patient_button_hold_timer.read_ms()/250;
deldering95 66:88c910cd4d9e 497 if(colour_wheel_filler>=8) {
deldering95 66:88c910cd4d9e 498 colour_wheel_filler=8;
deldering95 66:88c910cd4d9e 499 circle_filled_new_patient = 10;
deldering95 66:88c910cd4d9e 500 circle_filling_new_patient = false;
deldering95 66:88c910cd4d9e 501 colour_wheel_drain_new_patient = false;
deldering95 66:88c910cd4d9e 502 new_patient_flag = 1;
deldering95 66:88c910cd4d9e 503 }
deldering95 66:88c910cd4d9e 504 px.SetAll(colourbuf[6]);
deldering95 66:88c910cd4d9e 505 for(int k =0; k<=colour_wheel_filler; k++) {
deldering95 66:88c910cd4d9e 506 px.Set((k+8)%16,colourbuf[5]);
deldering95 66:88c910cd4d9e 507 px.Set((16-k+8)%16,colourbuf[5]);
deldering95 66:88c910cd4d9e 508 }
deldering95 66:88c910cd4d9e 509 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 510 }
deldering95 66:88c910cd4d9e 511
deldering95 66:88c910cd4d9e 512 if(!reposition_button_hold_timer.read_ms()&&circle_filling_reposition&&!colour_wheel_drain_reposition) {
deldering95 68:1663f305ac33 513 //usb_serial.printf("Short hold\n");
deldering95 66:88c910cd4d9e 514 px.SetAll(colourbuf[6]);
deldering95 66:88c910cd4d9e 515 for(int k =0; k<=colour_wheel_filler; k++) {
deldering95 66:88c910cd4d9e 516 px.Set(k,colourbuf[5]);
deldering95 66:88c910cd4d9e 517 px.Set(16-k,colourbuf[5]);
deldering95 66:88c910cd4d9e 518 }
deldering95 66:88c910cd4d9e 519 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 520 colour_wheel_filler++;
deldering95 66:88c910cd4d9e 521 if(colour_wheel_filler>=5) {
deldering95 66:88c910cd4d9e 522 colour_wheel_drain_reposition=true;
deldering95 66:88c910cd4d9e 523 circle_filling_reposition=false;
deldering95 66:88c910cd4d9e 524 }
deldering95 66:88c910cd4d9e 525 }
deldering95 66:88c910cd4d9e 526
deldering95 66:88c910cd4d9e 527 if(!new_patient_button_hold_timer.read_ms()&&circle_filling_new_patient&&!colour_wheel_drain_new_patient) {
deldering95 68:1663f305ac33 528 //usb_serial.printf("Short hold\n");
deldering95 66:88c910cd4d9e 529 px.SetAll(colourbuf[6]);
deldering95 66:88c910cd4d9e 530 for(int k =0; k<=colour_wheel_filler; k++) {
deldering95 66:88c910cd4d9e 531 px.Set((k+8)%16,colourbuf[5]);
deldering95 66:88c910cd4d9e 532 px.Set((16-k+8)%16,colourbuf[5]);
deldering95 66:88c910cd4d9e 533 }
deldering95 66:88c910cd4d9e 534 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 535 colour_wheel_filler++;
deldering95 66:88c910cd4d9e 536 if(colour_wheel_filler>=5) {
deldering95 66:88c910cd4d9e 537 colour_wheel_drain_new_patient=true;
deldering95 66:88c910cd4d9e 538 circle_filling_new_patient=false;
deldering95 66:88c910cd4d9e 539 }
deldering95 66:88c910cd4d9e 540 }
deldering95 66:88c910cd4d9e 541
deldering95 66:88c910cd4d9e 542 if(colour_wheel_drain_reposition) {
deldering95 68:1663f305ac33 543 //usb_serial.printf("drain\n");
deldering95 66:88c910cd4d9e 544 px.SetAll(colourbuf[6]);
deldering95 66:88c910cd4d9e 545 for(int k =0; k<=colour_wheel_filler; k++) {
deldering95 66:88c910cd4d9e 546 px.Set(k,colourbuf[5]);
deldering95 66:88c910cd4d9e 547 px.Set(16-k,colourbuf[5]);
deldering95 66:88c910cd4d9e 548 }
deldering95 66:88c910cd4d9e 549 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 550 colour_wheel_filler--;
deldering95 66:88c910cd4d9e 551 if(!colour_wheel_filler)colour_wheel_drain_reposition=false;
deldering95 66:88c910cd4d9e 552 }
deldering95 66:88c910cd4d9e 553
deldering95 66:88c910cd4d9e 554 if(colour_wheel_drain_new_patient) {
deldering95 68:1663f305ac33 555 //usb_serial.printf("drain\n");
deldering95 66:88c910cd4d9e 556 px.SetAll(colourbuf[6]);
deldering95 66:88c910cd4d9e 557 for(int k =0; k<=colour_wheel_filler; k++) {
deldering95 66:88c910cd4d9e 558 px.Set((k+8)%16,colourbuf[5]);
deldering95 66:88c910cd4d9e 559 px.Set((16-k+8)%16,colourbuf[5]);
deldering95 66:88c910cd4d9e 560 }
deldering95 66:88c910cd4d9e 561 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 562 colour_wheel_filler--;
deldering95 66:88c910cd4d9e 563 if(!colour_wheel_filler)colour_wheel_drain_new_patient=false;
deldering95 66:88c910cd4d9e 564 }
deldering95 66:88c910cd4d9e 565
deldering95 66:88c910cd4d9e 566 if(circle_filled_reposition) {
deldering95 68:1663f305ac33 567 //usb_serial.printf("circle_filled_repo\n");
deldering95 66:88c910cd4d9e 568 px.SetAll(colourbuf[6]);
deldering95 66:88c910cd4d9e 569 for(int k =0; k<=circle_filled_reposition; k++) {
deldering95 66:88c910cd4d9e 570 px.Set((16-k)%16,colourbuf[5]);
deldering95 66:88c910cd4d9e 571 }
deldering95 66:88c910cd4d9e 572 px.Set((16-circle_filled_reposition)%16,colourbuf[4]);
deldering95 66:88c910cd4d9e 573 px.SetAllI(int(intensity*2.55));
deldering95 66:88c910cd4d9e 574 circle_filled_reposition--;
deldering95 67:1b300aa30923 575 percentage_tester=0;
deldering95 66:88c910cd4d9e 576 }
deldering95 66:88c910cd4d9e 577
deldering95 66:88c910cd4d9e 578 if(circle_filled_new_patient) {
deldering95 68:1663f305ac33 579 //usb_serial.printf("circle_filled_new_patient\n");
deldering95 66:88c910cd4d9e 580 px.SetAll(colourbuf[5]);
deldering95 66:88c910cd4d9e 581 px.SetAllI(int(intensity*2.55*(7-(circle_filled_new_patient%5))/7));
deldering95 66:88c910cd4d9e 582 circle_filled_new_patient--;
deldering95 66:88c910cd4d9e 583 }
deldering95 68:1663f305ac33 584 if(!connection_test_sensorplate||i2c_error) {
deldering95 68:1663f305ac33 585 px.Set(5,colourbuf[0]);
deldering95 67:1b300aa30923 586 px.Set(6,colourbuf[0]);
deldering95 68:1663f305ac33 587 px.Set(7,colourbuf[0]);
deldering95 68:1663f305ac33 588 px.SetI(5,int(intensity*0.5));
deldering95 67:1b300aa30923 589 px.SetI(6,int(intensity*2.55));
deldering95 68:1663f305ac33 590 px.SetI(7,int(intensity*0.5));
deldering95 67:1b300aa30923 591 }
deldering95 66:88c910cd4d9e 592 ws.write_offsets(px.getBuf(),0,0,0);
deldering95 66:88c910cd4d9e 593 ring_colour_old=ring_colour;
deldering95 66:88c910cd4d9e 594 }
deldering95 66:88c910cd4d9e 595
deldering95 43:d09814c177a0 596 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 597 {
ricardo_95 36:d10f368d037b 598 set_intensity_LEDs(); // Call function set_intensity_LEDs to set the intensity for LED's to wall and above buttons.
deldering95 43:d09814c177a0 599 ws.setII(2.55*intensity);
deldering95 43:d09814c177a0 600 switch(nLED_colour) {
deldering95 43:d09814c177a0 601 case 'r' :
deldering95 43:d09814c177a0 602 px.SetAll(colourbuf[0]);
Ishy 55:b74e7076d7a0 603 LED_red_state = 1;
Ishy 55:b74e7076d7a0 604 LED_yellow_state = 0;
deldering95 43:d09814c177a0 605 break;
deldering95 43:d09814c177a0 606 case 'g' :
deldering95 43:d09814c177a0 607 px.SetAll(colourbuf[1]);
Ishy 55:b74e7076d7a0 608 LED_green_state = 1;
Ishy 55:b74e7076d7a0 609 LED_red_state = 0;
deldering95 43:d09814c177a0 610 break;
deldering95 43:d09814c177a0 611 case 'b' :
deldering95 43:d09814c177a0 612 px.SetAll(colourbuf[2]);
deldering95 43:d09814c177a0 613 break;
deldering95 43:d09814c177a0 614 case 'y' :
deldering95 43:d09814c177a0 615 px.SetAll(colourbuf[3]);
Ishy 55:b74e7076d7a0 616 LED_yellow_state = 1;
Ishy 55:b74e7076d7a0 617 LED_green_state = 0;
deldering95 43:d09814c177a0 618 break;
deldering95 43:d09814c177a0 619 default :
deldering95 43:d09814c177a0 620 px.SetAll(colourbuf[4]);
deldering95 43:d09814c177a0 621 }
deldering95 43:d09814c177a0 622
deldering95 43:d09814c177a0 623 if (calibration_flash >= 1) {
deldering95 43:d09814c177a0 624 if ((calibration_flash % 2) == 0) {
deldering95 48:aca02ef5cd01 625 px.SetAll(colourbuf[2]);
deldering95 43:d09814c177a0 626 } else {
deldering95 43:d09814c177a0 627 ws.setII(0);
deldering95 43:d09814c177a0 628 }
deldering95 43:d09814c177a0 629 calibration_flash--;
deldering95 43:d09814c177a0 630 }
deldering95 51:69087c44e8ac 631 //for (int z=WS2812_BUF; z >= 0 ; z--) {
deldering95 53:54c882995514 632 ws.write_offsets(px.getBuf(),0,0,0);
deldering95 51:69087c44e8ac 633 //}
ricardo_95 21:13e4824bc364 634 }
ricardo_95 21:13e4824bc364 635
ricardo_95 36:d10f368d037b 636 void trigger_lock() // If rising edge lock button is detected start locktimer.
ricardo_95 21:13e4824bc364 637 {
ricardo_95 34:1614f4f2b841 638 if (test_mode == 1) {
ricardo_95 36:d10f368d037b 639 usb_serial.printf("Lock triggered.\n");
ricardo_95 34:1614f4f2b841 640 }
Ishy 55:b74e7076d7a0 641 if (lock_state == 0) pi_serial.printf(">44\n");
Ishy 55:b74e7076d7a0 642 else if (lock_state == 1) pi_serial.printf(">00\n");
ricardo_95 36:d10f368d037b 643 button_lock_hold_timer.reset();
ricardo_95 36:d10f368d037b 644 button_lock_hold_timer.start();
ricardo_95 36:d10f368d037b 645 delay_between_button_pressed.reset();
ricardo_95 36:d10f368d037b 646 delay_between_button_pressed.start();
ricardo_95 21:13e4824bc364 647 }
ricardo_95 21:13e4824bc364 648
ricardo_95 36:d10f368d037b 649 void end_timer_lock_button() // End timer lock.
ricardo_95 21:13e4824bc364 650 {
deldering95 43:d09814c177a0 651 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 652 usb_serial.printf("Lock released.\n");
ricardo_95 36:d10f368d037b 653 }
ricardo_95 36:d10f368d037b 654 lock_flag = 0; // Set lock_flag off.
Ishy 55:b74e7076d7a0 655 lock_is_logged = 0;
ricardo_95 36:d10f368d037b 656 button_lock_hold_timer.stop(); // Stop and reset holdtimer
ricardo_95 36:d10f368d037b 657 button_lock_hold_timer.reset();
ricardo_95 36:d10f368d037b 658 }
ricardo_95 36:d10f368d037b 659
ricardo_95 36:d10f368d037b 660 void reposition_button_triggered()
ricardo_95 36:d10f368d037b 661 {
ricardo_95 36:d10f368d037b 662 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 663 lock_flash = 10;
deldering95 58:8cfa736d8553 664 }
deldering95 58:8cfa736d8553 665
Ishy 55:b74e7076d7a0 666 delay_between_button_pressed.reset();
Ishy 55:b74e7076d7a0 667 delay_between_button_pressed.start();
Ishy 55:b74e7076d7a0 668 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 669 usb_serial.printf("Reposition triggered.\n");
Ishy 55:b74e7076d7a0 670 LED_on_dev_board1 = !LED_on_dev_board1;
Ishy 55:b74e7076d7a0 671 }
deldering95 58:8cfa736d8553 672
deldering95 66:88c910cd4d9e 673 if (lock_state == 1||button_new_patient.read()==0) reposition_lock_flag = 1;
deldering95 58:8cfa736d8553 674 else {
deldering95 66:88c910cd4d9e 675 reposition_button_hold_timer.reset();
deldering95 66:88c910cd4d9e 676 reposition_button_hold_timer.start();
deldering95 66:88c910cd4d9e 677
ricardo_95 36:d10f368d037b 678 reposition_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 679 pi_serial.printf("&05\n");
deldering95 58:8cfa736d8553 680 if(test_mode == 1) usb_serial.printf("&05\n");
ricardo_95 21:13e4824bc364 681 }
ricardo_95 21:13e4824bc364 682 }
ricardo_95 21:13e4824bc364 683
deldering95 43:d09814c177a0 684 void rise_reposition() // Interrupt for rising edge reposition function (deactivation; active low).
ricardo_95 21:13e4824bc364 685 {
deldering95 43:d09814c177a0 686 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 687 usb_serial.printf("Reposition released.\n");
ricardo_95 34:1614f4f2b841 688 }
deldering95 58:8cfa736d8553 689
Ishy 55:b74e7076d7a0 690 if (reposition_feedback_LED != 0) {
Ishy 55:b74e7076d7a0 691 pi_serial.printf("&50\n");
deldering95 58:8cfa736d8553 692
Ishy 55:b74e7076d7a0 693 if(test_mode == 1) usb_serial.printf("&50\n");
Ishy 55:b74e7076d7a0 694 }
deldering95 66:88c910cd4d9e 695 reposition_button_hold_timer.stop();
deldering95 66:88c910cd4d9e 696 reposition_button_hold_timer.reset();
ricardo_95 36:d10f368d037b 697 reposition_feedback_LED = 0;
ricardo_95 21:13e4824bc364 698 }
Ishy 55:b74e7076d7a0 699 //TODO rename to calibration
ricardo_95 36:d10f368d037b 700 void mute_button_triggered()
ricardo_95 21:13e4824bc364 701 {
ricardo_95 21:13e4824bc364 702
ricardo_95 36:d10f368d037b 703 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 704 lock_flash = 10;
Ishy 55:b74e7076d7a0 705 }
deldering95 58:8cfa736d8553 706
Ishy 55:b74e7076d7a0 707 delay_between_button_pressed.reset();
Ishy 55:b74e7076d7a0 708 delay_between_button_pressed.start();
Ishy 55:b74e7076d7a0 709 button_calibration_hold_timer.reset(); // inline ?
Ishy 55:b74e7076d7a0 710 button_calibration_hold_timer.start();
deldering95 58:8cfa736d8553 711
Ishy 55:b74e7076d7a0 712 if (lock_state == 1) {
Ishy 55:b74e7076d7a0 713 mute_lock_flag = 1;
Ishy 55:b74e7076d7a0 714 }
deldering95 58:8cfa736d8553 715
Ishy 55:b74e7076d7a0 716 else {
deldering95 58:8cfa736d8553 717 mute_feedback_LED = control_LED_intensity;
deldering95 58:8cfa736d8553 718 pi_serial.printf("&07\n");
deldering95 58:8cfa736d8553 719 if (test_mode == 1) usb_serial.printf("&07\n");
deldering95 58:8cfa736d8553 720 mute_flag = 1;
Ishy 55:b74e7076d7a0 721 }
deldering95 58:8cfa736d8553 722
Ishy 55:b74e7076d7a0 723 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 724 usb_serial.printf("Calibration triggered\n");
Ishy 55:b74e7076d7a0 725 LED_on_dev_board1 = !LED_on_dev_board1;
Ishy 55:b74e7076d7a0 726 }
deldering95 43:d09814c177a0 727
deldering95 58:8cfa736d8553 728
ricardo_95 21:13e4824bc364 729 }
ricardo_95 21:13e4824bc364 730
deldering95 51:69087c44e8ac 731 void rise_mute() // Interrupt for rising edge reposition function (deactivation; active low).
ricardo_95 21:13e4824bc364 732 {
deldering95 43:d09814c177a0 733 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 734 usb_serial.printf("Calibration released.\n");
ricardo_95 34:1614f4f2b841 735 }
Ishy 55:b74e7076d7a0 736 if(lock_state == 0 && mute_feedback_LED != 0) {
Ishy 55:b74e7076d7a0 737 pi_serial.printf("&70\n");
Ishy 55:b74e7076d7a0 738 if (test_mode == 1) usb_serial.printf("&70\n");
Ishy 55:b74e7076d7a0 739 }
deldering95 58:8cfa736d8553 740
deldering95 51:69087c44e8ac 741 mute_feedback_LED = 0;
ricardo_95 21:13e4824bc364 742
deldering95 43:d09814c177a0 743 button_calibration_hold_timer.stop(); // Timer reset for calibration function of new patient button.
ricardo_95 36:d10f368d037b 744 button_calibration_hold_timer.reset();
ricardo_95 21:13e4824bc364 745
ricardo_95 36:d10f368d037b 746 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 747 } else {
ricardo_95 21:13e4824bc364 748 if (calibration_flag == 0) {
ricardo_95 21:13e4824bc364 749
deldering95 43:d09814c177a0 750 if (LED_on_dev_board1 == 0) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 751 LED_on_dev_board1 = 1;
ricardo_95 21:13e4824bc364 752 } else {
ricardo_95 36:d10f368d037b 753 LED_on_dev_board1 = 0;
ricardo_95 21:13e4824bc364 754 }
ricardo_95 21:13e4824bc364 755
ricardo_95 21:13e4824bc364 756 } else {
ricardo_95 21:13e4824bc364 757 calibration_flag = 0;
ricardo_95 21:13e4824bc364 758 }
ricardo_95 21:13e4824bc364 759 }
deldering95 51:69087c44e8ac 760
deldering95 51:69087c44e8ac 761 }
deldering95 51:69087c44e8ac 762
deldering95 51:69087c44e8ac 763 void trigger_new_patient() // Function to trigger hold timer for new patient and calibration function.
deldering95 51:69087c44e8ac 764 {
deldering95 51:69087c44e8ac 765
deldering95 51:69087c44e8ac 766 if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) {
deldering95 51:69087c44e8ac 767 lock_flash = 10;
Ishy 55:b74e7076d7a0 768 }
deldering95 58:8cfa736d8553 769
Ishy 55:b74e7076d7a0 770 delay_between_button_pressed.reset();
Ishy 55:b74e7076d7a0 771 delay_between_button_pressed.start();
deldering95 58:8cfa736d8553 772
deldering95 66:88c910cd4d9e 773 if (lock_state == 1||button_reposition.read()==0) new_patient_lock_flag = 1;
Ishy 55:b74e7076d7a0 774 else {
deldering95 66:88c910cd4d9e 775 new_patient_button_hold_timer.reset();
deldering95 66:88c910cd4d9e 776 new_patient_button_hold_timer.start();
deldering95 66:88c910cd4d9e 777
Ishy 55:b74e7076d7a0 778 new_patient_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 779 pi_serial.printf("&06\n");
deldering95 66:88c910cd4d9e 780 //new_patient_flag = 1;
deldering95 58:8cfa736d8553 781
deldering95 58:8cfa736d8553 782 if(test_mode == 1) {
Ishy 55:b74e7076d7a0 783 usb_serial.printf("&06\n");
deldering95 51:69087c44e8ac 784 }
deldering95 51:69087c44e8ac 785 }
deldering95 58:8cfa736d8553 786
Ishy 55:b74e7076d7a0 787 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 788 usb_serial.printf("New patient triggered.\n");
Ishy 55:b74e7076d7a0 789 }
deldering95 58:8cfa736d8553 790
deldering95 51:69087c44e8ac 791 }
deldering95 51:69087c44e8ac 792
deldering95 51:69087c44e8ac 793 void activate_new_patient_function() // Timer calibration function.
deldering95 51:69087c44e8ac 794 {
deldering95 51:69087c44e8ac 795 if (test_mode == 1) { // If statement for test purposal.
deldering95 51:69087c44e8ac 796 usb_serial.printf("New patient released.\n");
deldering95 58:8cfa736d8553 797
Ishy 55:b74e7076d7a0 798 }
deldering95 66:88c910cd4d9e 799 new_patient_button_hold_timer.stop();
deldering95 66:88c910cd4d9e 800 new_patient_button_hold_timer.reset();
deldering95 66:88c910cd4d9e 801
deldering95 58:8cfa736d8553 802
Ishy 55:b74e7076d7a0 803 if (new_patient_feedback_LED != 0) {
Ishy 55:b74e7076d7a0 804 pi_serial.printf("&60\n");
Ishy 55:b74e7076d7a0 805 if(test_mode) usb_serial.printf("&60\n");
deldering95 51:69087c44e8ac 806 }
deldering95 51:69087c44e8ac 807 new_patient_feedback_LED = 0;
ricardo_95 21:13e4824bc364 808 }
ricardo_95 21:13e4824bc364 809
deldering95 43:d09814c177a0 810 void timer_functions() // Function which contains statements using timers.
ricardo_95 21:13e4824bc364 811 {
deldering95 48:aca02ef5cd01 812 if (button_lock == 1) {
deldering95 44:dcbde3175a37 813 button_lock_hold_timer.stop();
deldering95 44:dcbde3175a37 814 button_lock_hold_timer.reset();
deldering95 44:dcbde3175a37 815 }
ricardo_95 36:d10f368d037b 816 if ((button_lock_hold_timer.read_ms() > locktime_ms) && lock_flag == 0 && button_lock == 0) { // If statement for lock function.
ricardo_95 21:13e4824bc364 817 lock_flag = 1;
ricardo_95 36:d10f368d037b 818 LED_on_dev_board2 = !LED_on_dev_board2;
ricardo_95 21:13e4824bc364 819 lock_state = !lock_state;
deldering95 43:d09814c177a0 820
deldering95 43:d09814c177a0 821 if (lock_state == 0) { // If statement to control lock feedback LED above button.
ricardo_95 36:d10f368d037b 822 lock_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 823 pi_serial.printf("&08\n");
deldering95 58:8cfa736d8553 824 if(test_mode == 1) usb_serial.printf("&08\n");
ricardo_95 21:13e4824bc364 825 } else {
ricardo_95 36:d10f368d037b 826 lock_feedback_LED = 0;
Ishy 55:b74e7076d7a0 827 pi_serial.printf("&80\n");
Ishy 55:b74e7076d7a0 828 if(test_mode == 1) usb_serial.printf("&80\n");
ricardo_95 21:13e4824bc364 829 }
ricardo_95 21:13e4824bc364 830 }
ricardo_95 21:13e4824bc364 831
deldering95 51:69087c44e8ac 832 if (button_mute == 1) {
deldering95 44:dcbde3175a37 833 button_calibration_hold_timer.stop();
deldering95 44:dcbde3175a37 834 button_calibration_hold_timer.reset();
deldering95 44:dcbde3175a37 835 }
deldering95 44:dcbde3175a37 836
deldering95 66:88c910cd4d9e 837 if (button_reposition == 1) {
deldering95 66:88c910cd4d9e 838 reposition_button_hold_timer.stop();
deldering95 66:88c910cd4d9e 839 reposition_button_hold_timer.reset();
deldering95 66:88c910cd4d9e 840 }
deldering95 66:88c910cd4d9e 841
deldering95 66:88c910cd4d9e 842
deldering95 51:69087c44e8ac 843 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 844 calibration_flag = 1;
ricardo_95 21:13e4824bc364 845 calibration_flash = 11;
deldering95 58:8cfa736d8553 846
Ishy 55:b74e7076d7a0 847 if (test_mode == 1) { // If statement for test purposal.
ricardo_95 36:d10f368d037b 848 usb_serial.printf("Calibrate triggered.\n");
ricardo_95 34:1614f4f2b841 849 }
Ishy 55:b74e7076d7a0 850 pi_serial.printf(">33\n");
deldering95 58:8cfa736d8553 851
ricardo_95 21:13e4824bc364 852 }
deldering95 66:88c910cd4d9e 853 /*
deldering95 66:88c910cd4d9e 854 if (delay_between_button_pressed.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active.
deldering95 66:88c910cd4d9e 855 lock_state = 1;
deldering95 66:88c910cd4d9e 856 LED_on_dev_board2 = 1;
deldering95 66:88c910cd4d9e 857 lock_feedback_LED = 0;
deldering95 66:88c910cd4d9e 858 if (!auto_lock_led_logged) {
deldering95 66:88c910cd4d9e 859 pi_serial.printf("&80\n");
deldering95 66:88c910cd4d9e 860 if(test_mode == 1) usb_serial.printf("&80\n");
deldering95 66:88c910cd4d9e 861 auto_lock_led_logged = 1;
deldering95 66:88c910cd4d9e 862 }
deldering95 66:88c910cd4d9e 863 } else {
deldering95 66:88c910cd4d9e 864 auto_lock_led_logged = 0;
deldering95 66:88c910cd4d9e 865 }*/
ricardo_95 21:13e4824bc364 866 }
ricardo_95 21:13e4824bc364 867
ricardo_95 36:d10f368d037b 868 void set_userinterface_LED() // Control functions for LED above buttons (added because of failures).
ricardo_95 21:13e4824bc364 869 {
ricardo_95 21:13e4824bc364 870 if (lock_state == 1) {
ricardo_95 21:13e4824bc364 871 } else {
ricardo_95 36:d10f368d037b 872 if (button_reposition == 0) {
ricardo_95 36:d10f368d037b 873 reposition_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 874 } else {
ricardo_95 36:d10f368d037b 875 reposition_feedback_LED = 0;
ricardo_95 21:13e4824bc364 876 }
ricardo_95 21:13e4824bc364 877
ricardo_95 36:d10f368d037b 878 if (button_new_patient == 0) {
ricardo_95 36:d10f368d037b 879 new_patient_feedback_LED = control_LED_intensity;
ricardo_95 21:13e4824bc364 880 } else {
ricardo_95 36:d10f368d037b 881 new_patient_feedback_LED = 0;
ricardo_95 21:13e4824bc364 882 }
ricardo_95 21:13e4824bc364 883 }
deldering95 48:aca02ef5cd01 884 if (lock_flash >= 1 && lock_state == 1) {
deldering95 48:aca02ef5cd01 885 if ((lock_flash % 2) == 0) {
deldering95 48:aca02ef5cd01 886 lock_feedback_LED = control_LED_intensity;
Ishy 55:b74e7076d7a0 887 pi_serial.printf("&08\n");
Ishy 55:b74e7076d7a0 888 if(test_mode == 1) usb_serial.printf("&08\n");
deldering95 48:aca02ef5cd01 889 } else {
deldering95 48:aca02ef5cd01 890 lock_feedback_LED = 0;
Ishy 55:b74e7076d7a0 891 pi_serial.printf("&80\n");
Ishy 55:b74e7076d7a0 892 if(test_mode == 1) usb_serial.printf("&80\n");
ricardo_95 21:13e4824bc364 893 }
deldering95 48:aca02ef5cd01 894 lock_flash--;
deldering95 48:aca02ef5cd01 895 } else {
deldering95 48:aca02ef5cd01 896 lock_flash = 0;
ricardo_95 21:13e4824bc364 897 }
ricardo_95 21:13e4824bc364 898 }
ricardo_95 21:13e4824bc364 899
deldering95 57:fac732476810 900 void sensorplate_detached()
deldering95 57:fac732476810 901 {
deldering95 58:8cfa736d8553 902 if(piezo_electric_sample_timer.read_us() > total_readout_cycle_time_us) {
deldering95 58:8cfa736d8553 903 NVIC_SystemReset();
deldering95 58:8cfa736d8553 904 }
deldering95 57:fac732476810 905 }
deldering95 57:fac732476810 906
deldering95 57:fac732476810 907
deldering95 43:d09814c177a0 908 int main() // Main function. inline function "Momo Init" bijvoorbeeld
deldering95 43:d09814c177a0 909 {
deldering95 66:88c910cd4d9e 910 pi_serial.attach(serial_read,Serial::RxIrq);
deldering95 66:88c910cd4d9e 911 ws.useII(WS2812::PER_PIXEL);
deldering95 66:88c910cd4d9e 912 ws.setII(255);
deldering95 66:88c910cd4d9e 913 px.SetAll(colourbuf[4]); // use global intensity scaling
deldering95 51:69087c44e8ac 914 set_intensity_LEDs(); // Initialize intensity for user interface LED's and LED's shines to wall.
deldering95 66:88c910cd4d9e 915 ws.write_offsets(px.getBuf(),0,0,0);
deldering95 53:54c882995514 916
deldering95 48:aca02ef5cd01 917 speaker1 = 1;
ricardo_95 36:d10f368d037b 918 wait_ms(boot_delay_ms); // Wait to boot sensorplate first.
deldering95 48:aca02ef5cd01 919 speaker1 = 0;
ricardo_95 39:cff99a9b7237 920 i2c_sensorplate_adc.frequency(i2c__frequency); // Set frequency for i2c connection to sensorplate (variable is declared in config part).
ricardo_95 36:d10f368d037b 921 usb_serial.baud(baud_rate); // Set serial USB connection baud rate (variable is declared in config part).
ricardo_95 36:d10f368d037b 922 pi_serial.baud(baud_rate); // Same as line 697, but now for serial PI connection.
ricardo_95 36:d10f368d037b 923 piezo_resistive_adc1.setGain(GAIN_TWOTHIRDS); // Set ranges of ADC to +/-6.144V (end is marked with #):
deldering95 43:d09814c177a0 924 piezo_resistive_adc2.setGain(GAIN_TWOTHIRDS);
deldering95 51:69087c44e8ac 925 piezo_electric_adc.setGain(GAIN_TWO);
deldering95 43:d09814c177a0 926 pi_serial.format(8, SerialBase::None, 1); // Set serial communication line with PI.
ricardo_95 7:dba5091c8b7d 927
deldering95 57:fac732476810 928 testpin_sensorplate.mode(PullUp);
deldering95 43:d09814c177a0 929 button_lock.mode(PullUp);
deldering95 43:d09814c177a0 930 button_reposition.mode(PullUp);
deldering95 43:d09814c177a0 931 button_mute.mode(PullUp);
deldering95 43:d09814c177a0 932 button_new_patient.mode(PullUp);
deldering95 43:d09814c177a0 933
deldering95 57:fac732476810 934 testpin_sensorplate.fall(&sensorplate_detached);
ricardo_95 36:d10f368d037b 935 button_lock.fall(&trigger_lock); // Interrupt for rising edge lock button.
ricardo_95 36:d10f368d037b 936 button_lock.rise(&end_timer_lock_button);
ricardo_95 36:d10f368d037b 937 button_reposition.fall(&reposition_button_triggered);
ricardo_95 36:d10f368d037b 938 button_reposition.rise(&rise_reposition);
ricardo_95 36:d10f368d037b 939 button_mute.fall(&mute_button_triggered);
deldering95 51:69087c44e8ac 940 button_mute.rise(&rise_mute);
ricardo_95 36:d10f368d037b 941 button_new_patient.fall(&trigger_new_patient); // New patient/calibration button rising event.
deldering95 43:d09814c177a0 942 button_new_patient.rise(&activate_new_patient_function); // Falling edge for calibration algorithm option.
deldering95 68:1663f305ac33 943 colour_code = (colour_code_1 << 1 | colour_code_0);
deldering95 68:1663f305ac33 944 if(colour_code != 0b00 && pi_active == false) {
deldering95 68:1663f305ac33 945 pi_active = true;
deldering95 66:88c910cd4d9e 946 }
ricardo_95 19:3b5999fa7b7e 947
deldering95 68:1663f305ac33 948 if(!pi_active) {
deldering95 68:1663f305ac33 949 __disable_irq();
deldering95 68:1663f305ac33 950 while(!pi_active) {
deldering95 68:1663f305ac33 951 comet_timer.reset();
deldering95 68:1663f305ac33 952 comet_timer.start();
deldering95 68:1663f305ac33 953 ws.useII(WS2812::PER_PIXEL);
deldering95 68:1663f305ac33 954 ws.setII(255);
deldering95 68:1663f305ac33 955 tail_step = (255-COMET_TAIL_END_INTENSITY)/TAIL_LENGTH;
deldering95 68:1663f305ac33 956 comet++;
deldering95 68:1663f305ac33 957 px.SetAll(colourbuf[6]);
deldering95 68:1663f305ac33 958 px.Set(comet%ALARMBUF,colourbuf[4]);
deldering95 68:1663f305ac33 959 px.SetI(comet%ALARMBUF,255);
deldering95 68:1663f305ac33 960 for(int i=1; i<=TAIL_LENGTH; i++) {
deldering95 68:1663f305ac33 961 tail=(comet-i);
deldering95 68:1663f305ac33 962 if(tail<0)tail=0;
deldering95 68:1663f305ac33 963 px.Set(tail%ALARMBUF,colourbuf[4]);
deldering95 68:1663f305ac33 964 px.SetI(tail%ALARMBUF,(255-(tail_step*i)));
deldering95 68:1663f305ac33 965 }
deldering95 68:1663f305ac33 966 colour_code = (colour_code_1 << 1 | colour_code_0);
deldering95 68:1663f305ac33 967 if(colour_code != 0b00 && pi_active == false && (comet%ALARMBUF)==(ALARMBUF-1)) {
deldering95 68:1663f305ac33 968 pi_active = true;
deldering95 68:1663f305ac33 969 }
deldering95 68:1663f305ac33 970 if(testpin_sensorplate.read()||i2c_error) {
deldering95 68:1663f305ac33 971 px.Set(5,colourbuf[0]);
deldering95 68:1663f305ac33 972 px.Set(6,colourbuf[0]);
deldering95 68:1663f305ac33 973 px.Set(7,colourbuf[0]);
deldering95 68:1663f305ac33 974 px.SetI(5,int(intensity*0.5));
deldering95 68:1663f305ac33 975 px.SetI(6,int(intensity*2.55));
deldering95 68:1663f305ac33 976 px.SetI(7,int(intensity*0.5));
deldering95 68:1663f305ac33 977 }
deldering95 68:1663f305ac33 978 ws.write_offsets(px.getBuf(),0,0,0);
deldering95 68:1663f305ac33 979 while(comet_timer.read_ms()<total_comet_cycle_time_ms) {}
deldering95 68:1663f305ac33 980 }
deldering95 68:1663f305ac33 981 for(int i=ALARMBUF-1; i>=0; i--) {
deldering95 68:1663f305ac33 982 comet_timer.reset();
deldering95 68:1663f305ac33 983 comet_timer.start();
deldering95 68:1663f305ac33 984 for(int j=1; j<=TAIL_LENGTH; j++) {
deldering95 68:1663f305ac33 985 tail=(i-j);
deldering95 68:1663f305ac33 986 if(tail>=0) {
deldering95 68:1663f305ac33 987 px.Set(tail,colourbuf[4]);
deldering95 68:1663f305ac33 988 px.SetI(tail,(int(255-(tail_step*j))));
deldering95 68:1663f305ac33 989 }
deldering95 68:1663f305ac33 990 }
deldering95 68:1663f305ac33 991 px.Set(i,colourbuf[4]);
deldering95 68:1663f305ac33 992 px.SetI(i,255);
deldering95 68:1663f305ac33 993 if(testpin_sensorplate.read()||i2c_error) {
deldering95 68:1663f305ac33 994 px.Set(5,colourbuf[0]);
deldering95 68:1663f305ac33 995 px.Set(6,colourbuf[0]);
deldering95 68:1663f305ac33 996 px.Set(7,colourbuf[0]);
deldering95 68:1663f305ac33 997 px.SetI(5,int(intensity*0.5));
deldering95 68:1663f305ac33 998 px.SetI(6,int(intensity*2.55));
deldering95 68:1663f305ac33 999 px.SetI(7,int(intensity*0.5));
deldering95 68:1663f305ac33 1000 }
deldering95 68:1663f305ac33 1001 while(comet_timer.read_ms()<total_comet_cycle_time_ms) {}
deldering95 68:1663f305ac33 1002 ws.write_offsets(px.getBuf(),0,0,0);
deldering95 68:1663f305ac33 1003 }
deldering95 68:1663f305ac33 1004
deldering95 68:1663f305ac33 1005 __enable_irq();
deldering95 68:1663f305ac33 1006 wait(1);
deldering95 68:1663f305ac33 1007 }
deldering95 51:69087c44e8ac 1008 delay_between_button_pressed.reset(); // Delaytimer reset en start.
deldering95 51:69087c44e8ac 1009 delay_between_button_pressed.start();
deldering95 51:69087c44e8ac 1010
deldering95 66:88c910cd4d9e 1011 // ws.useII(WS2812::GLOBAL);
deldering95 51:69087c44e8ac 1012 reposition_feedback_LED = 0;
deldering95 51:69087c44e8ac 1013 new_patient_feedback_LED = 0;
deldering95 51:69087c44e8ac 1014 mute_feedback_LED = 0;
deldering95 51:69087c44e8ac 1015 usb_serial.printf("Lock State: %d\n",lock_state);
deldering95 48:aca02ef5cd01 1016 lock_feedback_LED = control_LED_intensity; // Lock LED initialization.
deldering95 51:69087c44e8ac 1017
DEldering 0:c0e44c46c573 1018 while (1) {
Ishy 42:673ddef4cfa4 1019 piezo_electric_sample_timer.reset(); // Clock gebruiken o.i.d.?
Ishy 42:673ddef4cfa4 1020 piezo_electric_sample_timer.start();
ricardo_95 46:a0e6e088a50a 1021 connection_test_sensorplate = !testpin_sensorplate && pi_active;
deldering95 68:1663f305ac33 1022 //usb_serial.printf("Loop\n");
deldering95 43:d09814c177a0 1023
Ishy 42:673ddef4cfa4 1024 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1025 // usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate);
Ishy 42:673ddef4cfa4 1026 }
deldering95 43:d09814c177a0 1027 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1028 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1029 }
deldering95 43:d09814c177a0 1030
Ishy 42:673ddef4cfa4 1031 if (connection_test_sensorplate == 1) {
deldering95 58:8cfa736d8553 1032 piezo_electric_array[0] = piezo_electric_adc.readADC_Differential_0_3(); // First PE readout.
deldering95 58:8cfa736d8553 1033 piezo_electric_array[3] = piezo_electric_adc.readADC_Differential_1_3(); // First PE readout.
deldering95 58:8cfa736d8553 1034
deldering95 48:aca02ef5cd01 1035 for (uint8_t k = 0; k < 4; ++k) {
deldering95 48:aca02ef5cd01 1036 piezo_resistive_array[k] = piezo_resistive_adc1.readADC_SingleEnded(k); // First 4 PR readout.
deldering95 48:aca02ef5cd01 1037 }
deldering95 43:d09814c177a0 1038 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1039 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1040 }
deldering95 58:8cfa736d8553 1041 while(piezo_electric_sample_timer.read_us()<(1*(total_readout_cycle_time_us/3))) {} // Wait untill 20% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
deldering95 58:8cfa736d8553 1042 piezo_electric_array[1] = piezo_electric_adc.readADC_Differential_0_3(); // Second PE readout.
deldering95 58:8cfa736d8553 1043 piezo_electric_array[4] = piezo_electric_adc.readADC_Differential_1_3(); // First PE readout.
deldering95 43:d09814c177a0 1044
deldering95 48:aca02ef5cd01 1045 for (uint8_t k = 0; k < 4; ++k) {
deldering95 48:aca02ef5cd01 1046 piezo_resistive_array[k+4] = piezo_resistive_adc2.readADC_SingleEnded(k); // Last 4 PR readout.
deldering95 48:aca02ef5cd01 1047 }
deldering95 43:d09814c177a0 1048 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1049 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1050 }
deldering95 58:8cfa736d8553 1051 while(piezo_electric_sample_timer.read_us()<(2*(total_readout_cycle_time_us/3))) {} // Wait untill 40% of cycle. Energy efficiency is not fine in this situation, correct if low energy is needed.
deldering95 58:8cfa736d8553 1052 piezo_electric_array[2] = piezo_electric_adc.readADC_Differential_0_3(); // Third PE readout.
deldering95 58:8cfa736d8553 1053 piezo_electric_array[5] = piezo_electric_adc.readADC_Differential_1_3(); // First PE readout.
deldering95 43:d09814c177a0 1054
deldering95 48:aca02ef5cd01 1055 angle_device_sensorplate.getAccelero(accelerometer_sensorplate); // Get accelerometer data.
deldering95 48:aca02ef5cd01 1056 angle = accelerometer_sensorplate[2]*100;
deldering95 48:aca02ef5cd01 1057 if(angle == 0) {
deldering95 51:69087c44e8ac 1058 MPU6050_belt angle_device_sensorplate(PB_9, PB_8);
deldering95 53:54c882995514 1059 angle_device_sensorplate.getAccelero(accelerometer_sensorplate);
deldering95 48:aca02ef5cd01 1060 angle = accelerometer_sensorplate[2]*100;
deldering95 48:aca02ef5cd01 1061 }
deldering95 53:54c882995514 1062 if((abs(accelerometer_sensorplate[0])+abs(accelerometer_sensorplate[1])+abs(accelerometer_sensorplate[2]))<= 6) {
deldering95 53:54c882995514 1063 for(uint8_t k = 0; k < 3; ++k) {
deldering95 53:54c882995514 1064 accelerometer_sensorplate[k]=accelerometer_sensorplate[k]*2;
deldering95 53:54c882995514 1065 }
deldering95 53:54c882995514 1066 }
deldering95 48:aca02ef5cd01 1067 angle_device_sensorplate.getGyro(gyroscope_sensorplate); // Get gyroscope data.
deldering95 43:d09814c177a0 1068 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1069 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1070 }
deldering95 48:aca02ef5cd01 1071 if (test_belt == 1) {
deldering95 68:1663f305ac33 1072 angle_device_reference_belt.getAccelero(accelerometer_reference_belt); // Get accelerometer data from belt.
deldering95 68:1663f305ac33 1073 if(accelerometer_reference_belt[0]==0) {
deldering95 68:1663f305ac33 1074 MPU6050 angle_device_reference_belt(PB_9, PB_8);
deldering95 68:1663f305ac33 1075 angle_device_reference_belt.getAccelero(accelerometer_reference_belt); // Get accelerometer data from belt.
deldering95 68:1663f305ac33 1076 }
deldering95 48:aca02ef5cd01 1077 angle_device_reference_belt.getGyro(gyroscope_reference_belt); // Get gyroscope data from Belt.
deldering95 48:aca02ef5cd01 1078 }
deldering95 43:d09814c177a0 1079
deldering95 48:aca02ef5cd01 1080 if (connection_test_sensorplate == 1) {
deldering95 48:aca02ef5cd01 1081 if (test_belt == 0) { // If statement for sending serial information sensorplate data when connection test is active.
deldering95 48:aca02ef5cd01 1082 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 1083 } else {
deldering95 53:54c882995514 1084 // 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 1085 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 68:1663f305ac33 1086 //usb_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 1087 }
deldering95 48:aca02ef5cd01 1088 } // binair print and convert in pi
deldering95 53:54c882995514 1089 else {
deldering95 53:54c882995514 1090 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 1091 }
Ishy 42:673ddef4cfa4 1092 }
deldering95 43:d09814c177a0 1093 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1094 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1095 }
Ishy 42:673ddef4cfa4 1096 timer_functions();
deldering95 43:d09814c177a0 1097 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1098 // usb_serial.printf("Loop time after timer_functions: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1099 }
deldering95 43:d09814c177a0 1100
deldering95 68:1663f305ac33 1101 if(buffer_counter>4) {
deldering95 68:1663f305ac33 1102 for(int i=0; i<4; i++) {
deldering95 68:1663f305ac33 1103 if(uart_input_buffer[i]=='='&&(uart_input_buffer[(i+2)%4]=='{'||uart_input_buffer[(i+2)%4]=='='))patient_present=uart_input_buffer[(i+1)];
deldering95 68:1663f305ac33 1104 if(uart_input_buffer[i]=='{'&&(uart_input_buffer[(i+2)%4]=='='||uart_input_buffer[(i+2)%4]=='{'))LED_colour_wheel_percentage=uart_input_buffer[(i+1)];
deldering95 68:1663f305ac33 1105 if((patient_present_old==4&&patient_present!=4)||(patient_present_old!=4&&patient_present==4))mixer=0;
deldering95 68:1663f305ac33 1106 usb_serial.putc(uart_input_buffer[i]);
deldering95 68:1663f305ac33 1107 }
deldering95 68:1663f305ac33 1108 patient_present_old=patient_present;
deldering95 68:1663f305ac33 1109 buffer_counter=0;
deldering95 68:1663f305ac33 1110 }
deldering95 66:88c910cd4d9e 1111 colour_wheel(LED_colour_wheel_percentage); // Function to select colour.
deldering95 67:1b300aa30923 1112 percentage_tester+=0.2;
deldering95 67:1b300aa30923 1113 //LED_colour_wheel_percentage=percentage_tester;
Ishy 42:673ddef4cfa4 1114 set_userinterface_LED(); // Set LED's of user interface (LED's above buttons).
deldering95 43:d09814c177a0 1115
deldering95 43:d09814c177a0 1116 if (test_mode == 1) { // If statement for test purposal.
Ishy 55:b74e7076d7a0 1117 // usb_serial.printf("Angle device sensorplate = %d\n",angle_device_sensorplate.testConnection());
Ishy 42:673ddef4cfa4 1118 }
deldering95 43:d09814c177a0 1119
Ishy 42:673ddef4cfa4 1120 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 1121
deldering95 43:d09814c177a0 1122 if (test_mode == 1) {
Ishy 55:b74e7076d7a0 1123 // usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1124 }
deldering95 43:d09814c177a0 1125
deldering95 43:d09814c177a0 1126 if (test_mode == 0) { // If statements for test purposal (untill * mark).
Ishy 55:b74e7076d7a0 1127 // usb_serial.printf("Loop time pre serial: %d ms\n",piezo_electric_sample_timer.read_ms());
deldering95 43:d09814c177a0 1128 }
deldering95 66:88c910cd4d9e 1129 //serial_read(); // Call function for reading information from PI by serial connection.
deldering95 43:d09814c177a0 1130 serial_log(); // Call function for logging information to PI by serial connection.
deldering95 68:1663f305ac33 1131 if(connection_test_sensorplate)i2c_error=i2c_error_detect();
deldering95 43:d09814c177a0 1132
deldering95 43:d09814c177a0 1133 if (test_mode == 0) { // If statements for test purposal (untill * mark).
deldering95 68:1663f305ac33 1134 //usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms());
Ishy 42:673ddef4cfa4 1135 }
deldering95 48:aca02ef5cd01 1136
deldering95 47:80cfc181f8b3 1137 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 1138 //if (test_pin == 1) {
Ishy 42:673ddef4cfa4 1139 // test_mode = 1;
Ishy 42:673ddef4cfa4 1140 // usb_serial.printf("%d\n",test_mode);
Ishy 42:673ddef4cfa4 1141 // }
Ishy 42:673ddef4cfa4 1142 // if (test_pin == 0) {
Ishy 42:673ddef4cfa4 1143 // test_mode = 0;
Ishy 42:673ddef4cfa4 1144 // usb_serial.printf("%d\n",test_mode);
Ishy 42:673ddef4cfa4 1145 // }
DEldering 0:c0e44c46c573 1146 }
ricardo_95 12:7b3a5940f911 1147 }