Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sat Oct 05 04:45:22 2013 +0000
Revision:
40:1324da35afd4
Parent:
39:2f17131d22a5
Child:
53:9cc158391bea
first commit of major overhaul to 2013-2014 mbed code. NOT YET FULLY TESTED. too many changes to list (fix local file receive, fix rates, external comm packes, big SD cards, get to comm win w/o SD, v8 config frame, v4 files, SBD buffering changes...)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #include "SnConfigFrame.h"
uci1 0:664899e0b988 2
uci1 0:664899e0b988 3 #include "mbed.h"
uci1 0:664899e0b988 4
uci1 0:664899e0b988 5 #include "SnBitUtils.h"
uci1 16:744ce85aede2 6 #include "SnHeaderFrame.h"
uci1 22:f957c4f840ad 7 #include "Watchdog.h"
uci1 0:664899e0b988 8
uci1 0:664899e0b988 9 extern "C" void mbed_mac_address(char *);
uci1 0:664899e0b988 10
uci1 31:b5bd3b189150 11 bool SnConfigFrame::fgApplySafetyNets = true;
uci1 40:1324da35afd4 12 const uint8_t SnConfigFrame::kIOVers = 8;
uci1 3:24c5f0f50bf1 13 const char* const SnConfigFrame::kDefConfFile = "/local/DEFCONF.DAT";
uci1 28:484943132bb0 14 const char* const SnConfigFrame::kDefIPflag = "DEF";
uci1 1:e392595b4b76 15 const uint32_t SnConfigFrame::kMinCommWinPrdLowPwr = 14400; // exclusive min low power comm win period (s)
uci1 1:e392595b4b76 16 const uint32_t SnConfigFrame::kMaxCommWinPrdLowPwr = 259200; // exclusive max low power comm win period (s)
uci1 1:e392595b4b76 17 const uint32_t SnConfigFrame::kMinCommWinDurLowPwr = 300; // exclusive min low power comm win duration (s)
uci1 1:e392595b4b76 18 const uint32_t SnConfigFrame::kMaxCommWinDurLowPwr = 3600; // exclusive max low power comm win duration (s)
uci1 1:e392595b4b76 19 const uint8_t SnConfigFrame::kConfLblLen;
uci1 0:664899e0b988 20
uci1 0:664899e0b988 21 uint64_t SnConfigFrame::fgMacAdr = 0;
uci1 0:664899e0b988 22
uci1 0:664899e0b988 23 void SnConfigFrame::SetMacAddress() {
uci1 0:664899e0b988 24 static const uint8_t b64 = sizeof(uint64_t);
uci1 0:664899e0b988 25 static char c[b64];
uci1 0:664899e0b988 26 mbed_mac_address(&(c[0]));
uci1 0:664899e0b988 27 // like a big endian union
uci1 0:664899e0b988 28 fgMacAdr = 0;
uci1 0:664899e0b988 29 const char* a = c+(b64-1);
uci1 0:664899e0b988 30 for (uint8_t i=0; i<b64; i++, a--) {
uci1 0:664899e0b988 31 fgMacAdr |= static_cast<uint64_t>(*a) << (i<<3);
uci1 0:664899e0b988 32 }
uci1 0:664899e0b988 33 }
uci1 0:664899e0b988 34
uci1 0:664899e0b988 35 void SnConfigFrame::SetHardDefaults() {
uci1 0:664899e0b988 36 sprintf(fLabel,"HardDefaults");
uci1 0:664899e0b988 37 fConfTime = 1338854400u; // Tue, 05 Jun 2012 00:00:00 GMT
uci1 0:664899e0b988 38 fRun = 0;
uci1 40:1324da35afd4 39 fFirstSeq = 0;
uci1 8:95a325df1f6b 40 fEvtsPerSeq = 1000;
uci1 8:95a325df1f6b 41 fRunMode = 0;
uci1 1:e392595b4b76 42 fStreamHiLoPlas = 0;
uci1 0:664899e0b988 43 fWvLoseLSB = 0;
uci1 1:e392595b4b76 44 fWvLoseMSB = 4;
uci1 0:664899e0b988 45 fWvBaseline = 0;
uci1 0:664899e0b988 46 fDatPackType = ~0;
uci1 0:664899e0b988 47 uint16_t* dc = &(fDAC[0][0]);
uci1 0:664899e0b988 48 for (uint16_t i=0; i<kTotDacs; i++, dc++) {
uci1 0:664899e0b988 49 *dc = 3072u;
uci1 0:664899e0b988 50 }
uci1 0:664899e0b988 51 fNumPlas = 1;
uci1 0:664899e0b988 52 uint16_t* pl = &(fPLA[0]);
uci1 0:664899e0b988 53 for (uint8_t j=0; j<kNplas; j++, pl++) {
uci1 0:664899e0b988 54 *pl = 0x7FFFu;
uci1 0:664899e0b988 55 }
uci1 28:484943132bb0 56 fNumCardsMajLog = 2;
uci1 0:664899e0b988 57 fEnableThermTrig = 1;
uci1 1:e392595b4b76 58 fForceTrigPeriod = 67u;
uci1 0:664899e0b988 59 fHeartBeatPeriod = 0;
uci1 0:664899e0b988 60 fAmpsOn = 0x0Fu;
uci1 0:664899e0b988 61 fEvtThrtlPeriodMs = 50u;
uci1 28:484943132bb0 62 fPowerMode = kAmpsDatTak|kCardDatTak|kAmpsComWin|kCardComWin|kIridComWin|kAfarComWin;
uci1 39:2f17131d22a5 63 fBatVoltToLowPwr = 0;
uci1 39:2f17131d22a5 64 fBatVoltFromLowPwr = 0;
uci1 28:484943132bb0 65 fVoltCheckPeriod = 600u;
uci1 28:484943132bb0 66 fCommWinPeriod = 3600u;
uci1 1:e392595b4b76 67 fCommWinDuration = 600u;
uci1 28:484943132bb0 68 fCommSendData = static_cast<int16_t>(kUseBits);
uci1 0:664899e0b988 69 fCommWinPrdLowPwr = 86100u;
uci1 0:664899e0b988 70 fCommWinDurLowPwr = 300u;
uci1 40:1324da35afd4 71 fCommWinConnectTOMin = 3u;
uci1 40:1324da35afd4 72 fCommWinListenTOMin = 3u;
uci1 22:f957c4f840ad 73 fWatchDogPeriod = WDFAILSAFE;
uci1 28:484943132bb0 74 SetDefaultIPs();
uci1 8:95a325df1f6b 75 fIsLowPower = false;
uci1 8:95a325df1f6b 76 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 77 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 78
uci1 8:95a325df1f6b 79
uci1 8:95a325df1f6b 80 }
uci1 8:95a325df1f6b 81
uci1 28:484943132bb0 82 void SnConfigFrame::SetDefaultRemoteServ() {
uci1 28:484943132bb0 83 sprintf(fRemoteServer,"%s","128.195.204.151");
uci1 28:484943132bb0 84 }
uci1 28:484943132bb0 85
uci1 28:484943132bb0 86 void SnConfigFrame::SetDefaultRemotePort() {
uci1 28:484943132bb0 87 fRemotePort = 6655;
uci1 28:484943132bb0 88 }
uci1 28:484943132bb0 89
uci1 28:484943132bb0 90 void SnConfigFrame::SetDefaultMbedIP() {
uci1 28:484943132bb0 91 const uint64_t ip = GetMacAddress();
uci1 28:484943132bb0 92 switch (ip) {
uci1 28:484943132bb0 93 case 0x0002F7F0C3B60000: // station 3
uci1 28:484943132bb0 94 sprintf(fMbedIP,"%s","157.132.94.35");
uci1 28:484943132bb0 95 break;
uci1 28:484943132bb0 96 case 0x0002F7F0C41C0000: // station 4
uci1 28:484943132bb0 97 sprintf(fMbedIP,"%s","157.132.94.37");
uci1 28:484943132bb0 98 break;
uci1 28:484943132bb0 99 case 0x0002F7F0AEE00000: // station 5
uci1 28:484943132bb0 100 sprintf(fMbedIP,"%s","157.132.94.39");
uci1 28:484943132bb0 101 break;
uci1 28:484943132bb0 102 case 0x0002F7F0C4450000: // station 6
uci1 28:484943132bb0 103 sprintf(fMbedIP,"%s","157.132.94.41");
uci1 28:484943132bb0 104 break;
uci1 28:484943132bb0 105 case 0x0002F7F0D2880000: // station 7
uci1 28:484943132bb0 106 sprintf(fMbedIP,"%s","157.132.94.43");
uci1 28:484943132bb0 107 break;
uci1 28:484943132bb0 108 case 0x0002F7F0C0F80000: // station 8
uci1 28:484943132bb0 109 sprintf(fMbedIP,"%s","157.132.94.45");
uci1 28:484943132bb0 110 break;
uci1 28:484943132bb0 111 default: // what??
uci1 28:484943132bb0 112 sprintf(fMbedIP,"%s","157.132.94.30"); // anciet station
uci1 28:484943132bb0 113 break;
uci1 28:484943132bb0 114 };
uci1 28:484943132bb0 115 }
uci1 28:484943132bb0 116 // brian dornick 3107
uci1 28:484943132bb0 117 void SnConfigFrame::SetDefaultMaskIP() {
uci1 28:484943132bb0 118 sprintf(fMbedMask,"%s","255.255.255.0");
uci1 28:484943132bb0 119 }
uci1 28:484943132bb0 120
uci1 28:484943132bb0 121 void SnConfigFrame::SetDefaultGateIP() {
uci1 28:484943132bb0 122 sprintf(fMbedGate,"%s","157.132.94.1");
uci1 28:484943132bb0 123 }
uci1 28:484943132bb0 124
uci1 28:484943132bb0 125 void SnConfigFrame::SetDefaultIPs() {
uci1 28:484943132bb0 126 SetDefaultRemoteServ();
uci1 28:484943132bb0 127 SetDefaultRemotePort();
uci1 28:484943132bb0 128 SetDefaultMbedIP();
uci1 28:484943132bb0 129 SetDefaultMaskIP();
uci1 28:484943132bb0 130 SetDefaultGateIP();
uci1 28:484943132bb0 131 }
uci1 28:484943132bb0 132
uci1 40:1324da35afd4 133 void SnConfigFrame::ApplyConnectListenSafetyNets() {
uci1 40:1324da35afd4 134 if (fCommWinConnectTOMin<kDefTimeoutMin) {
uci1 40:1324da35afd4 135 fCommWinConnectTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 136 }
uci1 40:1324da35afd4 137 if (fCommWinConnectTOMin>(fCommWinDuration/static_cast<float>(kNcomms))) {
uci1 40:1324da35afd4 138 fCommWinConnectTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 139 }
uci1 40:1324da35afd4 140 if (fCommWinListenTOMin<kDefTimeoutMin) {
uci1 40:1324da35afd4 141 fCommWinListenTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 142 }
uci1 40:1324da35afd4 143 if (fCommWinListenTOMin>(fCommWinDuration/static_cast<float>(kNcomms))) {
uci1 40:1324da35afd4 144 fCommWinListenTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 145 }
uci1 40:1324da35afd4 146 }
uci1 40:1324da35afd4 147
uci1 28:484943132bb0 148 void SnConfigFrame::ApplySafetyNets() {
uci1 40:1324da35afd4 149 if (fFirstSeq>kMaxFirstSeq) {
uci1 40:1324da35afd4 150 fFirstSeq=kMaxFirstSeq;
uci1 40:1324da35afd4 151 }
uci1 28:484943132bb0 152 if (fNumPlas>kNplas) {
uci1 28:484943132bb0 153 fNumPlas=kNplas;
uci1 28:484943132bb0 154 }
uci1 28:484943132bb0 155 if (fNumCardsMajLog>kNchans) {
uci1 28:484943132bb0 156 fNumCardsMajLog=kNchans;
uci1 28:484943132bb0 157 }
uci1 28:484943132bb0 158 if (fNumCardsMajLog<1u) {
uci1 28:484943132bb0 159 fNumCardsMajLog=1u;
uci1 28:484943132bb0 160 }
uci1 28:484943132bb0 161 if ( (fForceTrigPeriod>0) &&
uci1 28:484943132bb0 162 (fForceTrigPeriod<kMinForcePer) ) {
uci1 28:484943132bb0 163 fForceTrigPeriod = kMinForcePer;
uci1 28:484943132bb0 164 }
uci1 28:484943132bb0 165 if (fEvtThrtlPeriodMs>kMaxThrottlePerMs) {
uci1 28:484943132bb0 166 fEvtThrtlPeriodMs=kMaxThrottlePerMs;
uci1 28:484943132bb0 167 }
uci1 28:484943132bb0 168 if ( (IsPoweredFor(kIridComWin)==false) &&
uci1 28:484943132bb0 169 (IsPoweredFor(kAfarComWin)==false) ) {
uci1 28:484943132bb0 170 EnablePowerFor(kIridComWin);
uci1 28:484943132bb0 171 EnablePowerFor(kAfarComWin);
uci1 28:484943132bb0 172 }
uci1 39:2f17131d22a5 173 if (fBatVoltToLowPwr>kMaxBatVoltLowPwr) {
uci1 39:2f17131d22a5 174 fBatVoltToLowPwr=kMaxBatVoltLowPwr;
uci1 39:2f17131d22a5 175 }
uci1 39:2f17131d22a5 176 if (fBatVoltFromLowPwr>kMaxBatVoltLowPwr) {
uci1 39:2f17131d22a5 177 fBatVoltFromLowPwr=kMaxBatVoltLowPwr;
uci1 39:2f17131d22a5 178 }
uci1 39:2f17131d22a5 179 if (fBatVoltFromLowPwr<fBatVoltToLowPwr) {
uci1 39:2f17131d22a5 180 fBatVoltFromLowPwr=fBatVoltToLowPwr;
uci1 28:484943132bb0 181 }
uci1 28:484943132bb0 182 if (fCommWinPeriod>kMaxCommWinPeriod) {
uci1 28:484943132bb0 183 fCommWinPeriod=kMaxCommWinPeriod;
uci1 28:484943132bb0 184 }
uci1 40:1324da35afd4 185 if (fCommWinPeriod<kMinCommWinPeriod) {
uci1 40:1324da35afd4 186 fCommWinPeriod=kMinCommWinPeriod;
uci1 40:1324da35afd4 187 }
uci1 28:484943132bb0 188 if (fCommWinDuration<kMinCommWinDur) {
uci1 28:484943132bb0 189 fCommWinDuration=kMinCommWinDur;
uci1 28:484943132bb0 190 }
uci1 28:484943132bb0 191 if (fCommWinPrdLowPwr>kMaxCommWinPeriod) {
uci1 28:484943132bb0 192 fCommWinPrdLowPwr=kMaxCommWinPeriod;
uci1 28:484943132bb0 193 }
uci1 28:484943132bb0 194 if (fCommWinDurLowPwr<kMinCommWinDur) {
uci1 28:484943132bb0 195 fCommWinDurLowPwr=kMinCommWinDur;
uci1 28:484943132bb0 196 }
uci1 40:1324da35afd4 197 ApplyConnectListenSafetyNets();
uci1 28:484943132bb0 198 if (fWatchDogPeriod>kMaxWatchDogPer) {
uci1 28:484943132bb0 199 fWatchDogPeriod=kMaxWatchDogPer;
uci1 28:484943132bb0 200 }
uci1 28:484943132bb0 201 if (fWatchDogPeriod<kMinWatchDogPer) {
uci1 28:484943132bb0 202 fWatchDogPeriod=kMinWatchDogPer;
uci1 28:484943132bb0 203 }
uci1 28:484943132bb0 204 }
uci1 28:484943132bb0 205
uci1 21:ce51bb0ba4a5 206 uint32_t SnConfigFrame::GetTimeoutTime(const uint32_t startTime,
uci1 21:ce51bb0ba4a5 207 const uint32_t delta) const {
uci1 40:1324da35afd4 208 // --.-----lst------.--delta--.--
uci1 40:1324da35afd4 209 // . . .
uci1 40:1324da35afd4 210 // start current returnVal
uci1 40:1324da35afd4 211 //
uci1 40:1324da35afd4 212 // lio=lst+delta bound by comm wind dur
uci1 40:1324da35afd4 213 // returns start + lio
uci1 40:1324da35afd4 214
uci1 21:ce51bb0ba4a5 215 const uint32_t ct = time(0);
uci1 40:1324da35afd4 216 uint32_t lst = ct-startTime;
uci1 21:ce51bb0ba4a5 217 if ( (ct<startTime) || (ct==0) ||
uci1 21:ce51bb0ba4a5 218 (lst>kSecsPerDay) ) {
uci1 21:ce51bb0ba4a5 219 // possible clock problems
uci1 40:1324da35afd4 220 lst = static_cast<uint32_t>(kDefTimeoutMin)*60u;
uci1 21:ce51bb0ba4a5 221 }
uci1 21:ce51bb0ba4a5 222 const uint32_t lio =
uci1 21:ce51bb0ba4a5 223 ((lst+delta) < GetCommWinDuration()) ?
uci1 21:ce51bb0ba4a5 224 lst+delta : GetCommWinDuration();
uci1 21:ce51bb0ba4a5 225 return lio+startTime;
uci1 21:ce51bb0ba4a5 226 }
uci1 21:ce51bb0ba4a5 227
uci1 8:95a325df1f6b 228 void SnConfigFrame::ChangeToLowPower() {
uci1 8:95a325df1f6b 229
uci1 8:95a325df1f6b 230 // save old label
uci1 8:95a325df1f6b 231 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 232
uci1 8:95a325df1f6b 233 // append label
uci1 8:95a325df1f6b 234 // this will allow the new config to be put in the DB
uci1 8:95a325df1f6b 235 int slen = strlen(fLabel);
uci1 8:95a325df1f6b 236 static const char* tag = "_LOWPOW";
uci1 8:95a325df1f6b 237 const int ml = strlen(tag)+1;
uci1 8:95a325df1f6b 238 if (slen > (kConfLblLen-ml) ) {
uci1 8:95a325df1f6b 239 memset(fLabel+kConfLblLen-ml, '\0', ml);
uci1 8:95a325df1f6b 240 }
uci1 8:95a325df1f6b 241 strncat(fLabel, tag, ml-1);
uci1 8:95a325df1f6b 242
uci1 8:95a325df1f6b 243 // save power settings
uci1 8:95a325df1f6b 244 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 245
uci1 8:95a325df1f6b 246 // change power settings
uci1 8:95a325df1f6b 247 DisablePowerFor(kAmpsDatTak);
uci1 8:95a325df1f6b 248 DisablePowerFor(kCardDatTak);
uci1 8:95a325df1f6b 249 DisablePowerFor(kIridDatTak);
uci1 8:95a325df1f6b 250 DisablePowerFor(kAfarDatTak);
uci1 8:95a325df1f6b 251 DisablePowerFor(kAmpsComWin);
uci1 8:95a325df1f6b 252 DisablePowerFor(kCardComWin);
uci1 8:95a325df1f6b 253 if ( (IsPoweredFor(kIridComWin)==false) &&
uci1 8:95a325df1f6b 254 (IsPoweredFor(kAfarComWin)==false) ) {
uci1 8:95a325df1f6b 255 // TODO: turn on only iridum maybe?
uci1 8:95a325df1f6b 256 EnablePowerFor(kIridComWin);
uci1 8:95a325df1f6b 257 EnablePowerFor(kAfarComWin);
uci1 8:95a325df1f6b 258 }
uci1 8:95a325df1f6b 259 // set mode to low power
uci1 8:95a325df1f6b 260 fIsLowPower = true;
uci1 8:95a325df1f6b 261 }
uci1 8:95a325df1f6b 262
uci1 8:95a325df1f6b 263 void SnConfigFrame::ChangeToNormPower() {
uci1 8:95a325df1f6b 264 // put label back
uci1 8:95a325df1f6b 265 memcpy(fLabel, fNormLabel, kConfLblLen);
uci1 8:95a325df1f6b 266 // put power settings back
uci1 8:95a325df1f6b 267 fPowerMode = fNormPowerMode;
uci1 8:95a325df1f6b 268 // set mode to normal
uci1 8:95a325df1f6b 269 fIsLowPower = false;
uci1 0:664899e0b988 270 }
uci1 0:664899e0b988 271
uci1 0:664899e0b988 272 void SnConfigFrame::GetPackParsFor(const EDatPackBit d,
uci1 0:664899e0b988 273 uint8_t& loseLSB, uint8_t& loseMSB,
uci1 0:664899e0b988 274 uint16_t& wvBase) const {
uci1 0:664899e0b988 275 const bool pack = IsDatPackedFor(d);
uci1 0:664899e0b988 276 loseLSB = pack ? GetWvLoseLSB() : 0u;
uci1 0:664899e0b988 277 loseMSB = pack ? GetWvLoseMSB() : 0u;
uci1 0:664899e0b988 278 wvBase = pack ? GetWvBaseline() : 0u;
uci1 0:664899e0b988 279 }
uci1 0:664899e0b988 280
uci1 0:664899e0b988 281 void SnConfigFrame::GetHiLoPlas(const uint16_t pla,
uci1 0:664899e0b988 282 uint16_t& hiPla,
uci1 0:664899e0b988 283 uint16_t& loPla,
uci1 0:664899e0b988 284 const bool r2l) {
uci1 0:664899e0b988 285 // split the PLA bitword into 2: one for the high threshold
uci1 0:664899e0b988 286 // and one for the low threshold. "lows" in the string will become
uci1 0:664899e0b988 287 // "highs" in the low threshold PLA.
uci1 0:664899e0b988 288 //
uci1 0:664899e0b988 289 // example 1)
uci1 0:664899e0b988 290 // PLA string = HLHL....
uci1 0:664899e0b988 291 // hi thresh = H.H.....
uci1 0:664899e0b988 292 // lo thresh = .H.H....
uci1 0:664899e0b988 293 //
uci1 0:664899e0b988 294 // example 2)
uci1 0:664899e0b988 295 // PLA string = HBL.....
uci1 0:664899e0b988 296 // hi thresh = HL......
uci1 0:664899e0b988 297 // lo thresh = .LH.....
uci1 0:664899e0b988 298 //
uci1 0:664899e0b988 299 // (with . = A here, to make the example more readable)
uci1 0:664899e0b988 300 //
uci1 0:664899e0b988 301 // A = 11, B = 00
uci1 0:664899e0b988 302 // H = 01 or 10, alternating
uci1 0:664899e0b988 303 // L = 10 or 01, alternating
uci1 0:664899e0b988 304 // 01 at leftmost bits is H
uci1 0:664899e0b988 305 // for example:
uci1 0:664899e0b988 306 // 0x7FFF = 01 11 11 11 11 11 11 11
uci1 0:664899e0b988 307 // => HAAAAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 308 // => AAAAAAAH for RIGHT TO LEFT
uci1 0:664899e0b988 309 // 0x56FF = 01 01 01 10 11 11 11 11
uci1 0:664899e0b988 310 // => HLHHAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 311 // => AAAAHHLH for RIGHT TO LEFT
uci1 0:664899e0b988 312 //
uci1 0:664899e0b988 313 // so HHHHHHHH is
uci1 0:664899e0b988 314 // 01 10 01 10 01 10 01 10 always (r2l or l2r)
uci1 0:664899e0b988 315 //
uci1 0:664899e0b988 316 // r2l = whether to read bits right to left (true) or not (false)
uci1 0:664899e0b988 317 // Mahshid liked right to left
uci1 0:664899e0b988 318 // Liang liked left to right
uci1 0:664899e0b988 319 // so we allow for either
uci1 0:664899e0b988 320
uci1 0:664899e0b988 321 const int8_t start = (r2l) ? 0 : BITS_IN_SHORT-2;
uci1 0:664899e0b988 322 const int8_t end = (r2l) ? BITS_IN_SHORT : -2;
uci1 39:2f17131d22a5 323 const int8_t step = (r2l) ? 2 : -2;
uci1 0:664899e0b988 324
uci1 0:664899e0b988 325 uint8_t hi= (r2l) ? 0x2 : 0x1;
uci1 0:664899e0b988 326 uint8_t lo= (r2l) ? 0x1 : 0x2;
uci1 0:664899e0b988 327
uci1 0:664899e0b988 328 // set all bits to 0
uci1 0:664899e0b988 329 hiPla = 0;
uci1 0:664899e0b988 330 loPla = 0;
uci1 0:664899e0b988 331
uci1 0:664899e0b988 332 for (int8_t i=start; i!=end; i+=step, hi^=0x3, lo^=0x3) {
uci1 0:664899e0b988 333 const uint8_t b = (pla & (0x3<<i)) >> i;
uci1 0:664899e0b988 334 if (b==hi) {
uci1 0:664899e0b988 335 hiPla |= hi << i;
uci1 0:664899e0b988 336 loPla |= 0x3 << i;
uci1 0:664899e0b988 337 } else if (b==lo) {
uci1 0:664899e0b988 338 hiPla |= 0x3 << i;
uci1 0:664899e0b988 339 loPla |= hi << i;
uci1 0:664899e0b988 340 } else if (b==0x3) {
uci1 0:664899e0b988 341 // any
uci1 0:664899e0b988 342 hiPla |= 0x3 << i;
uci1 0:664899e0b988 343 loPla |= 0x3 << i;
uci1 0:664899e0b988 344 } else {
uci1 0:664899e0b988 345 // no check that b is something else.. should be impossible.
uci1 0:664899e0b988 346 // between
uci1 0:664899e0b988 347 hiPla |= lo << i;
uci1 0:664899e0b988 348 loPla |= lo << i;
uci1 0:664899e0b988 349 }
uci1 0:664899e0b988 350 }
uci1 0:664899e0b988 351
uci1 0:664899e0b988 352 }
uci1 0:664899e0b988 353
uci1 0:664899e0b988 354 bool SnConfigFrame::ReadFromFile(const char* cfile) {
uci1 0:664899e0b988 355 // intended only for reading default config file
uci1 0:664899e0b988 356
uci1 3:24c5f0f50bf1 357 /*
uci1 3:24c5f0f50bf1 358 DIR* d;
uci1 3:24c5f0f50bf1 359 struct dirent* dent;
uci1 3:24c5f0f50bf1 360 printf("files in /local:\r\n");
uci1 3:24c5f0f50bf1 361 if ( (d = opendir( "/local" ))!=NULL ) {
uci1 3:24c5f0f50bf1 362 while ( (dent = readdir(d))!=NULL ) {
uci1 3:24c5f0f50bf1 363 printf("%s\r\n",dent->d_name);
uci1 3:24c5f0f50bf1 364 }
uci1 3:24c5f0f50bf1 365 closedir(d);
uci1 3:24c5f0f50bf1 366 }
uci1 3:24c5f0f50bf1 367 */
uci1 0:664899e0b988 368 bool ret = false;
uci1 0:664899e0b988 369 FILE* cf = fopen(cfile,"rb");
uci1 0:664899e0b988 370 if (cf!=0) {
uci1 16:744ce85aede2 371 // check the header and file size to be
uci1 16:744ce85aede2 372 // protect a bit against corruption
uci1 16:744ce85aede2 373 uint8_t hc; uint32_t hl;
uci1 16:744ce85aede2 374 SnHeaderFrame::ReadFrom(cf, hc, hl);
uci1 16:744ce85aede2 375 if (hc==SnHeaderFrame::kConfigCode) {
uci1 16:744ce85aede2 376 const int fpos = ftell(cf);
uci1 16:744ce85aede2 377 // how many bytes?
uci1 16:744ce85aede2 378 fseek(cf, 0, SEEK_END); // go to end
uci1 16:744ce85aede2 379 const int fend = ftell(cf);
uci1 16:744ce85aede2 380 fseek(cf, fpos, SEEK_SET); // go back
uci1 16:744ce85aede2 381
uci1 16:744ce85aede2 382 if (hl == fend-fpos) {
uci1 16:744ce85aede2 383 ReadFrom(cf);
uci1 16:744ce85aede2 384 ret = (ferror(cf)==0);
uci1 16:744ce85aede2 385 }
uci1 16:744ce85aede2 386 }
uci1 0:664899e0b988 387 fclose(cf);
uci1 0:664899e0b988 388 }
uci1 0:664899e0b988 389 return ret;
uci1 0:664899e0b988 390 }
uci1 0:664899e0b988 391
uci1 0:664899e0b988 392 bool SnConfigFrame::WriteToFile(const char* cfile) const {
uci1 0:664899e0b988 393 // intended only for writing default config file
uci1 0:664899e0b988 394
uci1 0:664899e0b988 395 bool ret = false;
uci1 0:664899e0b988 396 FILE* cf = fopen(cfile,"wb");
uci1 0:664899e0b988 397 if (cf!=0) {
uci1 0:664899e0b988 398 WriteTo(cf);
uci1 0:664899e0b988 399 ret = (ferror(cf)==0);
uci1 0:664899e0b988 400 fclose(cf);
uci1 0:664899e0b988 401 }
uci1 0:664899e0b988 402 return ret;
uci1 0:664899e0b988 403 }