2019-05-15 22:29:34 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
|
|
|
* Copyright (c) 2019 Elvis Pfutzenreuter <epxx@epxx.co>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/runtime0.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
#include "shared-bindings/ps2io/Ps2.h"
|
|
|
|
#include "shared-bindings/util.h"
|
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| class Ps2:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Communicate with a PS/2 keyboard or mouse
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| Ps2 implements the PS/2 keyboard/mouse serial protocol, used in
|
|
|
|
//| legacy devices. It is similar to UART but there are only two
|
|
|
|
//| lines (Data and Clock). PS/2 devices are 5V, so bidirectional
|
|
|
|
//| level converters must be used to connect the I/O lines to pins
|
|
|
|
//| of 3.3V boards."""
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| def __init__(self, data_pin: microcontroller.Pin, clock_pin: microcontroller.Pin):
|
|
|
|
//| """Create a Ps2 object associated with the given pins.
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| :param ~microcontroller.Pin data_pin: Pin tied to data wire.
|
|
|
|
//| :param ~microcontroller.Pin clock_pin: Pin tied to clock wire.
|
|
|
|
//| This pin must support interrupts.
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| Read one byte from PS/2 keyboard and turn on Scroll Lock LED::
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| import ps2io
|
|
|
|
//| import board
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| kbd = ps2io.Ps2(board.D10, board.D11)
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| while len(kbd) == 0:
|
|
|
|
//| pass
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| print(kbd.popleft())
|
|
|
|
//| print(kbd.sendcmd(0xed))
|
|
|
|
//| print(kbd.sendcmd(0x01))"""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
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) {
|
|
|
|
enum { ARG_datapin, ARG_clkpin };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_datapin, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_clkpin, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-02-28 23:32:24 -05:00
|
|
|
|
2020-03-05 16:35:31 -05:00
|
|
|
const mcu_pin_obj_t* clkpin = validate_obj_is_free_pin(args[ARG_clkpin].u_obj);
|
|
|
|
const mcu_pin_obj_t* datapin = validate_obj_is_free_pin(args[ARG_datapin].u_obj);
|
2019-05-15 22:29:34 -04:00
|
|
|
|
|
|
|
ps2io_ps2_obj_t *self = m_new_obj(ps2io_ps2_obj_t);
|
|
|
|
self->base.type = &ps2io_ps2_type;
|
|
|
|
|
|
|
|
common_hal_ps2io_ps2_construct(self, datapin, clkpin);
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def deinit(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Deinitialises the Ps2 and releases any hardware resources for reuse."""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t ps2io_ps2_deinit(mp_obj_t self_in) {
|
|
|
|
ps2io_ps2_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_ps2io_ps2_deinit(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ps2io_ps2_deinit_obj, ps2io_ps2_deinit);
|
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(ps2io_ps2_obj_t *self) {
|
|
|
|
if (common_hal_ps2io_ps2_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __enter__(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """No-op used by Context Managers."""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
// Provided by context manager helper.
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __exit__(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Automatically deinitializes the hardware when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t ps2io_ps2_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args;
|
|
|
|
common_hal_ps2io_ps2_deinit(args[0]);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ps2io_ps2___exit___obj, 4, 4, ps2io_ps2_obj___exit__);
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def popleft(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Removes and returns the oldest received byte. When buffer
|
|
|
|
//| is empty, raises an IndexError exception."""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t ps2io_ps2_obj_popleft(mp_obj_t self_in) {
|
|
|
|
ps2io_ps2_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2019-05-15 22:29:34 -04:00
|
|
|
|
|
|
|
int b = common_hal_ps2io_ps2_popleft(self);
|
|
|
|
if (b < 0) {
|
|
|
|
mp_raise_IndexError(translate("Pop from an empty Ps2 buffer"));
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(b);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(ps2io_ps2_popleft_obj, ps2io_ps2_obj_popleft);
|
|
|
|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| def sendcmd(self, byte: int) -> Any:
|
|
|
|
//| """Sends a command byte to PS/2. Returns the response byte, typically
|
|
|
|
//| the general ack value (0xFA). Some commands return additional data
|
|
|
|
//| which is available through :py:func:`popleft()`.
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| Raises a RuntimeError in case of failure. The root cause can be found
|
|
|
|
//| by calling :py:func:`clear_errors()`. It is advisable to call
|
|
|
|
//| :py:func:`clear_errors()` before :py:func:`sendcmd()` to flush any
|
|
|
|
//| previous errors.
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| :param int byte: byte value of the command"""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t ps2io_ps2_obj_sendcmd(mp_obj_t self_in, mp_obj_t ob) {
|
|
|
|
ps2io_ps2_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2019-05-15 22:29:34 -04:00
|
|
|
mp_int_t cmd = mp_obj_get_int(ob) & 0xff;
|
|
|
|
int resp = common_hal_ps2io_ps2_sendcmd(self, cmd);
|
|
|
|
if (resp < 0) {
|
|
|
|
mp_raise_RuntimeError(translate("Failed sending command."));
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(resp);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(ps2io_ps2_sendcmd_obj, ps2io_ps2_obj_sendcmd);
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def clear_errors(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Returns and clears a bitmap with latest recorded communication errors.
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| Reception errors (arise asynchronously, as data is received):
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x01: start bit not 0
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x02: timeout
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x04: parity bit error
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x08: stop bit not 1
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x10: buffer overflow, newest data discarded
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| Transmission errors (can only arise in the course of sendcmd()):
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x100: clock pin didn't go to LO in time
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x200: clock pin didn't go to HI in time
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x400: data pin didn't ACK
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x800: clock pin didn't ACK
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x1000: device didn't respond to RTS
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
2020-05-11 10:48:11 -04:00
|
|
|
//| 0x2000: device didn't send a response byte in time"""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t ps2io_ps2_obj_clear_errors(mp_obj_t self_in) {
|
|
|
|
ps2io_ps2_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2019-05-15 22:29:34 -04:00
|
|
|
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_ps2io_ps2_clear_errors(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(ps2io_ps2_clear_errors_obj, ps2io_ps2_obj_clear_errors);
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __len__(self) -> Any:
|
2020-05-11 10:48:11 -04:00
|
|
|
//| """Returns the number of received bytes in buffer, available
|
|
|
|
//| to :py:func:`popleft()`."""
|
|
|
|
//| ...
|
2019-05-15 22:29:34 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t ps2_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|
|
|
ps2io_ps2_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2019-05-15 22:29:34 -04:00
|
|
|
uint16_t len = common_hal_ps2io_ps2_get_len(self);
|
|
|
|
switch (op) {
|
|
|
|
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
|
|
|
|
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
|
|
|
|
default: return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t ps2io_ps2_locals_dict_table[] = {
|
|
|
|
// Methods
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&ps2io_ps2_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&ps2io_ps2___exit___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&ps2io_ps2_popleft_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sendcmd), MP_ROM_PTR(&ps2io_ps2_sendcmd_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_clear_errors), MP_ROM_PTR(&ps2io_ps2_clear_errors_obj) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(ps2io_ps2_locals_dict, ps2io_ps2_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t ps2io_ps2_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Ps2,
|
|
|
|
.make_new = ps2io_ps2_make_new,
|
|
|
|
.unary_op = ps2_unary_op,
|
|
|
|
.locals_dict = (mp_obj_dict_t*)&ps2io_ps2_locals_dict,
|
|
|
|
};
|