YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
display.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#ifdef MSVC
7 #define _USE_MATH_DEFINES
8#endif
9#include <cmath>
10
11#include <yarp/os/Time.h>
12#include <yarp/os/Log.h>
13#include <yarp/os/LogStream.h>
14
15#include "display.h"
16#include <QGraphicsPixmapItem>
17#include <QBitmap>
18
19
20
22{
23 if (mainTimer)
24 {
25 delete mainTimer;
26 mainTimer = nullptr;
27 }
28 if (ui)
29 {
30 delete ui;
31 ui = nullptr;
32 }
33 if (scene)
34 {
35 delete scene;
36 scene = nullptr;
37 }
38}
39
40void MainWindow::updateMain()
41{
42 if (ibat)
43 {
44 //yDebug("received battery info");
45 bool ret = true;
46 ret &= ibat->getBatteryVoltage(voltage);
47 ret &= ibat->getBatteryCurrent(current);
48 ret &= ibat->getBatteryCharge(charge);
49 if (enable_ask_info)
50 {
51 ret &= ibat->getBatteryInfo(info);
52 char buff[5000];
53 sprintf(buff, "%s", info.c_str());
54 yDebug("info: %s",buff);
55 }
56 connected = ret;
57 }
58 else
59 {
60 connected = false;
61 yError("TIMEOUT: unable to receive data from battery manager ");
62 }
63
64 scene->clear();
65 scene->setSceneRect(0, 0, 200, 180);
66
67 //For debug purpose only
68 //connected = true;
69 //charge = 100;
70 //voltage = 40.1;
71 //current = -10.3;
72
73 //the background
74 if (connected && charge > 12)
75 {
76 QRect rect(0, 0, 200, 180);
77 QPixmap qpm = img_background1.copy(rect);
78 QGraphicsPixmapItem *p1 = scene->addPixmap(qpm);
79 p1->setScale(1);
80 p1->setPos(0, 0);
81 }
82 else
83 {
84 QRect rect(0, 0, 200, 180);
85 QPixmap qpm = img_background2.copy(rect);
86 QGraphicsPixmapItem *p1 = scene->addPixmap(qpm);
87 p1->setScale(1);
88 p1->setPos(0, 0);
89 }
90
91 //the charge text
92 {
93 if (current<-0.3)
94 {
95 QRect rect(0, 8, 48, 8);
96 QPixmap qpm = img_charge.copy(rect);
97 QGraphicsPixmapItem *p1 = scene->addPixmap(qpm);
98 p1->setScale(1);
99 p1->setPos(56, 77);
100 }
101 else
102 {
103 QRect rect(0, 0, 48, 8);
104 QPixmap qpm = img_charge.copy(rect);
105 QGraphicsPixmapItem *p1 = scene->addPixmap(qpm);
106 p1->setScale(1);
107 p1->setPos(56, 77);
108 }
109 }
110
111 //the charge indicator
112 {
113 QRect rect0(0, 0, 14, 7);
114 QRect rect1(0, 15, 14, 7);
115 QRect rect2(0, 30, 14, 7);
116 QPixmap qpm0 = img_blocks.copy(rect0);
117 QPixmap qpm1 = img_blocks.copy(rect1);
118 QPixmap qpm2 = img_blocks.copy(rect2);
119 QPixmap* qpp = &qpm2;
120 int n_blocks = int(charge * 11 / 100.0);
121 for (int i = 0; i < n_blocks; i++)
122 {
123 if (current < -0.3) {
124 qpp = &qpm0; //draw charging arrows
125 } else {
126 qpp = &qpm1; //draw standard boxes
127 }
128
129 int xpos = 166;
130 int ypos = 135 - i * 6;
131
132 QGraphicsPixmapItem *p1 = scene->addPixmap(*qpp);
133 p1->setScale(1);
134 p1->setPos(xpos, ypos);
135 }
136 }
137
138 //the voltage
139 {
140 char buff[10];
141 sprintf(buff, "%4.1f", voltage);
142 int len = strlen(buff);
143 int point_off = 0;
144 for (int i = 0; i < len; i++)
145 {
146 if (buff[i] == '.') {
147 point_off = 17;
148 }
149 if (buff[i] >= '0' && buff[i] <= '9')
150 {
151 QRect rect((buff[i] - '0') * 29, 0, 29, 52);
152 QPixmap qpm = img_numbers.copy(rect);
153 QGraphicsPixmapItem *p1 = scene->addPixmap(qpm);
154 p1->setScale(1);
155 p1->setPos(19 + i * 29 - point_off, 21);
156 }
157 }
158 }
159
160 //the current
161 {
162 char buff[10];
163 sprintf(buff, "%4.1f", fabs(current));
164 int len = strlen(buff);
165 int point_off = 0;
166 for (int i = 0; i < len; i++)
167 {
168 if (buff[i] == '.') {
169 point_off = 17;
170 }
171 if (buff[i] >= '0' && buff[i] <= '9')
172 {
173 QRect rect((buff[i] - '0') * 29, 0, 29, 52);
174 QPixmap qpm = img_numbers.copy(rect);
175 QGraphicsPixmapItem *p1 = scene->addPixmap(qpm);
176 p1->setScale(1);
177 p1->setPos(19 + i * 29 - point_off, 88);
178 }
179 }
180 }
181 ui->graphicsView->setScene(scene);
182
183 return;
184}
185
186MainWindow::MainWindow(const yarp::os::ResourceFinder& rf, yarp::dev::IBattery* p_ibat, QWidget *parent, double refresh_period) : QMainWindow(parent),
187 ibat(p_ibat),
188 drv(nullptr),
189 ui(new Ui::MainWindow),
190 connected(false),
191 enable_ask_info(false),
192 voltage(0),
193 current(0),
194 charge(0)
195{
196 ui->setupUi(this);
197 mainTimer = new QTimer(this);
198 connect(mainTimer, SIGNAL(timeout()), this, SLOT(updateMain()));
199 mainTimer->start(1000*refresh_period); //10 seconds
200
201 //this->setWindowFlags(Qt::BypassWindowManagerHint); //Set window with no title bar
202 //this->setWindowFlags(Qt::CustomizeWindowHint); //Set window with no title bar
203 this->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint); //Set window to fixed size
204 //this->setWindowFlags(Qt::FramelessWindowHint); //Set a frameless window
205 //this->setWindowFlags(Qt::WindowStaysOnTopHint); //Always on top
206
207 bool ret_load = true;
208 ret_load &= img_background1.load(":/images/background.bmp");
209 ret_load &= img_background2.load(":/images/background2.bmp");
210 ret_load &= img_blocks.load(":/images/batt_blocks.bmp");
211 ret_load &= img_charge.load(":/images/charge.bmp");
212 ret_load &= img_numbers.load(":/images/numbers.bmp");
213 if (ret_load == false)
214 {
215 yError("Failed loading graphics");
216 }
217
218 img_blocks.setMask(img_blocks.createMaskFromColor(QColor(255,0,255)));
219 img_charge.setMask(img_charge.createMaskFromColor(QColor(255, 0, 255)));
220 img_numbers.setMask(img_numbers.createMaskFromColor(QColor(255, 0, 255)));
221
222 scene = new QGraphicsScene;
223 QTimer::singleShot(0, this, &MainWindow::updateMain);
224}
bool ret
#define yError(...)
Definition Log.h:361
#define yDebug(...)
Definition Log.h:275
int SIGNAL(int pid, int signum)
MainWindow class.
Definition display.h:22
yarp::dev::IBattery * ibat
Definition display.h:35
MainWindow(const yarp::os::ResourceFinder &rf, yarp::dev::IBattery *ibat, QWidget *parent=0, double refresh_period=10.0)
Definition display.cpp:186
QGraphicsScene * scene
Definition display.h:37
A generic battery interface.
Definition IBattery.h:26
virtual yarp::dev::ReturnValue getBatteryInfo(std::string &battery_info)=0
get the battery hardware characteristics (e.g.
virtual yarp::dev::ReturnValue getBatteryCharge(double &charge)=0
get the battery status of charge
virtual yarp::dev::ReturnValue getBatteryCurrent(double &current)=0
Get the instantaneous current measurement.
virtual yarp::dev::ReturnValue getBatteryVoltage(double &voltage)=0
Get the instantaneous voltage measurement.
Helper class for finding config files and other external resources.
Definition aboutdlg.h:11