From a835a66a3536a07fa3d74c502dc3276f873959cf Mon Sep 17 00:00:00 2001
From: Alvise De Faveri <alvise.defaveri@skywarder.eu>
Date: Wed, 15 Nov 2017 13:29:04 +0100
Subject: [PATCH] Create gamma868 (ex dsgamma) object We discovered that the
 real name is gamma868, so this will be the name of the object. Added a simple
 communication test that works on 2 discovery boards, (1 gamma each).

---
 src/entrypoints/dsgamma-test.cpp         | 60 ++++++++++++++++++------
 src/shared/drivers/gamma868/Gamma868.cpp | 16 ++-----
 src/shared/drivers/gamma868/Gamma868.h   | 17 +++----
 3 files changed, 58 insertions(+), 35 deletions(-)

diff --git a/src/entrypoints/dsgamma-test.cpp b/src/entrypoints/dsgamma-test.cpp
index 05703aed9..5481cf0d2 100644
--- a/src/entrypoints/dsgamma-test.cpp
+++ b/src/entrypoints/dsgamma-test.cpp
@@ -31,6 +31,8 @@
 using namespace std;
 using namespace miosix;
 
+#define MSG_LEN 6
+
 typedef Gpio<GPIOG_BASE,13> greenLed;
 typedef Gpio<GPIOG_BASE,14> redLed;
 typedef Gpio<GPIOA_BASE,0> button;
@@ -38,8 +40,12 @@ typedef Gpio<GPIOA_BASE,0> button;
 int ping = 0;
 Gamma868 gamma("/dev/auxtty"); 
 
-void sender(void *arg);
-void receiver();
+long long sendTime = 0;
+int nTentativi = 0;
+int tot = 0;
+
+void sender();
+void receiver(void *arg);
 int main() {
     
     //Discovery gpio setup
@@ -50,41 +56,65 @@ int main() {
         button::mode(Mode::INPUT);
     }
     
-    Thread::create(sender, STACK_MIN);
-    
-    receiver();
+    sender();
     
     
     return 0;
 }
 
-void sender(void *arg){
+void sender(){
+
     while(1){
+        
         while(1){
             if(button::value()==1) break;       //Wait for button
         }
         
         printf("Writing... \n");
-        char msg[2];
-        msg[0] = '#';
-        msg[1] = '?';
+        
+        nTentativi++;
+        if(nTentativi == 1) Thread::create(receiver, STACK_MIN);
+
+        char msg[MSG_LEN];
+        for(int i = 0; i < nTentativi; i++){
+            msg[i] = '#';
+        }
+        
+        sendTime = miosix::getTick();
         gamma.send(msg);
+        
+        printf("Ok \n" );
         Thread::sleep(200);
     }
 }
 
-void receiver(){
+void receiver(void *arg){
+    
     while(1){
         //read 
-        int conflen = 5;
-        char config[conflen];
+        char inputBuf[MSG_LEN];
         printf("Reading: \n");
-        gamma.waitFor(conflen, config);
+        gamma.receive(MSG_LEN, inputBuf);
         
-        for(int i = 0; i < conflen ; i++){
-            printf("Received: %02X\n", config[i]);
+        for(int i = 0; i < MSG_LEN ; i++){
+            printf("Received: %c\n", inputBuf[i]);
         }
         
+        long long arrivalTime = miosix::getTick();
+        int rtt = arrivalTime - sendTime;
+        printf("RTT: %d\n", rtt);
+        
+        printf("--------------RESULT------------");
+        tot += rtt;
+        
+        if(nTentativi > 0)
+        printf("Tentativi: %d  Media: %d \n", nTentativi, tot/(2 * nTentativi));
+        
+        // RECEIVER ONLY
+        //Thread::sleep(50);
+        //gamma.send(inputBuf);
+         
+        
         //Thread::sleep(200);
     }
 }
\ No newline at end of file
diff --git a/src/shared/drivers/gamma868/Gamma868.cpp b/src/shared/drivers/gamma868/Gamma868.cpp
index 816a8a4fc..f1700f099 100644
--- a/src/shared/drivers/gamma868/Gamma868.cpp
+++ b/src/shared/drivers/gamma868/Gamma868.cpp
@@ -37,11 +37,6 @@ using namespace miosix;
 
 #endif //_MIOSIX
 
-#define MAX_IN_BUFFER 100
-#define MAX_OUT_BUFFER 100
-#define CMD_LEN 3
-#define DATA_LEN 10
-
 Gamma868::Gamma868(const char *serialPath)
 {
     fd=open(serialPath,O_RDWR);
@@ -56,16 +51,13 @@ bool Gamma868::send(const char *msg)
     return true;
 }
 
-bool Gamma868::waitFor(int bufLen, char *buf)
+bool Gamma868::receive(int bufLen, char *buf)
 {
     //TODO synchronize
-    char received[bufLen];
-    read(fd, &received, bufLen);
+    char received[bufLen+2];
+    read(fd, &received, bufLen+2);
     
-    for(int i = 0; i < bufLen; i++){
-            printf("Received obj: %02X\n", received[i]);
-        }
-    for(int i = 0; i < bufLen; i++){
+    for(int i = 0; bufLen < bufLen; i++){
         buf[i] = received[i];
     }
     return true;
diff --git a/src/shared/drivers/gamma868/Gamma868.h b/src/shared/drivers/gamma868/Gamma868.h
index 7e2514ac6..ebabe6fcc 100644
--- a/src/shared/drivers/gamma868/Gamma868.h
+++ b/src/shared/drivers/gamma868/Gamma868.h
@@ -38,18 +38,19 @@ class Gamma868 {
     public:
         Gamma868(const char *serialPath);
         bool send(const char *msg);
-        bool waitFor(int bufLen, char *buf);
+        bool receive(int bufLen, char *buf);
         //~Gamma868();
         
-        //bool isConnected() checks if learn switch is pulled up
-        bool enterLearnMode();
-        char *readConfiguration();
-        bool configure(Configuration newConf);
-        void exitLearnMode();
+        /*
+         * TODO:
+         * bool isConnected() checks if learn switch is pulled up??
+         * bool enterLearnMode();
+         * char *readConfiguration();
+         * bool configure(Configuration newConf);
+         * void exitLearnMode();
+         */
        
     private:
-        int checkConfiguration(Configuration config);
-        int timeout(int millis);
         
         int fd;
         //pthread_t thread;
-- 
GitLab