From e84673c0e011a91ec61a19fc596fd8180327a4f6 Mon Sep 17 00:00:00 2001 From: Rahix <rahix@rahix.de> Date: Thu, 6 Jun 2019 20:41:26 +0200 Subject: [PATCH] Add buzzer --- ports/card10/Makefile | 4 ++++ ports/card10/buzzer.c | 44 +++++++++++++++++++++++++++++++++++++ ports/card10/mpconfigport.h | 1 + 3 files changed, 49 insertions(+) create mode 100644 ports/card10/buzzer.c diff --git a/ports/card10/Makefile b/ports/card10/Makefile index 85382c042..58e310379 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 000000000..e2c8fcaec --- /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 3faa728ef..763a4cd67 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 -- GitLab