Commit 6125ba4a authored by Damien George's avatar Damien George
Browse files

stmhal: Make switch a class, to follow suit of all other modules.

Instead of pyb.switch() as a function, it's more consistent (with
respect to all the other modules and peripherals) to have
pyb.Switch() create a switch object.  This then generalises to having
multiple switches.  Call the object to get its state.  Use sw.callback
to set the callback function for when the switch is pressed.
parent af797f47
......@@ -205,7 +205,9 @@ int main(void) {
pendsv_init();
timer_tim3_init();
led_init();
#if MICROPY_HW_HAS_SWITCH
switch_init0();
#endif
int first_soft_reset = true;
......@@ -297,11 +299,6 @@ soft_reset:
pin_init();
extint_init();
#if MICROPY_HW_HAS_SWITCH
// must come after extint_init
switch_init();
#endif
#if MICROPY_HW_HAS_LCD
// LCD init (just creates class, init hardware by calling LCD())
lcd_init();
......
......@@ -295,7 +295,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
#endif
#if MICROPY_HW_HAS_SWITCH
{ MP_OBJ_NEW_QSTR(MP_QSTR_switch), (mp_obj_t)&pyb_switch_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_Switch), (mp_obj_t)&pyb_switch_type },
#endif
#if MICROPY_HW_HAS_SDCARD
......
......@@ -20,8 +20,6 @@ Q(gc)
Q(repl_info)
Q(delay)
Q(udelay)
Q(switch)
Q(SW)
Q(servo)
Q(pwm)
Q(read)
......@@ -79,6 +77,10 @@ Q(off)
Q(toggle)
Q(intensity)
// for Switch class
Q(Switch)
Q(callback)
// for UART class
Q(UART)
Q(baudrate)
......
#include <stdio.h>
#include <stm32f4xx_hal.h>
#include "stm32f4xx_hal.h"
#include "misc.h"
#include "mpconfig.h"
......@@ -27,16 +28,6 @@
//
// pyb.switch(switch_pressed)
static mp_obj_t switch_user_callback_obj;
static mp_obj_t switch_callback(mp_obj_t line) {
if (switch_user_callback_obj != mp_const_none) {
mp_call_function_0(switch_user_callback_obj);
}
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback);
// this function inits the switch GPIO so that it can be used
void switch_init0(void) {
GPIO_InitTypeDef init;
......@@ -47,34 +38,80 @@ void switch_init0(void) {
HAL_GPIO_Init(MICROPY_HW_USRSW_PIN.gpio, &init);
}
// this function inits the callback pointer
void switch_init(void) {
switch_user_callback_obj = mp_const_none;
}
int switch_get(void) {
int val = ((MICROPY_HW_USRSW_PIN.gpio->IDR & MICROPY_HW_USRSW_PIN.pin_mask) != 0);
return val == MICROPY_HW_USRSW_PRESSED;
}
/******************************************************************************/
/* Micro Python bindings */
static mp_obj_t pyb_switch(uint n_args, mp_obj_t *args) {
if (n_args == 0) {
return switch_get() ? mp_const_true : mp_const_false;
} else {
switch_user_callback_obj = args[0];
// Init the EXTI each time this function is called, since the EXTI
// may have been disabled by an exception in the interrupt, or the
// user disabling the line explicitly.
extint_register((mp_obj_t)&MICROPY_HW_USRSW_PIN,
MICROPY_HW_USRSW_EXTI_MODE,
MICROPY_HW_USRSW_PULL,
switch_user_callback_obj == mp_const_none ? mp_const_none : (mp_obj_t)&switch_callback_obj,
true, NULL);
return mp_const_none;
// Micro Python bindings
typedef struct _pyb_switch_obj_t {
mp_obj_base_t base;
mp_obj_t callback;
} pyb_switch_obj_t;
STATIC pyb_switch_obj_t pyb_switch_obj;
void pyb_switch_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
print(env, "Switch()");
}
STATIC mp_obj_t pyb_switch_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// check arguments
mp_arg_check_num(n_args, n_kw, 0, 0, false);
// init the switch object
pyb_switch_obj.base.type = &pyb_switch_type;
// No need to clear the callback member: if it's already been set and registered
// with extint then we don't want to reset that behaviour. If it hasn't been set,
// then no extint will be called until it is set via the callback method.
// return static switch object
return (mp_obj_t)&pyb_switch_obj;
}
mp_obj_t pyb_switch_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// get switch state
mp_arg_check_num(n_args, n_kw, 0, 0, false);
return switch_get() ? mp_const_true : mp_const_false;
}
STATIC mp_obj_t switch_callback(mp_obj_t line) {
if (pyb_switch_obj.callback != mp_const_none) {
mp_call_function_0(pyb_switch_obj.callback);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback);
mp_obj_t pyb_switch_callback(mp_obj_t self_in, mp_obj_t callback) {
pyb_switch_obj_t *self = self_in;
self->callback = callback;
// Init the EXTI each time this function is called, since the EXTI
// may have been disabled by an exception in the interrupt, or the
// user disabling the line explicitly.
extint_register((mp_obj_t)&MICROPY_HW_USRSW_PIN,
MICROPY_HW_USRSW_EXTI_MODE,
MICROPY_HW_USRSW_PULL,
callback == mp_const_none ? mp_const_none : (mp_obj_t)&switch_callback_obj,
true, NULL);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_switch_callback_obj, pyb_switch_callback);
STATIC const mp_map_elem_t pyb_switch_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pyb_switch_callback_obj },
};
STATIC MP_DEFINE_CONST_DICT(pyb_switch_locals_dict, pyb_switch_locals_dict_table);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_switch_obj, 0, 1, pyb_switch);
const mp_obj_type_t pyb_switch_type = {
{ &mp_type_type },
.name = MP_QSTR_Switch,
.print = pyb_switch_print,
.make_new = pyb_switch_make_new,
.call = pyb_switch_call,
.locals_dict = (mp_obj_t)&pyb_switch_locals_dict,
};
void switch_init0(void);
void switch_init(void);
int switch_get(void);
MP_DECLARE_CONST_FUN_OBJ(pyb_switch_obj);
extern const mp_obj_type_t pyb_switch_type;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment