S K UCI / Mbed 2 deprecated AutonomousDAQ

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
0:664899e0b988
Child:
1:e392595b4b76
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SnCommUsb.cpp	Sat Jun 30 02:03:51 2012 +0000
@@ -0,0 +1,143 @@
+#include "SnCommUsb.h"
+
+#include "Timer.h"
+
+#include "SnStatusFrame.h"
+#include "SnConfigFrame.h"
+#include "SnEventFrame.h"
+
+MODSERIAL* SnCommUsb::fgCpu     =  0;
+
+SnCommWin::ECommWinResult SnCommUsb::SetupPort(MODSERIAL& cpu) {
+    fgCpu = &cpu;
+    
+    // set up serial-usb port
+    fgCpu->baud( 230400 );
+    fgCpu->format( 8, Serial::None, 1 );
+    fgCpu->txBufferFlush();
+    fgCpu->rxBufferFlush();
+    return kOkNoMsg;
+}
+
+SnCommWin::ECommWinResult SnCommUsb::OpenWindow(Timer& timer,
+                                                const uint32_t timeout,
+                                                const bool sendStatus,
+                                                const SnConfigFrame& conf,
+                                                const SnEventFrame& evt,
+                                                char* const evtBuf,
+                                                char* const statBuf) {
+    // usb port always connected (or never)
+    SnCommWin::ECommWinResult ret = SnCommWin::kConnected;
+    
+    if (sendStatus) {
+        ret = SendStatus(conf, evt, evtBuf, statBuf);
+    }
+    
+    return ret;
+}
+
+SnCommWin::ECommWinResult SnCommUsb::GetConfig(SnConfigFrame& conf,
+                                               Timer& timer,
+                                               const uint32_t timeOut,
+                                               char* const confBuf) {
+    // confBuf not used by usb. bytes read directly from serial port
+    
+    SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
+    
+    bool hasData = fgCpu->readable();
+
+    while ( (hasData==false) && (timer.read() < timeOut) ) {        
+        wait_ms(250);
+        
+        hasData = fgCpu->readable();
+    }
+    
+    if( hasData ) {
+        conf.ReadFrom(*fgCpu);
+        res = SnCommWin::kOkWithMsg;
+    } else {
+        res = SnCommWin::kOkNoMsg;
+    }
+
+    return res;
+}
+
+SnCommWin::ECommWinResult SnCommUsb::SendStatus(const SnConfigFrame& conf,
+                                                const SnEventFrame& evt,
+                                                char* const evtBuf,
+                                                char* const statBuf) {
+    // statBuf not used. status written directly to serial port
+    return SnStatusFrame::WriteTo(*fgCpu, SnConfigFrame::kUSB, conf, evt, evtBuf);
+}
+
+SnCommWin::ECommWinResult SnCommUsb::SendData(FILE* inf) {
+    int c;
+    do {
+        c = getc(inf);
+        fgCpu->putc(c);
+    } while (c!=EOF);
+    return SnCommWin::kOkMsgSent;
+}
+
+SnCommWin::ECommWinResult SnCommUsb::SendConfAndEvents(FILE* inf,
+                                                    const SnConfigFrame& curConf,
+                                                    SnEventFrame& evt,
+                                                    char* const evtBuf,
+                                                    char* const confBuf,
+                                                    const uint32_t nevts,
+                                                    const uint32_t firstEvt) {
+    // firstEvt==0 ==> start at beginning
+    // nevts==0 ==> NO events! (see SnCommWin::SendData
+    //    for the fcn to send the full file)
+    
+    // TODO: check memory for temporary config/event frames?
+    SnConfigFrame conf;
+    conf.ReadFrom(inf);
+    conf.WriteTo(*fgCpu);
+
+    uint8_t sLoseLSB=0, sLoseMSB=0;
+    uint16_t sWvBase=0;
+    conf.GetPackParsFor(SnConfigFrame::kUSB, sLoseLSB, sLoseMSB, sWvBase);
+    // do we have to unpack & repack events?
+    const bool repack =    (sLoseLSB != conf.GetWvLoseLSB())
+                        && (sLoseMSB != conf.GetWvLoseMSB())
+                        && (sWvBase  != conf.GetWvBaseline());
+    
+    // size of event in file
+    const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
+                                                conf.GetWvLoseMSB());
+    // move up to first event
+    fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
+    if (repack) {
+        // repack ==> send event by event
+        // size of event sent over afar
+        const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
+        char* b;
+        register uint32_t i;
+        for (i=0; (i<nevts) && (ferror(inf)==0) && (feof(inf)==0); i++) {
+            evt.ReadFrom(inf, evtBuf,
+                conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
+                conf.GetWvBaseline());
+            evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
+            b = evtBuf;
+            for (uint32_t j=0; j<ssize; j++, b++) {
+                fgCpu->putc( *b );
+            }
+        }
+        if (i!=nevts) {
+            return SnCommWin::kFailPartSent;
+        }
+    } else {
+        // no repacking ==> just send the bytes from the file
+        const uint32_t nbytes = nevts*esize;
+        register uint32_t i;
+        for (i=0; i<nbytes && (ferror(inf)==0) && (feof(inf)==0); i++) {
+            fgCpu->putc( fgetc(inf) );
+        }
+        if (i!=nbytes) {
+            return SnCommWin::kFailPartSent;
+        }
+    }
+    
+    return SnCommWin::kOkMsgSent;
+}