Nucleo-transfer
Dependencies: ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813
Fork of Nucleo-transfer by
Diff: Sensorplate/main.cpp
- Revision:
- 55:b74e7076d7a0
- Parent:
- 53:54c882995514
- Child:
- 56:97dea631c5f2
--- a/Sensorplate/main.cpp Mon Feb 12 10:23:58 2018 +0000 +++ b/Sensorplate/main.cpp Thu Feb 15 16:32:24 2018 +0000 @@ -102,14 +102,14 @@ int colourbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff,0xffa500,0xffffff}; // hex codes for the different colours 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'). bool lock_state = false, lock_flag = 0, mute_state = 0, alarm = 0, calibration_flag = 0, intensity_select = 1; // Boolean variables for logging states. -bool mute_flag = 0, new_patient_flag = 0, reposition_flag = 0; // Flag variables. +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. bool speaker_state = 0, LED_red_state = 0, LED_yellow_state = 0, LED_green_state = 0, power_plug_state = 0; -bool speaker_logged = 0, LED_red_logged = 0, LED_yellow_logged = 0, LED_green_logged = 0, power_plug_logged = 0; // is toevoegen +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 int locktime_ms = 2000; // Waittime for lock user interface in ms. int calibrationtime_ms = 2000; // Time to press new_patient button for calibration system. int calibration_flash = 0; // Variable for flash LED's to indicate calibration. int lock_flash = 0; -int buttondelay_ms = 750; // Button delay in ms. +int buttondelay_ms = 250; // Button delay in ms. Default: 750 int delay_lock_interface = 3000*60; // Delay for non using interface locktime. int speaker_active_ms = 750; // Time to iterate speaker on and off when alarm occurs. int alarm_voltage = 2400; // Needed voltage for alarm expressed as a digital 15 bit value (= 20% of max battery voltage). @@ -129,7 +129,7 @@ bool test_belt = 0; // Set test mode on (log functions to pc serial: interrupts, LED intensity and serial messages): -bool test_mode = 0; +bool test_mode = 1; // Variable for connection test (should be changed): bool connection_test_sensorplate; @@ -146,8 +146,8 @@ control_LED_intensity = (intensity/100); if (test_mode == 1) { // If statement for test purposal LED_intensity values. if def gebruiken voor testmode - usb_serial.printf("Intensity LED's shines to wall = %f\n", intensity); - usb_serial.printf("Intensity LED's above buttons = %f\n", control_LED_intensity); +// usb_serial.printf("Intensity LED's shines to wall = %f\n", intensity); +// usb_serial.printf("Intensity LED's above buttons = %f\n", control_LED_intensity); } } @@ -179,7 +179,7 @@ } if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("Intensity_select = %d en LED_colour = %d\n", intensity_select, LED_colour); +// usb_serial.printf("Intensity_select = %d en LED_colour = %d\n", intensity_select, LED_colour); } if (test_mode == 0) { @@ -189,139 +189,184 @@ } } -void serial_log() // Function for serial logging. See link to table with code declarations above in code. -{ - if (mute_flag == 1) { // If statement to control logging for mute button. +void serial_log() { // Function for serial logging. See link to table with code declarations above in code. + + if (reposition_flag == 1) { // If statement to control logging for reposition button. pi_serial.printf(">01\n"); if (test_mode == 1) { // If statement for test purposal. usb_serial.printf(">01\n"); } - mute_flag = 0; + reposition_flag = 0; + } + + if (reposition_lock_flag == 1) { // If statement to control logging for reposition button. + pi_serial.printf(">10\n"); + + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf(">10\n"); + } + + reposition_lock_flag = 0; } if (new_patient_flag == 1) { // If statement to control logging for new patient button. + pi_serial.printf(">02\n"); + + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf(">02\n"); + } + + new_patient_flag = 0; + } + + if (new_patient_lock_flag == 1) { // If statement to control logging for new patient button. + pi_serial.printf(">20\n"); + + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf(">20\n"); + } + + new_patient_lock_flag = 0; + } + // The new calibration button trigger + if (mute_flag == 1) { // If statement to control logging for new patient button. pi_serial.printf(">03\n"); if (test_mode == 1) { // If statement for test purposal. usb_serial.printf(">03\n"); } - new_patient_flag = 0; + mute_flag = 0; } - - if (reposition_flag == 1) { // If statement to control logging for reposition button. - pi_serial.printf(">02\n"); + // The new calibration button trigger + if (mute_lock_flag == 1) { // If statement to control logging for new patient button. + pi_serial.printf(">30\n"); if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf(">02\n"); + usb_serial.printf(">30\n"); } - reposition_flag = 0; + mute_lock_flag = 0; + } + + if (lock_flag == 1 && !lock_is_logged) { + if (lock_state == 0) // If statement to control logging for lock button. + pi_serial.printf(">04\n"); + else if(lock_state == 1) + pi_serial.printf(">40\n"); + + if (test_mode == 1) { + if (lock_state == 0) // If statement to control logging for lock button. + usb_serial.printf(">04\n"); + else if(lock_state == 1) + usb_serial.printf(">40\n"); // If statement for test purposal. + } + lock_is_logged = 1; } if (LED_red_logged != LED_red_state) { // If statement to control logging for LED_red. if (LED_red_state == 1) { - pi_serial.printf("&04\n"); + pi_serial.printf("&01\n"); LED_red_logged = LED_red_state; if (test_mode == 1) { - usb_serial.printf("&04\n"); + usb_serial.printf("&01\n"); } } if (LED_red_state == 0) { - pi_serial.printf("&40\n"); + pi_serial.printf("&10\n"); LED_red_logged = LED_red_state; if (test_mode == 1) { - usb_serial.printf("&40\n"); + usb_serial.printf("&10\n"); } } } if (LED_yellow_logged != LED_yellow_state) { // If statement to control logging for LED_yellow. if (LED_yellow_state == 1) { - pi_serial.printf("&06\n"); + pi_serial.printf("&02\n"); LED_yellow_logged = LED_yellow_state; if (test_mode == 1) { - usb_serial.printf("&06\n"); + usb_serial.printf("&02\n"); } } if (LED_yellow_state == 0) { - pi_serial.printf("&60\n"); + pi_serial.printf("&20\n"); LED_yellow_logged = LED_yellow_state; if (test_mode == 1) { - usb_serial.printf("&60\n"); + usb_serial.printf("&20\n"); } } } if (LED_green_logged != LED_green_state) { // If statement to control logging for LED_green. if (LED_green_state == 1) { - pi_serial.printf("&05\n"); + pi_serial.printf("&03\n"); LED_green_logged = LED_green_state; if (test_mode == 1) { - usb_serial.printf("&05\n"); + usb_serial.printf("&03\n"); } } if (LED_green_state == 0) { - pi_serial.printf("&50\n"); + pi_serial.printf("&30\n"); LED_green_logged = LED_green_state; if (test_mode == 1) { - usb_serial.printf("&50\n"); + usb_serial.printf("&30\n"); } } } if (speaker_logged != speaker_state) { // If statement to control logging for speaker. if (speaker_state == 1) { - pi_serial.printf("&07\n"); + pi_serial.printf("&04\n"); speaker_logged = speaker_state; if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("&07\n"); + usb_serial.printf("&04\n"); } } if (speaker_state == 0) { - pi_serial.printf("&70\n"); + pi_serial.printf("&40\n"); speaker_logged = speaker_state; if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("&70\n"); + usb_serial.printf("&40\n"); } } } - if (power_plug_logged != power_plug_state) { // If statement to control the logging for the state of the power plug. - if (power_plug_state == 1) { - pi_serial.printf("#08\n"); - - if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("#08\n"); - } - power_plug_logged = power_plug_state; - } - - if (power_plug_state == 0) { - pi_serial.printf("#80\n"); - - if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("#80\n"); - } - power_plug_logged = power_plug_state; - } - } +// if (power_plug_logged != power_plug_state) { // If statement to control the logging for the state of the power plug. +// if (power_plug_state == 1) { +// pi_serial.printf("#08\n"); +// +// if (test_mode == 1) { // If statement for test purposal. +// usb_serial.printf("#08\n"); +// } +// power_plug_logged = power_plug_state; +// } +// +// if (power_plug_state == 0) { +// pi_serial.printf("#80\n"); +// +// if (test_mode == 1) { // If statement for test purposal. +// usb_serial.printf("#80\n"); +// } +// power_plug_logged = power_plug_state; +// } +// } if (connection_test_sensorplate == 1) { // If statement for sending serial information sensorplate data when connection test is active. // Receiving order sensor information: 8 resistive sensors, 5 electric readings. Is splitted in two parts - part 1/2. 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 if (test_mode == 1) { - 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 +// 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 } } else { 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); @@ -336,15 +381,21 @@ switch(nLED_colour) { case 'r' : px.SetAll(colourbuf[0]); + LED_red_state = 1; + LED_yellow_state = 0; break; case 'g' : px.SetAll(colourbuf[1]); + LED_green_state = 1; + LED_red_state = 0; break; case 'b' : px.SetAll(colourbuf[2]); break; case 'y' : px.SetAll(colourbuf[3]); + LED_yellow_state = 1; + LED_green_state = 0; break; default : px.SetAll(colourbuf[4]); @@ -368,7 +419,8 @@ if (test_mode == 1) { usb_serial.printf("Lock triggered.\n"); } - + if (lock_state == 0) pi_serial.printf(">44\n"); + else if (lock_state == 1) pi_serial.printf(">00\n"); button_lock_hold_timer.reset(); button_lock_hold_timer.start(); delay_between_button_pressed.reset(); @@ -381,6 +433,7 @@ usb_serial.printf("Lock released.\n"); } lock_flag = 0; // Set lock_flag off. + lock_is_logged = 0; button_lock_hold_timer.stop(); // Stop and reset holdtimer button_lock_hold_timer.reset(); } @@ -389,16 +442,21 @@ { 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. lock_flash = 10; - } else { - delay_between_button_pressed.reset(); - delay_between_button_pressed.start(); - if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("Reposition triggered.\n"); - LED_on_dev_board1 = !LED_on_dev_board1; - } + } + + delay_between_button_pressed.reset(); + delay_between_button_pressed.start(); + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf("Reposition triggered.\n"); + LED_on_dev_board1 = !LED_on_dev_board1; + } + + if (lock_state == 1) reposition_lock_flag = 1; + else { reposition_flag = 1; - reposition_feedback_LED = control_LED_intensity; + pi_serial.printf("&05\n"); + if(test_mode == 1) usb_serial.printf("&05\n"); } } @@ -407,36 +465,57 @@ if (test_mode == 1) { // If statement for test purposal. usb_serial.printf("Reposition released.\n"); } + + if (reposition_feedback_LED != 0) { + pi_serial.printf("&50\n"); + + if(test_mode == 1) usb_serial.printf("&50\n"); + } + reposition_feedback_LED = 0; - } - +//TODO rename to calibration void mute_button_triggered() { 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. lock_flash = 10; - } else { - delay_between_button_pressed.reset(); - delay_between_button_pressed.start(); - button_calibration_hold_timer.reset(); // inline ? - button_calibration_hold_timer.start(); - mute_feedback_LED = control_LED_intensity; + } + + delay_between_button_pressed.reset(); + delay_between_button_pressed.start(); + button_calibration_hold_timer.reset(); // inline ? + button_calibration_hold_timer.start(); + + if (lock_state == 1) { + mute_lock_flag = 1; + } + + else { + mute_feedback_LED = control_LED_intensity; + pi_serial.printf("&07\n"); + if (test_mode == 1) usb_serial.printf("&07\n"); + mute_flag = 1; + } + + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf("Calibration triggered\n"); + LED_on_dev_board1 = !LED_on_dev_board1; + } - if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("Mute triggered %d.\n",mute_state); - LED_on_dev_board1 = !LED_on_dev_board1; - } - - mute_flag = 1; - } + } void rise_mute() // Interrupt for rising edge reposition function (deactivation; active low). { if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("Mute released.\n"); + usb_serial.printf("Calibration released.\n"); } + if(lock_state == 0 && mute_feedback_LED != 0) { + pi_serial.printf("&70\n"); + if (test_mode == 1) usb_serial.printf("&70\n"); + } + mute_feedback_LED = 0; button_calibration_hold_timer.stop(); // Timer reset for calibration function of new patient button. @@ -464,21 +543,38 @@ if (lock_state == 1 | (delay_between_button_pressed.read_ms() < buttondelay_ms)) { lock_flash = 10; - } else { - delay_between_button_pressed.reset(); - delay_between_button_pressed.start(); - new_patient_feedback_LED = control_LED_intensity;; + } + + delay_between_button_pressed.reset(); + delay_between_button_pressed.start(); + + if (lock_state == 1) new_patient_lock_flag = 1; + else { + new_patient_feedback_LED = control_LED_intensity; + pi_serial.printf("&06\n"); new_patient_flag = 1; - if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("New patient triggered.\n"); + + if(test_mode == 1){ + usb_serial.printf("&06\n"); } } + + if (test_mode == 1) { // If statement for test purposal. + usb_serial.printf("New patient triggered.\n"); + } + } void activate_new_patient_function() // Timer calibration function. { if (test_mode == 1) { // If statement for test purposal. usb_serial.printf("New patient released.\n"); + + } + + if (new_patient_feedback_LED != 0) { + pi_serial.printf("&60\n"); + if(test_mode) usb_serial.printf("&60\n"); } new_patient_feedback_LED = 0; } @@ -496,8 +592,12 @@ if (lock_state == 0) { // If statement to control lock feedback LED above button. lock_feedback_LED = control_LED_intensity; + pi_serial.printf("&08\n"); + if(test_mode == 1) usb_serial.printf("&08\n"); } else { lock_feedback_LED = 0; + pi_serial.printf("&80\n"); + if(test_mode == 1) usb_serial.printf("&80\n"); } } @@ -509,18 +609,25 @@ if ((button_calibration_hold_timer.read_ms() > calibrationtime_ms) && calibration_flag == 0 && button_mute == 0 && lock_state == 0) { // If statement for calibration algorithm. calibration_flag = 1; calibration_flash = 11; - - if (test_mode == 1) { // If statement for test purposal. + + if (test_mode == 1) { // If statement for test purposal. usb_serial.printf("Calibrate triggered.\n"); } - - pi_serial.printf(">30\n"); // Print statement for serial communication to inform algorithm to calibrate. + pi_serial.printf(">33\n"); + } if (delay_between_button_pressed.read_ms() > delay_lock_interface) { // If buttons are not pressed for 3 minutes, set lock active. lock_state = 1; LED_on_dev_board2 = 1; lock_feedback_LED = 0; + if (!auto_lock_led_logged) { + pi_serial.printf("&80\n"); + if(test_mode == 1) usb_serial.printf("&80\n"); + auto_lock_led_logged = 1; + } + } else { + auto_lock_led_logged = 0; } } @@ -543,8 +650,12 @@ if (lock_flash >= 1 && lock_state == 1) { if ((lock_flash % 2) == 0) { lock_feedback_LED = control_LED_intensity; + pi_serial.printf("&08\n"); + if(test_mode == 1) usb_serial.printf("&08\n"); } else { lock_feedback_LED = 0; + pi_serial.printf("&80\n"); + if(test_mode == 1) usb_serial.printf("&80\n"); } lock_flash--; } else { @@ -629,10 +740,10 @@ connection_test_sensorplate = !testpin_sensorplate && pi_active; if (test_mode == 1) { - usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate); +// usb_serial.printf("Connection test sensorplate = %d\n", connection_test_sensorplate); } if (test_mode == 1) { - usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); } if (connection_test_sensorplate == 1) { @@ -642,7 +753,7 @@ piezo_resistive_array[k] = piezo_resistive_adc1.readADC_SingleEnded(k); // First 4 PR readout. } if (test_mode == 1) { - usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); } 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. @@ -652,7 +763,7 @@ piezo_resistive_array[k+4] = piezo_resistive_adc2.readADC_SingleEnded(k); // Last 4 PR readout. } if (test_mode == 1) { - usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); } 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. @@ -672,7 +783,7 @@ } angle_device_sensorplate.getGyro(gyroscope_sensorplate); // Get gyroscope data. if (test_mode == 1) { - usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); } if (test_belt == 1) { angle_device_reference_belt.getGyro(gyroscope_reference_belt); // Get gyroscope data from Belt. @@ -696,11 +807,11 @@ piezo_electric_array[3] = piezo_electric_adc.readADC_Differential_0_1(); // Fourth PE readout. } if (test_mode == 1) { - usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); } timer_functions(); if (test_mode == 1) { - usb_serial.printf("Loop time after timer_functions: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time after timer_functions: %d ms\n",piezo_electric_sample_timer.read_ms()); } colour_select_indicating_LED_wall(LED_colour); // Function to select colour. @@ -709,7 +820,7 @@ 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. if (test_mode == 1) { // If statement for test purposal. - usb_serial.printf("Angle device sensorplate = %d\n",angle_device_sensorplate.testConnection()); +// usb_serial.printf("Angle device sensorplate = %d\n",angle_device_sensorplate.testConnection()); } if (connection_test_sensorplate == 1) { @@ -719,11 +830,11 @@ 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. if (test_mode == 1) { - usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time: %d ms\n",piezo_electric_sample_timer.read_ms()); } if (test_mode == 0) { // If statements for test purposal (untill * mark). - usb_serial.printf("Loop time pre serial: %d ms\n",piezo_electric_sample_timer.read_ms()); +// usb_serial.printf("Loop time pre serial: %d ms\n",piezo_electric_sample_timer.read_ms()); } serial_read(); // Call function for reading information from PI by serial connection. serial_log(); // Call function for logging information to PI by serial connection.