Accelerometer simulator

Dependencies:   mbed

Revision:
2:fbb699c3994f
Parent:
1:a31564d0b86c
Child:
3:b87d5296015c
--- a/main.cpp	Tue Oct 20 14:40:30 2020 +0000
+++ b/main.cpp	Tue Nov 03 15:23:36 2020 +0000
@@ -1,31 +1,58 @@
 #include "mbed.h"
+#include <stdlib.h>
 
-Serial pc(PA_2,PA_3,115200);
+union MYFLOAT {
+    float value;
+    uint8_t byte[4];
+} example,sampPer;
+
+union MYINT {
+    uint16_t value;
+    uint8_t byte[2];
+} nSamples;
+
+Serial pc(PA_2,PA_3,921600);
 DigitalIn myBtn(PC_13);
 Timer timer;
 // Clock frequency 84MHz 12ns Instruction clock
-unsigned int nSamples,cnt;
-float sampPer,elapTime;
+unsigned int cnt;
+float elapTime,value = 17.1234567;
 
 int main()
 {
-    pc.printf("Press the user button to start\r\n");
+    /*    example.value = 2.343456795e1;
+        pc.printf("The value is %.9f.\r\n",example.value);
+    //    pc.printf("The value is ");
+        for(int j=0;j<10;j++){
+            for(int i=0;i<4;i++){
+                pc.putc(example.byte[i]);
+            }
+            example.value = example.value + 1.0f;
+        }
+    //    pc.printf("\r\n");
+    */
     while(myBtn);
     while(!myBtn);
-    pc.printf("Good Job!!!\r\n");
-    timer.start();
-        while(1) {
-        pc.printf("How many samples do you desire? ");
-        pc.scanf("%u",&nSamples);
-        pc.printf("\r\nEntered %d.\r\n",nSamples);
-        pc.printf("Please, provide the sampling period [s] ");
-        pc.scanf("%f",&sampPer);
-        pc.printf("\r\nEntered %f.\r\n",sampPer);
-        for(cnt=0; cnt<nSamples; cnt++) {
+    pc.printf("Hello world!!!\r\n");
+//    timer.start();
+    while(1) {
+        for(cnt=0; cnt<2; cnt++)
+            nSamples.byte[cnt] = pc.getc();
+//        pc.scanf("%u",&nSamples);
+        pc.printf("Entered %d.\r\n",nSamples.value);
+//        pc.printf("Please, provide the sampling period [s] ");
+        for(cnt=0; cnt<4; cnt++)
+            sampPer.byte[cnt] = pc.getc();
+//        pc.scanf("%f",&sampPer);
+        pc.printf("Entered %e.\r\n",sampPer.value);
+        for(cnt=0; cnt<nSamples.value; cnt++) {
             timer.reset();
-            pc.printf("Value: %u\r\n",cnt);
+            example.value = value + (float)(rand()%100)/100;
+            for(int i=0; i<4; i++) {
+                pc.putc(example.byte[i]);
+            }
             elapTime = timer.read();
-            wait(sampPer - elapTime);
+            wait(sampPer.value - elapTime);
         }
     }
 }
\ No newline at end of file