Hello there

Dependencies:   PM2_Libary

Committer:
pmic
Date:
Sat Mar 13 15:21:09 2021 +0100
Revision:
5:887081decd5c
Parent:
4:dcdcb25d1069
Child:
6:6c1c38d4faa4
Added various tests for pc.printf with Buffered Serial.

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 "iostream"
pmic 5:887081decd5c 4 #include "string"
pmic 4:dcdcb25d1069 5 #include "BufferedSerial.h"
pmic 5:887081decd5c 6 #include <math.h>
pmic 5:887081decd5c 7 #include <stdio.h>
pmic 4:dcdcb25d1069 8
pmic 1:4e0e4d0363d9 9 #define pi 3.14159265358979323846
pmic 1:4e0e4d0363d9 10
pmic 3:aa1d854807fe 11 using namespace std::chrono;
pmic 3:aa1d854807fe 12
pmic 5:887081decd5c 13 InterruptIn user_button(USER_BUTTON);
pmic 5:887081decd5c 14 DigitalOut led(LED1);
pmic 5:887081decd5c 15 BufferedSerial pc(USBTX, USBRX);
boro 0:5d4d21d56334 16
pmic 1:4e0e4d0363d9 17 bool executeMainTask = false;
pmic 1:4e0e4d0363d9 18 Timer user_button_timer, loop_timer;
pmic 4:dcdcb25d1069 19 int Ts_ms = 1000;
boro 0:5d4d21d56334 20
pmic 2:4ba1937ce284 21 void button_fall(); // stuff
pmic 2:4ba1937ce284 22 void button_rise(); // stuff
boro 0:5d4d21d56334 23
pmic 5:887081decd5c 24 void reverse(char* str, int len);
pmic 5:887081decd5c 25 int intToStr(int x, char str[], int d);
pmic 5:887081decd5c 26 void ftoa(float n, char* res, int afterpoint);
pmic 5:887081decd5c 27
pmic 1:4e0e4d0363d9 28 AnalogIn analogIn(PA_0);
pmic 1:4e0e4d0363d9 29 float dist = 0.0f;
boro 0:5d4d21d56334 30
boro 0:5d4d21d56334 31 int main()
boro 0:5d4d21d56334 32 {
pmic 3:aa1d854807fe 33 // pc.baud(115200);
pmic 1:4e0e4d0363d9 34 user_button.fall(&button_fall);
pmic 1:4e0e4d0363d9 35 user_button.rise(&button_rise);
pmic 1:4e0e4d0363d9 36 loop_timer.reset();
boro 0:5d4d21d56334 37
boro 0:5d4d21d56334 38 while (true) {
pmic 1:4e0e4d0363d9 39
pmic 1:4e0e4d0363d9 40 /* ------------- start hacking ------------- -------------*/
boro 0:5d4d21d56334 41
pmic 1:4e0e4d0363d9 42 if(executeMainTask) {
pmic 4:dcdcb25d1069 43 // dist = analogIn.read()*3.3f;
pmic 4:dcdcb25d1069 44 // printf("measurement: %d\r\n", (static_cast<int>(dist * 1000)));
pmic 4:dcdcb25d1069 45
pmic 4:dcdcb25d1069 46 Timer s;
pmic 5:887081decd5c 47
pmic 5:887081decd5c 48 s.start();
pmic 5:887081decd5c 49 char res0[20];
pmic 5:887081decd5c 50 float val0 = 233.007;
pmic 5:887081decd5c 51 ftoa(val0, res0, 4);
pmic 5:887081decd5c 52 pc.write(res0, sizeof(res0));
pmic 5:887081decd5c 53 int buffered_time_ftoa = duration_cast<milliseconds>(s.elapsed_time()).count();
pmic 5:887081decd5c 54 thread_sleep_for(100);
pmic 4:dcdcb25d1069 55
pmic 5:887081decd5c 56 s.reset();
pmic 5:887081decd5c 57 char res1[20];
pmic 5:887081decd5c 58 float val1 = 233.007;
pmic 5:887081decd5c 59 ftoa(val1, res1, 4);
pmic 5:887081decd5c 60 printf("%s\r\n", res1);
pmic 5:887081decd5c 61 int polled_time_ftoa = duration_cast<milliseconds>(s.elapsed_time()).count();
pmic 5:887081decd5c 62 thread_sleep_for(100);
pmic 5:887081decd5c 63
pmic 5:887081decd5c 64 s.reset();
pmic 4:dcdcb25d1069 65 char msg[] = "Hello World - buffered\n";
pmic 4:dcdcb25d1069 66 pc.write(msg, sizeof(msg));
pmic 4:dcdcb25d1069 67 int buffered_time = duration_cast<milliseconds>(s.elapsed_time()).count();
pmic 5:887081decd5c 68 thread_sleep_for(100);
pmic 4:dcdcb25d1069 69
pmic 4:dcdcb25d1069 70 s.reset();
pmic 4:dcdcb25d1069 71 printf("Hello World - blocking\n");
pmic 4:dcdcb25d1069 72 int polled_time = duration_cast<milliseconds>(s.elapsed_time()).count();
pmic 4:dcdcb25d1069 73 s.stop();
pmic 5:887081decd5c 74 thread_sleep_for(100);
pmic 4:dcdcb25d1069 75
pmic 5:887081decd5c 76 printf("printf buffered took %d us\n", buffered_time_ftoa);
pmic 5:887081decd5c 77 printf("printf blocking took %d us\n", polled_time_ftoa);
pmic 4:dcdcb25d1069 78 printf("printf buffered took %d us\n", buffered_time);
pmic 4:dcdcb25d1069 79 printf("printf blocking took %d us\n", polled_time);
pmic 5:887081decd5c 80 thread_sleep_for(100);
pmic 4:dcdcb25d1069 81
pmic 4:dcdcb25d1069 82
pmic 4:dcdcb25d1069 83
boro 0:5d4d21d56334 84 } else {
boro 0:5d4d21d56334 85
boro 0:5d4d21d56334 86 }
pmic 1:4e0e4d0363d9 87
pmic 1:4e0e4d0363d9 88 /* ------------- stop hacking ------------- -------------*/
pmic 1:4e0e4d0363d9 89
pmic 1:4e0e4d0363d9 90 if(executeMainTask) {
pmic 1:4e0e4d0363d9 91 led = !led;
pmic 1:4e0e4d0363d9 92 }
pmic 3:aa1d854807fe 93 int dT_loop = Ts_ms - duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 94 thread_sleep_for(dT_loop);
boro 0:5d4d21d56334 95 }
boro 0:5d4d21d56334 96 }
pmic 1:4e0e4d0363d9 97
pmic 1:4e0e4d0363d9 98 void button_fall()
pmic 1:4e0e4d0363d9 99 {
pmic 1:4e0e4d0363d9 100 user_button_timer.reset();
pmic 1:4e0e4d0363d9 101 user_button_timer.start();
pmic 1:4e0e4d0363d9 102 }
pmic 1:4e0e4d0363d9 103
pmic 1:4e0e4d0363d9 104 void button_rise()
pmic 1:4e0e4d0363d9 105 {
pmic 3:aa1d854807fe 106 int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 107 user_button_timer.stop();
pmic 1:4e0e4d0363d9 108 if(t_button > 200) executeMainTask = !executeMainTask;
pmic 5:887081decd5c 109 }
pmic 5:887081decd5c 110
pmic 5:887081decd5c 111 void reverse(char* str, int len)
pmic 5:887081decd5c 112 {
pmic 5:887081decd5c 113 int i = 0, j = len - 1, temp;
pmic 5:887081decd5c 114 while (i < j) {
pmic 5:887081decd5c 115 temp = str[i];
pmic 5:887081decd5c 116 str[i] = str[j];
pmic 5:887081decd5c 117 str[j] = temp;
pmic 5:887081decd5c 118 i++;
pmic 5:887081decd5c 119 j--;
pmic 5:887081decd5c 120 }
pmic 5:887081decd5c 121 }
pmic 5:887081decd5c 122
pmic 5:887081decd5c 123 int intToStr(int x, char str[], int d)
pmic 5:887081decd5c 124 {
pmic 5:887081decd5c 125 int i = 0;
pmic 5:887081decd5c 126 while (x) {
pmic 5:887081decd5c 127 str[i++] = (x % 10) + '0';
pmic 5:887081decd5c 128 x = x / 10;
pmic 5:887081decd5c 129 }
pmic 5:887081decd5c 130
pmic 5:887081decd5c 131 // If number of digits required is more, then
pmic 5:887081decd5c 132 // add 0s at the beginning
pmic 5:887081decd5c 133 while (i < d)
pmic 5:887081decd5c 134 str[i++] = '0';
pmic 5:887081decd5c 135
pmic 5:887081decd5c 136 reverse(str, i);
pmic 5:887081decd5c 137 str[i] = '\0';
pmic 5:887081decd5c 138 return i;
pmic 5:887081decd5c 139 }
pmic 5:887081decd5c 140
pmic 5:887081decd5c 141 void ftoa(float n, char* res, int afterpoint)
pmic 5:887081decd5c 142 {
pmic 5:887081decd5c 143 // Extract integer part
pmic 5:887081decd5c 144 int ipart = (int)n;
pmic 5:887081decd5c 145
pmic 5:887081decd5c 146 // Extract floating part
pmic 5:887081decd5c 147 float fpart = n - (float)ipart;
pmic 5:887081decd5c 148
pmic 5:887081decd5c 149 // convert integer part to string
pmic 5:887081decd5c 150 int i = intToStr(ipart, res, 0);
pmic 5:887081decd5c 151
pmic 5:887081decd5c 152 // check for display option after point
pmic 5:887081decd5c 153 if (afterpoint != 0) {
pmic 5:887081decd5c 154 res[i] = '.'; // add dot
pmic 5:887081decd5c 155
pmic 5:887081decd5c 156 // Get the value of fraction part upto given no.
pmic 5:887081decd5c 157 // of points after dot. The third parameter
pmic 5:887081decd5c 158 // is needed to handle cases like 233.007
pmic 5:887081decd5c 159 fpart = fpart * pow(10, afterpoint);
pmic 5:887081decd5c 160
pmic 5:887081decd5c 161 intToStr((int)fpart, res + i + 1, afterpoint);
pmic 5:887081decd5c 162 }
pmic 5:887081decd5c 163 }