From d8137178bb3d0e755278703f66efe33cf07398eb Mon Sep 17 00:00:00 2001 From: danicampora Date: Wed, 14 Oct 2015 09:46:18 +0200 Subject: [PATCH] cc3200: Create wipy module, remove HeartBeat class. The heartbeat is now controllable via a single function within the wipy module. --- cc3200/application.mk | 1 + cc3200/misc/mperror.c | 61 +++++++++------------------------------ cc3200/misc/mperror.h | 2 ++ cc3200/mods/modmachine.c | 2 -- cc3200/mods/modwipy.c | 32 ++++++++++++++++++++ cc3200/mpconfigport.h | 2 ++ cc3200/qstrdefsport.h | 9 +++--- tests/wipy/modwipy.py | 21 ++++++++++++++ tests/wipy/modwipy.py.exp | 4 +++ 9 files changed, 79 insertions(+), 55 deletions(-) create mode 100644 cc3200/mods/modwipy.c create mode 100644 tests/wipy/modwipy.py create mode 100644 tests/wipy/modwipy.py.exp diff --git a/cc3200/application.mk b/cc3200/application.mk index 3ce539b77..9d1db6134 100644 --- a/cc3200/application.mk +++ b/cc3200/application.mk @@ -92,6 +92,7 @@ APP_MODS_SRC_C = $(addprefix mods/,\ modusocket.c \ modussl.c \ modutime.c \ + modwipy.c \ modwlan.c \ pybadc.c \ pybpin.c \ diff --git a/cc3200/misc/mperror.c b/cc3200/misc/mperror.c index 845a2a814..624a9d91d 100644 --- a/cc3200/misc/mperror.c +++ b/cc3200/misc/mperror.c @@ -149,17 +149,13 @@ void mperror_heartbeat_switch_off (void) { void mperror_heartbeat_signal (void) { if (mperror_heart_beat.do_disable) { mperror_heart_beat.do_disable = false; - mperror_heartbeat_switch_off(); - mperror_heart_beat.enabled = false; - } - else if (mperror_heart_beat.enabled) { + } else if (mperror_heart_beat.enabled) { if (!mperror_heart_beat.beating) { if ((mperror_heart_beat.on_time = HAL_GetTick()) - mperror_heart_beat.off_time > MPERROR_HEARTBEAT_OFF_MS) { MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, MICROPY_SYS_LED_PORT_PIN); mperror_heart_beat.beating = true; } - } - else { + } else { if ((mperror_heart_beat.off_time = HAL_GetTick()) - mperror_heart_beat.on_time > MPERROR_HEARTBEAT_ON_MS) { MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0); mperror_heart_beat.beating = false; @@ -199,48 +195,17 @@ void nlr_jump_fail(void *val) { #endif } -#ifndef BOOTLOADER -/******************************************************************************/ -// Micro Python bindings - -/// \classmethod \constructor() -/// -/// Return the heart beat object -STATIC mp_obj_t pyb_heartbeat_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - // check arguments - mp_arg_check_num(n_args, n_kw, 0, 0, false); - - // return constant object - return (mp_obj_t)&pyb_heartbeat_obj; +void mperror_enable_heartbeat (bool enable) { + if (enable) { + mperror_heart_beat.enabled = true; + mperror_heart_beat.do_disable = false; + mperror_heartbeat_switch_off(); + } else { + mperror_heart_beat.do_disable = true; + mperror_heart_beat.enabled = false; + } } -/// \function enable() -/// Enables the heartbeat signal -STATIC mp_obj_t pyb_enable_heartbeat(mp_obj_t self) { - mperror_heart_beat.enabled = true; - return mp_const_none; +bool mperror_is_heartbeat_enabled (void) { + return mperror_heart_beat.enabled; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_enable_heartbeat_obj, pyb_enable_heartbeat); - -/// \function disable() -/// Disables the heartbeat signal -STATIC mp_obj_t pyb_disable_heartbeat(mp_obj_t self) { - mperror_heart_beat.do_disable = true; - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_disable_heartbeat_obj, pyb_disable_heartbeat); - -STATIC const mp_map_elem_t pyb_heartbeat_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&pyb_enable_heartbeat_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&pyb_disable_heartbeat_obj }, -}; -STATIC MP_DEFINE_CONST_DICT(pyb_heartbeat_locals_dict, pyb_heartbeat_locals_dict_table); - -const mp_obj_type_t pyb_heartbeat_type = { - { &mp_type_type }, - .name = MP_QSTR_HeartBeat, - .make_new = pyb_heartbeat_make_new, - .locals_dict = (mp_obj_t)&pyb_heartbeat_locals_dict, -}; - -#endif diff --git a/cc3200/misc/mperror.h b/cc3200/misc/mperror.h index 70a69c528..44c77909c 100644 --- a/cc3200/misc/mperror.h +++ b/cc3200/misc/mperror.h @@ -40,5 +40,7 @@ void mperror_deinit_sfe_pin (void); void mperror_signal_error (void); void mperror_heartbeat_switch_off (void); void mperror_heartbeat_signal (void); +void mperror_enable_heartbeat (bool enable); +bool mperror_is_heartbeat_enabled (void); #endif // MPERROR_H_ diff --git a/cc3200/mods/modmachine.c b/cc3200/mods/modmachine.c index ddf80e9ee..2508c661c 100644 --- a/cc3200/mods/modmachine.c +++ b/cc3200/mods/modmachine.c @@ -61,7 +61,6 @@ #include "pybtimer.h" #include "utils.h" #include "gccollect.h" -#include "mperror.h" #ifdef DEBUG @@ -194,7 +193,6 @@ STATIC const mp_map_elem_t machine_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_WDT), (mp_obj_t)&pyb_wdt_type }, - { MP_OBJ_NEW_QSTR(MP_QSTR_HeartBeat), (mp_obj_t)&pyb_heartbeat_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sd_type }, // class constants diff --git a/cc3200/mods/modwipy.c b/cc3200/mods/modwipy.c new file mode 100644 index 000000000..08c3f0029 --- /dev/null +++ b/cc3200/mods/modwipy.c @@ -0,0 +1,32 @@ +#include "py/mpconfig.h" +#include MICROPY_HAL_H +#include "py/obj.h" +#include "py/runtime.h" +#include "mperror.h" + + +/******************************************************************************/ +// Micro Python bindings + +STATIC mp_obj_t mod_wipy_heartbeat (mp_uint_t n_args, const mp_obj_t *args) { + if (n_args) { + mperror_enable_heartbeat (mp_obj_is_true(args[0])); + return mp_const_none; + } else { + return mp_obj_new_bool(mperror_is_heartbeat_enabled()); + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_wipy_heartbeat_obj, 0, 1, mod_wipy_heartbeat); + +STATIC const mp_map_elem_t wipy_module_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_wipy) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_heartbeat), (mp_obj_t)&mod_wipy_heartbeat_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(wipy_module_globals, wipy_module_globals_table); + +const mp_obj_module_t wipy_module = { + .base = { &mp_type_module }, + .name = MP_QSTR_wipy, + .globals = (mp_obj_dict_t*)&wipy_module_globals, +}; diff --git a/cc3200/mpconfigport.h b/cc3200/mpconfigport.h index dd7713a83..3dadb539d 100644 --- a/cc3200/mpconfigport.h +++ b/cc3200/mpconfigport.h @@ -113,6 +113,7 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj; // extra built in modules to add to the list of known ones extern const struct _mp_obj_module_t machine_module; +extern const struct _mp_obj_module_t wipy_module; extern const struct _mp_obj_module_t mp_module_ure; extern const struct _mp_obj_module_t mp_module_ujson; extern const struct _mp_obj_module_t mp_module_uos; @@ -125,6 +126,7 @@ extern const struct _mp_obj_module_t mp_module_ussl; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&machine_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_wipy), (mp_obj_t)&wipy_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_uos }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \ diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h index 1b0413075..4e7e69d5f 100644 --- a/cc3200/qstrdefsport.h +++ b/cc3200/qstrdefsport.h @@ -55,6 +55,10 @@ Q(WLAN_WAKE) Q(PIN_WAKE) Q(RTC_WAKE) +// for wipy module +Q(wipy) +Q(heartbeat) + // entries for sys.path Q(/flash) Q(/flash/lib) @@ -315,11 +319,6 @@ Q(WDT) Q(feed) Q(timeout) -// for HeartBeat class -Q(HeartBeat) -Q(enable) -Q(disable) - // for irq class Q(irq) Q(init) diff --git a/tests/wipy/modwipy.py b/tests/wipy/modwipy.py new file mode 100644 index 000000000..7571af075 --- /dev/null +++ b/tests/wipy/modwipy.py @@ -0,0 +1,21 @@ +''' +wipy module test for the CC3200 based boards +''' + +import os +import wipy + +mch = os.uname().machine +if not 'LaunchPad' in mch and not'WiPy' in mch: + raise Exception('Board not supported!') + +print(wipy.heartbeat() == True) +wipy.heartbeat(False) +print(wipy.heartbeat() == False) +wipy.heartbeat(True) +print(wipy.heartbeat() == True) + +try: + wipy.heartbeat(True, 1) +except: + print('Exception') diff --git a/tests/wipy/modwipy.py.exp b/tests/wipy/modwipy.py.exp new file mode 100644 index 000000000..52eeb534e --- /dev/null +++ b/tests/wipy/modwipy.py.exp @@ -0,0 +1,4 @@ +True +True +True +Exception