SMACC2
Loading...
Searching...
No Matches
cl_keyboard.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
19#include <smacc2/smacc.hpp>
20
21#include <boost/asio.hpp>
22#include <boost/asio/posix/stream_descriptor.hpp>
23#include <iostream>
24
25#include <std_msgs/msg/u_int16.hpp>
26
27namespace cl_keyboard
28{
29//----------------- TIMER sc::event DEFINITION ----------------------------------------------
30template <typename TSource, typename TOrthogonal>
31struct EvKeyPressA : sc::event<EvKeyPressA<TSource, TOrthogonal>>
32{
33};
34
35template <typename TSource, typename TOrthogonal>
36struct EvKeyPressB : sc::event<EvKeyPressB<TSource, TOrthogonal>>
37{
38};
39
40template <typename TSource, typename TOrthogonal>
41struct EvKeyPressC : sc::event<EvKeyPressC<TSource, TOrthogonal>>
42{
43};
44
45template <typename TSource, typename TOrthogonal>
46struct EvKeyPressD : sc::event<EvKeyPressD<TSource, TOrthogonal>>
47{
48};
49
50template <typename TSource, typename TOrthogonal>
51struct EvKeyPressE : sc::event<EvKeyPressE<TSource, TOrthogonal>>
52{
53};
54
55template <typename TSource, typename TOrthogonal>
56struct EvKeyPressF : sc::event<EvKeyPressF<TSource, TOrthogonal>>
57{
58};
59
60template <typename TSource, typename TOrthogonal>
61struct EvKeyPressG : sc::event<EvKeyPressG<TSource, TOrthogonal>>
62{
63};
64
65template <typename TSource, typename TOrthogonal>
66struct EvKeyPressH : sc::event<EvKeyPressH<TSource, TOrthogonal>>
67{
68};
69
70template <typename TSource, typename TOrthogonal>
71struct EvKeyPressI : sc::event<EvKeyPressI<TSource, TOrthogonal>>
72{
73};
74
75template <typename TSource, typename TOrthogonal>
76struct EvKeyPressJ : sc::event<EvKeyPressJ<TSource, TOrthogonal>>
77{
78};
79
80template <typename TSource, typename TOrthogonal>
81struct EvKeyPressK : sc::event<EvKeyPressK<TSource, TOrthogonal>>
82{
83};
84
85template <typename TSource, typename TOrthogonal>
86struct EvKeyPressL : sc::event<EvKeyPressL<TSource, TOrthogonal>>
87{
88};
89
90template <typename TSource, typename TOrthogonal>
91struct EvKeyPressM : sc::event<EvKeyPressM<TSource, TOrthogonal>>
92{
93};
94
95template <typename TSource, typename TOrthogonal>
96struct EvKeyPressN : sc::event<EvKeyPressN<TSource, TOrthogonal>>
97{
98};
99
100template <typename TSource, typename TOrthogonal>
101struct EvKeyPressO : sc::event<EvKeyPressO<TSource, TOrthogonal>>
102{
103};
104
105template <typename TSource, typename TOrthogonal>
106struct EvKeyPressP : sc::event<EvKeyPressP<TSource, TOrthogonal>>
107{
108};
109
110template <typename TSource, typename TOrthogonal>
111struct EvKeyPressQ : sc::event<EvKeyPressQ<TSource, TOrthogonal>>
112{
113};
114
115template <typename TSource, typename TOrthogonal>
116struct EvKeyPressR : sc::event<EvKeyPressR<TSource, TOrthogonal>>
117{
118};
119
120template <typename TSource, typename TOrthogonal>
121struct EvKeyPressS : sc::event<EvKeyPressS<TSource, TOrthogonal>>
122{
123};
124
125template <typename TSource, typename TOrthogonal>
126struct EvKeyPressT : sc::event<EvKeyPressT<TSource, TOrthogonal>>
127{
128};
129
130template <typename TSource, typename TOrthogonal>
131struct EvKeyPressU : sc::event<EvKeyPressU<TSource, TOrthogonal>>
132{
133};
134
135template <typename TSource, typename TOrthogonal>
136struct EvKeyPressV : sc::event<EvKeyPressV<TSource, TOrthogonal>>
137{
138};
139
140template <typename TSource, typename TOrthogonal>
141struct EvKeyPressW : sc::event<EvKeyPressW<TSource, TOrthogonal>>
142{
143};
144
145template <typename TSource, typename TOrthogonal>
146struct EvKeyPressX : sc::event<EvKeyPressX<TSource, TOrthogonal>>
147{
148};
149
150template <typename TSource, typename TOrthogonal>
151struct EvKeyPressY : sc::event<EvKeyPressY<TSource, TOrthogonal>>
152{
153};
154
155template <typename TSource, typename TOrthogonal>
156struct EvKeyPressZ : sc::event<EvKeyPressZ<TSource, TOrthogonal>>
157{
158};
159
160//------------------ KEYBOARD CLIENT ---------------------------------------------
161
162class ClKeyboard : public smacc2::client_bases::SmaccSubscriberClient<std_msgs::msg::UInt16>
163{
164public:
165 ClKeyboard();
166 virtual ~ClKeyboard();
167 virtual void onInitialize() override;
168
169 smacc2::SmaccSignal<void(char keypress)> OnKeyPress_;
170
171 template <typename T>
172 void OnKeyPress(void (T::*callback)(char keypress), T * object)
173 {
174 //this->connectSignal(OnKeyPress_, callback, object);
175 this->getStateMachine()->createSignalConnection(OnKeyPress_, callback, object);
176 }
177
178 std::function<void(std_msgs::msg::UInt16)> postEventKeyPress;
179
180 template <typename TOrthogonal, typename TSourceObject>
182 {
183 // call to the base configuration to properly handling the message and initial message smacc events
185 TOrthogonal, TSourceObject>();
186
187 postEventKeyPress = [=](auto unicode_keychar)
188 {
189 char character = (char)unicode_keychar.data;
190 RCLCPP_WARN(getLogger(), "detected keyboard: %c", character);
191
192 if (character == 'a')
193 this->postKeyEvent<EvKeyPressA<ClKeyboard, TOrthogonal>>();
194 else if (character == 'b')
195 this->postKeyEvent<EvKeyPressB<ClKeyboard, TOrthogonal>>();
196 else if (character == 'c')
197 this->postKeyEvent<EvKeyPressC<ClKeyboard, TOrthogonal>>();
198 else if (character == 'd')
199 this->postKeyEvent<EvKeyPressD<ClKeyboard, TOrthogonal>>();
200 else if (character == 'e')
201 this->postKeyEvent<EvKeyPressE<ClKeyboard, TOrthogonal>>();
202 else if (character == 'f')
203 this->postKeyEvent<EvKeyPressF<ClKeyboard, TOrthogonal>>();
204 else if (character == 'g')
205 this->postKeyEvent<EvKeyPressG<ClKeyboard, TOrthogonal>>();
206 else if (character == 'h')
207 this->postKeyEvent<EvKeyPressH<ClKeyboard, TOrthogonal>>();
208 else if (character == 'y')
209 this->postKeyEvent<EvKeyPressI<ClKeyboard, TOrthogonal>>();
210 else if (character == 'j')
211 this->postKeyEvent<EvKeyPressJ<ClKeyboard, TOrthogonal>>();
212 else if (character == 'k')
213 this->postKeyEvent<EvKeyPressK<ClKeyboard, TOrthogonal>>();
214 else if (character == 'l')
215 this->postKeyEvent<EvKeyPressL<ClKeyboard, TOrthogonal>>();
216 else if (character == 'm')
217 this->postKeyEvent<EvKeyPressM<ClKeyboard, TOrthogonal>>();
218 else if (character == 'n')
219 this->postKeyEvent<EvKeyPressN<ClKeyboard, TOrthogonal>>();
220 else if (character == 'o')
221 this->postKeyEvent<EvKeyPressO<ClKeyboard, TOrthogonal>>();
222 else if (character == 'p')
223 this->postKeyEvent<EvKeyPressP<ClKeyboard, TOrthogonal>>();
224 else if (character == 'q')
225 this->postKeyEvent<EvKeyPressQ<ClKeyboard, TOrthogonal>>();
226 else if (character == 'r')
227 this->postKeyEvent<EvKeyPressR<ClKeyboard, TOrthogonal>>();
228 else if (character == 's')
229 this->postKeyEvent<EvKeyPressS<ClKeyboard, TOrthogonal>>();
230 else if (character == 't')
231 this->postKeyEvent<EvKeyPressT<ClKeyboard, TOrthogonal>>();
232 else if (character == 'u')
233 this->postKeyEvent<EvKeyPressU<ClKeyboard, TOrthogonal>>();
234 else if (character == 'v')
235 this->postKeyEvent<EvKeyPressV<ClKeyboard, TOrthogonal>>();
236 else if (character == 'w')
237 this->postKeyEvent<EvKeyPressW<ClKeyboard, TOrthogonal>>();
238 else if (character == 'x')
239 this->postKeyEvent<EvKeyPressX<ClKeyboard, TOrthogonal>>();
240 else if (character == 'y')
241 this->postKeyEvent<EvKeyPressY<ClKeyboard, TOrthogonal>>();
242 else if (character == 'z')
243 this->postKeyEvent<EvKeyPressZ<ClKeyboard, TOrthogonal>>();
244 OnKeyPress_(character); /* */
245 };
246 }
247
248 void onKeyboardMessage(const std_msgs::msg::UInt16 & unicode_keychar);
249
250 template <typename TEv>
252 {
253 RCLCPP_WARN(
254 getLogger(), "ClKeyboard ev: %s", smacc2::demangleSymbol(typeid(TEv).name()).c_str());
255 this->postEvent<TEv>();
256 }
257
258private:
260};
261} // namespace cl_keyboard
std::function< void(std_msgs::msg::UInt16)> postEventKeyPress
smacc2::SmaccSignal< void(char keypress)> OnKeyPress_
void onKeyboardMessage(const std_msgs::msg::UInt16 &unicode_keychar)
Definition: cl_keyboard.cpp:38
virtual void onInitialize() override
Definition: cl_keyboard.cpp:27
void OnKeyPress(void(T::*callback)(char keypress), T *object)
ISmaccStateMachine * getStateMachine()
rclcpp::Logger getLogger()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
std::string demangleSymbol()