Update various internal key handling stuff to be CircuitPython-compatible

This commit is contained in:
Josh Klar 2018-10-07 00:44:04 -07:00
parent d379acfc97
commit a23acc6131
No known key found for this signature in database
GPG Key ID: 220F99BD7DB7A99E
4 changed files with 27 additions and 23 deletions

View File

@ -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:

View File

@ -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

View File

@ -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):

View File

@ -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)