Tetsuya Yamamoto / Mbed 2 deprecated MD-Tutorial

Dependencies:   mbed

Revision:
7:0bc2bc07f2fe
Parent:
6:17e3a28338dc
--- a/main.cpp	Sat Nov 28 07:48:16 2020 +0000
+++ b/main.cpp	Wed Dec 02 08:06:45 2020 +0000
@@ -190,81 +190,31 @@
     }
 }
 
-
-/*
-int head;
-char bytes[5];
-
-void control_rx()
-{
-
-    while(serial.readable()) {
-        char c = serial.getc();
-        if(c == 0x3A) {
-            head = 0;
-        }
-        if(head < 5 && head != -1) {
-            bytes[head] = c;
-            head++;
-        } else {
-            printf("error: failed get data[head:%d]\n\r",head);
-            head = -1;
-            break;
-        }
-        if(head == 5) {
-            if(bytes[0] == 0x3A) {
-                //  cast float to double
-                uint16_t uintX = ((bytes[1] << 8 | bytes[2]) & 0xffff);
-                uint16_t uintY = ((bytes[3] << 8 | bytes[4]) & 0xffff);
-                double formatX = (double)uintX / 65536;
-                double formatY = (double)uintY / 65536;
-                //  save
-                x = formatX;
-                y = formatY;
-
-                //  reset connect timer
-                connectTimer.reset();
-                break;
-            }else{
-                head = -1;
-                break;
-            }
-        }
-    }
-}*/
-
 void load_data()
 {
-    /*
-    //  buffer check
-    int buffer_length = sizeof(_buffer_serial._rx_buffer) / sizeof(_buffer_serial._rx_buffer[0]);
-    for(int i = 0; i <= buffer_length; i++) {
-        printf("%x ",_buffer_serial._rx_buffer[i]);
+    int size = 32;
+    char bytes[size];
+    size_t index = _buffer_serial.readBytesUntil('\n',(char*)bytes,size);
+    int startIndex = index - 5;
+    
+    if(size - 5 < startIndex){
+        printf("index error\n\r");
     }
-    printf("\n\r");
-    */
-    char bytes[6];
-    size_t index = _buffer_serial.readBytesUntil('\n',(char*)bytes,6);
-    if(index != 5){
-        //printf("e:index %d\n\r",index);
-        return;
-    }
-    if(bytes[0] == 0x3A) {
+    if(bytes[startIndex] == 0x3A) {
         //  cast float to double
-        uint16_t uintX = ((bytes[1] << 8 | bytes[2]) & 0xffff);
-        uint16_t uintY = ((bytes[3] << 8 | bytes[4]) & 0xffff);
+        uint16_t uintX = ((bytes[startIndex + 1] << 8 | bytes[startIndex + 2]) & 0xffff);
+        uint16_t uintY = ((bytes[startIndex + 3] << 8 | bytes[startIndex + 4]) & 0xffff);
         double formatX = (double)uintX / 65536;
         double formatY = (double)uintY / 65536;
         //  save
         x = formatX;
         y = formatY;
-
-        //printf("%lf %lf\n\r",formatX,formatY);
-        printf("%x %x %x %x %x %x %lf %lf\n\r",bytes[0],bytes[1],bytes[2],bytes[3],bytes[4],bytes[5],formatX,formatY);
+        
+        printf("%lf %lf\n\r",formatX,formatY);
         //  reset connect timer
         connectTimer.reset();
 
     } else {
-        //printf("failed load_data()\n\r");
+        printf("error\n\r");
     }
 }