1
+ #include " ScriptHandler.h"
2
+ #include " Debug.h"
3
+
4
+
5
+ void ScriptHandler::SetKeyboard (Keyboard_ *keyboard)
6
+ {
7
+ this ->ourKeyboard = keyboard;
8
+ }
9
+
10
+ void ScriptHandler::SetKeypressDelay (unsigned long delay_ms)
11
+ {
12
+ this ->keypressDelay = delay_ms;
13
+ }
14
+
15
+ inline void ScriptHandler::WriteCharacter (uint8_t c)
16
+ {
17
+ ourKeyboard->press (c);
18
+ delay (keypressDelay);
19
+ ourKeyboard->release (c);
20
+ }
21
+
22
+ void ScriptHandler::WriteString (String s)
23
+ {
24
+ for (unsigned int i = 0 ; i < s.length (); i++)
25
+ {
26
+ WriteCharacter (s.charAt (i));
27
+ }
28
+ }
29
+
30
+ // Simple function to output the content of a file in the sdcard as escape chars
31
+ // For example, in linux you can use ' echo -n -e "\xAA\xBB\XCC" > /tmp/file '
32
+ void ScriptHandler::EchoFileHex (String sdFileName)
33
+ {
34
+ return ;
35
+ /*
36
+ DEBUG_PRINT("echoFileHex: Opening file '" + sdFileName + "'");
37
+ fromSD = SD.open(sdFileName);
38
+ if (!fromSD)
39
+ {
40
+ DEBUG_PRINT("echoFileHex: Couldn't find file: '" + sdFileName + "'");
41
+ return;
42
+ }
43
+ else
44
+ {
45
+ while (fromSD.available())
46
+ {
47
+ int c = fromSD.read();
48
+ String hex = String(c, HEX);
49
+ if (hex.length() > 1)
50
+ {
51
+ WriteCharacterString("\\x" + hex);
52
+ }
53
+ else
54
+ {
55
+ WriteCharacterString("\\x0" + hex);
56
+ }
57
+ }
58
+ fromSD.close();
59
+ }
60
+ */
61
+ }
62
+
63
+ void ScriptHandler::RepeatLastLine (int times)
64
+ {
65
+ // Won't repeat a REPEAT
66
+ if (lastLine.startsWith (" REPEAT " ))
67
+ {
68
+ return ;
69
+ }
70
+ for (int i = 0 ; i < times; i++)
71
+ {
72
+ ParseLine (lastLine);
73
+ }
74
+ }
75
+
76
+ void ScriptHandler::ParseLine (String line)
77
+ {
78
+
79
+ if (line.startsWith (" REM " ))
80
+ return ;
81
+ else if (line.startsWith (" DEFAULTDELAY " ))
82
+ commandDelay = line.substring (12 ).toInt ();
83
+ else if (line.startsWith (" DEFAULT_DELAY " ))
84
+ commandDelay = line.substring (13 ).toInt ();
85
+ else if (line.startsWith (" DEFAULTCHARDELAY " ))
86
+ keypressDelay = line.substring (16 ).toInt ();
87
+ else if (line.startsWith (" DEFAULT_CHAR_DELAY " ))
88
+ keypressDelay = line.substring (18 ).toInt ();
89
+ else if (line.startsWith (" DELAY " ))
90
+ delay (line.substring (6 ).toInt ());
91
+ else if (line.startsWith (" ECHOFILEHEX " ))
92
+ EchoFileHex (line.substring (12 ));
93
+ else if (line.startsWith (" STRING " ))
94
+ WriteString (line.substring (7 ));
95
+ else if (line.startsWith (" REPEAT " ))
96
+ RepeatLastLine (line.substring (7 ).toInt ());
97
+ else
98
+ ParseKeys (line);
99
+ ourKeyboard->releaseAll ();
100
+ delay (commandDelay);
101
+ // preserve the last line for potential repeats
102
+ lastLine = line;
103
+ }
104
+
105
+ void ScriptHandler::ParseKeys (String keys)
106
+ {
107
+ String key;
108
+ String nextKeys = keys;
109
+ int p;
110
+
111
+ DEBUG_PRINT (" ParseKeys: '" + nextKeys + " '" );
112
+
113
+ nextKeys.trim ();
114
+ p = nextKeys.indexOf (" " );
115
+ while (p > 0 )
116
+ {
117
+ key = nextKeys.substring (0 , p);
118
+ nextKeys = nextKeys.substring (p + 1 );
119
+ key.trim ();
120
+ DEBUG_PRINT (" Key: '" + key + " ' Next:'" + nextKeys + " ' p:" + String (p));
121
+ ParseKey (key);
122
+ p = nextKeys.indexOf (" " );
123
+ }
124
+ nextKeys.trim ();
125
+ ParseKey (nextKeys);
126
+ }
127
+
128
+ void ScriptHandler::ParseKey (String key)
129
+ {
130
+ DEBUG_PRINT (" ParseKey: '" + key + " '" );
131
+
132
+ if (key.length () == 1 )
133
+ ourKeyboard->press (key.charAt (0 ));
134
+ else if (key.equals (" ENTER" ))
135
+ ourKeyboard->press (KEY_RETURN);
136
+ else if (key.equals (" GUI" ) || key.equals (" WINDOWS" ))
137
+ ourKeyboard->press (KEY_LEFT_GUI);
138
+ else if (key.equals (" SHIFT" ))
139
+ ourKeyboard->press (KEY_LEFT_SHIFT);
140
+ else if (key.equals (" ALT" ))
141
+ ourKeyboard->press (KEY_LEFT_ALT);
142
+ else if (key.equals (" CTRL" ) || key.equals (" CONTROL" ))
143
+ ourKeyboard->press (KEY_LEFT_CTRL);
144
+ else if (key.equals (" CAPSLOCK" ))
145
+ ourKeyboard->press (KEY_CAPS_LOCK);
146
+ else if (key.equals (" DELETE" ))
147
+ ourKeyboard->press (KEY_DELETE);
148
+ else if (key.equals (" END" ))
149
+ ourKeyboard->press (KEY_END);
150
+ else if (key.equals (" ESC" ) || key.equals (" ESCAPE" ))
151
+ ourKeyboard->press (KEY_ESC);
152
+ else if (key.equals (" HOME" ))
153
+ ourKeyboard->press (KEY_HOME);
154
+ else if (key.equals (" INSERT" ))
155
+ ourKeyboard->press (KEY_INSERT);
156
+ else if (key.equals (" PAGEUP" ))
157
+ ourKeyboard->press (KEY_PAGE_UP);
158
+ else if (key.equals (" PAGEDOWN" ))
159
+ ourKeyboard->press (KEY_PAGE_DOWN);
160
+ else if (key.equals (" SPACE" ))
161
+ ourKeyboard->press (' ' );
162
+ else if (key.equals (" TAB" ))
163
+ ourKeyboard->press (KEY_TAB);
164
+ else if (key.equals (" UP" ) || key.equals (" UPARROW" ))
165
+ ourKeyboard->press (KEY_UP_ARROW);
166
+ else if (key.equals (" DOWN" ) || key.equals (" DOWNARROW" ))
167
+ ourKeyboard->press (KEY_DOWN_ARROW);
168
+ else if (key.equals (" LEFT" ) || key.equals (" LEFTARROW" ))
169
+ ourKeyboard->press (KEY_LEFT_ARROW);
170
+ else if (key.equals (" RIGHT" ) || key.equals (" RIGHTARROW" ))
171
+ ourKeyboard->press (KEY_RIGHT_ARROW);
172
+ else if (key.equals (" PRINTSCREEN" ))
173
+ ourKeyboard->press (PRINTSCREEN);
174
+ else if (key.equals (" F1" ))
175
+ ourKeyboard->press (KEY_F1);
176
+ else if (key.equals (" F2" ))
177
+ ourKeyboard->press (KEY_F2);
178
+ else if (key.equals (" F3" ))
179
+ ourKeyboard->press (KEY_F3);
180
+ else if (key.equals (" F4" ))
181
+ ourKeyboard->press (KEY_F4);
182
+ else if (key.equals (" F5" ))
183
+ ourKeyboard->press (KEY_F5);
184
+ else if (key.equals (" F6" ))
185
+ ourKeyboard->press (KEY_F6);
186
+ else if (key.equals (" F7" ))
187
+ ourKeyboard->press (KEY_F7);
188
+ else if (key.equals (" F8" ))
189
+ ourKeyboard->press (KEY_F8);
190
+ else if (key.equals (" F9" ))
191
+ ourKeyboard->press (KEY_F9);
192
+ else if (key.equals (" F10" ))
193
+ ourKeyboard->press (KEY_F10);
194
+ else if (key.equals (" F11" ))
195
+ ourKeyboard->press (KEY_F11);
196
+ else if (key.equals (" F12" ))
197
+ ourKeyboard->press (KEY_F12);
198
+ else if (key.equals (" NUM_0" ))
199
+ WriteCharacter (KEYPAD_0);
200
+ else if (key.equals (" NUM_1" ))
201
+ WriteCharacter (KEYPAD_1);
202
+ else if (key.equals (" NUM_2" ))
203
+ WriteCharacter (KEYPAD_2);
204
+ else if (key.equals (" NUM_3" ))
205
+ WriteCharacter (KEYPAD_3);
206
+ else if (key.equals (" NUM_4" ))
207
+ WriteCharacter (KEYPAD_4);
208
+ else if (key.equals (" NUM_5" ))
209
+ WriteCharacter (KEYPAD_5);
210
+ else if (key.equals (" NUM_6" ))
211
+ WriteCharacter (KEYPAD_6);
212
+ else if (key.equals (" NUM_7" ))
213
+ WriteCharacter (KEYPAD_7);
214
+ else if (key.equals (" NUM_8" ))
215
+ WriteCharacter (KEYPAD_8);
216
+ else if (key.equals (" NUM_9" ))
217
+ WriteCharacter (KEYPAD_9);
218
+ else if (key.equals (" NUM_ASTERIX" ))
219
+ WriteCharacter (KEYPAD_ASTERIX);
220
+ else if (key.equals (" NUM_ENTER" ))
221
+ WriteCharacter (KEYPAD_ENTER);
222
+ else if (key.equals (" NUM_Minus" ))
223
+ WriteCharacter (KEYPAD_MINUS);
224
+ else if (key.equals (" NUM_PERIOD" ))
225
+ WriteCharacter (KEYPAD_PERIOD);
226
+ else if (key.equals (" NUM_PLUS" ))
227
+ WriteCharacter (KEYPAD_PLUS);
228
+ else
229
+ DEBUG_PRINT (" ParseKey: failed" );
230
+ }
0 commit comments