-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactuator_viewer.cpp
190 lines (156 loc) · 7.02 KB
/
actuator_viewer.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
/*
* This file is part of the ct-Bot remote viewer tool.
* Copyright (c) 2020-2022 Timo Sandmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file actuator_viewer.cpp
* @brief Actuator viewer component
* @author Timo Sandmann
* @date 11.04.2020
*/
#include <QQmlApplicationEngine>
#include <QString>
#include <QDebug>
#include <cstring>
#include <iostream>
#include "actuator_viewer.h"
#include "connection_manager.h"
#include "command.h"
ActuatorViewerV1::ActuatorViewerV1(QQmlApplicationEngine* p_engine, ConnectionManagerV1& command_eval) : ValueViewer { p_engine }, p_lcd_ {} {
qmlRegisterType<ValueModel>("Actuators", 1, 0, "ActuatorModel");
qmlRegisterUncreatableType<ValueList>("Actuators", 1, 0, "ValueList", QStringLiteral("Actuators should not be created in QML"));
list_.appendItem(MOTOR_L_.cbegin());
list_.appendItem(MOTOR_R_.cbegin());
list_.appendItem(LEDS_.cbegin());
update_map();
register_model(QStringLiteral("actuatorModel"));
command_eval.register_cmd(ctbot::CommandCodes::CMD_AKT_MOT, [this](const ctbot::CommandBase& cmd) {
// std::cout << "CMD_AKT_MOT received: " << cmd << "\n";
model_.setData(map_[MOTOR_L_.cbegin()], cmd.get_cmd_data_l(), ValueModel::Value);
model_.setData(map_[MOTOR_R_.cbegin()], cmd.get_cmd_data_r(), ValueModel::Value);
return true;
});
command_eval.register_cmd(ctbot::CommandCodes::CMD_AKT_LED, [this](const ctbot::CommandBase& cmd) {
// std::cout << "CMD_AKT_LED received: " << cmd << std::endl;
model_.setData(map_[LEDS_.cbegin()], cmd.get_cmd_data_l(), ValueModel::Value);
return true;
});
command_eval.register_cmd(ctbot::CommandCodes::CMD_AKT_LCD, [this](const ctbot::CommandBase& cmd) {
// std::cout << "CMD_AKT_LCD received: " << cmd << "\n";
if (!p_lcd_) {
auto root { p_engine_->rootObjects() };
p_lcd_ = root.first()->findChild<QObject*>("LCD");
if (!p_lcd_) {
return false;
}
}
switch (cmd.get_cmd_subcode()) {
case ctbot::CommandCodes::CMD_SUB_LCD_CLEAR: {
// qDebug() << "Display CLEAR received.";
std::memset(&lcd_text_[0][0], ' ', sizeof(lcd_text_));
lcd_text_[0][20] = 0;
lcd_text_[1][20] = 0;
lcd_text_[2][20] = 0;
lcd_text_[3][20] = 0;
QString data = lcd_text_[0];
data += "\n";
data += lcd_text_[1];
data += "\n";
data += lcd_text_[2];
data += "\n";
data += lcd_text_[3];
p_lcd_->setProperty("text", data);
return true;
}
case ctbot::CommandCodes::CMD_SUB_LCD_DATA: {
// std::string payload { reinterpret_cast<const char*>(cmd.get_payload().data()), cmd.get_payload_size() };
// qDebug() << "Display DATA received: col =" << cmd.get_cmd_data_l() << " row = " << cmd.get_cmd_data_r() << " len = " <<
// cmd.get_payload().size() << " text = " << QString::fromStdString(payload);
size_t col { static_cast<size_t>(cmd.get_cmd_data_l()) };
if (col >= 20) {
qDebug() << "invalid display command received: col =" << col;
// std::cout << cmd << std::endl;
col = 19;
}
size_t row { static_cast<size_t>(cmd.get_cmd_data_r()) };
if (row >= 4) {
qDebug() << "invalid display command received: row =" << row;
// std::cout << cmd << std::endl;
return false;
}
size_t len { cmd.get_payload_size() };
if (len + col > 20) {
// qDebug() << "len updated: len =" << len << " col =" << col;
len = 20 - col;
}
if (!len) {
qDebug() << "invalid display command received: len =" << len << " col =" << col;
std::cout << cmd << std::endl;
return false;
}
std::strncpy(&lcd_text_[row][col], reinterpret_cast<const char*>(cmd.get_payload().data()), len);
lcd_text_[row][col + len] = 0;
QString data = lcd_text_[0];
data += "\n";
data += lcd_text_[1];
data += "\n";
data += lcd_text_[2];
data += "\n";
data += lcd_text_[3];
data.replace(regex_replace_0_, ".");
data.replace(regex_replace_1_, ".");
data.replace(regex_replace_2_, "#");
// qDebug() << "data = " << data;
p_lcd_->setProperty("text", data);
return true;
}
default: return false;
}
});
}
ActuatorViewerV2::ActuatorViewerV2(QQmlApplicationEngine* p_engine, ConnectionManagerV2& command_eval) : ValueViewer { p_engine } {
qmlRegisterType<ValueModel>("Actuators", 1, 0, "ActuatorModel");
qmlRegisterUncreatableType<ValueList>("Actuators", 1, 0, "ValueList", QStringLiteral("Actuators should not be created in QML"));
list_.appendItem(MOTOR_L_.cbegin());
list_.appendItem(MOTOR_R_.cbegin());
list_.appendItem(SERVO_1_.cbegin());
list_.appendItem(SERVO_2_.cbegin());
list_.appendItem(LEDS_.cbegin());
update_map();
register_model(QStringLiteral("actuatorModelV2"));
command_eval.register_cmd("act", [this, &command_eval](const std::string_view& str) {
if (command_eval.get_version() != command_eval.version_active()) {
return false;
}
if (!str.length()) {
return false;
}
int16_t values[2];
if (parse(str, motor_regex_, values[0], values[1])) {
model_.setData(map_[MOTOR_L_.cbegin()], values[0], ValueModel::Value);
model_.setData(map_[MOTOR_R_.cbegin()], values[1], ValueModel::Value);
}
if (parse(str, servo1_regex_, values[0])) {
model_.setData(map_[SERVO_1_.cbegin()], values[0], ValueModel::Value);
}
if (parse(str, servo2_regex_, values[0])) {
model_.setData(map_[SERVO_2_.cbegin()], values[0], ValueModel::Value);
}
if (parse(str, led_regex_, values[0])) {
model_.setData(map_[LEDS_.cbegin()], values[0], ValueModel::Value);
}
return true;
});
}