2021-06-14 16:00:15 -04:00
/*
* This file is part of the MicroPython project , http : //micropython.org/
*
* The MIT License ( MIT )
*
* Copyright ( c ) 2021 Dan Halbert for Adafruit Industries
*
* 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 "py/obj.h"
# include "py/objproperty.h"
# include "py/runtime.h"
# include "shared-bindings/keypad/Event.h"
2021-09-16 22:08:05 -04:00
# include "shared-bindings/supervisor/__init__.h"
2021-06-14 16:00:15 -04:00
//| class Event:
//| """A key transition event."""
2021-09-21 08:41:25 -04:00
//| def __init__(self, key_number: int=0, pressed: bool=True, timestamp:Optional[int]=None) -> None:
2021-06-14 16:00:15 -04:00
//| """Create a key transition event, which reports a key-pressed or key-released transition.
//|
2022-04-07 18:28:59 -04:00
//| :param int key_number: The key number.
2021-06-14 16:00:15 -04:00
//| :param bool pressed: ``True`` if the key was pressed; ``False`` if it was released.
2021-09-16 22:08:05 -04:00
//| :param int timestamp: The time in milliseconds that the keypress occurred in the `supervisor.ticks_ms` time system. If specified as None, the current value of `supervisor.ticks_ms` is used.
2021-06-14 16:00:15 -04:00
//| """
//| ...
//|
2021-10-15 14:43:12 -04:00
STATIC mp_obj_t keypad_event_make_new ( const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
2021-06-14 16:00:15 -04:00
keypad_event_obj_t * self = m_new_obj ( keypad_event_obj_t ) ;
self - > base . type = & keypad_event_type ;
2021-09-16 22:08:05 -04:00
enum { ARG_key_number , ARG_pressed , ARG_timestamp } ;
2021-06-14 16:00:15 -04:00
static const mp_arg_t allowed_args [ ] = {
2021-06-23 09:18:40 -04:00
{ MP_QSTR_key_number , MP_ARG_INT , { . u_int = 0 } } ,
2021-06-21 08:18:06 -04:00
{ MP_QSTR_pressed , MP_ARG_BOOL , { . u_bool = true } } ,
2021-09-16 22:08:05 -04:00
{ MP_QSTR_timestamp , MP_ARG_OBJ , { . u_obj = mp_const_none } } ,
2021-06-14 16:00:15 -04:00
} ;
mp_arg_val_t args [ MP_ARRAY_SIZE ( allowed_args ) ] ;
2021-10-15 14:43:12 -04:00
mp_arg_parse_all_kw_array ( n_args , n_kw , all_args , MP_ARRAY_SIZE ( allowed_args ) , allowed_args , args ) ;
2021-06-14 16:00:15 -04:00
2021-06-23 09:18:40 -04:00
const mp_uint_t key_number =
( mp_uint_t ) mp_arg_validate_int_min ( args [ ARG_key_number ] . u_int , 0 , MP_QSTR_key_number ) ;
2021-06-14 16:00:15 -04:00
2021-09-16 22:08:05 -04:00
mp_obj_t timestamp = args [ ARG_timestamp ] . u_obj ;
if ( timestamp = = mp_const_none ) {
timestamp = supervisor_ticks_ms ( ) ;
}
( void ) mp_obj_get_int_truncated ( timestamp ) ; // ensure that timesamp is an integer
common_hal_keypad_event_construct ( self , key_number , args [ ARG_pressed ] . u_bool , timestamp ) ;
2021-06-14 16:00:15 -04:00
return MP_OBJ_FROM_PTR ( self ) ;
}
2021-06-23 09:18:40 -04:00
//| key_number: int
2021-06-14 16:00:15 -04:00
//| """The key number."""
//|
2021-06-23 09:18:40 -04:00
STATIC mp_obj_t keypad_event_get_key_number ( mp_obj_t self_in ) {
2021-06-14 16:00:15 -04:00
keypad_event_obj_t * self = MP_OBJ_TO_PTR ( self_in ) ;
2021-06-23 09:18:40 -04:00
return MP_OBJ_NEW_SMALL_INT ( common_hal_keypad_event_get_key_number ( self ) ) ;
2021-06-14 16:00:15 -04:00
}
2021-06-23 09:18:40 -04:00
MP_DEFINE_CONST_FUN_OBJ_1 ( keypad_event_get_key_number_obj , keypad_event_get_key_number ) ;
2021-06-14 16:00:15 -04:00
2021-06-23 09:18:40 -04:00
const mp_obj_property_t keypad_event_key_number_obj = {
2021-06-14 16:00:15 -04:00
. base . type = & mp_type_property ,
2021-06-23 09:18:40 -04:00
. proxy = { ( mp_obj_t ) & keypad_event_get_key_number_obj ,
2021-06-14 16:00:15 -04:00
MP_ROM_NONE ,
MP_ROM_NONE } ,
} ;
//| pressed: bool
2021-06-16 15:27:59 -04:00
//| """``True`` if the event represents a key down (pressed) transition.
2021-06-15 11:15:09 -04:00
//| The opposite of `released`.
//| """
2021-06-14 16:00:15 -04:00
//|
2021-06-16 15:27:59 -04:00
STATIC mp_obj_t keypad_event_get_pressed ( mp_obj_t self_in ) {
2021-06-14 16:00:15 -04:00
keypad_event_obj_t * self = MP_OBJ_TO_PTR ( self_in ) ;
return mp_obj_new_bool ( common_hal_keypad_event_get_pressed ( self ) ) ;
}
2021-06-16 15:27:59 -04:00
MP_DEFINE_CONST_FUN_OBJ_1 ( keypad_event_get_pressed_obj , keypad_event_get_pressed ) ;
2021-06-14 16:00:15 -04:00
const mp_obj_property_t keypad_event_pressed_obj = {
. base . type = & mp_type_property ,
. proxy = { ( mp_obj_t ) & keypad_event_get_pressed_obj ,
MP_ROM_NONE ,
MP_ROM_NONE } ,
} ;
//| released: bool
2021-06-16 15:27:59 -04:00
//| """``True`` if the event represents a key up (released) transition.
2021-06-15 11:15:09 -04:00
//| The opposite of `pressed`.
//| """
2021-06-14 16:00:15 -04:00
//|
2021-06-16 15:27:59 -04:00
STATIC mp_obj_t keypad_event_get_released ( mp_obj_t self_in ) {
2021-06-14 16:00:15 -04:00
keypad_event_obj_t * self = MP_OBJ_TO_PTR ( self_in ) ;
return mp_obj_new_bool ( common_hal_keypad_event_get_released ( self ) ) ;
}
2021-06-16 15:27:59 -04:00
MP_DEFINE_CONST_FUN_OBJ_1 ( keypad_event_get_released_obj , keypad_event_get_released ) ;
2021-06-14 16:00:15 -04:00
const mp_obj_property_t keypad_event_released_obj = {
. base . type = & mp_type_property ,
. proxy = { ( mp_obj_t ) & keypad_event_get_released_obj ,
MP_ROM_NONE ,
MP_ROM_NONE } ,
} ;
2021-09-16 22:08:05 -04:00
//| timestamp: int
2022-04-07 18:28:59 -04:00
//| """The timestamp."""
2021-09-16 22:08:05 -04:00
//|
STATIC mp_obj_t keypad_event_get_timestamp ( mp_obj_t self_in ) {
keypad_event_obj_t * self = MP_OBJ_TO_PTR ( self_in ) ;
return common_hal_keypad_event_get_timestamp ( self ) ;
}
MP_DEFINE_CONST_FUN_OBJ_1 ( keypad_event_get_timestamp_obj , keypad_event_get_timestamp ) ;
const mp_obj_property_t keypad_event_timestamp_obj = {
. base . type = & mp_type_property ,
. proxy = { ( mp_obj_t ) & keypad_event_get_timestamp_obj ,
MP_ROM_NONE ,
MP_ROM_NONE } ,
} ;
2021-06-15 11:15:09 -04:00
//| def __eq__(self, other: object) -> bool:
2021-06-23 09:18:40 -04:00
//| """Two `Event` objects are equal if their `key_number`
2021-06-15 11:15:09 -04:00
//| and `pressed`/`released` values are equal.
2021-09-16 22:08:05 -04:00
//| Note that this does not compare the event timestamps.
2021-06-15 11:15:09 -04:00
//| """
//| ...
//|
STATIC mp_obj_t keypad_event_binary_op ( mp_binary_op_t op , mp_obj_t lhs_in , mp_obj_t rhs_in ) {
switch ( op ) {
case MP_BINARY_OP_EQUAL :
if ( mp_obj_is_type ( rhs_in , & keypad_event_type ) ) {
keypad_event_obj_t * lhs = MP_OBJ_TO_PTR ( lhs_in ) ;
keypad_event_obj_t * rhs = MP_OBJ_TO_PTR ( rhs_in ) ;
return mp_obj_new_bool (
2021-06-23 09:18:40 -04:00
( common_hal_keypad_event_get_key_number ( lhs ) = =
common_hal_keypad_event_get_key_number ( rhs ) ) & &
2021-06-15 11:15:09 -04:00
( common_hal_keypad_event_get_pressed ( lhs ) = =
2021-06-23 09:18:40 -04:00
common_hal_keypad_event_get_pressed ( rhs ) )
) ;
2021-06-15 11:15:09 -04:00
} else {
return mp_const_false ;
}
default :
return MP_OBJ_NULL ; // op not supported
}
}
//| def __hash__(self) -> int:
2021-09-16 22:08:05 -04:00
//| """Returns a hash for the `Event`, so it can be used in dictionaries, etc..
//|
//| Note that as events with different timestamps compare equal, they also hash to the same value."""
2021-06-15 11:15:09 -04:00
//| ...
//|
STATIC mp_obj_t keypad_event_unary_op ( mp_unary_op_t op , mp_obj_t self_in ) {
2021-10-19 19:04:10 -04:00
keypad_event_obj_t * self = MP_OBJ_TO_PTR ( self_in ) ;
2021-06-15 11:15:09 -04:00
switch ( op ) {
case MP_UNARY_OP_HASH : {
2021-06-23 09:18:40 -04:00
const mp_int_t key_number = common_hal_keypad_event_get_key_number ( self ) ;
2021-06-15 11:15:09 -04:00
const bool pressed = common_hal_keypad_event_get_pressed ( self ) ;
2021-06-23 09:18:40 -04:00
return MP_OBJ_NEW_SMALL_INT ( ( pressed < < 15 ) + key_number ) ;
2021-06-15 11:15:09 -04:00
}
default :
return MP_OBJ_NULL ; // op not supported
}
}
2021-06-14 16:00:15 -04:00
2021-06-14 22:59:17 -04:00
STATIC void keypad_event_print ( const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
keypad_event_obj_t * self = MP_OBJ_TO_PTR ( self_in ) ;
2021-06-23 09:18:40 -04:00
mp_printf ( print , " <Event: key_number %d %s> " ,
common_hal_keypad_event_get_key_number ( self ) ,
2021-06-14 22:59:17 -04:00
common_hal_keypad_event_get_pressed ( self ) ? " pressed " : " released " ) ;
}
2021-06-15 11:15:09 -04:00
STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table [ ] = {
// Properties
2021-06-23 09:18:40 -04:00
{ MP_ROM_QSTR ( MP_QSTR_key_number ) , MP_ROM_PTR ( & keypad_event_key_number_obj ) } ,
{ MP_ROM_QSTR ( MP_QSTR_pressed ) , MP_ROM_PTR ( & keypad_event_pressed_obj ) } ,
{ MP_ROM_QSTR ( MP_QSTR_released ) , MP_ROM_PTR ( & keypad_event_released_obj ) } ,
2021-09-16 22:08:05 -04:00
{ MP_ROM_QSTR ( MP_QSTR_timestamp ) , MP_ROM_PTR ( & keypad_event_timestamp_obj ) } ,
2021-06-15 11:15:09 -04:00
} ;
STATIC MP_DEFINE_CONST_DICT ( keypad_event_locals_dict , keypad_event_locals_dict_table ) ;
2021-06-14 16:00:15 -04:00
const mp_obj_type_t keypad_event_type = {
{ & mp_type_type } ,
2021-07-09 15:59:37 -04:00
. flags = MP_TYPE_FLAG_EXTENDED ,
2021-06-14 22:59:17 -04:00
. name = MP_QSTR_Event ,
2021-06-14 16:00:15 -04:00
. make_new = keypad_event_make_new ,
2021-06-14 22:59:17 -04:00
. print = keypad_event_print ,
2021-06-14 16:00:15 -04:00
. locals_dict = ( mp_obj_dict_t * ) & keypad_event_locals_dict ,
2021-07-12 07:57:59 -04:00
MP_TYPE_EXTENDED_FIELDS (
2021-07-06 11:57:35 -04:00
. unary_op = keypad_event_unary_op ,
. binary_op = keypad_event_binary_op ,
) ,
2021-06-14 16:00:15 -04:00
} ;