BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Files at this revision

API Documentation at this revision

Comitter:
gustavatmel
Date:
Tue May 01 15:55:34 2018 +0000
Parent:
1:9c5af431a1f1
Commit message:
bluetooth

Changed in this revision

Main.cpp Show annotated file Show diff for this revision Revisions of this file
SerialCom.cpp Show annotated file Show diff for this revision Revisions of this file
SerialCom.h Show annotated file Show diff for this revision Revisions of this file
SerialServerRes.cpp Show diff for this revision Revisions of this file
SerialServerRes.h Show diff for this revision Revisions of this file
SerialServerSend.cpp Show diff for this revision Revisions of this file
SerialServerSend.h Show diff for this revision Revisions of this file
--- a/Main.cpp	Tue May 01 15:47:08 2018 +0000
+++ b/Main.cpp	Tue May 01 15:55:34 2018 +0000
@@ -6,8 +6,7 @@
  *  Created on: 06.10.2017
  *      Author: Marcel Honegger
  */
-#include "SerialServerSend.h"
-#include "SerialServerRes.h"
+#include "SerialCom.h"
 #include <cstdlib>
 #include <mbed.h>
 
@@ -63,8 +62,7 @@
     reset = 0; Thread::wait(100);
     reset = 1; Thread::wait(100);
     
