FabGL
ESP32 Display Controller and Graphics Library
ps2controller.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019-2021 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6 
7 * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 
9 
10 * This library and related software is available under GPL v3.
11 
12  FabGL is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  FabGL is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
29 
30 
38 #include "freertos/FreeRTOS.h"
39 #include "freertos/task.h"
40 #include "freertos/timers.h"
41 
42 #include "fabutils.h"
43 #include "fabglconf.h"
44 
45 
46 namespace fabgl {
47 
48 
52 enum class PS2Preset {
57  MousePort0,
58  MousePort1,
59 };
60 
61 
65 enum class KbdMode {
69 };
70 
71 
72 class Keyboard;
73 class Mouse;
74 
75 
83 
84 public:
85 
86  PS2Controller();
87  ~PS2Controller();
88 
89  // unwanted methods
90  PS2Controller(PS2Controller const&) = delete;
91  void operator=(PS2Controller const&) = delete;
92 
106  static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO = GPIO_UNUSED, gpio_num_t port1_datGPIO = GPIO_UNUSED);
107 
124 
125  static void end();
126 
127  static bool initialized() { return s_initDone; }
128 
136  static bool dataAvailable(int PS2Port);
137 
146  static int getData(int PS2Port, int timeOutMS);
147 
154  static void sendData(uint8_t data, int PS2Port);
155 
163  static void disableRX(int PS2Port);
164 
172  static void enableRX(int PS2Port);
173 
179  static Keyboard * keyboard() { return s_keyboard; }
180 
181  static void setKeyboard(Keyboard * value) { s_keyboard = value; }
182 
188  static Mouse * mouse() { return s_mouse; }
189 
190  static void setMouse(Mouse * value) { s_mouse = value; }
191 
197  static PS2Controller * instance() { return s_instance; }
198 
199  static bool parityError(int PS2Port) { return s_parityError[PS2Port]; }
200 
201  static bool syncError(int PS2Port) { return s_syncError[PS2Port]; }
202 
203  static bool CLKTimeOutError(int PS2Port) { return s_CLKTimeOutError[PS2Port]; }
204 
213  static bool lock(int PS2Port, int timeOutMS);
214 
220  static void unlock(int PS2Port);
221 
222 
223 private:
224 
225 
226  static void IRAM_ATTR ULPWakeISR(void * arg);
227 
228  static PS2Controller * s_instance;
229 
230  // Keyboard and Mouse instances can be created by PS2Controller in one of the begin() calls, or can be
231  // set using setKeyboard() and setMouse() calls.
232  static Keyboard * s_keyboard;
233  static Mouse * s_mouse;
234 
235  static bool s_keyboardAllocated;
236  static bool s_mouseAllocated;
237 
238  static bool s_portEnabled[2];
239 
240  static intr_handle_t s_ULPWakeISRHandle;
241 
242  // true if last call to getData() had a parity, sync error (start or stop missing bits) or CLK timeout
243  static bool s_parityError[2];
244  static bool s_syncError[2];
245  static bool s_CLKTimeOutError[2];
246 
247  // one word queue (contains just the last received word)
248  static QueueHandle_t s_dataIn[2];
249 
250  static SemaphoreHandle_t s_portLock[2];
251 
252  static bool s_initDone;
253 
254 };
255 
256 
257 struct PS2PortAutoDisableRX {
258  PS2PortAutoDisableRX(int PS2Port) : port(PS2Port) {
260  }
261  ~PS2PortAutoDisableRX() {
263  }
264 private:
265  int port;
266 };
267 
268 
269 
270 
271 } // end of namespace
272 
273 
274 
275 
276 
277 
uint8_t const * data
static bool lock(int PS2Port, int timeOutMS)
Gets exclusive access to the specified PS/2 port.
static Mouse * mouse()
Returns the instance of Mouse object automatically created by PS2Controller.
The PS2 Keyboard controller class.
Definition: keyboard.h:77
static bool dataAvailable(int PS2Port)
Determines if one byte has been received from the specified port.
static void sendData(uint8_t data, int PS2Port)
Sends a command to the device.
static int getData(int PS2Port, int timeOutMS)
Gets a scancode from the queue.
static void unlock(int PS2Port)
Releases port from exclusive access.
This file contains some utility classes and functions.
static PS2Controller * instance()
Returns the singleton instance of PS2Controller class.
Definition: canvas.cpp:36
KbdMode
This enum defines how handle keyboard virtual keys.
Definition: ps2controller.h:65
This file contains FabGL library configuration settings, like number of supported colors...
static Keyboard * keyboard()
Returns the instance of Keyboard object automatically created by PS2Controller.
The PS2 device controller class.
Definition: ps2controller.h:82
static void disableRX(int PS2Port)
Disables inputs from PS/2 port driving the CLK line Low.
static void enableRX(int PS2Port)
Enables inputs from PS/2 port releasing CLK line.
PS2Preset
This enum defines what is connected to PS/2 ports.
Definition: ps2controller.h:52
The PS2 Mouse controller class.
Definition: mouse.h:111
static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO=GPIO_UNUSED, gpio_num_t port1_datGPIO=GPIO_UNUSED)
Initializes PS2 device controller.