Liam Decaster
/
PM2_Example_IRSensor
Hello there
main.cpp@10:f459b443f676, 2021-03-13 (annotated)
- Committer:
- pmic
- Date:
- Sat Mar 13 17:37:03 2021 +0100
- Revision:
- 10:f459b443f676
- Parent:
- 9:c5e1e1facb02
- Child:
- 11:cfc9b8e963db
Example for student with printf mbed-os 6 version.
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 | 7:dc463bf54be6 | 17 | AnalogIn analogIn(PA_0); |
pmic | 7:dc463bf54be6 | 18 | float dist = 0.0f; |
pmic | 9:c5e1e1facb02 | 19 | |
boro | 0:5d4d21d56334 | 20 | int main() |
boro | 0:5d4d21d56334 | 21 | { |
pmic | 1:4e0e4d0363d9 | 22 | user_button.fall(&button_fall); |
pmic | 1:4e0e4d0363d9 | 23 | user_button.rise(&button_rise); |
pmic | 6:6c1c38d4faa4 | 24 | loop_timer.start(); |
pmic | 9:c5e1e1facb02 | 25 | |
boro | 0:5d4d21d56334 | 26 | while (true) { |
pmic | 9:c5e1e1facb02 | 27 | |
pmic | 6:6c1c38d4faa4 | 28 | loop_timer.reset(); |
pmic | 9:c5e1e1facb02 | 29 | |
pmic | 1:4e0e4d0363d9 | 30 | /* ------------- start hacking ------------- -------------*/ |
pmic | 9:c5e1e1facb02 | 31 | |
pmic | 1:4e0e4d0363d9 | 32 | if(executeMainTask) { |
pmic | 9:c5e1e1facb02 | 33 | |
pmic | 9:c5e1e1facb02 | 34 | dist = analogIn.read()*3.3f; |
pmic | 4:dcdcb25d1069 | 35 | |
pmic | 9:c5e1e1facb02 | 36 | /* do only output what's really necessary, outputting "Measured value in mV: "" within the loop is no good solution */ |
pmic | 10:f459b443f676 | 37 | printf("Measured value in mV: %d\r\n", (static_cast<int>(dist * 1e3))); |
pmic | 9:c5e1e1facb02 | 38 | |
pmic | 6:6c1c38d4faa4 | 39 | /* visual feedback that the main task is executed */ |
pmic | 6:6c1c38d4faa4 | 40 | led = !led; |
pmic | 9:c5e1e1facb02 | 41 | |
boro | 0:5d4d21d56334 | 42 | } else { |
pmic | 6:6c1c38d4faa4 | 43 | led = 0; |
boro | 0:5d4d21d56334 | 44 | } |
pmic | 9:c5e1e1facb02 | 45 | |
pmic | 1:4e0e4d0363d9 | 46 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 9:c5e1e1facb02 | 47 | |
pmic | 6:6c1c38d4faa4 | 48 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:6c1c38d4faa4 | 49 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 7:dc463bf54be6 | 50 | thread_sleep_for(dT_loop_ms); |
boro | 0:5d4d21d56334 | 51 | } |
boro | 0:5d4d21d56334 | 52 | } |
pmic | 9:c5e1e1facb02 | 53 | |
pmic | 1:4e0e4d0363d9 | 54 | void button_fall() |
pmic | 1:4e0e4d0363d9 | 55 | { |
pmic | 1:4e0e4d0363d9 | 56 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 57 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 58 | } |
pmic | 9:c5e1e1facb02 | 59 | |
pmic | 1:4e0e4d0363d9 | 60 | void button_rise() |
pmic | 1:4e0e4d0363d9 | 61 | { |
pmic | 3:aa1d854807fe | 62 | int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 63 | user_button_timer.stop(); |
pmic | 1:4e0e4d0363d9 | 64 | if(t_button > 200) executeMainTask = !executeMainTask; |
pmic | 6:6c1c38d4faa4 | 65 | } |