Severin Kiefer
/
PM2_Example_IRSensor
Workshop 1
Diff: main.cpp
- Revision:
- 6:6c1c38d4faa4
- Parent:
- 5:887081decd5c
- Child:
- 7:dc463bf54be6
--- a/main.cpp Sat Mar 13 15:21:09 2021 +0100 +++ b/main.cpp Sat Mar 13 16:46:56 2021 +0100 @@ -1,97 +1,67 @@ #include "mbed.h" #include "platform/mbed_thread.h" -#include "iostream" #include "string" -#include "BufferedSerial.h" -#include <math.h> -#include <stdio.h> - -#define pi 3.14159265358979323846 using namespace std::chrono; -InterruptIn user_button(USER_BUTTON); -DigitalOut led(LED1); +InterruptIn user_button(USER_BUTTON); +DigitalOut led(LED1); BufferedSerial pc(USBTX, USBRX); bool executeMainTask = false; Timer user_button_timer, loop_timer; -int Ts_ms = 1000; +int Ts_ms = 50; -void button_fall(); // stuff -void button_rise(); // stuff - -void reverse(char* str, int len); -int intToStr(int x, char str[], int d); -void ftoa(float n, char* res, int afterpoint); +void button_fall(); +void button_rise(); AnalogIn analogIn(PA_0); -float dist = 0.0f; +float dist = 0.0f; + +float floatNumber = 0.003713f; +int integerNumber = 777; int main() { - // pc.baud(115200); user_button.fall(&button_fall); user_button.rise(&button_rise); - loop_timer.reset(); + loop_timer.start(); while (true) { + loop_timer.reset(); + /* ------------- start hacking ------------- -------------*/ if(executeMainTask) { - // dist = analogIn.read()*3.3f; - // printf("measurement: %d\r\n", (static_cast<int>(dist * 1000))); - Timer s; - - s.start(); - char res0[20]; - float val0 = 233.007; - ftoa(val0, res0, 4); - pc.write(res0, sizeof(res0)); - int buffered_time_ftoa = duration_cast<milliseconds>(s.elapsed_time()).count(); - thread_sleep_for(100); + dist = analogIn.read()*3.3f; - s.reset(); - char res1[20]; - float val1 = 233.007; - ftoa(val1, res1, 4); - printf("%s\r\n", res1); - int polled_time_ftoa = duration_cast<milliseconds>(s.elapsed_time()).count(); - thread_sleep_for(100); + // printf("Measval: %d\r\n", (static_cast<int>(dist * 1000))); - s.reset(); - char msg[] = "Hello World - buffered\n"; + string msg_str = to_string((static_cast<int>(dist * 1e6))); + msg_str.append(";"); + msg_str.append(to_string((static_cast<int>(floatNumber * 1e6)))); + msg_str.append(";"); + msg_str.append(to_string(integerNumber)); + msg_str.append(";\r\n"); + char msg[msg_str.length() + 1]; + strcpy(msg, msg_str.c_str()); pc.write(msg, sizeof(msg)); - int buffered_time = duration_cast<milliseconds>(s.elapsed_time()).count(); - thread_sleep_for(100); - - s.reset(); - printf("Hello World - blocking\n"); - int polled_time = duration_cast<milliseconds>(s.elapsed_time()).count(); - s.stop(); - thread_sleep_for(100); - - printf("printf buffered took %d us\n", buffered_time_ftoa); - printf("printf blocking took %d us\n", polled_time_ftoa); - printf("printf buffered took %d us\n", buffered_time); - printf("printf blocking took %d us\n", polled_time); - thread_sleep_for(100); + /* visual feedback that the main task is executed */ + led = !led; - } else { - + led = 0; } /* ------------- stop hacking ------------- -------------*/ - if(executeMainTask) { - led = !led; - } - int dT_loop = Ts_ms - duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); - thread_sleep_for(dT_loop); + int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); + int dT_loop_ms = Ts_ms - T_loop_ms; + printf("T_loop_ms: %d\r\n", dT_loop_ms); + thread_sleep_for(500); } } @@ -106,58 +76,4 @@ int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); user_button_timer.stop(); if(t_button > 200) executeMainTask = !executeMainTask; -} - -void reverse(char* str, int len) -{ - int i = 0, j = len - 1, temp; - while (i < j) { - temp = str[i]; - str[i] = str[j]; - str[j] = temp; - i++; - j--; - } -} - -int intToStr(int x, char str[], int d) -{ - int i = 0; - while (x) { - str[i++] = (x % 10) + '0'; - x = x / 10; - } - - // If number of digits required is more, then - // add 0s at the beginning - while (i < d) - str[i++] = '0'; - - reverse(str, i); - str[i] = '\0'; - return i; -} - -void ftoa(float n, char* res, int afterpoint) -{ - // Extract integer part - int ipart = (int)n; - - // Extract floating part - float fpart = n - (float)ipart; - - // convert integer part to string - int i = intToStr(ipart, res, 0); - - // check for display option after point - if (afterpoint != 0) { - res[i] = '.'; // add dot - - // Get the value of fraction part upto given no. - // of points after dot. The third parameter - // is needed to handle cases like 233.007 - fpart = fpart * pow(10, afterpoint); - - intToStr((int)fpart, res + i + 1, afterpoint); - } -} \ No newline at end of file +} \ No newline at end of file