2014-03-15 02:41:28 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stm32f4xx_hal.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime.h"
|
2014-04-18 17:38:09 -04:00
|
|
|
#include "extint.h"
|
2014-03-15 02:41:28 -04:00
|
|
|
#include "pin.h"
|
2014-04-17 13:03:27 -04:00
|
|
|
#include "genhdr/pins.h"
|
2014-04-18 17:48:59 -04:00
|
|
|
#include "usrsw.h"
|
2014-03-15 02:41:28 -04:00
|
|
|
|
|
|
|
// Usage Model:
|
|
|
|
//
|
|
|
|
// pyb.switch() returns True if the user switch is pressed, False otherwise.
|
|
|
|
//
|
|
|
|
// pyb.switch(callback) will register a callback to be called when the user
|
|
|
|
// switch is pressed.
|
|
|
|
//
|
|
|
|
// pyb.switch(None) will remove the callback.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// def switch_pressed():
|
|
|
|
// print("User Switch pressed")
|
|
|
|
//
|
|
|
|
// 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) {
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_call_function_0(switch_user_callback_obj);
|
2014-03-15 02:41:28 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
static MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback);
|
|
|
|
|
2014-03-29 20:00:15 -04:00
|
|
|
// this function inits the switch GPIO so that it can be used
|
|
|
|
void switch_init0(void) {
|
|
|
|
GPIO_InitTypeDef init;
|
2014-03-30 07:30:35 -04:00
|
|
|
init.Pin = MICROPY_HW_USRSW_PIN.pin_mask;
|
2014-03-29 20:00:15 -04:00
|
|
|
init.Mode = GPIO_MODE_INPUT;
|
2014-03-30 07:30:35 -04:00
|
|
|
init.Pull = MICROPY_HW_USRSW_PULL;
|
2014-03-29 20:00:15 -04:00
|
|
|
init.Speed = GPIO_SPEED_FAST;
|
2014-03-30 07:30:35 -04:00
|
|
|
HAL_GPIO_Init(MICROPY_HW_USRSW_PIN.gpio, &init);
|
2014-03-29 20:00:15 -04:00
|
|
|
}
|
|
|
|
|
2014-04-08 17:35:42 -04:00
|
|
|
// this function inits the callback pointer
|
2014-03-15 02:41:28 -04:00
|
|
|
void switch_init(void) {
|
|
|
|
switch_user_callback_obj = mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
int switch_get(void) {
|
2014-03-30 07:30:35 -04:00
|
|
|
int val = ((MICROPY_HW_USRSW_PIN.gpio->IDR & MICROPY_HW_USRSW_PIN.pin_mask) != 0);
|
|
|
|
return val == MICROPY_HW_USRSW_PRESSED;
|
2014-03-15 02:41:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* 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;
|
2014-04-08 17:35:42 -04:00
|
|
|
} 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.
|
2014-04-18 17:38:09 -04:00
|
|
|
extint_register((mp_obj_t)&MICROPY_HW_USRSW_PIN,
|
2014-04-19 20:25:58 -04:00
|
|
|
MICROPY_HW_USRSW_EXTI_MODE,
|
|
|
|
MICROPY_HW_USRSW_PULL,
|
2014-04-18 17:38:09 -04:00
|
|
|
switch_user_callback_obj == mp_const_none ? mp_const_none : (mp_obj_t)&switch_callback_obj,
|
|
|
|
true, NULL);
|
2014-04-08 17:35:42 -04:00
|
|
|
return mp_const_none;
|
2014-03-15 02:41:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_switch_obj, 0, 1, pyb_switch);
|