2014-02-20 06:28:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stm32f4xx.h>
|
2014-01-05 12:52:37 +00:00
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2014-01-05 12:52:37 +00:00
|
|
|
#include "obj.h"
|
2014-02-20 06:28:36 +00:00
|
|
|
#include "runtime.h"
|
2014-01-05 12:52:37 +00:00
|
|
|
#include "usrsw.h"
|
|
|
|
|
2014-02-20 06:28:36 +00:00
|
|
|
#include "exti.h"
|
|
|
|
#include "gpio.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "build/pins.h"
|
2014-01-05 12:52:37 +00:00
|
|
|
|
2014-02-20 06:28:36 +00: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.
|
2014-02-27 04:24:51 +00:00
|
|
|
//
|
2014-02-20 06:28:36 +00:00
|
|
|
// pyb.switch(None) will remove the callback.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2014-02-27 04:24:51 +00:00
|
|
|
// def switch_pressed():
|
2014-02-20 06:28:36 +00:00
|
|
|
// print("User Switch pressed")
|
|
|
|
//
|
2014-02-27 04:24:51 +00:00
|
|
|
// pyb.switch(switch_pressed)
|
2014-01-05 12:52:37 +00:00
|
|
|
|
2014-02-20 06:28:36 +00:00
|
|
|
static mp_obj_t switch_user_callback_obj;
|
2014-01-05 12:52:37 +00:00
|
|
|
|
2014-02-27 04:24:51 +00:00
|
|
|
static mp_obj_t switch_callback(mp_obj_t line) {
|
2014-02-20 06:28:36 +00:00
|
|
|
if (switch_user_callback_obj != mp_const_none) {
|
|
|
|
rt_call_function_0(switch_user_callback_obj);
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-27 04:24:51 +00:00
|
|
|
static MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback);
|
2014-01-05 12:52:37 +00:00
|
|
|
|
2014-02-20 06:28:36 +00:00
|
|
|
void switch_init(void) {
|
|
|
|
switch_user_callback_obj = mp_const_none;
|
|
|
|
exti_register((mp_obj_t)&USRSW_PIN,
|
|
|
|
MP_OBJ_NEW_SMALL_INT(EXTI_Mode_Interrupt),
|
|
|
|
MP_OBJ_NEW_SMALL_INT(USRSW_EXTI_EDGE),
|
|
|
|
(mp_obj_t)&switch_callback_obj,
|
2014-02-27 04:24:51 +00:00
|
|
|
NULL);
|
2014-02-20 06:28:36 +00:00
|
|
|
pyb_gpio_input((mp_obj_t)&USRSW_PIN, MP_OBJ_NEW_SMALL_INT(USRSW_PUPD));
|
2014-01-05 12:52:37 +00:00
|
|
|
}
|
|
|
|
|
2014-01-05 14:04:55 +00:00
|
|
|
int switch_get(void) {
|
2014-02-20 06:28:36 +00:00
|
|
|
int val = ((USRSW_PIN.gpio->IDR & USRSW_PIN.pin_mask) != 0);
|
|
|
|
return val == USRSW_PRESSED;
|
2014-01-05 12:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings */
|
|
|
|
|
2014-02-20 06:28:36 +00:00
|
|
|
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-01-05 12:52:37 +00:00
|
|
|
}
|
2014-02-20 06:28:36 +00:00
|
|
|
switch_user_callback_obj = args[0];
|
|
|
|
return mp_const_none;
|
2014-01-05 12:52:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 06:28:36 +00:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_switch_obj, 0, 1, pyb_switch);
|