Our Programm to build up from the PES Board Example

Dependencies:   PM2_Libary

Committer:
pmic
Date:
Sat Mar 13 16:46:56 2021 +0100
Revision:
6:6c1c38d4faa4
Parent:
5:887081decd5c
Child:
7:dc463bf54be6
Get BufferedSerial for several int values running.

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 5:887081decd5c 3 #include "string"
pmic 1:4e0e4d0363d9 4
pmic 3:aa1d854807fe 5 using namespace std::chrono;
pmic 3:aa1d854807fe 6
pmic 6:6c1c38d4faa4 7 InterruptIn user_button(USER_BUTTON);
pmic 6:6c1c38d4faa4 8 DigitalOut led(LED1);
pmic 5:887081decd5c 9 BufferedSerial pc(USBTX, USBRX);
boro 0:5d4d21d56334 10
pmic 1:4e0e4d0363d9 11 bool executeMainTask = false;
pmic 1:4e0e4d0363d9 12 Timer user_button_timer, loop_timer;
pmic 6:6c1c38d4faa4 13 int Ts_ms = 50;
boro 0:5d4d21d56334 14
pmic 6:6c1c38d4faa4 15 void button_fall();
pmic 6:6c1c38d4faa4 16 void button_rise();
pmic 5:887081decd5c 17
pmic 1:4e0e4d0363d9 18 AnalogIn analogIn(PA_0);
pmic 6:6c1c38d4faa4 19 float dist = 0.0f;
pmic 6:6c1c38d4faa4 20
pmic 6:6c1c38d4faa4 21 float floatNumber = 0.003713f;
pmic 6:6c1c38d4faa4 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 6:6c1c38d4faa4 63 printf("T_loop_ms: %d\r\n", dT_loop_ms);
pmic 6:6c1c38d4faa4 64 thread_sleep_for(500);
boro 0:5d4d21d56334 65 }
boro 0:5d4d21d56334 66 }
pmic 1:4e0e4d0363d9 67
pmic 1:4e0e4d0363d9 68 void button_fall()
pmic 1:4e0e4d0363d9 69 {
pmic 1:4e0e4d0363d9 70 user_button_timer.reset();
pmic 1:4e0e4d0363d9 71 user_button_timer.start();
pmic 1:4e0e4d0363d9 72 }
pmic 1:4e0e4d0363d9 73
pmic 1:4e0e4d0363d9 74 void button_rise()
pmic 1:4e0e4d0363d9 75 {
pmic 3:aa1d854807fe 76 int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 77 user_button_timer.stop();
pmic 1:4e0e4d0363d9 78 if(t_button > 200) executeMainTask = !executeMainTask;
pmic 6:6c1c38d4faa4 79 }