Denise Ackermann
/
PM2_PES_board
PM2_PES_board
main.cpp@0:f44522e28559, 2021-03-30 (annotated)
- Committer:
- pmic
- Date:
- Tue Mar 30 12:50:43 2021 +0000
- Revision:
- 0:f44522e28559
- Child:
- 1:93d997d6b232
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 0:f44522e28559 | 1 | #include "mbed.h" |
pmic | 0:f44522e28559 | 2 | #include "platform/mbed_thread.h" |
pmic | 0:f44522e28559 | 3 | |
pmic | 0:f44522e28559 | 4 | using namespace std::chrono; |
pmic | 0:f44522e28559 | 5 | |
pmic | 0:f44522e28559 | 6 | InterruptIn user_button(USER_BUTTON); |
pmic | 0:f44522e28559 | 7 | DigitalOut led(LED1); |
pmic | 0:f44522e28559 | 8 | |
pmic | 0:f44522e28559 | 9 | bool executeMainTask = false; |
pmic | 0:f44522e28559 | 10 | Timer user_button_timer, loop_timer; |
pmic | 0:f44522e28559 | 11 | int Ts_ms = 50; |
pmic | 0:f44522e28559 | 12 | |
pmic | 0:f44522e28559 | 13 | void button_fall(); |
pmic | 0:f44522e28559 | 14 | void button_rise(); |
pmic | 0:f44522e28559 | 15 | |
pmic | 0:f44522e28559 | 16 | /* input your stuff here */ |
pmic | 0:f44522e28559 | 17 | AnalogIn analogIn(PA_0); |
pmic | 0:f44522e28559 | 18 | float dist = 0.0f; |
pmic | 0:f44522e28559 | 19 | |
pmic | 0:f44522e28559 | 20 | int main() |
pmic | 0:f44522e28559 | 21 | { |
pmic | 0:f44522e28559 | 22 | user_button.fall(&button_fall); |
pmic | 0:f44522e28559 | 23 | user_button.rise(&button_rise); |
pmic | 0:f44522e28559 | 24 | loop_timer.start(); |
pmic | 0:f44522e28559 | 25 | |
pmic | 0:f44522e28559 | 26 | while (true) { |
pmic | 0:f44522e28559 | 27 | |
pmic | 0:f44522e28559 | 28 | loop_timer.reset(); |
pmic | 0:f44522e28559 | 29 | |
pmic | 0:f44522e28559 | 30 | /* ------------- start hacking ------------- -------------*/ |
pmic | 0:f44522e28559 | 31 | |
pmic | 0:f44522e28559 | 32 | if(executeMainTask) { |
pmic | 0:f44522e28559 | 33 | |
pmic | 0:f44522e28559 | 34 | dist = analogIn.read()*3.3f; |
pmic | 0:f44522e28559 | 35 | |
pmic | 0:f44522e28559 | 36 | /* do only output what's really necessary, outputting "Measured value in mV: "" within the loop is no good solution */ |
pmic | 0:f44522e28559 | 37 | printf("Measured value in mV: %d\r\n", (static_cast<int>(dist * 1e3))); |
pmic | 0:f44522e28559 | 38 | |
pmic | 0:f44522e28559 | 39 | /* visual feedback that the main task is executed */ |
pmic | 0:f44522e28559 | 40 | led = !led; |
pmic | 0:f44522e28559 | 41 | |
pmic | 0:f44522e28559 | 42 | } else { |
pmic | 0:f44522e28559 | 43 | led = 0; |
pmic | 0:f44522e28559 | 44 | } |
pmic | 0:f44522e28559 | 45 | |
pmic | 0:f44522e28559 | 46 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 0:f44522e28559 | 47 | |
pmic | 0:f44522e28559 | 48 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 0:f44522e28559 | 49 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 0:f44522e28559 | 50 | thread_sleep_for(dT_loop_ms); |
pmic | 0:f44522e28559 | 51 | } |
pmic | 0:f44522e28559 | 52 | } |
pmic | 0:f44522e28559 | 53 | |
pmic | 0:f44522e28559 | 54 | void button_fall() |
pmic | 0:f44522e28559 | 55 | { |
pmic | 0:f44522e28559 | 56 | user_button_timer.reset(); |
pmic | 0:f44522e28559 | 57 | user_button_timer.start(); |
pmic | 0:f44522e28559 | 58 | } |
pmic | 0:f44522e28559 | 59 | |
pmic | 0:f44522e28559 | 60 | void button_rise() |
pmic | 0:f44522e28559 | 61 | { |
pmic | 0:f44522e28559 | 62 | int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 0:f44522e28559 | 63 | user_button_timer.stop(); |
pmic | 0:f44522e28559 | 64 | if(t_button > 200) executeMainTask = !executeMainTask; |
pmic | 0:f44522e28559 | 65 | } |