Hello there

Dependencies:   PM2_Libary

Committer:
pmic
Date:
Sat Mar 13 13:49:08 2021 +0100
Revision:
4:dcdcb25d1069
Parent:
3:aa1d854807fe
Child:
5:887081decd5c
Example of BufferedSerial and time difference.

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 4:dcdcb25d1069 4 #include "BufferedSerial.h"
pmic 4:dcdcb25d1069 5
pmic 1:4e0e4d0363d9 6 #define pi 3.14159265358979323846
pmic 1:4e0e4d0363d9 7
pmic 3:aa1d854807fe 8 using namespace std::chrono;
pmic 3:aa1d854807fe 9
pmic 1:4e0e4d0363d9 10 InterruptIn user_button(USER_BUTTON);
pmic 1:4e0e4d0363d9 11 DigitalOut led(LED1);
pmic 3:aa1d854807fe 12 // Serial pc(SERIAL_TX, SERIAL_RX);
pmic 4:dcdcb25d1069 13 BufferedSerial pc(USBTX, USBRX);
boro 0:5d4d21d56334 14
pmic 1:4e0e4d0363d9 15 bool executeMainTask = false;
pmic 1:4e0e4d0363d9 16 Timer user_button_timer, loop_timer;
pmic 4:dcdcb25d1069 17 int Ts_ms = 1000;
boro 0:5d4d21d56334 18
pmic 2:4ba1937ce284 19 void button_fall(); // stuff
pmic 2:4ba1937ce284 20 void button_rise(); // stuff
boro 0:5d4d21d56334 21
pmic 1:4e0e4d0363d9 22 AnalogIn analogIn(PA_0);
pmic 1:4e0e4d0363d9 23 float dist = 0.0f;
boro 0:5d4d21d56334 24
boro 0:5d4d21d56334 25 int main()
boro 0:5d4d21d56334 26 {
pmic 3:aa1d854807fe 27 // pc.baud(115200);
pmic 1:4e0e4d0363d9 28 user_button.fall(&button_fall);
pmic 1:4e0e4d0363d9 29 user_button.rise(&button_rise);
pmic 1:4e0e4d0363d9 30 loop_timer.reset();
boro 0:5d4d21d56334 31
boro 0:5d4d21d56334 32 while (true) {
pmic 1:4e0e4d0363d9 33
pmic 1:4e0e4d0363d9 34 /* ------------- start hacking ------------- -------------*/
boro 0:5d4d21d56334 35
pmic 1:4e0e4d0363d9 36 if(executeMainTask) {
pmic 4:dcdcb25d1069 37 // dist = analogIn.read()*3.3f;
pmic 4:dcdcb25d1069 38 // printf("measurement: %d\r\n", (static_cast<int>(dist * 1000)));
pmic 4:dcdcb25d1069 39
pmic 4:dcdcb25d1069 40 Timer s;
pmic 4:dcdcb25d1069 41
pmic 4:dcdcb25d1069 42 s.start();
pmic 4:dcdcb25d1069 43 char msg[] = "Hello World - buffered\n";
pmic 4:dcdcb25d1069 44 pc.write(msg, sizeof(msg));
pmic 4:dcdcb25d1069 45 int buffered_time = duration_cast<milliseconds>(s.elapsed_time()).count();
pmic 4:dcdcb25d1069 46 thread_sleep_for(100);; // give time for the buffer to empty
pmic 4:dcdcb25d1069 47
pmic 4:dcdcb25d1069 48 s.reset();
pmic 4:dcdcb25d1069 49 printf("Hello World - blocking\n");
pmic 4:dcdcb25d1069 50 int polled_time = duration_cast<milliseconds>(s.elapsed_time()).count();
pmic 4:dcdcb25d1069 51 s.stop();
pmic 4:dcdcb25d1069 52 thread_sleep_for(100);; // give time for the buffer to empty
pmic 4:dcdcb25d1069 53
pmic 4:dcdcb25d1069 54 printf("printf buffered took %d us\n", buffered_time);
pmic 4:dcdcb25d1069 55 printf("printf blocking took %d us\n", polled_time);
pmic 4:dcdcb25d1069 56 thread_sleep_for(100);; // give time for the buffer to empty
pmic 4:dcdcb25d1069 57
pmic 4:dcdcb25d1069 58
pmic 4:dcdcb25d1069 59
boro 0:5d4d21d56334 60 } else {
boro 0:5d4d21d56334 61
boro 0:5d4d21d56334 62 }
pmic 1:4e0e4d0363d9 63
pmic 1:4e0e4d0363d9 64 /* ------------- stop hacking ------------- -------------*/
pmic 1:4e0e4d0363d9 65
pmic 1:4e0e4d0363d9 66 if(executeMainTask) {
pmic 1:4e0e4d0363d9 67 led = !led;
pmic 1:4e0e4d0363d9 68 }
pmic 3:aa1d854807fe 69 int dT_loop = Ts_ms - duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 70 thread_sleep_for(dT_loop);
boro 0:5d4d21d56334 71 }
boro 0:5d4d21d56334 72 }
pmic 1:4e0e4d0363d9 73
pmic 1:4e0e4d0363d9 74 void button_fall()
pmic 1:4e0e4d0363d9 75 {
pmic 1:4e0e4d0363d9 76 user_button_timer.reset();
pmic 1:4e0e4d0363d9 77 user_button_timer.start();
pmic 1:4e0e4d0363d9 78 }
pmic 1:4e0e4d0363d9 79
pmic 1:4e0e4d0363d9 80 void button_rise()
pmic 1:4e0e4d0363d9 81 {
pmic 3:aa1d854807fe 82 int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 83 user_button_timer.stop();
pmic 1:4e0e4d0363d9 84 if(t_button > 200) executeMainTask = !executeMainTask;
pmic 1:4e0e4d0363d9 85 }