asdasdasdasd

Dependencies:   Audio EthernetInterface mbed-rtos mbed

Fork of FRDM_TCP_v4_copy by Tigaresi

Files at this revision

API Documentation at this revision

Comitter:
irsanjul
Date:
Fri Aug 10 09:12:27 2018 +0000
Parent:
10:0468b439a484
Commit message:
asdasdas

Changed in this revision

Audio.lib Show annotated file Show diff for this revision Revisions of this file
CommChannel.cpp Show annotated file Show diff for this revision Revisions of this file
CommChannel.h Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
LokerFraming.cpp Show annotated file Show diff for this revision Revisions of this file
LokerFraming.h Show annotated file Show diff for this revision Revisions of this file
const_defines.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
diff -r 0468b439a484 -r 9280da8e40bd Audio.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Audio.lib	Fri Aug 10 09:12:27 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/irsanjul/code/Audio/#899519c9dfe2
diff -r 0468b439a484 -r 9280da8e40bd CommChannel.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CommChannel.cpp	Fri Aug 10 09:12:27 2018 +0000
@@ -0,0 +1,99 @@
+//#include "mbed.h"
+#include "EthernetInterface.h"
+#include "CommChannel.h"
+#include "const_defines.h"
+
+extern Serial dbg;
+
+//-----------------------------------------------------------------
+extern std::vector<int> RCV_BUFF;   // temporary buffer receiving progress
+std::vector<int> RCVD;       // buffer completed receiving
+
+//-----------------------------------------------------------------
+// Mechanism for receive timeout
+//
+extern int timerRxTOutCnt;
+
+//-----------------------------------------------------------------
+//  TCP definition
+//
+extern TCPSocketConnection socket;
+
+//-----------------------------------------------------------------
+//  Wrapper class
+//
+CommChannel::CommChannel()
+{
+//    dbg.printf("<CommChannel>");
+}
+
+CommChannel::~CommChannel()
+{
+
+}
+
+void CommChannel::connect(const std::string &host)
+{
+    // Connect to Server
+    int timeout = 0;
+    while (socket.connect(host.c_str(), 5002) < 0) {
+        dbg.printf(RED"Unable to connect to (%s) on port (%d)\r\n"DEF, host.c_str(), 5002);
+        timeout++;
+        if(timeout > 60)break;
+        wait(1);
+    }
+}
+
+int CommChannel::dataAvailable()
+{
+    return RCVD.size();
+}
+
+std::vector<int> CommChannel::read()
+{
+    std::vector<int> result;
+
+    for(int i=0; i<RCVD.size(); i++)
+        result.push_back(RCVD[i]);
+
+    RCVD.clear();
+    return result;
+}
+
+void CommChannel::write(const std::vector<int> &s)
+{
+    char sData[s.size()];
+    
+    for(int i=0; i<s.size(); i++)
+    {
+        sData[i] = s[i] & 0x0ff;
+    }
+    
+    socket.send_all(sData, sizeof(sData));
+}
+
+void CommChannel::write(char *s, int sz)
+{   
+    socket.send_all(s, sz);
+}
+
+void CommChannel::Tick10ms()
+{
+    timerRxTOutCnt++;
+    if(timerRxTOutCnt >= 10)
+    {
+        timerRxTOutCnt = 0;
+        
+        if(RCV_BUFF.size() > 0)
+        {
+            for(int i=0; i<RCV_BUFF.size(); i++)
+            {
+                RCVD.push_back(RCV_BUFF[i]);
+//                dbg.printf("[%02X]", RCV_BUFF[i] & 0x0FF);
+            } 
+            RCV_BUFF.clear();
+        }
+    }    
+}
+
+//-----------------------------------------------------------------
diff -r 0468b439a484 -r 9280da8e40bd CommChannel.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CommChannel.h	Fri Aug 10 09:12:27 2018 +0000
@@ -0,0 +1,27 @@
+#ifndef COMMCHANNEL_H
+#define COMMCHANNEL_H
+
+#include <vector>
+#include <string>
+
+class CommChannel
+{
+public:
+    CommChannel();
+    ~CommChannel();
+
+    void connect(const std::string &host);
+
+    int dataAvailable();
+    std::vector<int> read();
+
+    void write(const std::vector<int> &s);
+    void write(char *s, int sz);
+    
+    void Tick10ms();
+
+private:
+    
+};
+
+#endif // COMMCHANNEL_H
diff -r 0468b439a484 -r 9280da8e40bd EthernetInterface.lib
--- a/EthernetInterface.lib	Tue Feb 14 16:37:18 2017 +0000
+++ b/EthernetInterface.lib	Fri Aug 10 09:12:27 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/EthernetInterface/#183490eb1b4a
+https://os.mbed.com/users/irsanjul/code/EthernetInterface/#de1462e95bdb
diff -r 0468b439a484 -r 9280da8e40bd LokerFraming.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LokerFraming.cpp	Fri Aug 10 09:12:27 2018 +0000
@@ -0,0 +1,40 @@
+#include "mbed.h"
+#include "LokerFraming.h"
+
+extern Serial dbg;
+
+LokerFraming::LokerFraming()
+{
+
+}
+
+LokerFraming::~LokerFraming()
+{
+
+}
+
+void LokerFraming::ProcessFromHost(const std::vector<int> &stream)
+{
+    LokerFrm f;
+	
+	f.stx = stream[0];
+	f.status = stream[1];
+	f.Data.push_back(stream[2]);
+	f.Data.push_back(stream[3]);
+	f.etx = stream[4];
+	RCV_QUE.push(f);
+}
+
+std::vector<int> LokerFraming::CreateFrame(const LokerFrm &f)
+{
+    std::vector<int> result;
+
+    result.push_back(f.stx);
+    result.push_back(f.status);
+	for(int i=0; i<f.Data.size(); i++)
+	{
+		result.push_back(f.Data[i] & 0x0FF);
+	}
+	result.push_back(f.etx);
+    return result;
+}
\ No newline at end of file
diff -r 0468b439a484 -r 9280da8e40bd LokerFraming.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LokerFraming.h	Fri Aug 10 09:12:27 2018 +0000
@@ -0,0 +1,52 @@
+#ifndef LOKERFRAME_H
+#define LOKERFRAME_H
+
+#include <vector>
+#include <queue>
+
+class LokerFrm;
+
+//----------------------------------------------------------------
+class LokerFraming
+{
+public:
+    LokerFraming();
+    ~LokerFraming();
+
+    void ProcessFromHost(const std::vector<int> &stream);
+
+    static std::vector<int> CreateFrame(const LokerFrm &f);
+
+    std::queue<LokerFrm> RCV_QUE;
+
+private:
+	
+};
+
+//----------------------------------------------------------------
+class LokerFrm
+{
+public:
+
+    int stx;
+	int status;
+//    int zData;
+    std::vector<int> Data;
+	int ceksum;
+	int etx;
+};
+
+//----------------------------------------------------------------
+class TxFrm : public LokerFrm
+{
+public:
+	TxFrm(std::vector<int> &Data_, int status_)
+	{
+		stx = 0x32;
+		status = status_;
+		Data = Data_;
+		ceksum = -1;
+		etx = 0x33;
+	}
+};
+#endif //LOKERFRAME_H
diff -r 0468b439a484 -r 9280da8e40bd const_defines.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/const_defines.h	Fri Aug 10 09:12:27 2018 +0000
@@ -0,0 +1,37 @@
+#ifndef CONST_DEFINES_H
+#define CONST_DEFINES_H
+
+// rows of lockers
+#define rowA 	0x01
+#define rowB 	0x02
+#define rowC 	0x04
+#define rowD 	0x08
+#define rowF 	0x20
+#define rowG	0x40
+#define rowH	0x80
+
+// status
+#define idle	0x30
+#define expect	0x31
+#define release	0x32
+
+// max colum of lockers
+#define MAX_COL	0x08
+#define MAX_ROW	0x08
+
+#if 1 // colored terminal output using ANSI escape sequences
+  #define COL(c) "\033[" c
+ #else
+  #define COL(c) 
+ #endif
+#define DEF COL("39m")
+#define BLA COL("30m")
+#define RED COL("31m")
+#define GRE COL("32m")
+#define YEL COL("33m")
+#define BLU COL("34m")
+#define MAG COL("35m")
+#define CYA COL("36m")
+#define WHY COL("37m")
+
+#endif // CONST_DEFINES_H
diff -r 0468b439a484 -r 9280da8e40bd main.cpp
--- a/main.cpp	Tue Feb 14 16:37:18 2017 +0000
+++ b/main.cpp	Fri Aug 10 09:12:27 2018 +0000
@@ -1,37 +1,200 @@
 #include "mbed.h"
