Samuel Rusterholz
/
PM2_Example_IRSensor
Workshop 1
main.cpp@7:dc463bf54be6, 2021-03-13 (annotated)
- Committer:
- pmic
- Date:
- Sat Mar 13 16:51:00 2021 +0100
- Revision:
- 7:dc463bf54be6
- Parent:
- 6:6c1c38d4faa4
- Child:
- 8:49f2b6a515ac
Example with BufferedSerial.
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 | 5:887081decd5c | 3 | #include "string" |
pmic | 1:4e0e4d0363d9 | 4 | |
pmic | 3:aa1d854807fe | 5 | using namespace std::chrono; |
pmic | 3:aa1d854807fe | 6 | |
pmic | 7:dc463bf54be6 | 7 | InterruptIn user_button(USER_BUTTON); |
pmic | 7:dc463bf54be6 | 8 | DigitalOut led(LED1); |
pmic | 5:887081decd5c | 9 | BufferedSerial pc(USBTX, USBRX); |
boro | 0:5d4d21d56334 | 10 | |
pmic | 7:dc463bf54be6 | 11 | bool executeMainTask = false; |
pmic | 7:dc463bf54be6 | 12 | Timer user_button_timer, loop_timer; |
pmic | 7:dc463bf54be6 | 13 | int Ts_ms = 50; |
boro | 0:5d4d21d56334 | 14 | |
pmic | 7:dc463bf54be6 | 15 | void button_fall(); |
pmic | 7:dc463bf54be6 | 16 | void button_rise(); |
pmic | 5:887081decd5c | 17 | |
pmic | 7:dc463bf54be6 | 18 | AnalogIn analogIn(PA_0); |
pmic | 7:dc463bf54be6 | 19 | float dist = 0.0f; |
pmic | 6:6c1c38d4faa4 | 20 | |
pmic | 7:dc463bf54be6 | 21 | float floatNumber = 0.003713f; |
pmic | 7:dc463bf54be6 | 22 | int integerNumber = 777; |
boro | 0:5d4d21d56334 | 23 | |
boro | 0:5d4d21d56334 | 24 | int main() |
boro | 0:5d4d21d56334 | 25 | { |
pmic | 1:4e0e4d0363d9 | 26 | user_button.fall(&button_fall); |
pmic | 1:4e0e4d0363d9 | 27 | user_button.rise(&button_rise); |
pmic | 6:6c1c38d4faa4 | 28 | loop_timer.start(); |
boro | 0:5d4d21d56334 | 29 | |
boro | 0:5d4d21d56334 | 30 | while (true) { |
pmic | 1:4e0e4d0363d9 | 31 | |
pmic | 6:6c1c38d4faa4 | 32 | loop_timer.reset(); |
pmic | 6:6c1c38d4faa4 | 33 | |
pmic | 1:4e0e4d0363d9 | 34 | /* ------------- start hacking ------------- -------------*/ |
boro | 0:5d4d21d56334 | 35 | |
pmic | 1:4e0e4d0363d9 | 36 | if(executeMainTask) { |
pmic | 4:dcdcb25d1069 | 37 | |
pmic | 6:6c1c38d4faa4 | 38 | dist = analogIn.read()*3.3f; |
pmic | 4:dcdcb25d1069 | 39 | |
pmic | 6:6c1c38d4faa4 | 40 | // printf("Measval: %d\r\n", (static_cast<int>(dist * 1000))); |
pmic | 5:887081decd5c | 41 | |
pmic | 6:6c1c38d4faa4 | 42 | string msg_str = to_string((static_cast<int>(dist * 1e6))); |
pmic | 6:6c1c38d4faa4 | 43 | msg_str.append(";"); |
pmic | 6:6c1c38d4faa4 | 44 | msg_str.append(to_string((static_cast<int>(floatNumber * 1e6)))); |
pmic | 6:6c1c38d4faa4 | 45 | msg_str.append(";"); |
pmic | 6:6c1c38d4faa4 | 46 | msg_str.append(to_string(integerNumber)); |
pmic | 6:6c1c38d4faa4 | 47 | msg_str.append(";\r\n"); |
pmic | 6:6c1c38d4faa4 | 48 | char msg[msg_str.length() + 1]; |
pmic | 6:6c1c38d4faa4 | 49 | strcpy(msg, msg_str.c_str()); |
pmic | 4:dcdcb25d1069 | 50 | pc.write(msg, sizeof(msg)); |
pmic | 4:dcdcb25d1069 | 51 | |
pmic | 6:6c1c38d4faa4 | 52 | /* visual feedback that the main task is executed */ |
pmic | 6:6c1c38d4faa4 | 53 | led = !led; |
pmic | 4:dcdcb25d1069 | 54 | |
boro | 0:5d4d21d56334 | 55 | } else { |
pmic | 6:6c1c38d4faa4 | 56 | led = 0; |
boro | 0:5d4d21d56334 | 57 | } |
pmic | 1:4e0e4d0363d9 | 58 | |
pmic | 1:4e0e4d0363d9 | 59 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 1:4e0e4d0363d9 | 60 | |
pmic | 6:6c1c38d4faa4 | 61 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:6c1c38d4faa4 | 62 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 7:dc463bf54be6 | 63 | thread_sleep_for(dT_loop_ms); |
boro | 0:5d4d21d56334 | 64 | } |
boro | 0:5d4d21d56334 | 65 | } |
pmic | 1:4e0e4d0363d9 | 66 | |
pmic | 1:4e0e4d0363d9 | 67 | void button_fall() |
pmic | 1:4e0e4d0363d9 | 68 | { |
pmic | 1:4e0e4d0363d9 | 69 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 70 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 71 | } |
pmic | 1:4e0e4d0363d9 | 72 | |
pmic | 1:4e0e4d0363d9 | 73 | void button_rise() |
pmic | 1:4e0e4d0363d9 | 74 | { |
pmic | 3:aa1d854807fe | 75 | int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 76 | user_button_timer.stop(); |
pmic | 1:4e0e4d0363d9 | 77 | if(t_button > 200) executeMainTask = !executeMainTask; |
pmic | 6:6c1c38d4faa4 | 78 | } |