Arianna station communication peripherals.
Dependents: AutonomousDAQ AutonomousDAQ
SnCommUsb.h@3:a7f72492f19e, 2013-09-05 (annotated)
- Committer:
- uci1
- Date:
- Thu Sep 05 22:30:53 2013 +0000
- Revision:
- 3:a7f72492f19e
- Parent:
- 0:26c9189e5924
- Child:
- 4:8328c2972290
add power down function, add finish sending in case of outgoing buffering, add outgoing buffer to SBD, change dynamic to static casts, remove timeout constants. changes untested.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:26c9189e5924 | 1 | #ifndef SN_SnCommUsb |
uci1 | 0:26c9189e5924 | 2 | #define SN_SnCommUsb |
uci1 | 0:26c9189e5924 | 3 | |
uci1 | 0:26c9189e5924 | 4 | #include "SnCommPeripheral.h" |
uci1 | 0:26c9189e5924 | 5 | |
uci1 | 0:26c9189e5924 | 6 | #ifdef ENABLE_USB |
uci1 | 0:26c9189e5924 | 7 | |
uci1 | 0:26c9189e5924 | 8 | |
uci1 | 0:26c9189e5924 | 9 | #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)) |
uci1 | 0:26c9189e5924 | 10 | |
uci1 | 0:26c9189e5924 | 11 | #ifdef USE_MODSERIAL |
uci1 | 0:26c9189e5924 | 12 | namespace AjK { |
uci1 | 0:26c9189e5924 | 13 | class MODSERIAL; |
uci1 | 0:26c9189e5924 | 14 | }; |
uci1 | 0:26c9189e5924 | 15 | #define SN_USB_SERIAL AjK::MODSERIAL |
uci1 | 0:26c9189e5924 | 16 | #else |
uci1 | 0:26c9189e5924 | 17 | #define SN_USB_SERIAL Serial |
uci1 | 0:26c9189e5924 | 18 | #endif |
uci1 | 0:26c9189e5924 | 19 | |
uci1 | 0:26c9189e5924 | 20 | class SnCommUsb : public SnCommPeripheral { |
uci1 | 0:26c9189e5924 | 21 | public: |
uci1 | 0:26c9189e5924 | 22 | typedef int (SN_USB_SERIAL::*USBCheckDataInBuf)(); |
uci1 | 0:26c9189e5924 | 23 | //typedef int (SnCommUsb::*USBPutGetC)(char* const); |
uci1 | 0:26c9189e5924 | 24 | |
uci1 | 0:26c9189e5924 | 25 | private: |
uci1 | 0:26c9189e5924 | 26 | static SN_USB_SERIAL* fgCpu; |
uci1 | 0:26c9189e5924 | 27 | |
uci1 | 0:26c9189e5924 | 28 | int GetC(char* const d); |
uci1 | 0:26c9189e5924 | 29 | int PutC(const char* const d); |
uci1 | 0:26c9189e5924 | 30 | |
uci1 | 0:26c9189e5924 | 31 | protected: |
uci1 | 0:26c9189e5924 | 32 | template<class DATA, class SENDRECV> |
uci1 | 0:26c9189e5924 | 33 | int DoIO(DATA data, |
uci1 | 0:26c9189e5924 | 34 | const uint32_t length, |
uci1 | 0:26c9189e5924 | 35 | const uint32_t timeout_clock, |
uci1 | 0:26c9189e5924 | 36 | USBCheckDataInBuf able, |
uci1 | 0:26c9189e5924 | 37 | SENDRECV fcn) { |
uci1 | 0:26c9189e5924 | 38 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 0:26c9189e5924 | 39 | int res=0; |
uci1 | 0:26c9189e5924 | 40 | uint32_t b=0; |
uci1 | 0:26c9189e5924 | 41 | while ( (length>b) ) { |
uci1 | 0:26c9189e5924 | 42 | if (IsTimedOut(timeout_clock)) { |
uci1 | 0:26c9189e5924 | 43 | break; |
uci1 | 0:26c9189e5924 | 44 | } |
uci1 | 0:26c9189e5924 | 45 | if ( CALL_MEMBER_FN(*fgCpu, able)() ) { // readable/writable ? |
uci1 | 0:26c9189e5924 | 46 | res = CALL_MEMBER_FN(*this, fcn)(data+b); |
uci1 | 0:26c9189e5924 | 47 | if (res<sizeof(char)) { |
uci1 | 0:26c9189e5924 | 48 | return b; // error |
uci1 | 0:26c9189e5924 | 49 | } else { |
uci1 | 0:26c9189e5924 | 50 | b += res; |
uci1 | 0:26c9189e5924 | 51 | } |
uci1 | 0:26c9189e5924 | 52 | } else { |
uci1 | 0:26c9189e5924 | 53 | wait_ms(10); |
uci1 | 0:26c9189e5924 | 54 | } |
uci1 | 0:26c9189e5924 | 55 | } |
uci1 | 0:26c9189e5924 | 56 | |
uci1 | 0:26c9189e5924 | 57 | return b; |
uci1 | 0:26c9189e5924 | 58 | } |
uci1 | 0:26c9189e5924 | 59 | /* |
uci1 | 0:26c9189e5924 | 60 | int DoIO(char* const data, |
uci1 | 0:26c9189e5924 | 61 | const uint32_t length, |
uci1 | 0:26c9189e5924 | 62 | const uint32_t timeout_clock, |
uci1 | 0:26c9189e5924 | 63 | USBCheckDataInBuf able, |
uci1 | 0:26c9189e5924 | 64 | USBPutGetC fcn); |
uci1 | 0:26c9189e5924 | 65 | */ |
uci1 | 0:26c9189e5924 | 66 | virtual int32_t ReceiveAll(char* const buf, const uint32_t mlen, |
uci1 | 0:26c9189e5924 | 67 | const uint32_t timeout_clock); |
uci1 | 0:26c9189e5924 | 68 | virtual int32_t SendAll(const char* const data, const uint32_t length, |
uci1 | 0:26c9189e5924 | 69 | const uint32_t timeout_clock); |
uci1 | 3:a7f72492f19e | 70 | virtual int32_t FinishSending(const uint32_t) { |
uci1 | 3:a7f72492f19e | 71 | // all data sent in SendAll; 0 bytes sent here |
uci1 | 3:a7f72492f19e | 72 | return 0; |
uci1 | 3:a7f72492f19e | 73 | } |
uci1 | 0:26c9189e5924 | 74 | |
uci1 | 0:26c9189e5924 | 75 | |
uci1 | 0:26c9189e5924 | 76 | public: |
uci1 | 0:26c9189e5924 | 77 | SnCommUsb(SN_USB_SERIAL* c=0) { if (c!=0) { SetupPort(c); } } |
uci1 | 0:26c9189e5924 | 78 | |
uci1 | 0:26c9189e5924 | 79 | virtual ~SnCommUsb() {} |
uci1 | 0:26c9189e5924 | 80 | |
uci1 | 0:26c9189e5924 | 81 | static |
uci1 | 0:26c9189e5924 | 82 | bool IsPortSetup() { return fgCpu!=0; } |
uci1 | 0:26c9189e5924 | 83 | |
uci1 | 0:26c9189e5924 | 84 | static |
uci1 | 0:26c9189e5924 | 85 | bool SetupPort(SN_USB_SERIAL* cpu); |
uci1 | 0:26c9189e5924 | 86 | |
uci1 | 0:26c9189e5924 | 87 | virtual bool Connect(const uint32_t timeout) |
uci1 | 0:26c9189e5924 | 88 | { return IsPortSetup(); } |
uci1 | 0:26c9189e5924 | 89 | |
uci1 | 0:26c9189e5924 | 90 | virtual bool CloseConn(const uint32_t) { return true; } |
uci1 | 3:a7f72492f19e | 91 | virtual bool PowerDown(const uint32_t) { return true; } |
uci1 | 0:26c9189e5924 | 92 | |
uci1 | 0:26c9189e5924 | 93 | |
uci1 | 0:26c9189e5924 | 94 | |
uci1 | 0:26c9189e5924 | 95 | }; |
uci1 | 0:26c9189e5924 | 96 | |
uci1 | 0:26c9189e5924 | 97 | #endif // ENABLE_USB |
uci1 | 0:26c9189e5924 | 98 | |
uci1 | 0:26c9189e5924 | 99 | |
uci1 | 0:26c9189e5924 | 100 | #endif // SN_SnCommUsb |