From a23acc61318548c6a88cacc780558c141d0e4f69 Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Sun, 7 Oct 2018 00:44:04 -0700 Subject: [PATCH] Update various internal key handling stuff to be CircuitPython-compatible --- kmk/common/event_defs.py | 7 ++----- kmk/common/kmktime.py | 28 ++++++++++++++++++++++++---- kmk/common/macros/simple.py | 2 +- kmk/common/util.py | 13 ------------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/kmk/common/event_defs.py b/kmk/common/event_defs.py index 0a232e6..51ba8eb 100644 --- a/kmk/common/event_defs.py +++ b/kmk/common/event_defs.py @@ -4,6 +4,7 @@ from collections import namedtuple from micropython import const from kmk.common.keycodes import Keycodes +from kmk.common.util import reset_bootloader KEY_UP_EVENT = const(1) KEY_DOWN_EVENT = const(2) @@ -117,11 +118,7 @@ def matrix_changed(new_pressed): dispatch(hid_report_event()) if Keycodes.KMK.KC_RESET in state.keys_pressed: - try: - import machine - machine.bootloader() - except ImportError: - logger.warning('Tried to reset to bootloader, but not supported on this chip?') + reset_bootloader() if state.pending_keys: for key in state.pending_keys: diff --git a/kmk/common/kmktime.py b/kmk/common/kmktime.py index 163b330..ea97a9e 100644 --- a/kmk/common/kmktime.py +++ b/kmk/common/kmktime.py @@ -1,13 +1,33 @@ +import math try: - import utime + import utime as time + USE_UTIME = True except ImportError: - pass + import time + USE_UTIME = False + + +def sleep_ms(ms): + ''' + Tries to sleep for a number of milliseconds in a cross-implementation + way. Will raise an ImportError if time is not available on the platform. + ''' + if USE_UTIME: + return time.sleep_ms(ms) + else: + return time.sleep(ms / 1000) def ticks_ms(): - return utime.ticks_ms() + if USE_UTIME: + return time.ticks_ms() + else: + return math.floor(time.monotonic() * 1000) def ticks_diff(new, old): - return utime.ticks_diff(new, old) + if USE_UTIME: + return time.ticks_diff(new, old) + else: + return new - old diff --git a/kmk/common/macros/simple.py b/kmk/common/macros/simple.py index 29f6c14..0bb77ba 100644 --- a/kmk/common/macros/simple.py +++ b/kmk/common/macros/simple.py @@ -3,7 +3,7 @@ import string from kmk.common.event_defs import (hid_report_event, keycode_down_event, keycode_up_event) from kmk.common.keycodes import Keycodes, Macro, RawKeycodes, char_lookup -from kmk.common.util import sleep_ms +from kmk.common.kmktime import sleep_ms def simple_key_sequence(seq): diff --git a/kmk/common/util.py b/kmk/common/util.py index e90d85b..2ff06d1 100644 --- a/kmk/common/util.py +++ b/kmk/common/util.py @@ -29,16 +29,3 @@ def reset_bootloader(): import microcontroller microcontroller.on_next_reset(microcontroller.RunMode.BOOTLOADER) microcontroller.reset() - - -def sleep_ms(ms): - ''' - Tries to sleep for a number of milliseconds in a cross-implementation - way. Will raise an ImportError if time is not available on the platform. - ''' - try: - import time - time.sleep_ms(ms) - except AttributeError: - import time - time.sleep(ms / 1000)