Nucleo-transfer

Dependencies:   ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813

Fork of Nucleo-transfer by Momo Medical

Committer:
deldering95
Date:
Fri Dec 01 16:09:08 2017 +0000
Revision:
44:dcbde3175a37
Parent:
43:d09814c177a0
Child:
45:7ebe860702b4
Fixed issue with holding buttons ;

Who changed what in which revision?

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