-
Notifications
You must be signed in to change notification settings - Fork 5
/
Protocol.cpp
133 lines (112 loc) · 3.45 KB
/
Protocol.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
// QWalkingPad - Simple desktop application for controlling the Kingsmith WalkingPad over BLE
// Copyright (C) 2021 Dorian Rudolph
//
// 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, either version 3 of the License, or (at your option)
// any later version.
//
// 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/>.
#include "Protocol.h"
namespace Pad {
QByteArray messageByte(uint8_t k, uint8_t v) {
uint8_t data[]{0xf7, 0xa2, k, v, uint8_t(0xa2 + k + v), 0xfd};
return QByteArray(reinterpret_cast<char *>(data), sizeof(data));
}
QByteArray messageInt(uint8_t k, uint32_t v) {
uint8_t data[]{0xf7, 0xa6, k, 0, uint8_t(v >> 16), uint8_t(v >> 8), uint8_t(v), 0, 0xfd};
for (int i = 1; i < 7; ++i) data[7] += data[i];
return QByteArray(reinterpret_cast<char *>(data), sizeof(data));
}
QByteArray query() {
return messageByte(0, 0);
}
QByteArray queryParams() {
return messageInt(0, 0);
}
QByteArray setSpeed(uint8_t speed) {
return messageByte(1, speed);
}
QByteArray setStartSpeed(uint8_t speed) {
return messageInt(4, speed);
}
QByteArray setMode(uint8_t mode) {
return messageByte(2, mode);
}
QByteArray start() {
return messageByte(4, 1);
}
QByteArray syncRecord(uint8_t n) {
uint8_t data[]{0xf7, 0xa7, 0xaa, n, uint8_t(0xa7 + 0xaa + n), 0xfd};
return QByteArray(reinterpret_cast<char *>(data), sizeof(data));
}
QByteArray setCali(uint8_t enable) {
return messageInt(2, enable);
}
QByteArray setMaxSpeed(uint8_t speed) {
return messageInt(3, speed);
}
QByteArray setAutoStart(uint8_t enable) {
return messageInt(5, enable);
}
QByteArray setSensitivity(uint8_t sensitivity) {
return messageInt(6, sensitivity);
}
QByteArray setUnit(uint8_t unit) {
return messageInt(8, unit);
}
QByteArray setLock(uint8_t enable) {
return messageInt(9, enable);
}
QByteArray setDisplayInfo(uint8_t info) {
return messageInt(7, info);
}
static uint32_t parseInt(const uint8_t *m, int i) {
return (m[i] << 16) | (m[i + 1] << 8) | m[i + 2];
}
Message parseMessage(const QByteArray &message) {
Message parsed;
auto m = reinterpret_cast<const uint8_t *>(message.constData());
auto sz = message.size();
auto typ = m[1];
if (typ == 0xa2 && sz >= 15) {
parsed = Info{
.state = uint8_t(m[2]),
.speed = uint8_t(m[3]),
.mode = uint8_t(m[4]),
.time = parseInt(m, 5),
.distance = parseInt(m, 8),
.steps = parseInt(m, 11),
};
} else if (typ == 0xa6 && sz >= 14) {
parsed = Params{
.goalType = m[2],
.goal = parseInt(m, 3),
.regulate = m[6],
.maxSpeed = m[7],
.startSpeed = m[8],
.startMode = m[9],
.sensitivity = m[10],
.display = m[11],
.lock = m[12],
.unit = m[13],
};
} else if (typ == 0xa7 && sz >= 18) {
parsed = Record{
.onTime = parseInt(m, 2),
.startTime = parseInt(m, 5),
.duration = parseInt(m, 8),
.distance = parseInt(m, 11),
.steps = parseInt(m, 14),
.remainingRecords = m[17]
};
}
return parsed;
}
}