+#include "rtos.h"
 #include "EthernetInterface.h"
+#include "CommChannel.h"
+#include "LokerFraming.h"
+#include "const_defines.h"
+#include <vector>
+
+/*******************************************************************************
+PRIVATE PinOut
+*******************************************************************************/
+Serial dbg(USBTX, USBRX);
+DigitalOut led(LED_BLUE);
+
+
+/*******************************************************************************
+PRIVATE VARIABLE
+*******************************************************************************/
+int device_status;
+int kirim_status;
+int COL_SCAN, ROW_SCAN;
+const char* SERVER_ADDRESS = "192.168.0.34";
+const char* IP = "192.168.0.111";
+const char* SM = "255.255.255.0";
+const char* GW = "192.168.0.1";
+
+TCPSocketConnection socket;
+CommChannel COM;
+bool comStat;
+
+std::vector<int> RCV_BUFF;
+int timerRxTOutCnt;
+bool tmr10msTick;
+Ticker timer10ms;
+void timer10msTick();
 
-const char* ECHO_SERVER_ADDRESS = "192.168.2.2";
-const int ECHO_SERVER_PORT = 7;
+bool tmr10sTick;
+Ticker timer10s;
+void timer10stick();
 
-int main() {
+void rxRoutine(void const *args)
+{
+    while(true)
+    {
+        int n=0;
+        char buf[256];
+        n = socket.receive(buf, 256);
+        if(n > 0)
+        {
+            buf[n] = '\0';
+//            dbg.printf("Received message from server: '%s'\n\r", buf);
+            for(int i=0; i<n; i++)
+            {
+                RCV_BUFF.push_back((int)buf[i]);
+            }
+            timerRxTOutCnt = 0;
+        }
+        Thread::wait(100);
+    }   
+}
+
+//const int ECHO_SERVER_PORT = 5002;
+
+Ticker run;
+void runled();
+
+int main()
+{
+    tmr10msTick = false;
+    tmr10sTick = false;
+    kirim_status = false;
+    
     EthernetInterface eth;
-    eth.init(); //Use DHCP
-    eth.connect();
-    printf("\nClient IP Address is %s\n", eth.getIPAddress());
+    eth.init(IP, SM, GW); //Use DHCP
+    int n = eth.connect();
+    if(n)dbg.printf(GRE"Connected with IP : %s\r\n"DEF, IP);
+    else dbg.printf(RED"Not Connected with IP : %s\r\n"DEF, IP);
+    
+    comStat = false;
+    COM.connect(SERVER_ADDRESS);
+    dbg.printf(GRE"Connected to Server at %s\r\n"DEF, SERVER_ADDRESS);
+    
+    timer10ms.attach_us(&timer10msTick, 10000);
+    
+    set_time(1483228800);
+    run.attach(&runled, 1.0f);
+    Thread thread1(rxRoutine);
+    
+    int Loop = 0;
     
-    // Connect to Server
-    TCPSocketConnection socket;
-    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
-        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
-        wait(1);
+    while(true)
+    {
+        char timeBuf[32];
+        time_t seconds = time(NULL);
+        strftime(timeBuf, 32, "%Y-%m-%d+%H:%M:%S", localtime(&seconds));
+        
+        comStat = socket.is_connected();
+        if(!comStat)
+        {
+            socket.close();
+            COM.connect(SERVER_ADDRESS);
+            dbg.printf(GRE"Connected to Server at %s\r\n"DEF, SERVER_ADDRESS);
+        }
+        
+        if(tmr10msTick)
+        {
+            tmr10msTick = false;
+            COM.Tick10ms();
+        }
+        
+        // Receive message from server
+        if(COM.dataAvailable() > 0)
+        {
+            dbg.printf(YEL"%s <<\t", timeBuf);
+            std::vector<int> r = COM.read();
+            for(int i=0; i<r.size(); i++)
+                dbg.printf("0x%02X ", r[i]);
+            dbg.printf("\r\n"DEF);
+            
+            LokerFraming frm;
+            frm.ProcessFromHost(r);
+            size_t sz = frm.RCV_QUE.size();
+            if(sz > 0)
+            {
+                LokerFrm lf = frm.RCV_QUE.front();
+                frm.RCV_QUE.pop();
+                
+                switch(lf.status)
+                {
+                    case '0' :
+                        device_status = idle;
+                        break;
+                    case '1' :
+                    case '2' :
+                        ROW_SCAN = lf.Data[0];
+                        COL_SCAN = lf.Data[1];
+                        dbg.printf(YEL"++ Scanning : %c %c\r\n\r\n"DEF, ROW_SCAN, COL_SCAN);
+                        device_status = lf.status;
+                        break;
+                    case '3' : // for send status of
+                        kirim_status = true;
+                        break;
+                    case '4' : // for setting the rtc
+                        break;
+                    default : break;
+                }
+            }
+        }
+        
+        // Send message to server
+        if(kirim_status)
+        {
+            dbg.printf(YEL"%s >>\t", timeBuf);
+            if(comStat)
+            {
+                srand(seconds);
+                std::vector<int>kirim;
+                for(int i=0; i<8; i++)kirim.push_back(rand() % 0xFF);
+                TxFrm tData(kirim, device_status);
+                
+                std::vector<int>tMsg = LokerFraming::CreateFrame(tData);
+                for(int i=0; i<tMsg.size(); i++)
+                    dbg.printf("0x%02X ", tMsg[i]);
+                dbg.printf("\r\n\r\n"DEF);
+                
+                COM.write(tMsg);
+                kirim_status = false;
+                Loop++;
+                if(Loop > 4)
+                {
+                    Loop = 0;
+                    device_status = idle;
+                }
+            }
+            else
+            {
+                socket.close();
+                COM.connect(SERVER_ADDRESS);
+            }
+        }
     }
-    printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
-    
-    // Send message to server
-    char hello[] = "Hello World";
-    printf("Sending  message to Server : '%s' \n",hello);
-    socket.send_all(hello, sizeof(hello) - 1);
-    
-    // Receive message from server
-    char buf[256];
-    int n = socket.receive(buf, 256);
-    buf[n] = '\0';
-    printf("Received message from server: '%s'\n", buf);
-    
-    // Clean up
-    socket.close();
-    eth.disconnect();
-    
-    while(true) {}
+}
+
+/*******************************************************************************
+PRIVATE FUNCTION
+*******************************************************************************/
+void runled()
+{
+    led = !led;
 }
+
+void timer10stick()
+{
+    tmr10sTick = true;
+}
+
+void timer10msTick()
+{
+    tmr10msTick = true;
+}