BLEProthesisInterface.cpp
Go to the documentation of this file.
2
3#include <sstream>
4
6
8 _worker{new BLEProthesisInterfaceQtWorkerThread{mac, *this}}, _protocol{protocol}
9{
10 _worker->start();
11}
12
14{
15 _worker->kill();
16 _worker->quit();
17 _worker->exit();
18 _worker->wait(1000); // ms
19 _worker->terminate();
20 _worker->wait();
21}
22
23std::int64_t
25{
26 return _thumbPWM;
27}
28
29std::int64_t
31{
32 return _thumbPos;
33}
34
35std::int64_t
37{
38 return _fingerPWM;
39}
40
41std::int64_t
43{
44 return _fingerPos;
45}
46
49{
50 return _state;
51}
52
53void
54BLEProthesisInterface::sendRaw(const std::string& cmd)
55{
56 if (_state != State::Running)
57 {
58 throw std::invalid_argument{"BLEProthesisInterface::sendRaw( cmd = " + cmd +
59 " ): The UART service is not connected!"};
60 }
61 _worker->sendCommand(cmd);
62}
63
64void
66{
67 if (n > getMaxG())
68 {
69 throw std::invalid_argument{"BLEProthesisInterface::sendGrasp( n = " + std::to_string(n) +
70 " ): the maximal value for n is " + std::to_string(getMaxG())};
71 }
72 sendRaw("g" + std::to_string(n) + "\n");
73}
74
75void
76BLEProthesisInterface::sendThumbPWM(uint64_t v, uint64_t mxPWM, uint64_t pos)
77{
78 if (v < getMinV() || v > getMaxV())
79 {
80 throw std::invalid_argument{
81 "sendThumbPWM( v = " + std::to_string(v) + " , mxPWM = " + std::to_string(mxPWM) +
82 " , pos = " + std::to_string(pos) + " ): The interval for v is [" +
83 std::to_string(getMinV()) + ", " + std::to_string(getMaxV()) + "]"};
84 }
85 if (mxPWM > getMaxPWM())
86 {
87 throw std::invalid_argument{
88 "sendThumbPWM( v = " + std::to_string(v) + " , mxPWM = " + std::to_string(mxPWM) +
89 " , pos = " + std::to_string(pos) + " ): The interval for mxPWM is [0, , " +
90 std::to_string(getMaxPWM()) + "]"};
91 }
92 if (pos > getMaxPosThumb())
93 {
94 throw std::invalid_argument{
95 "sendThumbPWM( v = " + std::to_string(v) + " , mxPWM = " + std::to_string(mxPWM) +
96 " , pos = " + std::to_string(pos) + " ): The interval for pos is [0, " +
97 std::to_string(getMaxPosThumb()) + "]"};
98 }
99 std::stringstream str;
100 str << 'M' << 2 << ',' << v << ',' << mxPWM << ',' << pos << '\n';
101 sendRaw(str.str());
102}
103
104void
105BLEProthesisInterface::sendFingerPWM(uint64_t v, uint64_t mxPWM, uint64_t pos)
106{
107 if (v < getMinV() || v > getMaxV())
108 {
109 throw std::invalid_argument{
110 "sendThumbPWM( v = " + std::to_string(v) + " , mxPWM = " + std::to_string(mxPWM) +
111 " , pos = " + std::to_string(pos) + " ): The interval for v is [" +
112 std::to_string(getMinV()) + ", " + std::to_string(getMaxV()) + "]"};
113 }
114 if (mxPWM > getMaxPWM())
115 {
116 throw std::invalid_argument{
117 "sendThumbPWM( v = " + std::to_string(v) + " , mxPWM = " + std::to_string(mxPWM) +
118 " , pos = " + std::to_string(pos) + " ): The interval for mxPWM is [0, , " +
119 std::to_string(getMaxPWM()) + "]"};
120 }
121 if (pos > getMaxPosFingers())
122 {
123 throw std::invalid_argument{
124 "sendThumbPWM( v = " + std::to_string(v) + " , mxPWM = " + std::to_string(mxPWM) +
125 " , pos = " + std::to_string(pos) + " ): The interval for pos is [0, " +
126 std::to_string(getMaxPosFingers()) + "]"};
127 }
128 std::stringstream str;
129 str << 'M' << 3 << ',' << v << ',' << mxPWM << ',' << pos << '\n';
130 sendRaw(str.str());
131}
132
133void
135{
136 _verboseReceive = b;
137}
138
139void
141{
142 _verboseSend = b;
143}
144
145std::string
147{
148 switch (s)
149 {
151 return "Created";
153 return "DiscoveringDevices";
155 return "DiscoveringDevicesDone";
157 return "Disconnected";
159 return "Connecting";
161 return "ConnectingDone";
163 return "DiscoveringServices";
165 return "DiscoveringServicesDone";
167 return "ConnectingService";
169 return "Running";
171 return "Killed";
172 }
173 return "UNKNOWN VALUE FOR BLEProthesisInterface::State: " + std::to_string(static_cast<int>(s));
174}
std::string to_string(BLEProthesisInterface::State s)
std::string str(const T &t)
static constexpr std::uint64_t getMaxV()
void sendThumbPWM(std::uint64_t v, std::uint64_t maxPWM, std::uint64_t pos)
std::int64_t getFingerPos() const
std::int64_t getThumbPos() const
std::int64_t getThumbPWM() const
void sendGrasp(std::uint64_t n)
static constexpr std::uint64_t getMaxPosThumb()
std::int64_t getFingerPWM() const
static constexpr std::uint64_t getMaxPosFingers()
BLEProthesisInterface(const std::string &mac, SensorValueProtocol protocol=SensorValueProtocol::mx_pos_pwm)
static constexpr std::uint64_t getMinV()
void sendFingerPWM(std::uint64_t v, std::uint64_t maxPWM, std::uint64_t pos)
static constexpr std::uint64_t getMaxPWM()
static constexpr std::uint64_t getMaxG()
void sendRaw(const std::string &cmd)