diff --git a/ports/card10/Makefile b/ports/card10/Makefile index 85382c042b73e4c0908461636ce01543ab879728..58e31037960bd8388638fe05f5820ddbd3aff140 100644 --- a/ports/card10/Makefile +++ b/ports/card10/Makefile @@ -44,6 +44,7 @@ LIBS = SRC_C = \ main.c \ uart_core.c \ + buzzer.c \ lib/utils/printf.c \ lib/utils/stdout_helpers.c \ lib/utils/pyexec.c \ @@ -52,6 +53,9 @@ SRC_C = \ OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +SRC_QSTR += buzzer.c +USER_C_MODULES += buzzer.c + # SDK compilation hack # ----------------------------------------------------------------------------- diff --git a/ports/card10/buzzer.c b/ports/card10/buzzer.c new file mode 100644 index 0000000000000000000000000000000000000000..e2c8fcaecbcaf0570121921b86c41c6dd6aea3e7 --- /dev/null +++ b/ports/card10/buzzer.c @@ -0,0 +1,44 @@ +#include "py/obj.h" +#include "py/runtime.h" +#include "py/builtin.h" +#include <stdio.h> +#include "gpio.h" + +static const gpio_cfg_t motor_pin = { + PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE +}; + +STATIC mp_obj_t buzzer_set(mp_obj_t state_obj) +{ + if (state_obj == mp_const_true) { + printf("Buzzer ON!\n"); + GPIO_OutSet(&motor_pin); + } else if (state_obj == mp_const_false){ + printf("Buzzer OFF!\n"); + GPIO_OutClr(&motor_pin); + } else { + mp_raise_TypeError("expected bool"); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(buzzer_set_obj, buzzer_set); + +// Define all properties of the example module. +// Table entries are key/value pairs of the attribute name (a string) +// and the MicroPython object reference. +// All identifiers and strings are written as MP_QSTR_xxx and will be +// optimized to word-sized integers by the build system (interned strings). +STATIC const mp_rom_map_elem_t buzzer_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_buzzer) }, + { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&buzzer_set_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(buzzer_module_globals, buzzer_module_globals_table); + +// Define module object. +const mp_obj_module_t buzzer_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&buzzer_module_globals, +}; + +// Register the module to make it available in Python +MP_REGISTER_MODULE(MP_QSTR_buzzer, buzzer_module, MODULE_BUZZER_ENABLED); diff --git a/ports/card10/mpconfigport.h b/ports/card10/mpconfigport.h index 3faa728eff91ba0f36d0d18a8aab6bab885bd062..763a4cd671258dc54890d6fd828000834f0d1401 100644 --- a/ports/card10/mpconfigport.h +++ b/ports/card10/mpconfigport.h @@ -59,6 +59,7 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PY_UTIME_MP_HAL (1) +#define MODULE_BUZZER_ENABLED (1) // type definitions for the specific machine