36
36
#include "shared-bindings/util.h"
37
37
#include "supervisor/shared/translate.h"
38
38
39
- //| .. currentmodule:: ps2io
39
+ //| class Ps2:
40
+ //| """.. currentmodule:: ps2io
40
41
//|
41
- //| :class:`Ps2` -- Communicate with a PS/2 keyboard or mouse
42
- //| =========================================================
42
+ //| :class:`Ps2` -- Communicate with a PS/2 keyboard or mouse
43
+ //| =========================================================
43
44
//|
44
- //| Ps2 implements the PS/2 keyboard/mouse serial protocol, used in
45
- //| legacy devices. It is similar to UART but there are only two
46
- //| lines (Data and Clock). PS/2 devices are 5V, so bidirectional
47
- //| level converters must be used to connect the I/O lines to pins
48
- //| of 3.3V boards.
45
+ //| Ps2 implements the PS/2 keyboard/mouse serial protocol, used in
46
+ //| legacy devices. It is similar to UART but there are only two
47
+ //| lines (Data and Clock). PS/2 devices are 5V, so bidirectional
48
+ //| level converters must be used to connect the I/O lines to pins
49
+ //| of 3.3V boards."""
49
50
//|
50
- //| .. class:: Ps2(data_pin, clock_pin)
51
+ //| def __init__(self, data_pin: microcontroller.Pin, clock_pin: microcontroller.Pin):
52
+ //| """Create a Ps2 object associated with the given pins.
51
53
//|
52
- //| Create a Ps2 object associated with the given pins.
54
+ //| :param ~microcontroller.Pin data_pin: Pin tied to data wire.
55
+ //| :param ~microcontroller.Pin clock_pin: Pin tied to clock wire.
56
+ //| This pin must support interrupts.
53
57
//|
54
- //| :param ~microcontroller.Pin data_pin: Pin tied to data wire.
55
- //| :param ~microcontroller.Pin clock_pin: Pin tied to clock wire.
56
- //| This pin must support interrupts.
58
+ //| Read one byte from PS/2 keyboard and turn on Scroll Lock LED::
57
59
//|
58
- //| Read one byte from PS/2 keyboard and turn on Scroll Lock LED::
60
+ //| import ps2io
61
+ //| import board
59
62
//|
60
- //| import ps2io
61
- //| import board
63
+ //| kbd = ps2io.Ps2(board.D10, board.D11)
62
64
//|
63
- //| kbd = ps2io.Ps2(board.D10, board.D11)
65
+ //| while len(kbd) == 0:
66
+ //| pass
64
67
//|
65
- //| while len(kbd) == 0:
66
- //| pass
67
- //|
68
- //| print(kbd.popleft())
69
- //| print(kbd.sendcmd(0xed))
70
- //| print(kbd.sendcmd(0x01))
68
+ //| print(kbd.popleft())
69
+ //| print(kbd.sendcmd(0xed))
70
+ //| print(kbd.sendcmd(0x01))"""
71
+ //| ...
71
72
//|
72
73
STATIC mp_obj_t ps2io_ps2_make_new (const mp_obj_type_t * type , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
73
74
enum { ARG_datapin , ARG_clkpin };
@@ -89,9 +90,9 @@ STATIC mp_obj_t ps2io_ps2_make_new(const mp_obj_type_t *type, size_t n_args, con
89
90
return MP_OBJ_FROM_PTR (self );
90
91
}
91
92
92
- //| .. method:: deinit()
93
- //|
94
- //| Deinitialises the Ps2 and releases any hardware resources for reuse .
93
+ //| def deinit(self, ) -> Any:
94
+ //| """Deinitialises the Ps2 and releases any hardware resources for reuse."""
95
+ //| .. .
95
96
//|
96
97
STATIC mp_obj_t ps2io_ps2_deinit (mp_obj_t self_in ) {
97
98
ps2io_ps2_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -106,16 +107,16 @@ STATIC void check_for_deinit(ps2io_ps2_obj_t *self) {
106
107
}
107
108
}
108
109
109
- //| .. method:: __enter__()
110
- //|
111
- //| No-op used by Context Managers .
110
+ //| def __enter__(self, ) -> Any:
111
+ //| """No-op used by Context Managers."""
112
+ //| .. .
112
113
//|
113
114
// Provided by context manager helper.
114
115
115
- //| .. method:: __exit__()
116
- //|
117
- //| Automatically deinitializes the hardware when exiting a context. See
118
- //| :ref:`lifetime-and-contextmanagers` for more info .
116
+ //| def __exit__(self, ) -> Any:
117
+ //| """Automatically deinitializes the hardware when exiting a context. See
118
+ //| :ref:`lifetime-and-contextmanagers` for more info."""
119
+ //| .. .
119
120
//|
120
121
STATIC mp_obj_t ps2io_ps2_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
121
122
(void )n_args ;
@@ -124,10 +125,10 @@ STATIC mp_obj_t ps2io_ps2_obj___exit__(size_t n_args, const mp_obj_t *args) {
124
125
}
125
126
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (ps2io_ps2___exit___obj , 4 , 4 , ps2io_ps2_obj___exit__ );
126
127
127
- //| .. method:: popleft()
128
- //|
129
- //| Removes and returns the oldest received byte. When buffer
130
- //| is empty, raises an IndexError exception .
128
+ //| def popleft(self, ) -> Any:
129
+ //| """Removes and returns the oldest received byte. When buffer
130
+ //| is empty, raises an IndexError exception."""
131
+ //| .. .
131
132
//|
132
133
STATIC mp_obj_t ps2io_ps2_obj_popleft (mp_obj_t self_in ) {
133
134
ps2io_ps2_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -141,18 +142,18 @@ STATIC mp_obj_t ps2io_ps2_obj_popleft(mp_obj_t self_in) {
141
142
}
142
143
MP_DEFINE_CONST_FUN_OBJ_1 (ps2io_ps2_popleft_obj , ps2io_ps2_obj_popleft );
143
144
144
- //| .. method:: sendcmd(byte)
145
- //|
146
- //| Sends a command byte to PS/2. Returns the response byte, typically
147
- //| the general ack value (0xFA). Some commands return additional data
148
- //| which is available through :py:func:`popleft()`.
145
+ //| def sendcmd(self, byte: int) -> Any:
146
+ //| """Sends a command byte to PS/2. Returns the response byte, typically
147
+ //| the general ack value (0xFA). Some commands return additional data
148
+ //| which is available through :py:func:`popleft()`.
149
149
//|
150
- //| Raises a RuntimeError in case of failure. The root cause can be found
151
- //| by calling :py:func:`clear_errors()`. It is advisable to call
152
- //| :py:func:`clear_errors()` before :py:func:`sendcmd()` to flush any
153
- //| previous errors.
150
+ //| Raises a RuntimeError in case of failure. The root cause can be found
151
+ //| by calling :py:func:`clear_errors()`. It is advisable to call
152
+ //| :py:func:`clear_errors()` before :py:func:`sendcmd()` to flush any
153
+ //| previous errors.
154
154
//|
155
- //| :param int byte: byte value of the command
155
+ //| :param int byte: byte value of the command"""
156
+ //| ...
156
157
//|
157
158
STATIC mp_obj_t ps2io_ps2_obj_sendcmd (mp_obj_t self_in , mp_obj_t ob ) {
158
159
ps2io_ps2_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -166,35 +167,35 @@ STATIC mp_obj_t ps2io_ps2_obj_sendcmd(mp_obj_t self_in, mp_obj_t ob) {
166
167
}
167
168
MP_DEFINE_CONST_FUN_OBJ_2 (ps2io_ps2_sendcmd_obj , ps2io_ps2_obj_sendcmd );
168
169
169
- //| .. method:: clear_errors()
170
+ //| def clear_errors(self, ) -> Any:
171
+ //| """Returns and clears a bitmap with latest recorded communication errors.
170
172
//|
171
- //| Returns and clears a bitmap with latest recorded communication errors.
173
+ //| Reception errors (arise asynchronously, as data is received):
172
174
//|
173
- //| Reception errors (arise asynchronously, as data is received):
175
+ //| 0x01: start bit not 0
174
176
//|
175
- //| 0x01: start bit not 0
177
+ //| 0x02: timeout
176
178
//|
177
- //| 0x02: timeout
179
+ //| 0x04: parity bit error
178
180
//|
179
- //| 0x04: parity bit error
181
+ //| 0x08: stop bit not 1
180
182
//|
181
- //| 0x08: stop bit not 1
183
+ //| 0x10: buffer overflow, newest data discarded
182
184
//|
183
- //| 0x10: buffer overflow, newest data discarded
185
+ //| Transmission errors (can only arise in the course of sendcmd()):
184
186
//|
185
- //| Transmission errors (can only arise in the course of sendcmd()):
187
+ //| 0x100: clock pin didn't go to LO in time
186
188
//|
187
- //| 0x100 : clock pin didn't go to LO in time
189
+ //| 0x200 : clock pin didn't go to HI in time
188
190
//|
189
- //| 0x200: clock pin didn't go to HI in time
191
+ //| 0x400: data pin didn't ACK
190
192
//|
191
- //| 0x400: data pin didn't ACK
193
+ //| 0x800: clock pin didn't ACK
192
194
//|
193
- //| 0x800: clock pin didn't ACK
195
+ //| 0x1000: device didn't respond to RTS
194
196
//|
195
- //| 0x1000: device didn't respond to RTS
196
- //|
197
- //| 0x2000: device didn't send a response byte in time
197
+ //| 0x2000: device didn't send a response byte in time"""
198
+ //| ...
198
199
//|
199
200
STATIC mp_obj_t ps2io_ps2_obj_clear_errors (mp_obj_t self_in ) {
200
201
ps2io_ps2_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -204,10 +205,10 @@ STATIC mp_obj_t ps2io_ps2_obj_clear_errors(mp_obj_t self_in) {
204
205
}
205
206
MP_DEFINE_CONST_FUN_OBJ_1 (ps2io_ps2_clear_errors_obj , ps2io_ps2_obj_clear_errors );
206
207
207
- //| .. method:: __len__()
208
- //|
209
- //| Returns the number of received bytes in buffer, available
210
- //| to :py:func:`popleft()` .
208
+ //| def __len__(self, ) -> Any:
209
+ //| """Returns the number of received bytes in buffer, available
210
+ //| to :py:func:`popleft()`."""
211
+ //| .. .
211
212
//|
212
213
STATIC mp_obj_t ps2_unary_op (mp_unary_op_t op , mp_obj_t self_in ) {
213
214
ps2io_ps2_obj_t * self = MP_OBJ_TO_PTR (self_in );
0 commit comments