-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screen.cpp
executable file
·240 lines (208 loc) · 6.88 KB
/
Screen.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "Screen.h"
#include <thread>
Screen::Screen()
: window("No name"), graphLines(sf::PrimitiveType::LineStrip), gridOx(sf::PrimitiveType::Lines, (NUMBER_OF_LINES_HOR)),
gridOy(sf::PrimitiveType::Lines, (NUMBER_OF_LINES_VERT)), sweepButton(font), scaleButton(font), triggerValue(font) , scaleCoef(2)
{
for (int i = 0;i < NUMBER_OF_LINES_HOR / 2;++i)
{
gridOx[2 * i].color = sf::Color(128, 128, 128);
gridOx[2 * i + 1].color = sf::Color(128, 128, 128);
gridOx[2 * i].position = sf::Vector2f(100, i == 0 ? 100 : 100 + STEPVERT * i);
gridOx[2 * i + 1].position = sf::Vector2f(WINDOW_GRID_SIZE_HOR, i == 0 ? 100 : 100 + STEPVERT * i);
}
for (int i = 0;i < NUMBER_OF_LINES_VERT / 2 - 1;++i)
{
gridOy[2 * i].color = sf::Color(128, 128, 128);
gridOy[2 * i + 1].color = sf::Color(128, 128, 128);
gridOy[2 * i].position = sf::Vector2f(i == 0 ? 100 : 100 + STEPHOR * i, 100);
gridOy[2 * i + 1].position = sf::Vector2f(i == 0 ? 100 : 100 + STEPHOR * i, WINDOW_GRID_SIZE_VERT + 100);
}
#if defined (__linux) || defined (unix)
if (!font.loadFromFile("/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf"))
#elif defined (_WIN32)
if (!font.loadFromFile("C:\\Windows\\Fonts\\Arial.ttf"))
#endif
std::cout << "Error reading font" << std::endl;
// button settings
sweepButton.setPosition(sf::Vector2f(1050, 150));
sweepButton.setLabel("mks/cell");
scaleButton.setPosition(sf::Vector2f(1050, 225));
scaleButton.setLabel("scale X");
triggerValue.setPosition(sf::Vector2f(1050, 300));
triggerValue.setSize(sf::Vector2f(100, 75));
}
void Screen::Update()
{
//Screen::ReadDataTTY();
ADC_Data.pseudo_data();
Screen::convertADC_DataForScreen(&(ADC_Data.getData()));
setStringOnDisplay(frequency, font, "Freq: " + std::to_string(frequencyCalc(&(ADC_Data.getData())) / 1000) + " kHz", 120, 610, 20, sf::Color::White);
setStringOnDisplay(period, font, "T: " + std::to_string(1 / frequencyCalc(&(ADC_Data.getData())) * 1000) + " ms", 450, 610, 20, sf::Color::White);
setStringOnDisplay(mksPerCell, font, "mks/cell " + std::to_string(50 * ADC_Data.timeConversion() * 1000000 / scaleCoef), 700, 610, 20, sf::Color::White);
triggerValue.setLabel("trigger\n " + std::to_string(ADC_Data.getTrigValue()));
window.Update();
// ADC_Data.TransmitData('r');
}
void Screen::LateUpdate()
{
static bool counterSweep = 0;
static bool counterScale = 0;
static bool triggerValueFlag = 0;
bool flag = 0;
if (sweepButton.buttonIsPressed(window.GetRef()))
{
counterSweep = 1;
flag = 1;
}
else if (scaleButton.buttonIsPressed(window.GetRef()))
{
counterScale = 1;
flag = 1;
}
else if (triggerValue.buttonIsPressed(window.GetRef()))
{
triggerValueFlag = 1;
flag = 1;
}
if ((counterSweep || counterScale || triggerValueFlag) && !flag)
{
if (counterSweep)
CBSweep();
else if (counterScale)
CBScale();
else
CBSetTriggerValue();
//counterSweep ? CBSweep() : CBScale();
}
if (!flag)
{
counterSweep = 0;
counterScale = 0;
triggerValueFlag = 0;
}
}
void Screen::Draw()
{
window.BeginDraw();
window.Draw(gridOx);
window.Draw(gridOy);
window.Draw(frequency);
window.Draw(period);
window.Draw(graphLines);
window.Draw(mksPerCell);
//button draw
window.Draw(sweepButton.getRefBframe());
window.Draw(sweepButton.getRefBbox());
window.Draw(sweepButton.getRefBtext());
window.Draw(scaleButton.getRefBframe());
window.Draw(scaleButton.getRefBbox());
window.Draw(scaleButton.getRefBtext());
window.Draw(triggerValue.getRefBframe());
window.Draw(triggerValue.getRefBbox());
window.Draw(triggerValue.getRefBtext());
//------------------------
window.EndDraw();
}
bool Screen::IsRunning()
{
while (window.IsOpen())
return true;
return false;
}
void Screen::ReadDataTTY()
{
ADC_Data.readData();
}
void Screen::convertADC_DataForScreen(uint16_t* bufferADC)
{
int offset = ADC_Data.offsetWithTrig();
bufferADC += offset;
int bufferSize = ADC_Data.getSizeBuffer() - offset;
graphLines.resize(bufferSize);
for (int i = 0;i < bufferSize;++i)
{
graphLines[i].color = (scaleCoef * i) < WINDOW_GRID_SIZE_HOR - 100 ? sf::Color::Blue : sf::Color(0x18, 0x18, 0x18); // in case if line > cell grid 0X
graphLines[i].position = sf::Vector2f(scaleCoef * i + 100, SIZE_VERT - bufferADC[i] / COEF - 100);
}
}
float Screen::frequencyCalc(uint16_t* bufferADC)
{
static float frequencyValue = 0;
static int countMeas = 0;
int startPoint = BUFFER_SIZE / 10;
int endpoint = BUFFER_SIZE - startPoint - 1;
int trigVal = ADC_Data.getTrigValue();
bool flagRising1 = 0;
int valFlagR1 = 0;
int valFlagR2 = 0;
for (int i = startPoint;i < endpoint;++i)
{
if (bufferADC[i] <trigVal && bufferADC[i + 1] > trigVal && flagRising1)
{
valFlagR2 = i;
countMeas++;
if (countMeas == 10)
{
frequencyValue = 1 / ((valFlagR2 - valFlagR1) * ADC_Data.timeConversion());
return frequencyValue;
}
return frequencyValue;
//return 1/((valFlagR2-valFlagR1)*timeConversion);
}
if (bufferADC[i] < trigVal && bufferADC[i + 1] > trigVal)
{
flagRising1 = 1;
valFlagR1 = i;
}
}
//return 0;
return frequencyValue;
}
void Screen::CBScale()
{
static int numPushButton = 0;
numPushButton++;
switch (numPushButton)
{
case 1:
setCoef(5);
break;
case 2:
setCoef(10);
break;
default:
setCoef(2);
numPushButton = 0;
break;
}
std::cout << "CBScale\n";
}
void Screen::CBSweep()
{
ADC_Data.TransmitData('c');
ADC_Data.setFlagIncrementCyclesADC();
std::cout << "CBSweep\n";
}
void Screen::CBSetTriggerValue()
{
int trigValue = ADC_Data.getTrigValue();
trigValue += 100;
if (trigValue >= 1000)
trigValue = 0;
ADC_Data.setTrigValue(trigValue);
std::cout << "CBTriggerValue\n";
}
void setStringOnDisplay(sf::Text& text, sf::Font& font, const std::string& str,
const float posX, const float posY, const unsigned int size,
sf::Color color)
{
text.setFont(font);
text.setString(str);
text.setCharacterSize(size);
text.setFillColor(color);
sf::Vector2f position;
position.x = posX;
position.y = posY;
text.setPosition(position);
}