-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastream.h
165 lines (125 loc) · 3.83 KB
/
datastream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// datastream.h
#pragma once
#include <stdint.h>
#include <list>
#include <vector>
#include "config.h"
#include "psi46test.h"
#include "datapipe.h"
#include "protocol.h"
using namespace std;
// === Error Messages =======================================================
class DS_buffer_overflow : public DataPipeException
{
public:
DS_buffer_overflow() : DataPipeException("Buffer overflow") {};
};
class DS_empty : public DataPipeException
{
public:
DS_empty() : DataPipeException("Buffer empty") {};
};
// === Binary Data Record Format ============================================
class CDataRecord
{
/*
bit 0 = misaligned start
bit 1 = no end detected
bit 2 = overflow
*/
vector<uint16_t> data;
unsigned int flags;
public:
void SetStartError() { flags |= 1; }
void SetEndError() { flags |= 2; }
void SetOverflow() { flags |= 4; }
void ResetStartError() { flags &= ~1; }
void ResetEndError() { flags &= ~2; }
void ResetOverflow() { flags &= ~4; }
void Clear() { flags = 0; data.clear(); }
bool IsStartError() { return (flags & 1) != 0; }
bool IsEndError() { return (flags & 2) != 0; }
bool IsOverflow() { return (flags & 4) != 0; }
unsigned int GetSize() { return data.size(); }
void Add(uint16_t value) { data.push_back(value); }
uint16_t operator[](int index) { return data[index]; }
};
struct CRocPixel
{
int raw;
int x;
int y;
int ph;
void DecodeRaw();
};
struct CRocEvent
{
unsigned short header;
vector<CRocPixel> pixel;
};
// === Data Sources =========================================================
// --- PixelDTB
#define DTB_SOURCE_BLOCK_SIZE 4096
class CDtbSource : public CSource<uint16_t>
{
volatile bool stopAtEmptyData;
// --- DTB control/state
CTestboard &tb;
uint32_t dtbRemainingSize;
uint8_t dtbState;
// --- data buffer
uint16_t lastSample;
unsigned int pos;
vector<uint16_t> buffer;
uint16_t FillBuffer();
// --- virtual data access methods
uint16_t Read() { return (pos < buffer.size()) ? lastSample = buffer[pos++] : FillBuffer(); }
uint16_t ReadLast() { return lastSample; }
public:
CDtbSource(CTestboard &src, bool endlesStream)
: stopAtEmptyData(endlesStream), tb(src), lastSample(0x4000), pos(0) {}
// --- control and status
uint8_t GetState() { return dtbState; }
uint32_t GetRemainingSize() { return dtbRemainingSize; }
void Stop() { stopAtEmptyData = true; }
};
// --- File
#define FILE_SOURCE_BLOCK_SIZE 16384
class CBinaryFileSource : public CSource<uint16_t>
{
FILE *f;
uint16_t lastSample;
unsigned int size;
unsigned int pos;
vector<uint16_t> buffer;
uint16_t FillBuffer();
uint16_t Read() { return (pos < size) ? lastSample = buffer[pos++] : FillBuffer(); }
uint16_t ReadLast() { return lastSample; }
public:
CBinaryFileSource() : f(0), lastSample(0), size(0), pos(0) { buffer.reserve(FILE_SOURCE_BLOCK_SIZE); }
~CBinaryFileSource() { Close(); }
bool Open(const char *filename) { return (f = fopen(filename, "rb")) != 0; }
void Close() { if (f) { fclose(f); f = 0; } }
};
// === CDataRecordScanner (CDataPipe<uint16_t>, CDataRecord*>) ==============
class CDataRecordScanner : public CDataPipe<uint16_t, CDataRecord*>
{
CDataRecord record;
CDataRecord* Read();
CDataRecord* ReadLast() { return &record; }
public:
CDataRecordScanner() {}
};
// === CDecoder (CDataRecord*, CRocEvent*) ==================================
class CRocDecoder : public CDataPipe<CDataRecord*, CRocEvent*>
{
CRocEvent roc_event;
CRocEvent* Read();
CRocEvent* ReadLast() { return &roc_event; }
};
// === CAnalyzer (CRocEvent*, CRocEvent*) ===================================
class CAnalyzer : public CDataPipe<CRocEvent*, CRocEvent*>
{
CRocEvent* Read() { return Get(); };
CRocEvent* ReadLast() { return GetLast(); }
};