SMACC2
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 char character = (char)unicode_keychar.data;
189 RCLCPP_WARN(getLogger(), "detected keyboard: %c", character);
190
191 if (character == 'a')
192 this->postKeyEvent<EvKeyPressA<ClKeyboard, TOrthogonal>>();
193 else if (character == 'b')
194 this->postKeyEvent<EvKeyPressB<ClKeyboard, TOrthogonal>>();
195 else if (character == 'c')
196 this->postKeyEvent<EvKeyPressC<ClKeyboard, TOrthogonal>>();
197 else if (character == 'd')
198 this->postKeyEvent<EvKeyPressD<ClKeyboard, TOrthogonal>>();
199 else if (character == 'e')
200 this->postKeyEvent<EvKeyPressE<ClKeyboard, TOrthogonal>>();
201 else if (character == 'f')
202 this->postKeyEvent<EvKeyPressF<ClKeyboard, TOrthogonal>>();
203 else if (character == 'g')
204 this->postKeyEvent<EvKeyPressG<ClKeyboard, TOrthogonal>>();
205 else if (character == 'h')
206 this->postKeyEvent<EvKeyPressH<ClKeyboard, TOrthogonal>>();
207 else if (character == 'y')
208 this->postKeyEvent<EvKeyPressI<ClKeyboard, TOrthogonal>>();
209 else if (character == 'j')
210 this->postKeyEvent<EvKeyPressJ<ClKeyboard, TOrthogonal>>();
211 else if (character == 'k')
212 this->postKeyEvent<EvKeyPressK<ClKeyboard, TOrthogonal>>();
213 else if (character == 'l')
214 this->postKeyEvent<EvKeyPressL<ClKeyboard, TOrthogonal>>();
215 else if (character == 'm')
216 this->postKeyEvent<EvKeyPressM<ClKeyboard, TOrthogonal>>();
217 else if (character == 'n')
218 this->postKeyEvent<EvKeyPressN<ClKeyboard, TOrthogonal>>();
219 else if (character == 'o')
220 this->postKeyEvent<EvKeyPressO<ClKeyboard, TOrthogonal>>();
221 else if (character == 'p')
222 this->postKeyEvent<EvKeyPressP<ClKeyboard, TOrthogonal>>();
223 else if (character == 'q')
224 this->postKeyEvent<EvKeyPressQ<ClKeyboard, TOrthogonal>>();
225 else if (character == 'r')
226 this->postKeyEvent<EvKeyPressR<ClKeyboard, TOrthogonal>>();
227 else if (character == 's')
228 this->postKeyEvent<EvKeyPressS<ClKeyboard, TOrthogonal>>();
229 else if (character == 't')
230 this->postKeyEvent<EvKeyPressT<ClKeyboard, TOrthogonal>>();
231 else if (character == 'u')
232 this->postKeyEvent<EvKeyPressU<ClKeyboard, TOrthogonal>>();
233 else if (character == 'v')
234 this->postKeyEvent<EvKeyPressV<ClKeyboard, TOrthogonal>>();
235 else if (character == 'w')
236 this->postKeyEvent<EvKeyPressW<ClKeyboard, TOrthogonal>>();
237 else if (character == 'x')
238 this->postKeyEvent<EvKeyPressX<ClKeyboard, TOrthogonal>>();
239 else if (character == 'y')
240 this->postKeyEvent<EvKeyPressY<ClKeyboard, TOrthogonal>>();
241 else if (character == 'z')
242 this->postKeyEvent<EvKeyPressZ<ClKeyboard, TOrthogonal>>();
243 OnKeyPress_(character); /* */
244 };
245 }
246
247 void onKeyboardMessage(const std_msgs::msg::UInt16 & unicode_keychar);
248
249 template <typename TEv>
251 {
252 RCLCPP_WARN(
253 getLogger(), "ClKeyboard ev: %s", smacc2::demangleSymbol(typeid(TEv).name()).c_str());
254 this->postEvent<TEv>();
255 }
256
257private:
259};
260} // 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(const std::string &name)
void callback(const image_tools::ROSCvMatContainer &img)