serial_2017

Files at this revision

API Documentation at this revision

Comitter:
Komazawa_sun
Date:
Tue Jun 27 07:55:09 2017 +0000
Commit message:
for jushinkun_ver2017 serial

Changed in this revision

ctrl_defines.h Show annotated file Show diff for this revision Revisions of this file
ctrl_serial.cpp Show annotated file Show diff for this revision Revisions of this file
ctrl_serial.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4be3cd232468 ctrl_defines.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ctrl_defines.h	Tue Jun 27 07:55:09 2017 +0000
@@ -0,0 +1,7 @@
+#ifndef CTRL_DEFINES_H_
+#define CTRL_DEFINES_H_
+
+#define CTRL_TX_PIN dp16
+#define CTRL_RX_PIN dp15
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 4be3cd232468 ctrl_serial.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ctrl_serial.cpp	Tue Jun 27 07:55:09 2017 +0000
@@ -0,0 +1,141 @@
+//受信くん用コントローラーシリアルライブラリ by駒澤
+
+#include "ctrl_serial.h"
+
+
+ctrl_serial::ctrl_serial(int baudrate,int size)//(dps,byte)
+    :serial_dev(CTRL_TX_PIN,CTRL_RX_PIN)//,usb(USBTX,USBRX)
+{    
+    //printf("super constructor\r\n");
+    serial_dev.baud(baudrate);
+    data_size = size;
+    error_status = No_Error;
+    allready_set_data = false;
+    serial_dev.attach(this,&ctrl_serial::ctrl_receive_data,Serial::RxIrq);
+    timeout_checker.attach(this,&ctrl_serial::timeout_check,timeout_st_seconds);
+    timeout_call = false;
+    time.start();
+}
+
+
+void ctrl_serial::ctrl_receive_data(void)
+{
+    if(serial_dev.readable())
+    {
+        timeout_call = true;
+        buffer[buffer_count] = serial_dev.getc();
+        //printf("%x ",buffer[buffer_count]);
+        if(buffer_count + 1 >= data_size)
+        {
+            //printf("get_8b\r\n");
+            data_table(data_check());
+        }
+        else
+        {
+            buffer_count++;
+        }
+    }     
+}
+
+ctrl_serial::Error_type ctrl_serial::data_check()
+{
+    if(buffer[0] == start_byte && buffer[buffer_count] == stop_byte)
+    {
+        //printf("data_get\r\n");
+        if(checksum_read() == true){
+            //printf("data_true\r\n");
+            return No_Error;
+        }
+        else
+        {
+            //printf("data_false\r\n");
+            return Receive_data_False;
+        }
+    }
+    else
+    {
+        //printf("data_get_false\r\n");
+        return Receive_Failed;
+    }
+}
+
+void ctrl_serial::data_table(ctrl_serial::Error_type error){
+    switch(error)
+    {
+        case No_Error:
+            error_status = No_Error;
+            buffer_count = 0;
+            sending_data();
+            allready_set_data = true;
+            break;
+            
+        case Receive_Failed:
+            error_status = Receive_Failed;
+            buffer_shift();
+            break;
+            
+        case Receive_data_False:
+            error_status = Receive_data_False;
+            buffer_shift();
+            break;
+            
+        default:
+            buffer_refresh();
+            sending_data();
+            break;
+    }
+            
+}
+
+void ctrl_serial::sending_data()
+{
+    for(int t = 0;t < data_size;t++)
+        ctrl_data[t] = buffer[t];
+}
+
+bool ctrl_serial::checksum_read()
+{
+    unsigned int sum = 0;
+    for(int t = 1;t < (data_size - 2);t++)
+        sum = sum + buffer[t];
+    unsigned char checksum = (unsigned char)(0xFF & sum);
+    //printf("%x)",checksum);
+    if(buffer[data_size - 2] == checksum)
+    {
+        return true;
+    }
+    else
+    {
+        return false;
+    }   
+}
+
+void ctrl_serial::timeout_check()
+{
+    if(timeout_call == true)
+    {
+        timeout_call = false;
+    }
+    else
+    {
+        //printf("time_out\r\n");
+        error_status = Conect_Timeout;
+        buffer_refresh();
+    }
+}
+
+void ctrl_serial::buffer_refresh()
+{
+    for(int t = 0;t < data_size;t++)
+        buffer[t] = 0;
+    allready_set_data = true;
+    buffer_count = 0;
+}
+
+void ctrl_serial::buffer_shift()
+{
+    for(int t = 0;t < data_size;t++)
+        buffer[t] = buffer[t + 1];
+        
+}
+    
\ No newline at end of file
diff -r 000000000000 -r 4be3cd232468 ctrl_serial.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ctrl_serial.h	Tue Jun 27 07:55:09 2017 +0000
@@ -0,0 +1,59 @@
+//受信くん用コントローラーシリアルライブラリ by駒澤
+
+#ifndef CTRL_SERIAL_H_
+#define CTRL_SERIAL_H_
+
+#include <mbed.h>
+#include "ctrl_defines.h"
+#include "gamma_defines.h"
+
+class ctrl_serial
+{
+    public:
+        
+        ctrl_serial(int baudrate,int size);//(dps)
+        
+        unsigned char ctrl_data[256];
+                    
+        enum Error_type
+        {
+            No_Error,
+            Conect_Timeout,
+            Receive_Failed,Receive_data_False,
+            Buffer_Busy   
+        };
+        
+         Error_type error_status;
+    
+    protected:
+        static const unsigned char start_byte = 0xAA;
+        int data_size;
+        static const unsigned char stop_byte = 0x55;
+        bool allready_set_data;
+    private:
+        Serial serial_dev;
+        //Serial usb;
+        int buffer_count;
+        char buffer[256];
+        unsigned char ctrl_raw_data[256];
+        void ctrl_receive_data(void);
+        void sending_data();
+        
+        Ticker timeout_checker;
+        void timeout_check();
+        bool timeout_call;
+        static const double timeout_st_seconds = 2.0;
+        
+        //void data_checker();
+        Error_type data_check();
+        void data_table(Error_type error);
+        bool checksum_read();
+        
+        void buffer_refresh();
+        void buffer_shift();
+        
+        Timer time;
+         
+};
+
+#endif
\ No newline at end of file