Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnEventFrame.cpp@110:d1da040a0cf2, 2015-11-24 (annotated)
- Committer:
- uci1
- Date:
- Tue Nov 24 21:52:27 2015 +0000
- Revision:
- 110:d1da040a0cf2
- Parent:
- 56:0bba0ef15697
- Child:
- 114:554fa3a956b4
Stn32 (slow) with conf name. SD stall fix. No interface chip. Safety nets.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 21:ce51bb0ba4a5 | 1 | #include "SnEventFrame.h" |
uci1 | 21:ce51bb0ba4a5 | 2 | |
uci1 | 21:ce51bb0ba4a5 | 3 | #include "SnCRCUtils.h" |
uci1 | 21:ce51bb0ba4a5 | 4 | |
uci1 | 21:ce51bb0ba4a5 | 5 | //#define DEBUG |
uci1 | 21:ce51bb0ba4a5 | 6 | |
uci1 | 56:0bba0ef15697 | 7 | #if CHIPBOARD==ATWD4CH |
uci1 | 21:ce51bb0ba4a5 | 8 | const uint8_t SnEventFrame::kIOVers = 1; // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!) |
uci1 | 56:0bba0ef15697 | 9 | #else // SST |
uci1 | 56:0bba0ef15697 | 10 | const uint8_t SnEventFrame::kIOVers = 2; // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!) |
uci1 | 56:0bba0ef15697 | 11 | #endif |
uci1 | 21:ce51bb0ba4a5 | 12 | |
uci1 | 21:ce51bb0ba4a5 | 13 | const char* SnEventFrame::ReadFrom(const char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 14 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 21:ce51bb0ba4a5 | 15 | const uint16_t wvBaseline) { |
uci1 | 21:ce51bb0ba4a5 | 16 | // no check on the length of buf is done here |
uci1 | 21:ce51bb0ba4a5 | 17 | // that should be been done already |
uci1 | 21:ce51bb0ba4a5 | 18 | // |
uci1 | 21:ce51bb0ba4a5 | 19 | // must match ::WriteToBuf |
uci1 | 21:ce51bb0ba4a5 | 20 | |
uci1 | 21:ce51bb0ba4a5 | 21 | // NOTE: on the mbed, this is reduntant (char is unsigned) |
uci1 | 21:ce51bb0ba4a5 | 22 | // but this way, the block can be copied to other computers |
uci1 | 21:ce51bb0ba4a5 | 23 | // and still work. |
uci1 | 21:ce51bb0ba4a5 | 24 | union Usub { |
uci1 | 21:ce51bb0ba4a5 | 25 | const char* s; |
uci1 | 21:ce51bb0ba4a5 | 26 | const uint8_t* u; |
uci1 | 21:ce51bb0ba4a5 | 27 | } b; |
uci1 | 21:ce51bb0ba4a5 | 28 | b.s = buf; |
uci1 | 56:0bba0ef15697 | 29 | |
uci1 | 21:ce51bb0ba4a5 | 30 | uint8_t Rv=0; |
uci1 | 21:ce51bb0ba4a5 | 31 | b.s = SnBitUtils::ReadFrom(b.s, Rv); // i/o version |
uci1 | 21:ce51bb0ba4a5 | 32 | if (Rv>0) { |
uci1 | 56:0bba0ef15697 | 33 | |
uci1 | 56:0bba0ef15697 | 34 | const uint16_t nsamps = GetTotSamplesForIOVers(Rv); |
uci1 | 56:0bba0ef15697 | 35 | |
uci1 | 21:ce51bb0ba4a5 | 36 | b.s = SnBitUtils::ReadFrom(b.s, fMbedTime); |
uci1 | 21:ce51bb0ba4a5 | 37 | b.s = SnBitUtils::ReadFrom(b.s, fEvtNum); |
uci1 | 21:ce51bb0ba4a5 | 38 | b.s = SnBitUtils::ReadFrom(b.s, fDTms); |
uci1 | 21:ce51bb0ba4a5 | 39 | b.s = SnBitUtils::ReadFrom(b.s, fTrgNum); |
uci1 | 21:ce51bb0ba4a5 | 40 | b.s = SnBitUtils::ReadFrom(b.s, fTrgBits); |
uci1 | 56:0bba0ef15697 | 41 | b.u = UnpackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline, nsamps); |
uci1 | 21:ce51bb0ba4a5 | 42 | b.s = SnBitUtils::ReadFrom(b.s, fCRC); |
uci1 | 56:0bba0ef15697 | 43 | |
uci1 | 56:0bba0ef15697 | 44 | #if CHIPBOARD!=ATWD4CH |
uci1 | 56:0bba0ef15697 | 45 | const uint16_t nstopBytes = GetStopBytesForIOVers(Rv); |
uci1 | 56:0bba0ef15697 | 46 | if (Rv>1) { |
uci1 | 56:0bba0ef15697 | 47 | b.s = SnBitUtils::ReadFrom(b.s, fStop, nstopBytes); |
uci1 | 56:0bba0ef15697 | 48 | } |
uci1 | 56:0bba0ef15697 | 49 | #endif |
uci1 | 21:ce51bb0ba4a5 | 50 | } |
uci1 | 21:ce51bb0ba4a5 | 51 | |
uci1 | 21:ce51bb0ba4a5 | 52 | return b.s; |
uci1 | 21:ce51bb0ba4a5 | 53 | } |
uci1 | 21:ce51bb0ba4a5 | 54 | |
uci1 | 21:ce51bb0ba4a5 | 55 | char* SnEventFrame::WriteTo(char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 56 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 21:ce51bb0ba4a5 | 57 | const uint16_t wvBaseline) const { |
uci1 | 21:ce51bb0ba4a5 | 58 | // no check on the length of the buf is done here |
uci1 | 21:ce51bb0ba4a5 | 59 | // that should be done already |
uci1 | 21:ce51bb0ba4a5 | 60 | // |
uci1 | 21:ce51bb0ba4a5 | 61 | // must match ReadFromBuf |
uci1 | 21:ce51bb0ba4a5 | 62 | |
uci1 | 21:ce51bb0ba4a5 | 63 | // NOTE: on the mbed, this is reduntant (char is unsigned) |
uci1 | 21:ce51bb0ba4a5 | 64 | // but this way, the block can be copied to other computers |
uci1 | 21:ce51bb0ba4a5 | 65 | // and still work. |
uci1 | 21:ce51bb0ba4a5 | 66 | union { |
uci1 | 21:ce51bb0ba4a5 | 67 | char* s; |
uci1 | 21:ce51bb0ba4a5 | 68 | uint8_t* u; |
uci1 | 21:ce51bb0ba4a5 | 69 | } b; |
uci1 | 21:ce51bb0ba4a5 | 70 | b.s = buf; |
uci1 | 21:ce51bb0ba4a5 | 71 | |
uci1 | 21:ce51bb0ba4a5 | 72 | b.s = SnBitUtils::WriteTo(b.s, kIOVers); // i/o version |
uci1 | 21:ce51bb0ba4a5 | 73 | |
uci1 | 56:0bba0ef15697 | 74 | const uint16_t nsamps = GetTotSamplesForIOVers(kIOVers); |
uci1 | 56:0bba0ef15697 | 75 | |
uci1 | 21:ce51bb0ba4a5 | 76 | b.s = SnBitUtils::WriteTo(b.s, fMbedTime); |
uci1 | 21:ce51bb0ba4a5 | 77 | b.s = SnBitUtils::WriteTo(b.s, fEvtNum); |
uci1 | 21:ce51bb0ba4a5 | 78 | b.s = SnBitUtils::WriteTo(b.s, fDTms); |
uci1 | 21:ce51bb0ba4a5 | 79 | b.s = SnBitUtils::WriteTo(b.s, fTrgNum); |
uci1 | 21:ce51bb0ba4a5 | 80 | b.s = SnBitUtils::WriteTo(b.s, fTrgBits); |
uci1 | 56:0bba0ef15697 | 81 | b.u = PackWavef(b.u, fData, loseLSB, loseMSB, wvBaseline, nsamps); |
uci1 | 21:ce51bb0ba4a5 | 82 | b.s = SnBitUtils::WriteTo(b.s, fCRC); |
uci1 | 21:ce51bb0ba4a5 | 83 | |
uci1 | 56:0bba0ef15697 | 84 | #if CHIPBOARD!=ATWD4CH |
uci1 | 56:0bba0ef15697 | 85 | const uint16_t nstopBytes = GetStopBytesForIOVers(kIOVers); |
uci1 | 56:0bba0ef15697 | 86 | if (kIOVers>1) { |
uci1 | 56:0bba0ef15697 | 87 | // we test kIOVers rather than the CHIPBOARD #define in order |
uci1 | 56:0bba0ef15697 | 88 | // to ensure that the i/o version is the ONLY variable that determines |
uci1 | 56:0bba0ef15697 | 89 | // what the event looks like in a file or communication. |
uci1 | 56:0bba0ef15697 | 90 | // this is the only way to ensure readability going forward. |
uci1 | 56:0bba0ef15697 | 91 | b.s = SnBitUtils::WriteTo(b.s, fStop, nstopBytes); |
uci1 | 56:0bba0ef15697 | 92 | } |
uci1 | 56:0bba0ef15697 | 93 | #endif |
uci1 | 56:0bba0ef15697 | 94 | |
uci1 | 21:ce51bb0ba4a5 | 95 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 96 | printf("SnEventFrame::WriteTo:\r\n"); |
uci1 | 21:ce51bb0ba4a5 | 97 | for (uint32_t i=0; i<b.s-buf; i++) { |
uci1 | 21:ce51bb0ba4a5 | 98 | printf("%02X ",buf[i]); |
uci1 | 21:ce51bb0ba4a5 | 99 | } |
uci1 | 21:ce51bb0ba4a5 | 100 | printf("\r\n"); |
uci1 | 21:ce51bb0ba4a5 | 101 | #endif |
uci1 | 21:ce51bb0ba4a5 | 102 | |
uci1 | 21:ce51bb0ba4a5 | 103 | return b.s; |
uci1 | 21:ce51bb0ba4a5 | 104 | } |
uci1 | 21:ce51bb0ba4a5 | 105 | |
uci1 | 21:ce51bb0ba4a5 | 106 | void SnEventFrame::CalcCRC() { |
uci1 | 21:ce51bb0ba4a5 | 107 | // CRC made using union on a little endian (mbed) processor |
uci1 | 42:ac162d15e578 | 108 | fCRC = SnCRCUtils::GetCRC32for(fData, kTotSamps); |
uci1 | 56:0bba0ef15697 | 109 | #if CHIPBOARD!=ATWD4CH |
uci1 | 56:0bba0ef15697 | 110 | fCRC = SnCRCUtils::GetUpdatedCRC32for(fCRC, fStop, kNstopBytes); |
uci1 | 56:0bba0ef15697 | 111 | #endif |
uci1 | 42:ac162d15e578 | 112 | #ifdef DEBUG |
uci1 | 42:ac162d15e578 | 113 | printf("SnEventFrame::CalcCRC crc=%u\r\n",fCRC); |
uci1 | 42:ac162d15e578 | 114 | #endif |
uci1 | 21:ce51bb0ba4a5 | 115 | } |
uci1 | 21:ce51bb0ba4a5 | 116 | |
uci1 | 21:ce51bb0ba4a5 | 117 | bool SnEventFrame::ReadFromFileToBuf(FILE* f, |
uci1 | 21:ce51bb0ba4a5 | 118 | char* const evtBuf, |
uci1 | 21:ce51bb0ba4a5 | 119 | const uint8_t loseLSB, |
uci1 | 21:ce51bb0ba4a5 | 120 | const uint8_t loseMSB) { |
uci1 | 21:ce51bb0ba4a5 | 121 | // file position expected to be at this event frame already |
uci1 | 21:ce51bb0ba4a5 | 122 | bool ret = false; |
uci1 | 21:ce51bb0ba4a5 | 123 | if (f!=0) { |
uci1 | 25:57b2627fe756 | 124 | if (fread(evtBuf, SizeOf(kIOVers, loseLSB, loseMSB), 1u, f)!=0) { |
uci1 | 21:ce51bb0ba4a5 | 125 | ret = true; |
uci1 | 21:ce51bb0ba4a5 | 126 | } |
uci1 | 21:ce51bb0ba4a5 | 127 | } |
uci1 | 21:ce51bb0ba4a5 | 128 | return ret; |
uci1 | 21:ce51bb0ba4a5 | 129 | } |
uci1 | 21:ce51bb0ba4a5 | 130 | |
uci1 | 21:ce51bb0ba4a5 | 131 | bool SnEventFrame::ReadFrom(FILE* f, |
uci1 | 21:ce51bb0ba4a5 | 132 | char* const evtBuf, |
uci1 | 21:ce51bb0ba4a5 | 133 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 21:ce51bb0ba4a5 | 134 | const uint16_t wvBaseline) { |
uci1 | 21:ce51bb0ba4a5 | 135 | // file position expected to be at this event frame already |
uci1 | 21:ce51bb0ba4a5 | 136 | const bool ret = ReadFromFileToBuf(f, evtBuf, loseLSB, loseMSB); |
uci1 | 21:ce51bb0ba4a5 | 137 | if (ret) { |
uci1 | 21:ce51bb0ba4a5 | 138 | ReadFrom(evtBuf, loseLSB, loseMSB, wvBaseline); |
uci1 | 21:ce51bb0ba4a5 | 139 | } |
uci1 | 21:ce51bb0ba4a5 | 140 | return ret; |
uci1 | 21:ce51bb0ba4a5 | 141 | } |
uci1 | 21:ce51bb0ba4a5 | 142 | |
uci1 | 21:ce51bb0ba4a5 | 143 | bool SnEventFrame::WriteTo(FILE* f, char* const evtBuf, |
uci1 | 21:ce51bb0ba4a5 | 144 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 21:ce51bb0ba4a5 | 145 | const uint16_t wvBaseline) const { |
uci1 | 21:ce51bb0ba4a5 | 146 | // file pointer should be in correct location |
uci1 | 21:ce51bb0ba4a5 | 147 | |
uci1 | 21:ce51bb0ba4a5 | 148 | WriteTo(evtBuf, loseLSB, loseMSB, wvBaseline); |
uci1 | 21:ce51bb0ba4a5 | 149 | return WriteToFileFromBuf(f, evtBuf, loseLSB, loseMSB); |
uci1 | 21:ce51bb0ba4a5 | 150 | } |
uci1 | 21:ce51bb0ba4a5 | 151 | |
uci1 | 21:ce51bb0ba4a5 | 152 | bool SnEventFrame::WriteToFileFromBuf(FILE* f, char* const evtBuf, |
uci1 | 21:ce51bb0ba4a5 | 153 | const uint8_t loseLSB, const uint8_t loseMSB) { |
uci1 | 21:ce51bb0ba4a5 | 154 | // file pointer should be in correct location |
uci1 | 21:ce51bb0ba4a5 | 155 | |
uci1 | 21:ce51bb0ba4a5 | 156 | bool ret = false; |
uci1 | 21:ce51bb0ba4a5 | 157 | if (f!=0) { |
uci1 | 21:ce51bb0ba4a5 | 158 | fwrite(evtBuf, SizeOf(kIOVers, loseLSB, loseMSB), 1u, f); |
uci1 | 21:ce51bb0ba4a5 | 159 | ret = (ferror(f)==false); |
uci1 | 21:ce51bb0ba4a5 | 160 | } |
uci1 | 21:ce51bb0ba4a5 | 161 | return ret; |
uci1 | 21:ce51bb0ba4a5 | 162 | |
uci1 | 21:ce51bb0ba4a5 | 163 | } |
uci1 | 21:ce51bb0ba4a5 | 164 | |
uci1 | 21:ce51bb0ba4a5 | 165 | |
uci1 | 21:ce51bb0ba4a5 | 166 | uint8_t* SnEventFrame::PackWavef(uint8_t* const buf, const uint16_t* const data, |
uci1 | 21:ce51bb0ba4a5 | 167 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 56:0bba0ef15697 | 168 | const uint16_t wvBaseline, |
uci1 | 56:0bba0ef15697 | 169 | const uint16_t nsamps) { |
uci1 | 21:ce51bb0ba4a5 | 170 | // Compress the data. This is potentially LOSSY; it depends |
uci1 | 21:ce51bb0ba4a5 | 171 | // on the dynamic range and on the options. |
uci1 | 21:ce51bb0ba4a5 | 172 | // See SnConfigFrame::fWvLoseLSB and SnConfigFrame::fWvLoseMSB. |
uci1 | 21:ce51bb0ba4a5 | 173 | // If the number of least signficant bits to lose is not 0, the |
uci1 | 21:ce51bb0ba4a5 | 174 | // compression will be lossy (decreased resolution -- this is ok |
uci1 | 21:ce51bb0ba4a5 | 175 | // if the noise of the signal is much greater than the resolution). |
uci1 | 21:ce51bb0ba4a5 | 176 | // Losing the most significant bits will only be lossy if the |
uci1 | 21:ce51bb0ba4a5 | 177 | // signal-SnConfigFrame::fWvBaseline cannot fit in the reduced |
uci1 | 21:ce51bb0ba4a5 | 178 | // dynamic range (each MSB bit reducing the DR by a factor of 2). |
uci1 | 21:ce51bb0ba4a5 | 179 | // |
uci1 | 21:ce51bb0ba4a5 | 180 | // Note that the mbed uses little endian. Behavior should be the |
uci1 | 21:ce51bb0ba4a5 | 181 | // same on big endian, but this has not been tested. |
uci1 | 21:ce51bb0ba4a5 | 182 | // |
uci1 | 21:ce51bb0ba4a5 | 183 | // Use an unsigned buffer to prevent bits being changed during |
uci1 | 21:ce51bb0ba4a5 | 184 | // an implicit unsigned -> signed cast. |
uci1 | 21:ce51bb0ba4a5 | 185 | // |
uci1 | 21:ce51bb0ba4a5 | 186 | // buf = the byte array into which the data should be packed |
uci1 | 21:ce51bb0ba4a5 | 187 | // data = the data to pack |
uci1 | 21:ce51bb0ba4a5 | 188 | // loseLSB = number of least significant bits to throw away |
uci1 | 21:ce51bb0ba4a5 | 189 | // loseMSB = number of most significant bits to throw away |
uci1 | 21:ce51bb0ba4a5 | 190 | // wvBaseline = baseline to subtract to from ADC before packing |
uci1 | 21:ce51bb0ba4a5 | 191 | |
uci1 | 21:ce51bb0ba4a5 | 192 | const uint32_t blen = SizeOfPackedWavef(loseLSB, loseMSB); |
uci1 | 21:ce51bb0ba4a5 | 193 | const uint8_t packSmpBits = BITS_IN_SHORT-loseLSB-loseMSB; |
uci1 | 21:ce51bb0ba4a5 | 194 | |
uci1 | 21:ce51bb0ba4a5 | 195 | // make sure this buffer space is all 0's to start |
uci1 | 21:ce51bb0ba4a5 | 196 | memset(buf, 0, blen*sizeof(uint8_t)); |
uci1 | 21:ce51bb0ba4a5 | 197 | |
uci1 | 21:ce51bb0ba4a5 | 198 | const uint16_t clipHi = uint16_t( |
uci1 | 21:ce51bb0ba4a5 | 199 | uint16_t(uint16_t(0xFFFFu >> loseLSB) << (loseLSB+loseMSB)) |
uci1 | 21:ce51bb0ba4a5 | 200 | >> loseMSB); |
uci1 | 21:ce51bb0ba4a5 | 201 | |
uci1 | 21:ce51bb0ba4a5 | 202 | uint8_t* b = buf; |
uci1 | 21:ce51bb0ba4a5 | 203 | const uint16_t* dev = data; |
uci1 | 21:ce51bb0ba4a5 | 204 | uint16_t dum; |
uci1 | 21:ce51bb0ba4a5 | 205 | int8_t sbit=0; |
uci1 | 56:0bba0ef15697 | 206 | for (uint16_t i=0; i<nsamps; ++i, ++dev) { |
uci1 | 21:ce51bb0ba4a5 | 207 | // dump the bits we don't want |
uci1 | 21:ce51bb0ba4a5 | 208 | dum = (*dev) - wvBaseline; |
uci1 | 21:ce51bb0ba4a5 | 209 | if (dum<clipHi) { |
uci1 | 21:ce51bb0ba4a5 | 210 | dum >>= loseLSB; |
uci1 | 21:ce51bb0ba4a5 | 211 | dum <<= (loseLSB+loseMSB); |
uci1 | 21:ce51bb0ba4a5 | 212 | } else { |
uci1 | 21:ce51bb0ba4a5 | 213 | dum = clipHi << loseMSB; |
uci1 | 21:ce51bb0ba4a5 | 214 | } |
uci1 | 21:ce51bb0ba4a5 | 215 | if (sbit<=0) { |
uci1 | 21:ce51bb0ba4a5 | 216 | // lose MSB's put in previous short (or none if sbit==0) |
uci1 | 21:ce51bb0ba4a5 | 217 | dum <<= -sbit; |
uci1 | 21:ce51bb0ba4a5 | 218 | *b |= dum >> 8u; // "first" byte of the short |
uci1 | 21:ce51bb0ba4a5 | 219 | dum <<= 8u; |
uci1 | 21:ce51bb0ba4a5 | 220 | *(b+1) |= dum >> 8u; // "second" |
uci1 | 21:ce51bb0ba4a5 | 221 | // move to next number (dev++ in the for loop) |
uci1 | 21:ce51bb0ba4a5 | 222 | // move starting bit up |
uci1 | 21:ce51bb0ba4a5 | 223 | // but stay in this byte of the buf (do not increment b) |
uci1 | 21:ce51bb0ba4a5 | 224 | // since kPackSmpBits <= kBitsInShort, sbit can't |
uci1 | 21:ce51bb0ba4a5 | 225 | // move past the end of the current two bytes |
uci1 | 21:ce51bb0ba4a5 | 226 | sbit += packSmpBits; |
uci1 | 21:ce51bb0ba4a5 | 227 | } else { |
uci1 | 21:ce51bb0ba4a5 | 228 | // first few bits towards the end of this short |
uci1 | 21:ce51bb0ba4a5 | 229 | dum >>= sbit; |
uci1 | 21:ce51bb0ba4a5 | 230 | *b |= dum >> 8u; // "first" byte of the short |
uci1 | 21:ce51bb0ba4a5 | 231 | dum <<= 8u; |
uci1 | 21:ce51bb0ba4a5 | 232 | *(b+1) |= dum >> 8u; // "second" |
uci1 | 21:ce51bb0ba4a5 | 233 | if ( (sbit+packSmpBits) >= BITS_IN_SHORT ) { |
uci1 | 21:ce51bb0ba4a5 | 234 | b+=2; // move to next short in the buf |
uci1 | 21:ce51bb0ba4a5 | 235 | i--; dev--; // but stay on this number |
uci1 | 21:ce51bb0ba4a5 | 236 | // move starting bit back into the short we just filled |
uci1 | 21:ce51bb0ba4a5 | 237 | sbit -= BITS_IN_SHORT; |
uci1 | 21:ce51bb0ba4a5 | 238 | } else { |
uci1 | 21:ce51bb0ba4a5 | 239 | // stay in this buffer and move to the next number |
uci1 | 21:ce51bb0ba4a5 | 240 | // move starting bit up |
uci1 | 21:ce51bb0ba4a5 | 241 | sbit += packSmpBits; |
uci1 | 21:ce51bb0ba4a5 | 242 | } |
uci1 | 21:ce51bb0ba4a5 | 243 | } |
uci1 | 21:ce51bb0ba4a5 | 244 | } |
uci1 | 21:ce51bb0ba4a5 | 245 | |
uci1 | 21:ce51bb0ba4a5 | 246 | return buf+blen; |
uci1 | 21:ce51bb0ba4a5 | 247 | } |
uci1 | 21:ce51bb0ba4a5 | 248 | |
uci1 | 21:ce51bb0ba4a5 | 249 | const uint8_t* SnEventFrame::UnpackWavef(const uint8_t* const buf, |
uci1 | 21:ce51bb0ba4a5 | 250 | uint16_t* const data, |
uci1 | 21:ce51bb0ba4a5 | 251 | const uint8_t loseLSB, |
uci1 | 21:ce51bb0ba4a5 | 252 | const uint8_t loseMSB, |
uci1 | 56:0bba0ef15697 | 253 | const uint16_t wvBaseline, |
uci1 | 56:0bba0ef15697 | 254 | const uint16_t nsamps) { |
uci1 | 21:ce51bb0ba4a5 | 255 | if (loseLSB==0 && loseMSB==0 && wvBaseline==0) { |
uci1 | 56:0bba0ef15697 | 256 | memcpy(data, buf, nsamps*sizeof(uint16_t)); |
uci1 | 21:ce51bb0ba4a5 | 257 | } else { |
uci1 | 21:ce51bb0ba4a5 | 258 | |
uci1 | 21:ce51bb0ba4a5 | 259 | const uint8_t packSmpBits = BITS_IN_SHORT-loseLSB-loseMSB; |
uci1 | 21:ce51bb0ba4a5 | 260 | |
uci1 | 21:ce51bb0ba4a5 | 261 | // make sure data is all 0's to start |
uci1 | 56:0bba0ef15697 | 262 | memset(data, 0, nsamps*sizeof(uint16_t)); |
uci1 | 21:ce51bb0ba4a5 | 263 | |
uci1 | 21:ce51bb0ba4a5 | 264 | const uint8_t* b = buf; |
uci1 | 21:ce51bb0ba4a5 | 265 | uint16_t* dev = data; |
uci1 | 21:ce51bb0ba4a5 | 266 | uint16_t dum; |
uci1 | 21:ce51bb0ba4a5 | 267 | int8_t sbit=0; |
uci1 | 56:0bba0ef15697 | 268 | for (uint16_t i=0; i<nsamps; ++i, ++dev) { |
uci1 | 21:ce51bb0ba4a5 | 269 | dum = (*b) << 8u; |
uci1 | 21:ce51bb0ba4a5 | 270 | dum |= *(b+1); |
uci1 | 21:ce51bb0ba4a5 | 271 | if (sbit<=0) { |
uci1 | 21:ce51bb0ba4a5 | 272 | dum >>= (-sbit+loseLSB+loseMSB); |
uci1 | 21:ce51bb0ba4a5 | 273 | dum <<= loseLSB; |
uci1 | 21:ce51bb0ba4a5 | 274 | *dev |= dum; |
uci1 | 21:ce51bb0ba4a5 | 275 | // add baseline and move to next number (dev++ in the for loop) |
uci1 | 21:ce51bb0ba4a5 | 276 | *dev += wvBaseline; |
uci1 | 21:ce51bb0ba4a5 | 277 | // but stay in this short of the buf (do not increment b) |
uci1 | 21:ce51bb0ba4a5 | 278 | // move starting bit up |
uci1 | 21:ce51bb0ba4a5 | 279 | sbit += packSmpBits; |
uci1 | 21:ce51bb0ba4a5 | 280 | } else { |
uci1 | 21:ce51bb0ba4a5 | 281 | dum <<= sbit; |
uci1 | 21:ce51bb0ba4a5 | 282 | dum >>= loseMSB+loseLSB; |
uci1 | 21:ce51bb0ba4a5 | 283 | dum <<= loseLSB; |
uci1 | 21:ce51bb0ba4a5 | 284 | *dev = dum; |
uci1 | 21:ce51bb0ba4a5 | 285 | if ( (sbit+packSmpBits) >= BITS_IN_SHORT ) { |
uci1 | 21:ce51bb0ba4a5 | 286 | b+=2; // move to next short in the buf |
uci1 | 21:ce51bb0ba4a5 | 287 | i--; dev--; // but stay on this number |
uci1 | 21:ce51bb0ba4a5 | 288 | // move starting bit back into the short we just read from |
uci1 | 21:ce51bb0ba4a5 | 289 | sbit -= BITS_IN_SHORT; |
uci1 | 21:ce51bb0ba4a5 | 290 | } else { |
uci1 | 21:ce51bb0ba4a5 | 291 | // add baseline and move to next number (dev++ in the for loop) |
uci1 | 21:ce51bb0ba4a5 | 292 | *dev += wvBaseline; |
uci1 | 21:ce51bb0ba4a5 | 293 | sbit += packSmpBits; |
uci1 | 21:ce51bb0ba4a5 | 294 | } |
uci1 | 21:ce51bb0ba4a5 | 295 | } |
uci1 | 21:ce51bb0ba4a5 | 296 | } |
uci1 | 21:ce51bb0ba4a5 | 297 | } |
uci1 | 21:ce51bb0ba4a5 | 298 | |
uci1 | 21:ce51bb0ba4a5 | 299 | return buf+SizeOfPackedWavef(loseLSB, loseMSB); |
uci1 | 21:ce51bb0ba4a5 | 300 | } |
uci1 | 21:ce51bb0ba4a5 | 301 |