Workshop 1

Dependencies:   PM2_Libary

Committer:
pmic
Date:
Sat Mar 13 12:09:11 2021 +0000
Revision:
3:aa1d854807fe
Parent:
2:4ba1937ce284
Child:
4:dcdcb25d1069
Update to mbed-os v6.8.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boro 0:5d4d21d56334 1 #include "mbed.h"
boro 0:5d4d21d56334 2 #include "platform/mbed_thread.h"
pmic 1:4e0e4d0363d9 3
pmic 1:4e0e4d0363d9 4 #define pi 3.14159265358979323846
pmic 1:4e0e4d0363d9 5
pmic 3:aa1d854807fe 6 using namespace std::chrono;
pmic 3:aa1d854807fe 7
pmic 1:4e0e4d0363d9 8 InterruptIn user_button(USER_BUTTON);
pmic 1:4e0e4d0363d9 9 DigitalOut led(LED1);
pmic 3:aa1d854807fe 10 // Serial pc(SERIAL_TX, SERIAL_RX);
boro 0:5d4d21d56334 11
pmic 1:4e0e4d0363d9 12 bool executeMainTask = false;
pmic 1:4e0e4d0363d9 13 Timer user_button_timer, loop_timer;
pmic 1:4e0e4d0363d9 14 int Ts_ms = 50;
boro 0:5d4d21d56334 15
pmic 2:4ba1937ce284 16 void button_fall(); // stuff
pmic 2:4ba1937ce284 17 void button_rise(); // stuff
boro 0:5d4d21d56334 18
pmic 1:4e0e4d0363d9 19 AnalogIn analogIn(PA_0);
pmic 1:4e0e4d0363d9 20 float dist = 0.0f;
boro 0:5d4d21d56334 21
boro 0:5d4d21d56334 22 int main()
boro 0:5d4d21d56334 23 {
pmic 3:aa1d854807fe 24 // pc.baud(115200);
pmic 1:4e0e4d0363d9 25 user_button.fall(&button_fall);
pmic 1:4e0e4d0363d9 26 user_button.rise(&button_rise);
pmic 1:4e0e4d0363d9 27 loop_timer.reset();
boro 0:5d4d21d56334 28
boro 0:5d4d21d56334 29 while (true) {
pmic 1:4e0e4d0363d9 30
pmic 1:4e0e4d0363d9 31 /* ------------- start hacking ------------- -------------*/
boro 0:5d4d21d56334 32
pmic 1:4e0e4d0363d9 33 if(executeMainTask) {
pmic 1:4e0e4d0363d9 34 dist = analogIn.read()*3.3f;
pmic 3:aa1d854807fe 35 // printf("measurement: %9.6f\r\n", dist);
boro 0:5d4d21d56334 36 } else {
boro 0:5d4d21d56334 37
boro 0:5d4d21d56334 38 }
pmic 1:4e0e4d0363d9 39
pmic 1:4e0e4d0363d9 40 /* ------------- stop hacking ------------- -------------*/
pmic 1:4e0e4d0363d9 41
pmic 1:4e0e4d0363d9 42 if(executeMainTask) {
pmic 1:4e0e4d0363d9 43 led = !led;
pmic 1:4e0e4d0363d9 44 }
pmic 3:aa1d854807fe 45 int dT_loop = Ts_ms - duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 46 thread_sleep_for(dT_loop);
boro 0:5d4d21d56334 47 }
boro 0:5d4d21d56334 48 }
pmic 1:4e0e4d0363d9 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 1:4e0e4d0363d9 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 1:4e0e4d0363d9 61 }