Our Programm to build up from the PES Board Example
Dependencies: PM2_Libary
main.cpp@14:db46f47b0480, 2021-04-06 (annotated)
- Committer:
- pmic
- Date:
- Tue Apr 06 12:20:31 2021 +0000
- Revision:
- 14:db46f47b0480
- Parent:
- 13:5d689f89d794
- Child:
- 15:a2c188ed294f
Autoalign the solution.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boro | 0:5d4d21d56334 | 1 | #include "mbed.h" |
boro | 0:5d4d21d56334 | 2 | #include "platform/mbed_thread.h" |
pmic | 13:5d689f89d794 | 3 | |
pmic | 3:aa1d854807fe | 4 | using namespace std::chrono; |
pmic | 13:5d689f89d794 | 5 | |
pmic | 13:5d689f89d794 | 6 | InterruptIn user_button(USER_BUTTON); |
pmic | 13:5d689f89d794 | 7 | DigitalOut led(LED1); |
pmic | 13:5d689f89d794 | 8 | |
pmic | 13:5d689f89d794 | 9 | bool executeMainTask = false; |
pmic | 13:5d689f89d794 | 10 | Timer user_button_timer, loop_timer; |
pmic | 13:5d689f89d794 | 11 | int Ts_ms = 50; |
pmic | 5:887081decd5c | 12 | |
pmic | 13:5d689f89d794 | 13 | /* declaration of custom button functions */ |
pmic | 13:5d689f89d794 | 14 | void button_fall(); |
pmic | 13:5d689f89d794 | 15 | void button_rise(); |
pmic | 11:cfc9b8e963db | 16 | |
pmic | 13:5d689f89d794 | 17 | /* create analog input object */ |
pmic | 13:5d689f89d794 | 18 | AnalogIn analogIn(PA_0); |
pmic | 13:5d689f89d794 | 19 | float dist = 0.0f; |
pmic | 13:5d689f89d794 | 20 | |
boro | 0:5d4d21d56334 | 21 | int main() |
boro | 0:5d4d21d56334 | 22 | { |
pmic | 1:4e0e4d0363d9 | 23 | user_button.fall(&button_fall); |
pmic | 1:4e0e4d0363d9 | 24 | user_button.rise(&button_rise); |
pmic | 6:6c1c38d4faa4 | 25 | loop_timer.start(); |
pmic | 13:5d689f89d794 | 26 | |
boro | 0:5d4d21d56334 | 27 | while (true) { |
pmic | 13:5d689f89d794 | 28 | |
pmic | 6:6c1c38d4faa4 | 29 | loop_timer.reset(); |
pmic | 13:5d689f89d794 | 30 | |
pmic | 13:5d689f89d794 | 31 | /* ------------- start hacking ------------- -------------*/ |
pmic | 13:5d689f89d794 | 32 | |
pmic | 13:5d689f89d794 | 33 | if (executeMainTask) { |
pmic | 13:5d689f89d794 | 34 | |
pmic | 13:5d689f89d794 | 35 | /* read analog input */ |
pmic | 13:5d689f89d794 | 36 | dist = analogIn.read()*3.3f; |
pmic | 14:db46f47b0480 | 37 | |
pmic | 13:5d689f89d794 | 38 | /* do only output what's really necessary, outputting "Measured value in mV: "" within the loop is no good solution */ |
pmic | 13:5d689f89d794 | 39 | printf("Measured value in mV: %d\r\n", (static_cast<int>(dist * 1e3))); |
pmic | 11:cfc9b8e963db | 40 | |
pmic | 6:6c1c38d4faa4 | 41 | /* visual feedback that the main task is executed */ |
pmic | 6:6c1c38d4faa4 | 42 | led = !led; |
pmic | 13:5d689f89d794 | 43 | |
boro | 0:5d4d21d56334 | 44 | } else { |
pmic | 6:6c1c38d4faa4 | 45 | led = 0; |
boro | 0:5d4d21d56334 | 46 | } |
pmic | 13:5d689f89d794 | 47 | |
pmic | 1:4e0e4d0363d9 | 48 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 13:5d689f89d794 | 49 | |
pmic | 6:6c1c38d4faa4 | 50 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:6c1c38d4faa4 | 51 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 7:dc463bf54be6 | 52 | thread_sleep_for(dT_loop_ms); |
boro | 0:5d4d21d56334 | 53 | } |
boro | 0:5d4d21d56334 | 54 | } |
pmic | 13:5d689f89d794 | 55 | |
pmic | 1:4e0e4d0363d9 | 56 | void button_fall() |
pmic | 1:4e0e4d0363d9 | 57 | { |
pmic | 1:4e0e4d0363d9 | 58 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 59 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 60 | } |
pmic | 13:5d689f89d794 | 61 | |
pmic | 1:4e0e4d0363d9 | 62 | void button_rise() |
pmic | 1:4e0e4d0363d9 | 63 | { |
pmic | 13:5d689f89d794 | 64 | int t_button_ms = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 65 | user_button_timer.stop(); |
pmic | 13:5d689f89d794 | 66 | if (t_button_ms > 200) { |
pmic | 13:5d689f89d794 | 67 | executeMainTask = !executeMainTask; |
pmic | 13:5d689f89d794 | 68 | } |
pmic | 6:6c1c38d4faa4 | 69 | } |