Fan Control Demo

Dependencies:   mbed-rtos mbed

Fork of mbed_fota by KIM HyoengJun

Revision:
2:dba344c91bce
Child:
3:1e70387e1337
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ext_fota/SerialManager.cpp	Mon Jun 22 09:43:13 2015 +0000
@@ -0,0 +1,162 @@
+#include "SerialManager.h"
+
+namespace sevencore_fota{
+
+
+SerialManager::SerialManager(Serial *_device)
+{
+    print_flag = 0;
+    device = _device;
+    FE_MSG_PACKET_TYPE = 0x05;
+    MAX_PACKET_LENGTH = 350;
+    bReceiveState = 0;
+    wDataLength = 0;
+    wReceivePos = 0;
+    bHdrBytesRead = 0;
+    memset(bReceiveElementArr,0,512);
+}
+
+SerialManager::SerialManager(Serial *_device, Serial *_hostpc)
+{
+    print_flag = 1;
+    device = _device;
+    hostpc = _hostpc;
+    FE_MSG_PACKET_TYPE = 0x05;
+    MAX_PACKET_LENGTH = 350;
+    bReceiveState = 0;
+    wDataLength = 0;
+    wReceivePos = 0;
+    bHdrBytesRead = 0;
+    memset(bReceiveElementArr,0,512);
+    PrintSerialManager();
+}
+
+SerialManager::~SerialManager(void)
+{
+}
+
+void SerialManager::PrintSerialManager(void)
+{
+    if(print_flag == 1)
+        hostpc->printf("SerialManager Start!!\n");
+}
+
+int SerialManager::SendToSerial(uint8_t *data,unsigned short size)
+{
+    int cnt = 0;
+    if(print_flag == 1)
+        hostpc->printf("send size = %hu\n",size);
+        
+    for(int i=0; i < size; i++)
+    {
+        if( device->writeable())
+        {
+            if(print_flag == 1)
+                hostpc->printf("%02X ", data[i]);
+            device->putc(data[i]);
+            cnt++;
+        }
+    }
+    return cnt;    
+}
+
+// return boolean : msg_size (receive complete) or -1 (receive not complete)
+int SerialManager::ReceiveToSerial(unsigned char *receive_msg) 
+{
+    int ret = -1;
+    unsigned char tmp;
+    
+    if(device->readable())
+    {
+        tmp = device->getc();
+        switch(bReceiveState)
+        {
+            case 0:
+                if( tmp == FE_MSG_PACKET_TYPE )
+                {
+                    bReceiveState = 1;
+                    wDataLength = 0;
+                    wReceivePos = 0;
+                    bHdrBytesRead = 0;
+                    
+                    bReceiveElementArr[wReceivePos] = tmp;
+                    wReceivePos++;
+                    if(print_flag == 1)
+                        hostpc->printf("\n[Receiver] Packet Type |: %02X \n", tmp);
+                }else
+                {
+                    if(print_flag == 1)
+                        hostpc->printf("\n[Receiver] Packet Type Error |: %02X \n", tmp);
+                }
+                break;
+            case 1:
+                if(print_flag == 1)
+                    hostpc->printf("R-%02X ",tmp);
+                bHdrBytesRead++;
+                bReceiveElementArr[wReceivePos] = tmp;
+                wReceivePos++;
+                if( bHdrBytesRead == 6 )
+                    bReceiveState = 2;
+                break;
+            case 2:
+                if(print_flag == 1)
+                    hostpc->printf("R-%02X ",tmp);
+                wDataLength += tmp;
+                if( wDataLength > MAX_PACKET_LENGTH )
+                    bReceiveState = 0;
+                else
+                {
+                    bReceiveElementArr[wReceivePos] = tmp;
+                    wReceivePos++;
+                    bReceiveState = 3;
+                }
+                break;
+            case 3:
+                if( print_flag == 1 )
+                    hostpc->printf("R-%02X ",tmp);
+                wDataLength += (unsigned short) (tmp*256);
+                if( wDataLength > MAX_PACKET_LENGTH )
+                {
+                    if( print_flag == 1 )
+                        hostpc->printf("\n[Receiver] Over SIZE: %d ", wDataLength);
+                    bReceiveState = 0;
+                }else if (wDataLength == 0)
+                {
+                    if( print_flag == 1 )
+                        hostpc->printf("\n[Receiver] Zero SIZE: %d ", wDataLength);
+                    memcpy(receive_msg,bReceiveElementArr,wReceivePos);
+                    ret = wReceivePos;
+                    bReceiveState = 0;
+                }else
+                {
+                    bReceiveElementArr[wReceivePos] = tmp;
+                    wReceivePos++;
+                    bReceiveState = 4;
+                }
+                break;
+            case 4:
+                if( print_flag == 1 )
+                    hostpc->printf("R-%02X ",tmp);
+                bReceiveElementArr[wReceivePos] = tmp;
+                wReceivePos++;
+                // 9 = 1(first byte = FE_MSG_PACKET_TYPE) + 2(type) +2(dstid) +2(srcid) +2(length size)
+                if(wReceivePos == wDataLength + 9)
+                {
+                    memcpy(receive_msg,bReceiveElementArr,wReceivePos);
+                    ret = wReceivePos;
+                    bReceiveState = 0;
+                    if( print_flag == 1 )
+                        hostpc->printf("\n[Receiver] Rcv Data SIZE: %d ", wDataLength);
+                }
+                break;
+            default:
+                if(print_flag == 1)
+                    hostpc->printf("ERROR STRAGE STATE\n");
+                break;
+        }
+    }
+    
+    return ret;   
+}
+
+}//namespace