Arianna station communication peripherals.
Dependents: AutonomousDAQ AutonomousDAQ
SnCommPeripheral.cpp@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 | #include "SnCommPeripheral.h" |
uci1 | 0:26c9189e5924 | 2 | |
uci1 | 0:26c9189e5924 | 3 | #include <string> |
uci1 | 0:26c9189e5924 | 4 | |
uci1 | 0:26c9189e5924 | 5 | #include "SnHeaderFrame.h" |
uci1 | 0:26c9189e5924 | 6 | |
uci1 | 0:26c9189e5924 | 7 | bool SnCommPeripheral::IsTimedOut(const uint32_t timeout_clock) const { |
uci1 | 0:26c9189e5924 | 8 | if (timeout_clock==0) { |
uci1 | 0:26c9189e5924 | 9 | // for not obeying timeout option |
uci1 | 0:26c9189e5924 | 10 | return false; |
uci1 | 0:26c9189e5924 | 11 | } else { |
uci1 | 0:26c9189e5924 | 12 | const uint32_t ct = time(0); |
uci1 | 0:26c9189e5924 | 13 | if ( (ct==0) || |
uci1 | 0:26c9189e5924 | 14 | (fabs(static_cast<double>(timeout_clock-ct))>kSecsPerDay) ) { |
uci1 | 0:26c9189e5924 | 15 | // clock problems! |
uci1 | 0:26c9189e5924 | 16 | // timeout now. hope the clock problems |
uci1 | 0:26c9189e5924 | 17 | // get fixed in the next comm window |
uci1 | 0:26c9189e5924 | 18 | return true; |
uci1 | 0:26c9189e5924 | 19 | } else { |
uci1 | 0:26c9189e5924 | 20 | return (ct>timeout_clock); |
uci1 | 0:26c9189e5924 | 21 | } |
uci1 | 0:26c9189e5924 | 22 | } |
uci1 | 0:26c9189e5924 | 23 | } |
uci1 | 0:26c9189e5924 | 24 | |
uci1 | 0:26c9189e5924 | 25 | bool SnCommPeripheral::SendString(const char* str, |
uci1 | 0:26c9189e5924 | 26 | const uint32_t timeout) { |
uci1 | 0:26c9189e5924 | 27 | #ifdef DEBUG |
uci1 | 0:26c9189e5924 | 28 | printf("SnCommPeripheral::SendString %s\r\n",str); |
uci1 | 0:26c9189e5924 | 29 | #endif |
uci1 | 0:26c9189e5924 | 30 | const size_t rlen = strlen(str); |
uci1 | 0:26c9189e5924 | 31 | const size_t slen = rlen > kMaxStrLen ? kMaxStrLen : rlen; |
uci1 | 0:26c9189e5924 | 32 | const int msiz = slen+SnHeaderFrame::SizeOf(); |
uci1 | 0:26c9189e5924 | 33 | char* const ts = new char[msiz]; |
uci1 | 0:26c9189e5924 | 34 | char* t = ts; |
uci1 | 0:26c9189e5924 | 35 | SnHeaderFrame::WriteTo(t, SnHeaderFrame::kStringCode, slen); |
uci1 | 0:26c9189e5924 | 36 | strncpy(t, str, slen); |
uci1 | 3:a7f72492f19e | 37 | int32_t mlen = SendAll(ts, msiz, timeout); |
uci1 | 0:26c9189e5924 | 38 | delete[] ts; |
uci1 | 3:a7f72492f19e | 39 | mlen += FinishSending(timeout); |
uci1 | 0:26c9189e5924 | 40 | return (msiz==mlen); |
uci1 | 0:26c9189e5924 | 41 | } |
uci1 | 0:26c9189e5924 | 42 | |
uci1 | 0:26c9189e5924 | 43 | void SnCommPeripheral::CapitalizeInPlace(std::string::iterator s, |
uci1 | 0:26c9189e5924 | 44 | const std::string::const_iterator send) { |
uci1 | 0:26c9189e5924 | 45 | static const char upd = 'a' - 'A'; // a>A |
uci1 | 0:26c9189e5924 | 46 | for (; s!=send; ++s) { |
uci1 | 0:26c9189e5924 | 47 | if ( ((*s)>='a') && ((*s)<='z') ) { |
uci1 | 0:26c9189e5924 | 48 | (*s) -= upd; |
uci1 | 0:26c9189e5924 | 49 | } |
uci1 | 0:26c9189e5924 | 50 | } |
uci1 | 0:26c9189e5924 | 51 | } |