Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Committer:
deldering95
Date:
Fri May 25 09:02:28 2018 +0000
Revision:
66:88c910cd4d9e
Parent:
60:2f7e82c6f916
Child:
67:1b300aa30923
All colour wheel funcionalities except for the patient detect and decompression time memory

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