Senso_3

Committer:
pmic
Date:
Sat Mar 13 17:34:29 2021 +0100
Revision:
9:c5e1e1facb02
Parent:
8:49f2b6a515ac
Child:
10:f459b443f676
Example for students with SerialBuffer class.

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 9:c5e1e1facb02 3 #include "string"
pmic 9:c5e1e1facb02 4
pmic 3:aa1d854807fe 5 using namespace std::chrono;
pmic 9:c5e1e1facb02 6
pmic 7:dc463bf54be6 7 InterruptIn user_button(USER_BUTTON);
pmic 7:dc463bf54be6 8 DigitalOut led(LED1);
pmic 9:c5e1e1facb02 9 BufferedSerial pc(USBTX, USBRX);
pmic 9:c5e1e1facb02 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;
pmic 9:c5e1e1facb02 14
pmic 7:dc463bf54be6 15 void button_fall();
pmic 7:dc463bf54be6 16 void button_rise();
pmic 5:887081decd5c 17
pmic 9:c5e1e1facb02 18 /* input your stuff here */
pmic 7:dc463bf54be6 19 AnalogIn analogIn(PA_0);
pmic 7:dc463bf54be6 20 float dist = 0.0f;
pmic 6:6c1c38d4faa4 21
pmic 9:c5e1e1facb02 22 /* those numbers are just for testing the SerialBuffer class */
pmic 9:c5e1e1facb02 23 float floatNumber = 0.003713f;
pmic 9:c5e1e1facb02 24 int integerNumber = 777;
pmic 9:c5e1e1facb02 25
boro 0:5d4d21d56334 26 int main()
boro 0:5d4d21d56334 27 {
pmic 1:4e0e4d0363d9 28 user_button.fall(&button_fall);
pmic 1:4e0e4d0363d9 29 user_button.rise(&button_rise);
pmic 6:6c1c38d4faa4 30 loop_timer.start();
pmic 9:c5e1e1facb02 31
boro 0:5d4d21d56334 32 while (true) {
pmic 9:c5e1e1facb02 33
pmic 6:6c1c38d4faa4 34 loop_timer.reset();
pmic 9:c5e1e1facb02 35
pmic 1:4e0e4d0363d9 36 /* ------------- start hacking ------------- -------------*/
pmic 9:c5e1e1facb02 37
pmic 1:4e0e4d0363d9 38 if(executeMainTask) {
pmic 9:c5e1e1facb02 39
pmic 9:c5e1e1facb02 40 dist = analogIn.read()*3.3f;
pmic 4:dcdcb25d1069 41
pmic 9:c5e1e1facb02 42 /* do only output what's really necessary, outputting "Measured value in mV: "" within the loop is no good solution */
pmic 9:c5e1e1facb02 43 // printf("Measured value in mV: %d\r\n", (static_cast<int>(dist * 1e3)));
pmic 9:c5e1e1facb02 44
pmic 9:c5e1e1facb02 45 /* using the BufferedSerial class as an example */
pmic 9:c5e1e1facb02 46 string msg_str = "Measured value in mV: ";
pmic 9:c5e1e1facb02 47 msg_str.append(to_string((static_cast<int>(dist * 1e3))));
pmic 9:c5e1e1facb02 48 msg_str.append(";");
pmic 9:c5e1e1facb02 49 msg_str.append(to_string((static_cast<int>(floatNumber * 1e6))));
pmic 9:c5e1e1facb02 50 msg_str.append(";");
pmic 9:c5e1e1facb02 51 msg_str.append(to_string(integerNumber));
pmic 9:c5e1e1facb02 52 msg_str.append(";\r\n");
pmic 9:c5e1e1facb02 53 char msg[msg_str.length() + 1];
pmic 9:c5e1e1facb02 54 strcpy(msg, msg_str.c_str());
pmic 9:c5e1e1facb02 55 pc.write(msg, sizeof(msg));
pmic 4:dcdcb25d1069 56
pmic 6:6c1c38d4faa4 57 /* visual feedback that the main task is executed */
pmic 6:6c1c38d4faa4 58 led = !led;
pmic 9:c5e1e1facb02 59
boro 0:5d4d21d56334 60 } else {
pmic 6:6c1c38d4faa4 61 led = 0;
boro 0:5d4d21d56334 62 }
pmic 9:c5e1e1facb02 63
pmic 1:4e0e4d0363d9 64 /* ------------- stop hacking ------------- -------------*/
pmic 9:c5e1e1facb02 65
pmic 6:6c1c38d4faa4 66 int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
pmic 6:6c1c38d4faa4 67 int dT_loop_ms = Ts_ms - T_loop_ms;
pmic 7:dc463bf54be6 68 thread_sleep_for(dT_loop_ms);
boro 0:5d4d21d56334 69 }
boro 0:5d4d21d56334 70 }
pmic 9:c5e1e1facb02 71
pmic 1:4e0e4d0363d9 72 void button_fall()
pmic 1:4e0e4d0363d9 73 {
pmic 1:4e0e4d0363d9 74 user_button_timer.reset();
pmic 1:4e0e4d0363d9 75 user_button_timer.start();
pmic 1:4e0e4d0363d9 76 }
pmic 9:c5e1e1facb02 77
pmic 1:4e0e4d0363d9 78 void button_rise()
pmic 1:4e0e4d0363d9 79 {
pmic 3:aa1d854807fe 80 int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 81 user_button_timer.stop();
pmic 1:4e0e4d0363d9 82 if(t_button > 200) executeMainTask = !executeMainTask;
pmic 6:6c1c38d4faa4 83 }