summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorSean Cross <sean@xobs.io>2020-05-22 12:35:29 +0800
committerSean Cross <sean@xobs.io>2020-05-27 11:28:49 +0800
commit08362c9cabce24ffdc2bd6018ab7fc3e9e6e4801 (patch)
tree8f74514dccbfd989b0abbe5b43898c371b85e834 /shared-bindings
parentec99dae95384fa08e4b1bc72273f1d34c6d646bb (diff)
watchdogtimer: refactor to new api
This refactors the WatchDogTimer API to use the format proposed in https://github.com/adafruit/circuitpython/pull/2933#issuecomment-632268227 Signed-off-by: Sean Cross <sean@xobs.io>
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/microcontroller/__init__.c2
-rw-r--r--shared-bindings/microcontroller/__init__.h4
-rw-r--r--shared-bindings/watchdog/WatchDogMode.c88
-rw-r--r--shared-bindings/watchdog/WatchDogMode.h47
-rw-r--r--shared-bindings/watchdog/__init__.c21
-rw-r--r--shared-bindings/watchdog/__init__.h2
6 files changed, 157 insertions, 7 deletions
diff --git a/shared-bindings/microcontroller/__init__.c b/shared-bindings/microcontroller/__init__.c
index 907f1ec0a..88fe9c224 100644
--- a/shared-bindings/microcontroller/__init__.c
+++ b/shared-bindings/microcontroller/__init__.c
@@ -168,7 +168,7 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&mp_const_none_obj) },
#endif
#if CIRCUITPY_WATCHDOG
- { MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&common_hal_mcu_watchdog_obj) },
+ { MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&common_hal_mcu_watchdogtimer_obj) },
#else
{ MP_ROM_QSTR(MP_QSTR_watchdog), MP_ROM_PTR(&mp_const_none_obj) },
#endif
diff --git a/shared-bindings/microcontroller/__init__.h b/shared-bindings/microcontroller/__init__.h
index 6d61c527e..8abdff763 100644
--- a/shared-bindings/microcontroller/__init__.h
+++ b/shared-bindings/microcontroller/__init__.h
@@ -54,8 +54,8 @@ extern const nvm_bytearray_obj_t common_hal_mcu_nvm_obj;
#endif
#if CIRCUITPY_WATCHDOG
-#include "common-hal/watchdog/__init__.h"
-extern const watchdog_obj_t common_hal_mcu_watchdog_obj;
+#include "common-hal/watchdog/WatchDogTimer.h"
+extern watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj;
#endif
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H
diff --git a/shared-bindings/watchdog/WatchDogMode.c b/shared-bindings/watchdog/WatchDogMode.c
new file mode 100644
index 000000000..c5c4b23bc
--- /dev/null
+++ b/shared-bindings/watchdog/WatchDogMode.c
@@ -0,0 +1,88 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Sean Cross for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/watchdog/WatchDogMode.h"
+
+//| class WatchDogMode:
+//| """run state of the watchdog timer"""
+//|
+//| def __init__(self, ):
+//| """Enum-like class to define the run mode of the watchdog timer."""
+//|
+//| NONE: Any = ...
+//| """Take no action if the watchdog timer expires.
+//|
+//| :type watchdog.WatchDogMode:"""
+//|
+//| RAISE: Any = ...
+//| """Raise an exception when the WatchDogTimer expires.
+//|
+//| :type watchdog.WatchDogMode:"""
+//|
+//| RESET: Any = ...
+//| """Reset the system if the WatchDogTimer expires.
+//|
+//| :type watchdog.WatchDogMode:"""
+//|
+const mp_obj_type_t watchdog_watchdogmode_type;
+
+const watchdog_watchdogmode_obj_t watchdog_watchdogmode_none_obj = {
+ { &watchdog_watchdogmode_type },
+};
+
+const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj = {
+ { &watchdog_watchdogmode_type },
+};
+
+const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj = {
+ { &watchdog_watchdogmode_type },
+};
+
+STATIC const mp_rom_map_elem_t watchdog_watchdogmode_locals_dict_table[] = {
+ {MP_ROM_QSTR(MP_QSTR_NONE), MP_ROM_PTR(&watchdog_watchdogmode_none_obj)},
+ {MP_ROM_QSTR(MP_QSTR_RAISE), MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)},
+ {MP_ROM_QSTR(MP_QSTR_RESET), MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)},
+};
+STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogmode_locals_dict, watchdog_watchdogmode_locals_dict_table);
+
+STATIC void watchdog_watchdogmode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ qstr runmode = MP_QSTR_NONE;
+ if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
+ runmode = MP_QSTR_RAISE;
+ }
+ else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
+ runmode = MP_QSTR_RESET;
+ }
+ mp_printf(print, "%q.%q.%q", MP_QSTR_watchdog, MP_QSTR_WatchDogMode,
+ runmode);
+}
+
+const mp_obj_type_t watchdog_watchdogmode_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_WatchDogMode,
+ .print = watchdog_watchdogmode_print,
+ .locals_dict = (mp_obj_t)&watchdog_watchdogmode_locals_dict,
+};
diff --git a/shared-bindings/watchdog/WatchDogMode.h b/shared-bindings/watchdog/WatchDogMode.h
new file mode 100644
index 000000000..77bf58db6
--- /dev/null
+++ b/shared-bindings/watchdog/WatchDogMode.h
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Sean Cross for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
+
+#include "py/obj.h"
+
+typedef enum {
+ WATCHDOGMODE_NONE,
+ WATCHDOGMODE_RAISE,
+ WATCHDOGMODE_RESET,
+} watchdog_watchdogmode_t;
+
+const mp_obj_type_t watchdog_watchdogmode_type;
+
+typedef struct {
+ mp_obj_base_t base;
+} watchdog_watchdogmode_obj_t;
+extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_none_obj;
+extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj;
+extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
diff --git a/shared-bindings/watchdog/__init__.c b/shared-bindings/watchdog/__init__.c
index 21e87d515..36993db93 100644
--- a/shared-bindings/watchdog/__init__.c
+++ b/shared-bindings/watchdog/__init__.c
@@ -28,6 +28,7 @@
#include "py/runtime.h"
#include "shared-bindings/watchdog/__init__.h"
+#include "shared-bindings/watchdog/WatchDogMode.h"
//| """Watchdog Timer
//|
@@ -47,7 +48,21 @@
//|
//| Example usage::
//|
-//| from microcontroller.watchdog import WatchDogTimer
-//| wdt = WatchDogTimer(timeout=2.5) # enable it with a timeout of 2.5 seconds
-//| wdt.feed()"""
+//| from microcontroller import watchdog as w
+//| from watchdog import WatchDogMode
+//| w.timeout=2.5 # Set a timeout of 2.5 seconds
+//| w.mode = WatchDogMode.RAISE
+//| w.feed()"""
//|
+
+STATIC const mp_rom_map_elem_t watchdog_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_watchdog) },
+ { MP_ROM_QSTR(MP_QSTR_WatchDogMode), MP_ROM_PTR(&watchdog_watchdogmode_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(watchdog_module_globals, watchdog_module_globals_table);
+
+const mp_obj_module_t watchdog_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&watchdog_module_globals,
+};
diff --git a/shared-bindings/watchdog/__init__.h b/shared-bindings/watchdog/__init__.h
index 90426c1e6..7203b8aa5 100644
--- a/shared-bindings/watchdog/__init__.h
+++ b/shared-bindings/watchdog/__init__.h
@@ -27,6 +27,6 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H
-extern const mp_obj_type_t watchdog_type;
+extern const mp_obj_module_t watchdog_module;
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG___INIT___H