Tetsuya Yamamoto / Mbed 2 deprecated MD-Tutorial

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
tetsu_0207
Date:
Wed Dec 02 08:06:45 2020 +0000
Parent:
6:17e3a28338dc
Commit message:
[Debug] success buffer serial library

Changed in this revision

BufferSerial.cpp Show annotated file Show diff for this revision Revisions of this file
BufferSerial.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/BufferSerial.cpp	Sat Nov 28 07:48:16 2020 +0000
+++ b/BufferSerial.cpp	Wed Dec 02 08:06:45 2020 +0000
@@ -1,7 +1,5 @@
 #include "BufferSerial.h"
 
-Serial pc(USBTX, USBRX);
-
 BufferSerial::BufferSerial(PinName tx, PinName rx,int baud,int timeout)
     :RawSerial(tx,rx,baud)
 {
@@ -36,9 +34,7 @@
     size_t index = 0;
     while (index < length) {
         int c = timedRead();
-        //pc.printf("debug_c: %x\n\r",c);
         if (c < 0 || c == charactor){
-            //pc.printf("break index:%d \n\r",index);
             break;
         }
         *buffer++ = (char)c;
@@ -55,7 +51,6 @@
         if (i != _rx_buffer_tail) {
             _rx_buffer[_rx_buffer_head] = c;
             _rx_buffer_head = i;
-            //pc.printf("%x\n\r",c);
         }
     }
 }
@@ -67,7 +62,6 @@
     _startMillis = timer.read_ms();
     do {
         c = read();
-        pc.printf("tr: %x \n\r",c);
         if (c >= 0) return c;
     } while(timer.read_ms() - _startMillis < _timeout);
     return -1;     // -1 indicates timeout
--- a/BufferSerial.h	Sat Nov 28 07:48:16 2020 +0000
+++ b/BufferSerial.h	Wed Dec 02 08:06:45 2020 +0000
@@ -6,7 +6,7 @@
 
 #ifndef SERIAL_RX_BUFFER_SIZE
 //  RX Buffer Max Size
-#define SERIAL_RX_BUFFER_SIZE 128
+#define SERIAL_RX_BUFFER_SIZE 64
 #endif
 
 #if  (SERIAL_RX_BUFFER_SIZE>256)
--- 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");
     }
 }