Hello there

Dependencies:   PM2_Libary

Revision:
5:887081decd5c
Parent:
4:dcdcb25d1069
Child:
6:6c1c38d4faa4
--- a/main.cpp	Sat Mar 13 13:49:08 2021 +0100
+++ b/main.cpp	Sat Mar 13 15:21:09 2021 +0100
@@ -1,16 +1,18 @@
 #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);
-// Serial       pc(SERIAL_TX, SERIAL_RX);
- BufferedSerial pc(USBTX, USBRX);
+InterruptIn    user_button(USER_BUTTON);
+DigitalOut     led(LED1);
+BufferedSerial pc(USBTX, USBRX);
 
 bool         executeMainTask = false;
 Timer        user_button_timer, loop_timer;
@@ -19,6 +21,10 @@
 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;
 
@@ -38,22 +44,40 @@
             // 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.start();
+            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);; // give time for the buffer to empty
+            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);; // give time for the buffer to empty
+            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);; // give time for the buffer to empty
+            thread_sleep_for(100);
 
 
             
@@ -82,4 +106,58 @@
     int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count();
     user_button_timer.stop();
     if(t_button > 200) executeMainTask = !executeMainTask;
-}
\ No newline at end of file
+}
+
+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