Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnEventFrame.h@119:b3d7699d0eb0, 2017-09-21 (annotated)
- Committer:
- uci1
- Date:
- Thu Sep 21 17:53:35 2017 +0000
- Revision:
- 119:b3d7699d0eb0
- Parent:
- 116:8099b754fbb4
2017-2018 season default for 4Ch (2GSa/s) SST
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:664899e0b988 | 1 | #ifndef SN_SnEventFrame |
uci1 | 0:664899e0b988 | 2 | #define SN_SnEventFrame |
uci1 | 0:664899e0b988 | 3 | |
uci1 | 0:664899e0b988 | 4 | #include <stdint.h> |
uci1 | 0:664899e0b988 | 5 | #include "SnConstants.h" |
uci1 | 0:664899e0b988 | 6 | |
uci1 | 15:f2569d8e4176 | 7 | //#define EVDEBUG |
uci1 | 15:f2569d8e4176 | 8 | |
uci1 | 0:664899e0b988 | 9 | class SnEventFrame { |
uci1 | 0:664899e0b988 | 10 | |
uci1 | 0:664899e0b988 | 11 | public: |
uci1 | 0:664899e0b988 | 12 | // i/o version |
uci1 | 8:95a325df1f6b | 13 | static const uint8_t kIOVers; // MUST BE INCREASED if any member var changes (==> also if kNchans, etc. change!) |
uci1 | 56:0bba0ef15697 | 14 | static const uint32_t kMaxSizeOfV1 = |
uci1 | 0:664899e0b988 | 15 | ((sizeof(uint32_t)*4u)+sizeof(int32_t)+sizeof(uint16_t) |
uci1 | 56:0bba0ef15697 | 16 | +(kTotSampsAtwd4ch*sizeof(uint16_t))+1u); |
uci1 | 56:0bba0ef15697 | 17 | static const uint32_t kMaxSizeOfV2 = kMaxSizeOfV1 |
uci1 | 116:8099b754fbb4 | 18 | - (kTotSampsAtwd4ch*sizeof(uint16_t)) |
uci1 | 116:8099b754fbb4 | 19 | + (kTotSampsSst4ch*sizeof(uint16_t)) |
uci1 | 56:0bba0ef15697 | 20 | + (kNstopBytesSst4ch*sizeof(uint8_t)); |
uci1 | 116:8099b754fbb4 | 21 | static const uint32_t kMaxSizeOfV3 = kMaxSizeOfV2; // same as V2 (just 1GHz clock) |
uci1 | 116:8099b754fbb4 | 22 | static const uint32_t kMaxSizeOfV4 = kMaxSizeOfV1 |
uci1 | 116:8099b754fbb4 | 23 | - (kTotSampsAtwd4ch*sizeof(uint16_t)) |
uci1 | 116:8099b754fbb4 | 24 | + (kTotSampsSst4ch512*sizeof(uint16_t)) |
uci1 | 116:8099b754fbb4 | 25 | + (kNstopBytesSst4ch512*sizeof(uint8_t)); |
uci1 | 116:8099b754fbb4 | 26 | static const uint32_t kMaxSizeOfV5 = kMaxSizeOfV4; // same as V4 (just 1GHz clock) |
uci1 | 119:b3d7699d0eb0 | 27 | static const uint32_t kMaxSizeOfV6 = kMaxSizeOfV1 |
uci1 | 119:b3d7699d0eb0 | 28 | - (kTotSampsAtwd4ch*sizeof(uint16_t)) |
uci1 | 119:b3d7699d0eb0 | 29 | + (kTotSampsSst8ch*sizeof(uint16_t)) |
uci1 | 119:b3d7699d0eb0 | 30 | + (kNstopBytesSst8ch*sizeof(uint8_t)); |
uci1 | 119:b3d7699d0eb0 | 31 | static const uint32_t kMaxSizeOfV7 = kMaxSizeOfV6; // same as V6 (just 1GHz clock) |
uci1 | 116:8099b754fbb4 | 32 | static const uint32_t kMaxSizeOf = kMaxSizeOfV5; // biggest one |
uci1 | 0:664899e0b988 | 33 | |
uci1 | 0:664899e0b988 | 34 | private: |
uci1 | 0:664899e0b988 | 35 | // !! |
uci1 | 0:664899e0b988 | 36 | // !! If any member variables change, update: SizeOf function and kIOVers value! (also if kNchans, etc. change!) |
uci1 | 0:664899e0b988 | 37 | // !! |
uci1 | 0:664899e0b988 | 38 | |
uci1 | 0:664899e0b988 | 39 | uint16_t fData[kTotSamps]; // the (uncompressed) waveform data |
uci1 | 0:664899e0b988 | 40 | uint32_t fMbedTime; // mbed time in seconds since epoch |
uci1 | 0:664899e0b988 | 41 | // TODO: time since last event? |
uci1 | 0:664899e0b988 | 42 | uint32_t fEvtNum; // written event number ... |
uci1 | 0:664899e0b988 | 43 | int32_t fDTms; // time since last written event (ms) |
uci1 | 0:664899e0b988 | 44 | uint32_t fTrgNum; // this event is trigger number ... |
uci1 | 0:664899e0b988 | 45 | uint16_t fTrgBits; // trigger bit word |
uci1 | 0:664899e0b988 | 46 | mutable uint32_t fCRC; // CRC on the uncompressed waveform data |
uci1 | 56:0bba0ef15697 | 47 | #if CHIPBOARD!=ATWD4CH |
uci1 | 56:0bba0ef15697 | 48 | uint8_t fStop[kNstopBytes];// the stop bit word (one bit per sample) |
uci1 | 56:0bba0ef15697 | 49 | #endif |
uci1 | 0:664899e0b988 | 50 | // TODO: check if other parameters should be added: |
uci1 | 0:664899e0b988 | 51 | // - stop position? |
uci1 | 0:664899e0b988 | 52 | // - card(s) producing trigger? |
uci1 | 0:664899e0b988 | 53 | |
uci1 | 0:664899e0b988 | 54 | void CalcCRC(); |
uci1 | 0:664899e0b988 | 55 | |
uci1 | 0:664899e0b988 | 56 | static |
uci1 | 0:664899e0b988 | 57 | bool ReadFromFileToBuf(FILE* f, |
uci1 | 0:664899e0b988 | 58 | char* const evtBuf, |
uci1 | 0:664899e0b988 | 59 | const uint8_t loseLSB, const uint8_t loseMSB); |
uci1 | 0:664899e0b988 | 60 | |
uci1 | 0:664899e0b988 | 61 | static |
uci1 | 0:664899e0b988 | 62 | bool WriteToFileFromBuf(FILE* f, char* const evtBuf, |
uci1 | 0:664899e0b988 | 63 | const uint8_t loseLSB, const uint8_t loseMSB); |
uci1 | 0:664899e0b988 | 64 | |
uci1 | 0:664899e0b988 | 65 | public: |
uci1 | 84:80b15993944e | 66 | SnEventFrame() { ClearEvent(true, true); } |
uci1 | 0:664899e0b988 | 67 | virtual ~SnEventFrame() {} |
uci1 | 40:1324da35afd4 | 68 | |
uci1 | 40:1324da35afd4 | 69 | virtual void CopyTo(SnEventFrame& evt) const { |
uci1 | 56:0bba0ef15697 | 70 | if (&evt!=this) { |
uci1 | 56:0bba0ef15697 | 71 | evt.fMbedTime = fMbedTime; |
uci1 | 56:0bba0ef15697 | 72 | evt.fEvtNum = fEvtNum; |
uci1 | 56:0bba0ef15697 | 73 | evt.fDTms = fDTms; |
uci1 | 56:0bba0ef15697 | 74 | evt.fTrgNum = fTrgNum; |
uci1 | 56:0bba0ef15697 | 75 | evt.fTrgBits = fTrgBits; |
uci1 | 56:0bba0ef15697 | 76 | evt.fCRC = fCRC; |
uci1 | 56:0bba0ef15697 | 77 | memcpy(evt.fData, fData, kTotSamps*sizeof(uint16_t)); |
uci1 | 56:0bba0ef15697 | 78 | #if CHIPBOARD!=ATWD4CH |
uci1 | 56:0bba0ef15697 | 79 | memcpy(evt.fStop, fStop, kNstopBytes*sizeof(uint8_t)); |
uci1 | 56:0bba0ef15697 | 80 | #endif |
uci1 | 56:0bba0ef15697 | 81 | } |
uci1 | 40:1324da35afd4 | 82 | } |
uci1 | 0:664899e0b988 | 83 | |
uci1 | 0:664899e0b988 | 84 | const uint16_t* GetData() const { return fData; } |
uci1 | 0:664899e0b988 | 85 | uint16_t* GetData() { return fData; } |
uci1 | 0:664899e0b988 | 86 | const uint16_t* GetData(const uint8_t ch) const { return fData + (ch*kNsamps); } |
uci1 | 0:664899e0b988 | 87 | uint16_t* GetData(const uint8_t ch) { return fData + (ch*kNsamps); } |
uci1 | 56:0bba0ef15697 | 88 | uint16_t GetData(const uint8_t ch, const uint16_t sm) const |
uci1 | 0:664899e0b988 | 89 | { return fData[(ch*kNsamps)+sm]; } |
uci1 | 56:0bba0ef15697 | 90 | uint16_t& GetData(const uint8_t ch, const uint16_t sm) |
uci1 | 0:664899e0b988 | 91 | { return fData[(ch*kNsamps)+sm]; } |
uci1 | 0:664899e0b988 | 92 | |
uci1 | 56:0bba0ef15697 | 93 | #if CHIPBOARD!=ATWD4CH |
uci1 | 56:0bba0ef15697 | 94 | const uint8_t* GetStop() const { return fStop; } |
uci1 | 56:0bba0ef15697 | 95 | uint8_t* GetStop() { return fStop; } |
uci1 | 56:0bba0ef15697 | 96 | #endif |
uci1 | 56:0bba0ef15697 | 97 | |
uci1 | 22:f957c4f840ad | 98 | uint32_t GetMbedTime() const { return fMbedTime; } |
uci1 | 84:80b15993944e | 99 | uint32_t GetEvtNum() const { return fEvtNum; } |
uci1 | 84:80b15993944e | 100 | int32_t GetDTms() const { return fDTms; } |
uci1 | 84:80b15993944e | 101 | uint32_t GetTrgNum() const { return fTrgNum; } |
uci1 | 84:80b15993944e | 102 | uint16_t GetTrgBits() const { return fTrgBits; } |
uci1 | 84:80b15993944e | 103 | uint32_t GetCRC() const { return fCRC; } |
uci1 | 22:f957c4f840ad | 104 | |
uci1 | 84:80b15993944e | 105 | void ClearEvent(const bool clearTrigs, |
uci1 | 84:80b15993944e | 106 | const bool clearWaveData) { |
uci1 | 21:ce51bb0ba4a5 | 107 | #ifdef EVDEBUG |
uci1 | 21:ce51bb0ba4a5 | 108 | printf("CLEARING EVENT!\r\n"); |
uci1 | 21:ce51bb0ba4a5 | 109 | #endif |
uci1 | 22:f957c4f840ad | 110 | fMbedTime = 0; |
uci1 | 84:80b15993944e | 111 | |
uci1 | 84:80b15993944e | 112 | if (clearWaveData) { |
uci1 | 84:80b15993944e | 113 | memset(fData, 0, kTotSamps*sizeof(int16_t)); |
uci1 | 56:0bba0ef15697 | 114 | #if CHIPBOARD!=ATWD4CH |
uci1 | 84:80b15993944e | 115 | memset(fStop, 0, kNstopBytes*sizeof(uint8_t)); |
uci1 | 56:0bba0ef15697 | 116 | #endif |
uci1 | 84:80b15993944e | 117 | fCRC = 0; |
uci1 | 84:80b15993944e | 118 | } |
uci1 | 84:80b15993944e | 119 | if (clearTrigs) { |
uci1 | 22:f957c4f840ad | 120 | fEvtNum = fTrgNum = 0; |
uci1 | 22:f957c4f840ad | 121 | fTrgBits = 0; |
uci1 | 22:f957c4f840ad | 122 | } |
uci1 | 0:664899e0b988 | 123 | } |
uci1 | 0:664899e0b988 | 124 | |
uci1 | 84:80b15993944e | 125 | void SetTrgBit(const ETrgBit_t t) { fTrgBits |= t; } |
uci1 | 116:8099b754fbb4 | 126 | void DisableTrgBit(const ETrgBit_t b) { fTrgBits &= ~b; } |
uci1 | 0:664899e0b988 | 127 | void SetTrgNum(const uint32_t t) { fTrgNum = t; } |
uci1 | 0:664899e0b988 | 128 | void SetEvtNum(const uint32_t n) { fEvtNum = n; } |
uci1 | 0:664899e0b988 | 129 | void SetDTms(const int32_t dtms) { fDTms = dtms; } |
uci1 | 0:664899e0b988 | 130 | void SetCurMbedTime() { fMbedTime = time(0); } |
uci1 | 0:664899e0b988 | 131 | |
uci1 | 84:80b15993944e | 132 | // bool IsForcedTrg() const { return (fTrgBits & kFrcTrg)!=0; } |
uci1 | 0:664899e0b988 | 133 | |
uci1 | 56:0bba0ef15697 | 134 | #if CHIPBOARD!=ATWD4CH |
uci1 | 56:0bba0ef15697 | 135 | void ReadWaveformsSST(SPI& spi, DigitalOut& readingData) { |
uci1 | 56:0bba0ef15697 | 136 | readingData = 1; // start reading |
uci1 | 56:0bba0ef15697 | 137 | |
uci1 | 56:0bba0ef15697 | 138 | // first get waveform data |
uci1 | 56:0bba0ef15697 | 139 | uint16_t* d = fData; |
uci1 | 56:0bba0ef15697 | 140 | for (uint8_t ch=0; ch<kNchans; ++ch) { |
uci1 | 56:0bba0ef15697 | 141 | #ifdef EVDEBUG |
uci1 | 56:0bba0ef15697 | 142 | printf("new channel %hhu\r\n",ch); |
uci1 | 56:0bba0ef15697 | 143 | #endif |
uci1 | 56:0bba0ef15697 | 144 | |
uci1 | 56:0bba0ef15697 | 145 | spi.format( 5, 1 ); // throw away leading bits (1 meaningless + 4 MSB 0's bits before 12 bit ADC value) |
uci1 | 56:0bba0ef15697 | 146 | spi.write(0); // get the throw away bits |
uci1 | 56:0bba0ef15697 | 147 | spi.format( 12, 1); // read remaining |
uci1 | 56:0bba0ef15697 | 148 | |
uci1 | 56:0bba0ef15697 | 149 | for (uint16_t i=0; i<kNsamps; ++i, ++d) { |
uci1 | 56:0bba0ef15697 | 150 | *d = spi.write(0x00)/* >> 1*/; // get rid of stop bit (TODO: is this right?) |
uci1 | 56:0bba0ef15697 | 151 | if (i==0) { |
uci1 | 56:0bba0ef15697 | 152 | spi.format( 16, 1); // back to normal |
uci1 | 56:0bba0ef15697 | 153 | } |
uci1 | 56:0bba0ef15697 | 154 | #ifdef EVDEBUG |
uci1 | 56:0bba0ef15697 | 155 | if (i==0 || i==1) { |
uci1 | 56:0bba0ef15697 | 156 | printf("d: spi gave (%hu) ",*d); |
uci1 | 56:0bba0ef15697 | 157 | SnBitUtils::printBits(*d, false); |
uci1 | 56:0bba0ef15697 | 158 | } |
uci1 | 56:0bba0ef15697 | 159 | #endif |
uci1 | 56:0bba0ef15697 | 160 | } |
uci1 | 56:0bba0ef15697 | 161 | } |
uci1 | 56:0bba0ef15697 | 162 | // now get stop data |
uci1 | 56:0bba0ef15697 | 163 | uint8_t* s = fStop; |
uci1 | 56:0bba0ef15697 | 164 | uint16_t sd(0); |
uci1 | 56:0bba0ef15697 | 165 | for (uint16_t i=0; i<kNstopBytes; i+=2, s+=2) { |
uci1 | 56:0bba0ef15697 | 166 | if (i==0) { |
uci1 | 56:0bba0ef15697 | 167 | // need to throw away the very first bit |
uci1 | 56:0bba0ef15697 | 168 | // but we can't ask spi to send only 1 bit |
uci1 | 56:0bba0ef15697 | 169 | spi.format( 5, 1 ); |
uci1 | 56:0bba0ef15697 | 170 | sd = spi.write(0x00); |
uci1 | 56:0bba0ef15697 | 171 | // toss the first bit totally |
uci1 | 56:0bba0ef15697 | 172 | // move the other 4 bits to the front |
uci1 | 56:0bba0ef15697 | 173 | sd <<= 12; |
uci1 | 56:0bba0ef15697 | 174 | spi.format( 12, 1); // read the rest of the real 16 bits |
uci1 | 56:0bba0ef15697 | 175 | sd |= spi.write(0x00); |
uci1 | 56:0bba0ef15697 | 176 | spi.format( 16, 1); // normal after this |
uci1 | 56:0bba0ef15697 | 177 | } else { |
uci1 | 56:0bba0ef15697 | 178 | sd = spi.write(0x00); |
uci1 | 56:0bba0ef15697 | 179 | } |
uci1 | 56:0bba0ef15697 | 180 | #ifdef EVDEBUG |
uci1 | 56:0bba0ef15697 | 181 | /* |
uci1 | 56:0bba0ef15697 | 182 | printf("s: spi gave (%hu) ",sd); |
uci1 | 56:0bba0ef15697 | 183 | SnBitUtils::printBits(sd, false); |
uci1 | 56:0bba0ef15697 | 184 | */ |
uci1 | 56:0bba0ef15697 | 185 | #endif |
uci1 | 56:0bba0ef15697 | 186 | *s = (sd & 0xFF00) >> BITS_IN_CHAR; |
uci1 | 56:0bba0ef15697 | 187 | *(s+1) = sd & 0x00FF; |
uci1 | 56:0bba0ef15697 | 188 | } |
uci1 | 56:0bba0ef15697 | 189 | readingData = 0; // done reading |
uci1 | 56:0bba0ef15697 | 190 | /* |
uci1 | 56:0bba0ef15697 | 191 | // get rid of stop bit (TODO: is this right?) |
uci1 | 56:0bba0ef15697 | 192 | *s >>= 1; |
uci1 | 56:0bba0ef15697 | 193 | *s <<= 1; |
uci1 | 56:0bba0ef15697 | 194 | */ |
uci1 | 56:0bba0ef15697 | 195 | #ifdef EVDEBUG |
uci1 | 56:0bba0ef15697 | 196 | /* |
uci1 | 56:0bba0ef15697 | 197 | d = fData; |
uci1 | 56:0bba0ef15697 | 198 | for (uint8_t ch=0; ch<kNchans; ch++) { |
uci1 | 56:0bba0ef15697 | 199 | for (uint16_t i=0; i<kNsamps; i++, d++) { |
uci1 | 56:0bba0ef15697 | 200 | printf("(%hhu,%03hhu,%hu) ",ch,i,*d); |
uci1 | 56:0bba0ef15697 | 201 | } |
uci1 | 56:0bba0ef15697 | 202 | printf("\r\n"); |
uci1 | 56:0bba0ef15697 | 203 | } |
uci1 | 56:0bba0ef15697 | 204 | s = fStop; |
uci1 | 56:0bba0ef15697 | 205 | for (uint16_t i=0; i<kNstopBytes; ++i, ++s) { |
uci1 | 56:0bba0ef15697 | 206 | for (int16_t j=(sizeof(uint8_t)*BITS_IN_CHAR)-1; j>-1; --j) { |
uci1 | 56:0bba0ef15697 | 207 | if ( (*s&(1<<j))!=0 ) { |
uci1 | 56:0bba0ef15697 | 208 | printf("stop bit at %hu\r\n", |
uci1 | 56:0bba0ef15697 | 209 | j+(BITS_IN_CHAR*sizeof(uint8_t)*i)); |
uci1 | 56:0bba0ef15697 | 210 | } |
uci1 | 56:0bba0ef15697 | 211 | } |
uci1 | 56:0bba0ef15697 | 212 | } |
uci1 | 56:0bba0ef15697 | 213 | */ |
uci1 | 56:0bba0ef15697 | 214 | #endif |
uci1 | 56:0bba0ef15697 | 215 | CalcCRC(); |
uci1 | 56:0bba0ef15697 | 216 | } |
uci1 | 56:0bba0ef15697 | 217 | #endif |
uci1 | 56:0bba0ef15697 | 218 | |
uci1 | 56:0bba0ef15697 | 219 | void ReadWaveformsATWD(SPI& spi, |
uci1 | 56:0bba0ef15697 | 220 | DigitalOut& cardHiBit, DigitalOut& cardLoBit) { |
uci1 | 0:664899e0b988 | 221 | uint16_t* dev=fData; |
uci1 | 0:664899e0b988 | 222 | for( uint8_t ch = 0; ch < kNchans; ch++ ) { |
uci1 | 0:664899e0b988 | 223 | // Pick which register to read. |
uci1 | 0:664899e0b988 | 224 | SnBitUtils::SetChanNumBits(ch, cardHiBit, cardLoBit); |
uci1 | 56:0bba0ef15697 | 225 | for( uint16_t i = 0; i < kNsamps; i++, dev++ ) { |
uci1 | 0:664899e0b988 | 226 | *dev = spi.write(0x00) >> 1; |
uci1 | 0:664899e0b988 | 227 | } |
uci1 | 0:664899e0b988 | 228 | } |
uci1 | 15:f2569d8e4176 | 229 | #ifdef EVDEBUG |
uci1 | 15:f2569d8e4176 | 230 | dev = fData; |
uci1 | 15:f2569d8e4176 | 231 | for (uint8_t ch=0; ch<kNchans; ch++) { |
uci1 | 56:0bba0ef15697 | 232 | for (uint16_t i=0; i<kNsamps; i++, dev++) { |
uci1 | 56:0bba0ef15697 | 233 | printf("(%hhu,%03hhu,%hu) ",ch,i,*dev); |
uci1 | 15:f2569d8e4176 | 234 | } |
uci1 | 15:f2569d8e4176 | 235 | printf("\r\n"); |
uci1 | 15:f2569d8e4176 | 236 | } |
uci1 | 15:f2569d8e4176 | 237 | #endif |
uci1 | 0:664899e0b988 | 238 | CalcCRC(); |
uci1 | 0:664899e0b988 | 239 | } |
uci1 | 0:664899e0b988 | 240 | |
uci1 | 56:0bba0ef15697 | 241 | |
uci1 | 56:0bba0ef15697 | 242 | static |
uci1 | 56:0bba0ef15697 | 243 | uint16_t GetTotSamplesForIOVers(const uint8_t rv) { |
uci1 | 56:0bba0ef15697 | 244 | if (rv<2) { |
uci1 | 56:0bba0ef15697 | 245 | return kTotSampsAtwd4ch; |
uci1 | 116:8099b754fbb4 | 246 | } else if ((rv==2)||(rv==3)) { |
uci1 | 116:8099b754fbb4 | 247 | return kTotSampsSst4ch; |
uci1 | 119:b3d7699d0eb0 | 248 | } else if ((rv==4)||(rv==5)) { |
uci1 | 119:b3d7699d0eb0 | 249 | return kTotSampsSst4ch512; |
uci1 | 119:b3d7699d0eb0 | 250 | } else if ((rv==6)||(rv==7)) { |
uci1 | 119:b3d7699d0eb0 | 251 | return kTotSampsSst8ch; |
uci1 | 56:0bba0ef15697 | 252 | } else { |
uci1 | 119:b3d7699d0eb0 | 253 | // ??? |
uci1 | 119:b3d7699d0eb0 | 254 | return kTotSampsSst4ch; |
uci1 | 56:0bba0ef15697 | 255 | } |
uci1 | 56:0bba0ef15697 | 256 | } |
uci1 | 56:0bba0ef15697 | 257 | |
uci1 | 56:0bba0ef15697 | 258 | static |
uci1 | 56:0bba0ef15697 | 259 | uint16_t GetStopBytesForIOVers(const uint8_t rv) { |
uci1 | 56:0bba0ef15697 | 260 | if (rv<2) { |
uci1 | 56:0bba0ef15697 | 261 | return 0; |
uci1 | 116:8099b754fbb4 | 262 | } else if ((rv==2)||(rv==3)) { |
uci1 | 56:0bba0ef15697 | 263 | return kNstopBytesSst4ch; |
uci1 | 119:b3d7699d0eb0 | 264 | } else if ((rv==4)||(rv==5)) { |
uci1 | 119:b3d7699d0eb0 | 265 | return kNstopBytesSst4ch512; |
uci1 | 119:b3d7699d0eb0 | 266 | } else if ((rv==6)||(rv==7)) { |
uci1 | 119:b3d7699d0eb0 | 267 | return kNstopBytesSst8ch; |
uci1 | 116:8099b754fbb4 | 268 | } else { |
uci1 | 119:b3d7699d0eb0 | 269 | // ??? |
uci1 | 119:b3d7699d0eb0 | 270 | return kNstopBytesSst4ch; |
uci1 | 56:0bba0ef15697 | 271 | } |
uci1 | 56:0bba0ef15697 | 272 | } |
uci1 | 116:8099b754fbb4 | 273 | |
uci1 | 116:8099b754fbb4 | 274 | static |
uci1 | 116:8099b754fbb4 | 275 | uint16_t GetStopBytesForIOVersBufferSafe(const uint8_t rv) { |
uci1 | 116:8099b754fbb4 | 276 | // get the max number of stop bytes that will fit in an fStop buffer |
uci1 | 116:8099b754fbb4 | 277 | const uint16_t nsb = GetStopBytesForIOVers(rv); |
uci1 | 116:8099b754fbb4 | 278 | return (nsb>kNstopBytes) ? kNstopBytes : nsb; // prevent buffer overflow |
uci1 | 116:8099b754fbb4 | 279 | } |
uci1 | 56:0bba0ef15697 | 280 | |
uci1 | 0:664899e0b988 | 281 | static |
uci1 | 8:95a325df1f6b | 282 | uint32_t SizeOf(const uint8_t rv, const uint8_t loseLSB, const uint8_t loseMSB) { |
uci1 | 0:664899e0b988 | 283 | // size of member vars + size of packed waveform + 1 for i/o version |
uci1 | 116:8099b754fbb4 | 284 | #ifdef EVDEBUG |
uci1 | 116:8099b754fbb4 | 285 | printf("event: rv=%hhu, loseLBS=%hhu, loseMSB=%hhu\r\n", rv, loseLSB, loseMSB); |
uci1 | 116:8099b754fbb4 | 286 | #endif |
uci1 | 56:0bba0ef15697 | 287 | uint32_t sz(0); |
uci1 | 56:0bba0ef15697 | 288 | const uint16_t ntotsamps = GetTotSamplesForIOVers(rv); |
uci1 | 56:0bba0ef15697 | 289 | if (rv==1) { |
uci1 | 56:0bba0ef15697 | 290 | sz = kMaxSizeOfV1; |
uci1 | 56:0bba0ef15697 | 291 | } else if (rv==2) { |
uci1 | 56:0bba0ef15697 | 292 | sz = kMaxSizeOfV2; |
uci1 | 114:554fa3a956b4 | 293 | } else if (rv==3) { |
uci1 | 114:554fa3a956b4 | 294 | sz = kMaxSizeOfV3; |
uci1 | 116:8099b754fbb4 | 295 | } else if (rv==4) { |
uci1 | 116:8099b754fbb4 | 296 | sz = kMaxSizeOfV4; |
uci1 | 116:8099b754fbb4 | 297 | } else if (rv==5) { |
uci1 | 116:8099b754fbb4 | 298 | sz = kMaxSizeOfV5; |
uci1 | 119:b3d7699d0eb0 | 299 | } else if (rv==6) { |
uci1 | 119:b3d7699d0eb0 | 300 | sz = kMaxSizeOfV6; |
uci1 | 119:b3d7699d0eb0 | 301 | } else if (rv==7) { |
uci1 | 119:b3d7699d0eb0 | 302 | sz = kMaxSizeOfV7; |
uci1 | 0:664899e0b988 | 303 | } else { |
uci1 | 56:0bba0ef15697 | 304 | sz = kMaxSizeOf; |
uci1 | 56:0bba0ef15697 | 305 | } |
uci1 | 116:8099b754fbb4 | 306 | #ifdef EVDEBUG |
uci1 | 116:8099b754fbb4 | 307 | printf("event: ntotsamps=%hu, sz=%u\r\n", ntotsamps, sz); |
uci1 | 116:8099b754fbb4 | 308 | #endif |
uci1 | 56:0bba0ef15697 | 309 | if ((loseLSB==0) && (loseMSB==0)) { |
uci1 | 56:0bba0ef15697 | 310 | return sz; |
uci1 | 56:0bba0ef15697 | 311 | } else { |
uci1 | 116:8099b754fbb4 | 312 | #ifdef EVDEBUG |
uci1 | 116:8099b754fbb4 | 313 | printf("event packed size=%u\r\n", |
uci1 | 116:8099b754fbb4 | 314 | (sz-(ntotsamps*sizeof(uint16_t)) |
uci1 | 119:b3d7699d0eb0 | 315 | +SizeOfPackedWavef(loseLSB, loseMSB, ntotsamps))); |
uci1 | 116:8099b754fbb4 | 316 | #endif |
uci1 | 56:0bba0ef15697 | 317 | return (sz-(ntotsamps*sizeof(uint16_t)) |
uci1 | 119:b3d7699d0eb0 | 318 | +SizeOfPackedWavef(loseLSB, loseMSB, ntotsamps)); |
uci1 | 0:664899e0b988 | 319 | } |
uci1 | 0:664899e0b988 | 320 | } |
uci1 | 0:664899e0b988 | 321 | |
uci1 | 0:664899e0b988 | 322 | static |
uci1 | 0:664899e0b988 | 323 | uint32_t SizeOfPackedWavef(const uint8_t loseLSB, |
uci1 | 119:b3d7699d0eb0 | 324 | const uint8_t loseMSB, |
uci1 | 119:b3d7699d0eb0 | 325 | const uint16_t totsamps=kTotSamps) { |
uci1 | 0:664899e0b988 | 326 | const uint8_t p = BITS_IN_SHORT-loseLSB-loseMSB; |
uci1 | 119:b3d7699d0eb0 | 327 | return ((p*totsamps)/8u) + (((p*totsamps)%8u)!=0 ? 1u : 0u); |
uci1 | 0:664899e0b988 | 328 | } |
uci1 | 0:664899e0b988 | 329 | |
uci1 | 3:24c5f0f50bf1 | 330 | const char* ReadFrom(const char* const buf, |
uci1 | 3:24c5f0f50bf1 | 331 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 3:24c5f0f50bf1 | 332 | const uint16_t wvBaseline); |
uci1 | 0:664899e0b988 | 333 | |
uci1 | 3:24c5f0f50bf1 | 334 | char* WriteTo(char* const buf, |
uci1 | 3:24c5f0f50bf1 | 335 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 3:24c5f0f50bf1 | 336 | const uint16_t wvBaseline) const; |
uci1 | 0:664899e0b988 | 337 | |
uci1 | 0:664899e0b988 | 338 | bool ReadFrom(FILE* f, char* const evtBuf, |
uci1 | 0:664899e0b988 | 339 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 0:664899e0b988 | 340 | const uint16_t wvBaseline); |
uci1 | 0:664899e0b988 | 341 | bool WriteTo(FILE* f, char* const evtBuf, |
uci1 | 0:664899e0b988 | 342 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 0:664899e0b988 | 343 | const uint16_t wvBaseline) const; |
uci1 | 0:664899e0b988 | 344 | |
uci1 | 0:664899e0b988 | 345 | static |
uci1 | 0:664899e0b988 | 346 | const uint8_t* UnpackWavef(const uint8_t* const buf, |
uci1 | 0:664899e0b988 | 347 | uint16_t* const data, |
uci1 | 0:664899e0b988 | 348 | const uint8_t loseLSB, |
uci1 | 0:664899e0b988 | 349 | const uint8_t loseMSB, |
uci1 | 56:0bba0ef15697 | 350 | const uint16_t wvBaseline, |
uci1 | 56:0bba0ef15697 | 351 | const uint16_t nsamps); |
uci1 | 0:664899e0b988 | 352 | |
uci1 | 0:664899e0b988 | 353 | static |
uci1 | 0:664899e0b988 | 354 | uint8_t* PackWavef(uint8_t* const buf, const uint16_t* const data, |
uci1 | 0:664899e0b988 | 355 | const uint8_t loseLSB, const uint8_t loseMSB, |
uci1 | 56:0bba0ef15697 | 356 | const uint16_t wvBaseline, |
uci1 | 56:0bba0ef15697 | 357 | const uint16_t nsamps); |
uci1 | 0:664899e0b988 | 358 | }; |
uci1 | 0:664899e0b988 | 359 | |
uci1 | 0:664899e0b988 | 360 | |
uci1 | 0:664899e0b988 | 361 | #endif // SN_SnEventFrame |