Our Programm to build up from the PES Board Example
Dependencies: PM2_Libary
main.cpp@20:c52e148d933f, 2022-03-16 (annotated)
- Committer:
- pmic
- Date:
- Wed Mar 16 11:59:06 2022 +0100
- Revision:
- 20:c52e148d933f
- Parent:
- 19:22e264bd960d
- Child:
- 21:f13e39291d80
Version for students.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boro | 0:5d4d21d56334 | 1 | #include "mbed.h" |
pmic | 16:2de2a437afdc | 2 | #include "PM2_Libary.h" |
pmic | 16:2de2a437afdc | 3 | |
pmic | 16:2de2a437afdc | 4 | // logical variable main task |
pmic | 16:2de2a437afdc | 5 | bool do_execute_main_task = false; // this variable will be toggled via the user button (blue button) to or not to execute the main task |
pmic | 16:2de2a437afdc | 6 | |
pmic | 16:2de2a437afdc | 7 | // user button on nucleo board |
pmic | 16:2de2a437afdc | 8 | Timer user_button_timer; // create Timer object which we use to check if user button was pressed for a certain time (robust against signal bouncing) |
pmic | 16:2de2a437afdc | 9 | InterruptIn user_button(PC_13); // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR) |
pmic | 16:2de2a437afdc | 10 | void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below |
pmic | 16:2de2a437afdc | 11 | void user_button_released_fcn(); |
pmic | 16:2de2a437afdc | 12 | |
pmic | 16:2de2a437afdc | 13 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 16:2de2a437afdc | 14 | int main_task_period_ms = 50; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second |
pmic | 16:2de2a437afdc | 15 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
pmic | 13:5d689f89d794 | 16 | |
pmic | 16:2de2a437afdc | 17 | // led on nucleo board |
pmic | 16:2de2a437afdc | 18 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
pmic | 16:2de2a437afdc | 19 | |
pmic | 16:2de2a437afdc | 20 | // additional Led |
pmic | 16:2de2a437afdc | 21 | DigitalOut extra_led(PB_9); // create DigitalOut object to command extra led (do add an aditional resistor, e.g. 220...500 Ohm) |
pmic | 16:2de2a437afdc | 22 | |
pmic | 16:2de2a437afdc | 23 | // mechanical button |
pmic | 16:2de2a437afdc | 24 | DigitalIn mechanical_button(PC_5); // create DigitalIn object to evaluate extra mechanical button, you need to specify the mode for proper usage, see below |
pmic | 16:2de2a437afdc | 25 | |
pmic | 16:2de2a437afdc | 26 | // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor |
pmic | 16:2de2a437afdc | 27 | float ir_distance_mV = 0.0f; // define variable to store measurement |
pmic | 20:c52e148d933f | 28 | // ??? // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 |
pmic | 16:2de2a437afdc | 29 | |
boro | 0:5d4d21d56334 | 30 | int main() |
boro | 0:5d4d21d56334 | 31 | { |
pmic | 16:2de2a437afdc | 32 | // attach button fall and rise functions to user button object |
pmic | 16:2de2a437afdc | 33 | user_button.fall(&user_button_pressed_fcn); |
pmic | 16:2de2a437afdc | 34 | user_button.rise(&user_button_released_fcn); |
pmic | 16:2de2a437afdc | 35 | |
pmic | 16:2de2a437afdc | 36 | // start timers |
pmic | 16:2de2a437afdc | 37 | main_task_timer.start(); |
pmic | 16:2de2a437afdc | 38 | |
pmic | 16:2de2a437afdc | 39 | // set pullup mode: add resistor between pin and 3.3 V, so that there is a defined potential |
pmic | 16:2de2a437afdc | 40 | mechanical_button.mode(PullUp); |
pmic | 16:2de2a437afdc | 41 | |
pmic | 16:2de2a437afdc | 42 | while (true) { // this loop will run forever |
pmic | 16:2de2a437afdc | 43 | |
pmic | 16:2de2a437afdc | 44 | main_task_timer.reset(); |
pmic | 16:2de2a437afdc | 45 | |
pmic | 16:2de2a437afdc | 46 | if (do_execute_main_task) { |
pmic | 13:5d689f89d794 | 47 | |
pmic | 16:2de2a437afdc | 48 | // read analog input |
pmic | 20:c52e148d933f | 49 | // ??? |
pmic | 13:5d689f89d794 | 50 | |
pmic | 17:085fff8287d0 | 51 | // if the mechanical button is pressed the extra led is blinking |
pmic | 16:2de2a437afdc | 52 | if (mechanical_button) { |
pmic | 17:085fff8287d0 | 53 | // visual feedback that the main task is executed |
pmic | 17:085fff8287d0 | 54 | extra_led = !extra_led; |
pmic | 16:2de2a437afdc | 55 | } else { |
pmic | 17:085fff8287d0 | 56 | extra_led = 1; |
pmic | 16:2de2a437afdc | 57 | } |
pmic | 14:db46f47b0480 | 58 | |
boro | 0:5d4d21d56334 | 59 | } else { |
pmic | 16:2de2a437afdc | 60 | |
pmic | 16:2de2a437afdc | 61 | ir_distance_mV = 0.0f; |
pmic | 16:2de2a437afdc | 62 | |
pmic | 16:2de2a437afdc | 63 | extra_led = 0; |
boro | 0:5d4d21d56334 | 64 | } |
pmic | 13:5d689f89d794 | 65 | |
pmic | 16:2de2a437afdc | 66 | user_led = !user_led; |
pmic | 13:5d689f89d794 | 67 | |
pmic | 16:2de2a437afdc | 68 | // do only output via serial what's really necessary (this makes your code slow) |
pmic | 17:085fff8287d0 | 69 | printf("IR sensor (mV): %3.3f\r\n", ir_distance_mV); |
pmic | 16:2de2a437afdc | 70 | |
pmic | 16:2de2a437afdc | 71 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 16:2de2a437afdc | 72 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 16:2de2a437afdc | 73 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
boro | 0:5d4d21d56334 | 74 | } |
boro | 0:5d4d21d56334 | 75 | } |
pmic | 13:5d689f89d794 | 76 | |
pmic | 16:2de2a437afdc | 77 | void user_button_pressed_fcn() |
pmic | 1:4e0e4d0363d9 | 78 | { |
pmic | 19:22e264bd960d | 79 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 80 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 81 | } |
pmic | 13:5d689f89d794 | 82 | |
pmic | 16:2de2a437afdc | 83 | void user_button_released_fcn() |
pmic | 1:4e0e4d0363d9 | 84 | { |
pmic | 16:2de2a437afdc | 85 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 16:2de2a437afdc | 86 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 87 | user_button_timer.stop(); |
pmic | 16:2de2a437afdc | 88 | if (user_button_elapsed_time_ms > 200) { |
pmic | 16:2de2a437afdc | 89 | do_execute_main_task = !do_execute_main_task; |
pmic | 13:5d689f89d794 | 90 | } |
pmic | 6:6c1c38d4faa4 | 91 | } |