Severin Kiefer
/
PM2_Example_IRSensor
Workshop 1
main.cpp
- Committer:
- pmic
- Date:
- 2021-03-13
- Revision:
- 5:887081decd5c
- Parent:
- 4:dcdcb25d1069
- Child:
- 6:6c1c38d4faa4
File content as of revision 5:887081decd5c:
#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); BufferedSerial pc(USBTX, USBRX); bool executeMainTask = false; Timer user_button_timer, loop_timer; int Ts_ms = 1000; 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); AnalogIn analogIn(PA_0); float dist = 0.0f; int main() { // pc.baud(115200); user_button.fall(&button_fall); user_button.rise(&button_rise); loop_timer.reset(); while (true) { /* ------------- 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); 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); s.reset(); char msg[] = "Hello World - buffered\n"; 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); } else { } /* ------------- stop hacking ------------- -------------*/ if(executeMainTask) { led = !led; } int dT_loop = Ts_ms - duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); thread_sleep_for(dT_loop); } } void button_fall() { user_button_timer.reset(); user_button_timer.start(); } void button_rise() { 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); } }