-
Notifications
You must be signed in to change notification settings - Fork 0
/
tachometer.cpp
executable file
·339 lines (277 loc) · 9.35 KB
/
tachometer.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "tachometer.h"
//#include <QPainter>
#include <QPainterPath>
#include <QPen>
#include <QTimer>
#include <QIcon>
#include <math.h>
#include <QtConcurrent/QtConcurrentRun>
static const int gear_up_first_level = 2800;
static const int gear_up_second_level = 4000;
static const int gear_up_blink_interval_ms = 350;
static const enum tacho_gear top_gear = GEAR_5;
tachometer::tachometer(QObject *parent) : QObject{parent}
{
create_tachometer(PSA_TACHO_7K);
}
tachometer::tachometer(tacho_range_t range, QObject *parent) : QObject{parent}
{
create_tachometer(range);
}
void tachometer::create_tachometer(tacho_range_t range)
{
this->gear_timer = new QTimer(this);
this->gear_up_blink_timer = new QTimer(this);
connect(this->gear_timer, &QTimer::timeout, this, &tachometer::update_gear_display);
connect(this->gear_up_blink_timer, &QTimer::timeout, this, &tachometer::toggle_gear_up_display);
this->speed_update_watcher = new QFutureWatcher<void>(this);
this->rpm_update_watcher = new QFutureWatcher<void>(this);
this->gear_up_visible = false;
QObject::connect(this->speed_update_watcher, &QFutureWatcher<void>::finished, this, &tachometer::update_display);
QObject::connect(this->rpm_update_watcher, &QFutureWatcher<void>::finished, this, &tachometer::update_display);
this->scene = new QGraphicsScene();
tacho_bg = new QPixmap(":/tachometer/tacho/background.png");
tacho_needle = new QPixmap(":/tachometer/tacho/needle.png");
// QIcon gear_up_qicon_temp = QIcon(":/tachometer/tacho/gear_up.svg");
gear_up_icon = new QPixmap(QIcon(":/tachometer/tacho/gear_up.svg").pixmap(QSize(45,45)));
switch(range)
{
case PSA_TACHO_7K:
default:
tacho_numbers = new QPixmap(":/tachometer/tacho/7k.png");
this->set_range = PSA_TACHO_7K;
break;
case PSA_TACHO_8K:
tacho_numbers = new QPixmap(":/tachometer/tacho/8k.png");
this->set_range = PSA_TACHO_8K;
break;
}
tacho_img_width = tacho_bg->width();
tacho_img_height = tacho_bg->height();
int scene_w = 800;
int scene_h = 400;
int tacho_bg_offset_x = (scene_w - tacho_img_width) / 2;
int tacho_bg_offset_y = (scene_h - tacho_img_height) / 2;
scene->setSceneRect(0,0,scene_w, scene_h);
tacho_needle_offset_x = (tacho_img_width - tacho_needle->width()) / 2;
tacho_needle_offset_y = (tacho_img_height - tacho_needle->height()) / 2;
tacho_numbers_offset_x = (tacho_img_width - tacho_numbers->width()) / 2;
tacho_numbers_offset_y = (tacho_img_height - tacho_numbers->height()) / 2;
tacho_bg_item = scene->addPixmap(*tacho_bg);
tacho_numbers_item = scene->addPixmap(*tacho_numbers);
tacho_needle_item = scene->addPixmap(*tacho_needle);
tacho_numbers_item->setParentItem(tacho_bg_item);
tacho_needle_item->setParentItem(tacho_bg_item);
tacho_bg_item->setTransformationMode(Qt::SmoothTransformation);
tacho_numbers_item->setTransformationMode(Qt::SmoothTransformation);
tacho_needle_item->setTransformationMode(Qt::SmoothTransformation);
tacho_bg_item->setTransformOriginPoint(tacho_img_width / 2, tacho_img_height / 2);
tacho_needle_item->setTransformOriginPoint(tacho_needle->width()/2,tacho_needle->height() / 2);
tacho_numbers_item->setTransformOriginPoint(tacho_numbers->width()/2,tacho_numbers->height() / 2);
tacho_bg_item->setPos(tacho_bg_offset_x, tacho_bg_offset_y);
tacho_needle_item->setPos(tacho_needle_offset_x +4 , tacho_needle_offset_y);
tacho_numbers_item->setPos(tacho_numbers_offset_x, tacho_numbers_offset_y);
//tacho_speed_font = new QFont("Azeret Mono", 40, 100, true);
tacho_speed_font = new QFont("DSEG7 Classic", 60, 100, false);
tacho_gear_font = new QFont("DSEG14 Classic", 35, 100, false);
//tacho_gear_font->
tacho_gear_bg_item = scene->addText("~", *tacho_gear_font);
tacho_gear_bg_item->setDefaultTextColor(QColor(0,0,0,40));
tacho_gear_bg_item->setPos(254,146);
tacho_gear_bg_item->setParentItem(tacho_bg_item);
tacho_gear_item = scene->addText("R", *tacho_gear_font);
tacho_gear_item->setDefaultTextColor(Qt::black);
tacho_gear_item->setPos(254,146);
tacho_gear_item->setParentItem(tacho_bg_item);
tacho_speed_bg_item = scene->addText("888", *tacho_speed_font);
tacho_speed_item = scene->addText("000", *tacho_speed_font);
tacho_speed_bg_item->setParentItem(tacho_bg_item);
tacho_speed_item->setParentItem(tacho_bg_item);
tacho_speed_item->setPos(152, 307);
tacho_speed_bg_item->setPos(152,307);
tacho_speed_item->setDefaultTextColor(Qt::black);
tacho_speed_bg_item->setDefaultTextColor(QColor(0,0,0,40));
gear_up_icon_item = scene->addPixmap(*this->gear_up_icon);
gear_up_icon_item->setPos(210,150);
gear_up_icon_item->setParentItem(tacho_bg_item);
gear_up_icon_item->setVisible(gear_up_visible);
set_rpm(0);
// set_speed(0.f);
}
void tachometer::toggle_gear_up_display()
{
this->gear_up_visible = !this->gear_up_visible;
this->gear_up_icon_item->setVisible(this->gear_up_visible);
}
void tachometer::set_gear(tacho_gear_t gear)
{
if(gear != new_gear)
{
this->gear_up_grace = true;
update_gear_up(this->rpm);
this->new_gear = gear;
this->gear_timer->stop();
this->gear_timer->start(1000);
}
}
void tachometer::set_gear(int gear)
{
set_gear((tacho_gear_t)gear);
}
void tachometer::update_gear_display()
{
this->gear_up_grace = false;
this->old_gear = this->new_gear;
update_gear_up(this->rpm);
this->gear_timer->stop();
switch(this->old_gear)
{
case GEAR_R:
tacho_gear_item->setPlainText("R");
break;
case GEAR_1:
tacho_gear_item->setPlainText("1");
break;
case GEAR_2:
tacho_gear_item->setPlainText("2");
break;
case GEAR_3:
tacho_gear_item->setPlainText("3");
break;
case GEAR_4:
tacho_gear_item->setPlainText("4");
break;
case GEAR_5:
tacho_gear_item->setPlainText("5");
break;
case GEAR_6:
tacho_gear_item->setPlainText("6");
break;
case GEAR_N:
default:
tacho_gear_item->setPlainText("N");
break;
}
}
QGraphicsScene* tachometer::get_scene()
{
return this->scene;
}
void tachometer::set_speed_pos(int x, int y)
{
tacho_speed_item->setPos(x,y);
}
void tachometer::update_gear_up(int rpm)
{
if(this->new_gear == top_gear || this->gear_up_grace || this->old_gear < GEAR_1)
{
if(gear_up_state != 0)
{
this->gear_up_icon_item->setVisible(false);
this->gear_up_visible = false;
this->gear_up_blink_timer->stop();
gear_up_state = 0;
}
}
else{
if(rpm < gear_up_first_level)
{
if(gear_up_state != 0)
{
this->gear_up_icon_item->setVisible(false);
this->gear_up_visible = false;
this->gear_up_blink_timer->stop();
gear_up_state = 0;
}
}
else if(rpm < gear_up_second_level)
{
if(gear_up_state != 1)
{
this->gear_up_icon_item->setVisible(true);
this->gear_up_visible = true;
this->gear_up_blink_timer->stop();
gear_up_state = 1;
}
}
else{
if(gear_up_state != 2)
{
this->toggle_gear_up_display();
this->gear_up_blink_timer->start(gear_up_blink_interval_ms);
gear_up_state = 2;
}
}
}
}
void tachometer::set_rpm(int rpm)
{
this->rpm = rpm;
this->update_gear_up(this->rpm);
//QFuture<void> future = QtConcurrent::run(this, &tachometer::get_angle_from_rpm);
//this->rpm_update_watcher->setFuture(future);
this->rpm_angle = get_angle_from_rpm();
update_display();
}
void tachometer::update_display()
{
tacho_needle_item->setRotation(this->rpm_angle);
tacho_speed_item->setPlainText(this->speed_text);
}
void tachometer::set_speed_text(int speed)
{
if(speed < 10)
{
this->speed_text = QString::asprintf("!!%1d", (int)speed);
}
else if(speed < 100)
{
this->speed_text = QString::asprintf("!%2d", (int)speed);
}
else{
this->speed_text = QString::asprintf("%3d", (int)speed);
}
}
void tachometer::set_speed(float speed)
{
this->speed = speed;
//QFuture<void> future = QtConcurrent::run(this, &tachometer::set_speed_text, this->speed);
//this->speed_update_watcher->setFuture(future);
set_speed_text((int)lround(speed));
update_display();
}
float tachometer::get_angle_from_rpm()
{
float offset = 65.f;
float angle = this->rpm * (230.f / 7000);
float value = offset + angle;
this->rpm_angle = value;
return this->rpm_angle;
}
void tachometer::set_scale(float new_scale)
{
tacho_bg_item->setScale(new_scale);
this->tacho_scale = new_scale;
}
float tachometer::get_scale()
{
return this->tacho_scale;
}
int tachometer::get_rpm()
{
return this->rpm;
}
tachometer::~tachometer()
{
scene->clear();
// delete tacho_bg_item;
// delete tacho_numbers_item;
// delete tacho_needle_item;
// delete tacho_speed_item;
// delete tacho_gear_item;
delete tacho_bg;
delete tacho_needle;
delete tacho_numbers;
delete tacho_speed_font;
delete tacho_gear_font;
}