-    SerialServerRes serialServerRes(serial);
-    //SerialServerSend serialServerSend(serial);
+    SerialCom serialCom(serial);
     
     // enter main loop
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialCom.cpp	Tue May 01 15:55:34 2018 +0000
@@ -0,0 +1,158 @@
+/*
+ * SerialServer.cpp
+ * Copyright (c) 2017, ZHAW
+ * All rights reserved.
+ *
+ *  Created on: 05.06.2017
+ *      Author: Marcel Honegger
+ */
+
+
+#include <mbed.h>
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <vector>
+#include "SerialCom.h"
+
+using namespace std;
+
+const float SerialCom::PERIOD = 0.0001f;     // the period of the timer interrupt, given in [s]
+const char SerialCom::DELIM = ',';
+
+/**
+ * Create a serial server object.
+ 
+
+ 
+SerialServer::SerialServer(RawSerial& serial, StateMachine& stateMachine) : serial(serial), stateMachine(stateMachine), thread(osPriorityRealtime, STACK_SIZE) {
+*/
+SerialCom::SerialCom(RawSerial& serial) : serial(serial), thread(osPriorityRealtime, STACK_SIZE) { 
+    // initialize local values
+    
+    input.clear();
+    
+    // start thread and ticker timer interrupt service routine
+    
+    thread.start(callback(this, &SerialCom::run));
+    ticker.attach(callback(this, &SerialCom::sendSignal), PERIOD);
+}
+
+/**
+ * Delete the serial server object and release all allocated resources.
+ */
+SerialCom::~SerialCom() {
+    
+    ticker.detach();
+}
+
+/**
+ * This method is called by the ticker timer interrupt service routine.
+ * It sends a signal to the thread to make it run again.
+ */
+void SerialCom::sendSignal() {
+    
+    thread.signal_set(signal);
+}
+
+/**
+ * This <code>run()</code> method contains an infinite loop with the run logic.
+ */
+void SerialCom::run() {
+    
+    Serial pc(PA_2, PA_3); // tx, rx
+    pc.baud(9600);
+    
+    while (true) {
+        
+        // wait for the periodic signal
+        
+        thread.signal_wait(signal);
+        
+        
+        
+        // read received characters while buffer is full
+        
+        while (serial.readable()) {
+            int32_t c = serial.getc();
+            //if (input.size() >= MESSAGE_SIZE) input.erase(0, 1);
+            input += static_cast<uint8_t>(c);
+        }
+        
+        // try to decode a message
+        
+        if (input[input.size()-1] != '&') {
+            
+            // message is too short, keep reading...
+            
+        } else {
+            pc.printf("command: %s", input);
+            splitString();
+            if (tokens[0] == "M") {
+                if (tokens.size() == 45+2) {
+                    float newK[3][15];
+                    int y = 0;
+                    int x = 0;
+                    float checksum = 0;       
+                    for (int i=1; i < tokens.size()-1; i++) {
+                        newK[y][x] = strtof((tokens[i]).c_str(), 0);
+                        pc.printf("Element %d ,  %d: %.10f\r\n", y, x, newK[y][x]);
+                        x++;
+                        checksum += newK[y][x];
+                        if (x == 15){
+                            x = 0;
+                            y++;
+                        }
+                    }
+                }
+                else{
+                    pc.printf("Data to short od long\r\n");
+                }
+                  
+            }
+            else if (tokens[0] == "G") {
+                pc.printf("Send data to pc\r\n");
+                if(sendData == false) {
+                    sendData = true;
+                    runCount = 0;
+                }
+                else{
+                    sendData = false;
+                }
+                
+            }
+            else{
+                pc.printf("Command not understood\r\n");    
+            }
+            
+           
+            tokens.clear();
+            input.clear();
+
+      
+        }
+        if(sendData == true) {
+            if (output.size() == 0 && runCount >= 1000){
+                output += "Hello nicolas\n";
+                runCount = 0;
+            }
+        } 
+        runCount++;
+        while (serial.writeable() && (output.size() > 0)) {
+            serial.putc(output[0]);
+            output.erase(0, 1);
+        }    
+
+    }
+    
+}
+
+
+void SerialCom::splitString() {
+    stringstream ss(input);
+    string item;
+    while (getline(ss, item, DELIM)) {
+        tokens.push_back(item);
+   } 
+    return;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialCom.h	Tue May 01 15:55:34 2018 +0000
@@ -0,0 +1,54 @@
+/*
+ * SerialServer.h
+ * Copyright (c) 2017, ZHAW
+ * All rights reserved.
+ *
+ *  Created on: 05.06.2017
+ *      Author: Marcel Honegger
+ */
+
+#ifndef SERIAL_COM_H_
+#define SERIAL_COM_H_
+
+#include <cstdlib>
+#include <string>
+#include <vector>
+#include <mbed.h>
+#include "Signal.h"
+
+using namespace std;
+
+
+
+/**
+ * This class implements a communication server using a serial interface.
+ */
+class SerialCom {
+    
+    public:
+        
+                    SerialCom(RawSerial& serial);
+        virtual     ~SerialCom();
+        
+    private:
+        
+        static const uint32_t   STACK_SIZE = 2048 ;      // stack size of thread, given in [bytes]
+        static const float      PERIOD;                 // the period of the timer interrupt, given in [s]
+        static const char       DELIM;
+        
+        RawSerial&      serial;
+        string          input;
+        string          output;
+        vector<string>  tokens;
+        Signal          signal;
+        Thread          thread;
+        Ticker          ticker;
+        bool            sendData;
+        int             runCount;
+
+        void            sendSignal();
+        void            run();
+        void            splitString();
+};
+
+#endif /* SERIAL_COM_H_ */
--- a/SerialServerRes.cpp	Tue May 01 15:47:08 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,158 +0,0 @@
-/*
- * SerialServer.cpp
- * Copyright (c) 2017, ZHAW
- * All rights reserved.
- *
- *  Created on: 05.06.2017
- *      Author: Marcel Honegger
- */
-
-
-#include <mbed.h>
-#include <iostream>
-#include <string>
-#include <sstream>
-#include <vector>
-#include "SerialServerRes.h"
-
-using namespace std;
-
-const float SerialServerRes::PERIOD = 0.0001f;     // the period of the timer interrupt, given in [s]
-const char SerialServerRes::DELIM = ',';
-
-/**
- * Create a serial server object.
- 
-
- 
-SerialServer::SerialServer(RawSerial& serial, StateMachine& stateMachine) : serial(serial), stateMachine(stateMachine), thread(osPriorityRealtime, STACK_SIZE) {
-*/
-SerialServerRes::SerialServerRes(RawSerial& serial) : serial(serial), thread(osPriorityRealtime, STACK_SIZE) { 
-    // initialize local values
-    
-    input.clear();
-    
-    // start thread and ticker timer interrupt service routine
-    
-    thread.start(callback(this, &SerialServerRes::run));
-    ticker.attach(callback(this, &SerialServerRes::sendSignal), PERIOD);
-}
-
-/**
- * Delete the serial server object and release all allocated resources.
- */
-SerialServerRes::~SerialServerRes() {
-    
-    ticker.detach();
-}
-
-/**
- * This method is called by the ticker timer interrupt service routine.
- * It sends a signal to the thread to make it run again.
- */
-void SerialServerRes::sendSignal() {
-    
-    thread.signal_set(signal);
-}
-
-/**
- * This <code>run()</code> method contains an infinite loop with the run logic.
- */
-void SerialServerRes::run() {
-    
-    Serial pc(PA_2, PA_3); // tx, rx
-    pc.baud(9600);
-    
-    while (true) {
-        
-        // wait for the periodic signal
-        
-        thread.signal_wait(signal);
-        
-        
-        
-        // read received characters while buffer is full
-        
-        while (serial.readable()) {
-            int32_t c = serial.getc();
-            //if (input.size() >= MESSAGE_SIZE) input.erase(0, 1);
-            input += static_cast<uint8_t>(c);
-        }
-        
-        // try to decode a message
-        
-        if (input[input.size()-1] != '&') {
-            
-            // message is too short, keep reading...
-            
-        } else {
-            pc.printf("command: %s", input);
-            splitString();
-            if (tokens[0] == "M") {
-                if (tokens.size() == 45+2) {
-                    float newK[3][15];
-                    int y = 0;
-                    int x = 0;
-                    float checksum = 0;       
-                    for (int i=1; i < tokens.size()-1; i++) {
-                        newK[y][x] = strtof((tokens[i]).c_str(), 0);
-                        pc.printf("Element %d ,  %d: %.10f\r\n", y, x, newK[y][x]);
-                        x++;
-                        checksum += newK[y][x];
-                        if (x == 15){
-                            x = 0;
-                            y++;
-                        }
-                    }
-                }
-                else{
-                    pc.printf("Data to short od long\r\n");
-                }
-                  
-            }
-            else if (tokens[0] == "G") {
-                pc.printf("Send data to pc\r\n");
-                if(sendData == false) {
-                    sendData = true;
-                    runCount = 0;
-                }
-                else{
-                    sendData = false;
-                }
-                
-            }
-            else{
-                pc.printf("Command not understood\r\n");    
-            }
-            
-           
-            tokens.clear();
-            input.clear();
-
-      
-        }
-        if(sendData == true) {
-            if (output.size() == 0 && runCount >= 1000){
-                output += "Hello nicolas\n";
-                runCount = 0;
-            }
-        } 
-        runCount++;
-        while (serial.writeable() && (output.size() > 0)) {
-            serial.putc(output[0]);
-            output.erase(0, 1);
-        }    
-
-    }
-    
-}
-
-
-void SerialServerRes::splitString() {
-    stringstream ss(input);
-    string item;
-    while (getline(ss, item, DELIM)) {
-        tokens.push_back(item);
-   } 
-    return;
-}
--- a/SerialServerRes.h	Tue May 01 15:47:08 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-/*
- * SerialServer.h
- * Copyright (c) 2017, ZHAW
- * All rights reserved.
- *
- *  Created on: 05.06.2017
- *      Author: Marcel Honegger
- */
-
-#ifndef SERIAL_SERVER_RES_H_
-#define SERIAL_SERVER_RES_H_
-
-#include <cstdlib>
-#include <string>
-#include <vector>
-#include <mbed.h>
-#include "Signal.h"
-
-using namespace std;
-
-
-
-/**
- * This class implements a communication server using a serial interface.
- */
-class SerialServerRes {
-    
-    public:
-        
-                    SerialServerRes(RawSerial& serial);
-        virtual     ~SerialServerRes();
-        
-    private:
-        
-        static const uint32_t   STACK_SIZE = 2048 ;      // stack size of thread, given in [bytes]
-        static const float      PERIOD;                 // the period of the timer interrupt, given in [s]
-        static const char       DELIM;
-        
-        RawSerial&      serial;
-        string          input;
-        string          output;
-        vector<string>  tokens;
-        Signal          signal;
-        Thread          thread;
-        Ticker          ticker;
-        bool            sendData;
-        int             runCount;
-
-        void            sendSignal();
-        void            run();
-        void            splitString();
-};
-
-#endif /* SERIAL_SERVER_H_ */
--- a/SerialServerSend.cpp	Tue May 01 15:47:08 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-/*
- * SerialServer.cpp
- * Copyright (c) 2017, ZHAW
- * All rights reserved.
- *
- *  Created on: 05.06.2017
- *      Author: Marcel Honegger
- */
-
-#include <vector>
-#include <mbed.h>
-#include "SerialServerSend.h"
-
-
-using namespace std;
-
-const float SerialServerSend::PERIOD = 0.001f;     // the period of the timer interrupt, given in [s]
-/**
- * Create a serial server object.
- 
-
- 
-SerialServer::SerialServer(RawSerial& serial, StateMachine& stateMachine) : serial(serial), stateMachine(stateMachine), thread(osPriorityRealtime, STACK_SIZE) {
-*/
-SerialServerSend::SerialServerSend(RawSerial& serial) : serial(serial), thread(osPriorityRealtime, STACK_SIZE) { 
-    // initialize local values
-    
-    input.clear();
-    output.clear();
-    
-    // start thread and ticker timer interrupt service routine
-    
-    thread.start(callback(this, &SerialServerSend::run));
-    ticker.attach(callback(this, &SerialServerSend::sendSignal), PERIOD);
-}
-
-/**
- * Delete the serial server object and release all allocated resources.
- */
-SerialServerSend::~SerialServerSend() {
-    
-    ticker.detach();
-}
-
-/**
- * This method is called by the ticker timer interrupt service routine.
- * It sends a signal to the thread to make it run again.
- */
-void SerialServerSend::sendSignal() {
-    
-    thread.signal_set(signal);
-}
-
-/**
- * This <code>run()</code> method contains an infinite loop with the run logic.
- */
-void SerialServerSend::run() {
-    
-    Serial pc(PA_2, PA_3); // tx, rx
-    pc.baud(9600);
-    
-    while (true) {
-        
-        // wait for the periodic signal
-        
-        thread.signal_wait(signal);
-
-        if (output.size() == 0){
-            output += "Hello nicolas\n";
-        } 
-       
-        // transmit output
-        while (serial.writeable() && (output.size() > 0)) {
-            //pc.printf("Send To P: %s\r\n",output[0]);
-
-            serial.putc(output[0]);
-            output.erase(0, 1);
-        }
-    }
-    
-}
--- a/SerialServerSend.h	Tue May 01 15:47:08 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-/*
- * SerialServer.h
- * Copyright (c) 2017, ZHAW
- * All rights reserved.
- *
- *  Created on: 05.06.2017
- *      Author: Marcel Honegger
- */
-
-#ifndef SERIAL_SERVER_SEND_H_
-#define SERIAL_SERVER_SEND_H_
-
-#include <cstdlib>
-#include <string>
-#include <mbed.h>
-#include "Signal.h"
-
-using namespace std;
-
-
-
-/**
- * This class implements a communication server using a serial interface.
- */
-class SerialServerSend {
-    
-    public:
-        
-                    SerialServerSend(RawSerial& serial);
-        virtual     ~SerialServerSend();
-       
-    private:
-        
-        static const uint32_t   STACK_SIZE = 2048;      // stack size of thread, given in [bytes]
-        static const float      PERIOD;                 // the period of the timer interrupt, given in [s]
-        static const int32_t    DATA_SIZE = 8;          // size of data message, without hex encoding and termination, in [bytes]
-        static const int32_t    MESSAGE_SIZE = 315+1;    // size of entire message, incl. termination, given in [bytes]
-        
-        RawSerial&      serial;
-        string          input;
-        string          output;
-        Signal          signal;
-        Thread          thread;
-        Ticker          ticker;
-
-        void        sendSignal();
-        void        run();
-};
-
-#endif /* SERIAL_SERVER_H_ */