Samuel Rusterholz
/
PM2_Example_IRSensor
Workshop 1
main.cpp@11:cfc9b8e963db, 2021-03-13 (annotated)
- Committer:
- pmic
- Date:
- Sat Mar 13 17:37:49 2021 +0100
- Revision:
- 11:cfc9b8e963db
- Parent:
- 10:f459b443f676
- Child:
- 13:5d689f89d794
This is what the students download.
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 | 9:c5e1e1facb02 | 3 | |
pmic | 3:aa1d854807fe | 4 | using namespace std::chrono; |
pmic | 9:c5e1e1facb02 | 5 | |
pmic | 7:dc463bf54be6 | 6 | InterruptIn user_button(USER_BUTTON); |
pmic | 7:dc463bf54be6 | 7 | DigitalOut led(LED1); |
pmic | 9:c5e1e1facb02 | 8 | |
pmic | 7:dc463bf54be6 | 9 | bool executeMainTask = false; |
pmic | 7:dc463bf54be6 | 10 | Timer user_button_timer, loop_timer; |
pmic | 7:dc463bf54be6 | 11 | int Ts_ms = 50; |
pmic | 9:c5e1e1facb02 | 12 | |
pmic | 7:dc463bf54be6 | 13 | void button_fall(); |
pmic | 7:dc463bf54be6 | 14 | void button_rise(); |
pmic | 5:887081decd5c | 15 | |
pmic | 9:c5e1e1facb02 | 16 | /* input your stuff here */ |
pmic | 11:cfc9b8e963db | 17 | |
pmic | 9:c5e1e1facb02 | 18 | |
boro | 0:5d4d21d56334 | 19 | int main() |
boro | 0:5d4d21d56334 | 20 | { |
pmic | 1:4e0e4d0363d9 | 21 | user_button.fall(&button_fall); |
pmic | 1:4e0e4d0363d9 | 22 | user_button.rise(&button_rise); |
pmic | 6:6c1c38d4faa4 | 23 | loop_timer.start(); |
pmic | 9:c5e1e1facb02 | 24 | |
boro | 0:5d4d21d56334 | 25 | while (true) { |
pmic | 9:c5e1e1facb02 | 26 | |
pmic | 6:6c1c38d4faa4 | 27 | loop_timer.reset(); |
pmic | 9:c5e1e1facb02 | 28 | |
pmic | 1:4e0e4d0363d9 | 29 | /* ------------- start hacking ------------- -------------*/ |
pmic | 9:c5e1e1facb02 | 30 | |
pmic | 1:4e0e4d0363d9 | 31 | if(executeMainTask) { |
pmic | 11:cfc9b8e963db | 32 | |
pmic | 4:dcdcb25d1069 | 33 | |
pmic | 11:cfc9b8e963db | 34 | |
pmic | 6:6c1c38d4faa4 | 35 | /* visual feedback that the main task is executed */ |
pmic | 6:6c1c38d4faa4 | 36 | led = !led; |
pmic | 9:c5e1e1facb02 | 37 | |
boro | 0:5d4d21d56334 | 38 | } else { |
pmic | 6:6c1c38d4faa4 | 39 | led = 0; |
boro | 0:5d4d21d56334 | 40 | } |
pmic | 9:c5e1e1facb02 | 41 | |
pmic | 1:4e0e4d0363d9 | 42 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 9:c5e1e1facb02 | 43 | |
pmic | 6:6c1c38d4faa4 | 44 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:6c1c38d4faa4 | 45 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 7:dc463bf54be6 | 46 | thread_sleep_for(dT_loop_ms); |
boro | 0:5d4d21d56334 | 47 | } |
boro | 0:5d4d21d56334 | 48 | } |
pmic | 9:c5e1e1facb02 | 49 | |
pmic | 1:4e0e4d0363d9 | 50 | void button_fall() |
pmic | 1:4e0e4d0363d9 | 51 | { |
pmic | 1:4e0e4d0363d9 | 52 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 53 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 54 | } |
pmic | 9:c5e1e1facb02 | 55 | |
pmic | 1:4e0e4d0363d9 | 56 | void button_rise() |
pmic | 1:4e0e4d0363d9 | 57 | { |
pmic | 3:aa1d854807fe | 58 | int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 59 | user_button_timer.stop(); |
pmic | 1:4e0e4d0363d9 | 60 | if(t_button > 200) executeMainTask = !executeMainTask; |
pmic | 6:6c1c38d4faa4 | 61 | } |