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