Skip to content

Commit

Permalink
Merge pull request #16 from tadeubas/develop_rc
Browse files Browse the repository at this point in the history
New settings for encoder debounce
  • Loading branch information
odudex authored Aug 27, 2023
2 parents af80f85 + c1405fb commit dc86469
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ poetry run python simulator.py --device maixpy_amigo_tft

# Run simulator with sd enabled and the small button-only m5stick, then use keyboard (arrow keys UP or DOWN and ENTER)
poetry run python simulator.py --device maixpy_m5stickv --sd

# Run simulator with the rotary encoder device dock, then use keyboard (arrow keys UP or DOWN and ENTER)
poetry run python simulator.py --device maixpy_dock
```

To be able to emulate a SD card, first create a folder called `sd` inside `simulator` folder.
Expand Down
2 changes: 2 additions & 0 deletions i18n/translations/de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"Device flash storage not detected.": "Geräte-Flash-Speicher nicht erkannt.",
"Done?": "Fertig?",
"Driver": "Driver",
"Encoder": "Encoder",
"Encoder Debounce": "Encoder-Entprellung",
"Encrypt Mnemonic": "Verschlüssel Mnemonic",
"Encrypted QR Code": "Verschlüsselter QR-Code",
"Encrypted mnemonic was not stored": "Verschlüsselte Mnemonic wurde nicht gespeichert",
Expand Down
2 changes: 2 additions & 0 deletions i18n/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"Device flash storage not detected.": "Device flash storage not detected.",
"Done?": "Done?",
"Driver": "Driver",
"Encoder": "Encoder",
"Encoder Debounce": "Encoder Debounce",
"Encrypt Mnemonic": "Encrypt Mnemonic",
"Encrypted QR Code": "Encrypted QR Code",
"Encrypted mnemonic was not stored": "Encrypted mnemonic was not stored",
Expand Down
2 changes: 2 additions & 0 deletions i18n/translations/es-MX.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"Device flash storage not detected.": "Almacenamiento flash del dispositivo no detectado.",
"Done?": "¿Listo?",
"Driver": "Operador",
"Encoder": "Codificador",
"Encoder Debounce": "Antirrebote del codificador",
"Encrypt Mnemonic": "Cifrado mnemónico",
"Encrypted QR Code": "Código QR cifrado",
"Encrypted mnemonic was not stored": "Mnemonic cifrado no se almacenó",
Expand Down
2 changes: 2 additions & 0 deletions i18n/translations/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"Device flash storage not detected.": "Stockage flash de l'appareil non détecté.",
"Done?": "Terminé?",
"Driver": "Conducteur",
"Encoder": "Encodeur",
"Encoder Debounce": "Anti-rebond de l'encodeur",
"Encrypt Mnemonic": "Crypter mnémonique",
"Encrypted QR Code": "Code QR crypté",
"Encrypted mnemonic was not stored": "Le mnémonique crypté n'a pas été stocké",
Expand Down
2 changes: 2 additions & 0 deletions i18n/translations/nl-NL.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"Device flash storage not detected.": "Opslag op apparaat is niet gedetecteerd.",
"Done?": "Klaar?",
"Driver": "Driver",
"Encoder": "Encoder",
"Encoder Debounce": "Encoder-debounce",
"Encrypt Mnemonic": "Geheugensteun versleutelen",
"Encrypted QR Code": "Versleutelde QR code",
"Encrypted mnemonic was not stored": "Versleutelde geheugensteun was niet opgeslagen",
Expand Down
2 changes: 2 additions & 0 deletions i18n/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"Device flash storage not detected.": "Armazenamento flash do dispositivo não detectado.",
"Done?": "Feito?",
"Driver": "Driver",
"Encoder": "Codificador",
"Encoder Debounce": "Espera do codificador",
"Encrypt Mnemonic": "Criptografar mnemônico",
"Encrypted QR Code": "Código QR criptografado",
"Encrypted mnemonic was not stored": "Mnemonic criptografado não foi armazenado",
Expand Down
2 changes: 2 additions & 0 deletions i18n/translations/vi-VN.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"Device flash storage not detected.": "Không phát hiện bộ lưu trữ flash của thiết bị.",
"Done?": "Hoàn tất?",
"Driver": "Người cầm lái",
"Encoder": "Mã hoá",
"Encoder Debounce": "Gỡ lỗi bộ mã hóa",
"Encrypt Mnemonic": "Mã hóa Mnemonic",
"Encrypted QR Code": "Mã QR được mã hóa",
"Encrypted mnemonic was not stored": "MNemon được mã hóa không được lưu trữ",
Expand Down
8 changes: 8 additions & 0 deletions simulator/kruxsim/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
AMIGO_IPS = "maixpy_amigo_ips"
AMIGO_TFT = "maixpy_amigo_tft"
PC = "maixpy_pc"
DOCK = "maixpy_dock"

WINDOW_SIZES = {
M5STICKV: (320, 640),
AMIGO_IPS: (480, 768),
AMIGO_TFT: (480, 768),
PC: (480, 640),
DOCK: (440, 640),
}


Expand All @@ -46,6 +48,8 @@ def load_image(device):
device = with_prefix(device)
if device == PC:
return None
if device == DOCK:
return None
if device not in images:
images[device] = pg.image.load(
os.path.join("assets", "%s.png" % device)
Expand All @@ -63,6 +67,10 @@ def load_font(device):
fonts[device] = pg.freetype.Font(
os.path.join("..", "firmware", "font", "ter-u14n.bdf")
)
elif device == DOCK:
fonts[device] = pg.freetype.Font(
os.path.join("..", "firmware", "font", "ter-u16n.bdf")
)
else:
fonts[device] = pg.freetype.Font(
os.path.join("..", "firmware", "font", "ter-u24b.bdf")
Expand Down
3 changes: 3 additions & 0 deletions simulator/kruxsim/mocks/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def register_device(device):
sys.modules["board"] = mock.MagicMock(config=BOARD_CONFIG)

BUTTON_A = BOARD_CONFIG["krux"]["pins"]["BUTTON_A"]
if "ENCODER" in BOARD_CONFIG["krux"]["pins"]:
del BOARD_CONFIG["krux"]["pins"]["ENCODER"]
BOARD_CONFIG["krux"]["pins"]["BUTTON_B"] = 37
BUTTON_B = BOARD_CONFIG["krux"]["pins"]["BUTTON_B"]
if "BUTTON_C" in BOARD_CONFIG["krux"]["pins"]:
BUTTON_C = BOARD_CONFIG["krux"]["pins"]["BUTTON_C"]
36 changes: 36 additions & 0 deletions simulator/kruxsim/mocks/rotary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# The MIT License (MIT)

# Copyright (c) 2021-2022 Krux contributors

# 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.
import sys
from unittest import mock

class RotaryEncoder:
def __init__(self):
self.debounce = 0
pass


encoder = RotaryEncoder() # Singleton

if "krux.rotary" not in sys.modules:
sys.modules["krux.rotary"] = mock.MagicMock(
RotaryEncoder=RotaryEncoder,
)
6 changes: 6 additions & 0 deletions simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
from kruxsim.mocks import ft6x36
from kruxsim.sequence import SequenceExecutor

from kruxsim.mocks import rotary

sequence_executor = None
if args.sequence:
sequence_executor = SequenceExecutor(args.sequence)
Expand Down Expand Up @@ -126,6 +128,10 @@ def run_krux():

device_image = devices.load_image(args.device)

if(args.device == devices.PC):
from kruxsim.mocks.board import BOARD_CONFIG
BOARD_CONFIG["type"] = "amigo_type"

t.start()


Expand Down
6 changes: 3 additions & 3 deletions src/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from krux.power import power_manager

MIN_SPLASH_TIME = 1000
MIN_SPLASH_WAIT_TIME = 1000


def splash():
Expand Down Expand Up @@ -124,8 +124,8 @@ def home(ctx_home):

# If importing happened too fast, sleep the difference so the logo
# will be shown
if preimport_ticks + MIN_SPLASH_TIME > postimport_ticks:
time.sleep_ms(preimport_ticks + MIN_SPLASH_TIME - postimport_ticks)
if preimport_ticks + MIN_SPLASH_WAIT_TIME > postimport_ticks:
time.sleep_ms(preimport_ticks + MIN_SPLASH_WAIT_TIME - postimport_ticks)

login(ctx)
gc.collect()
Expand Down
8 changes: 5 additions & 3 deletions src/krux/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
MAIN_BOOT_CONFIG_SECTOR_ADDRESS = 0x00004000
BACKUP_BOOT_CONFIG_SECTOR_ADDRESS = 0x00005000

FLASH_IO_WAIT_TIME = 100


def find_active_firmware(sector):
"""Returns a tuple of the active firmware's configuration"""
Expand Down Expand Up @@ -128,12 +130,12 @@ def write_data(

cur_address = i * chunk_size + address
flash.erase(cur_address, chunk_size)
time.sleep_ms(100)
time.sleep_ms(FLASH_IO_WAIT_TIME)
if header and i == 0:
flash.write(cur_address, b"\x00" + data_size.to_bytes(4, "little"))
time.sleep_ms(100)
time.sleep_ms(FLASH_IO_WAIT_TIME)
flash.write(cur_address + header_offset, buffer[:chunk_size_after_header])
time.sleep_ms(100)
time.sleep_ms(FLASH_IO_WAIT_TIME)
i += 1
num_read = 0
chunk_read = 0
Expand Down
4 changes: 3 additions & 1 deletion src/krux/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
PRESSED = 0
RELEASED = 1

BUTTON_WAIT_PRESS_DELAY = 10


class Input:
"""Input is a singleton interface for interacting with the device's buttons"""
Expand Down Expand Up @@ -167,7 +169,7 @@ def wait_for_press(self, block=True, wait_duration=QR_ANIM_PERIOD):
wdt.feed() # here is where krux spends most of its time
if not block and time.ticks_ms() > start_time + wait_duration:
return None
time.sleep_ms(10)
time.sleep_ms(BUTTON_WAIT_PRESS_DELAY)

def wait_for_button(self, block=True):
"""Waits for any button to release, optionally blocking if block=True.
Expand Down
17 changes: 17 additions & 0 deletions src/krux/krux_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ def label(self, attr):
}[attr]


class EncoderSettings(SettingsNamespace):
"""Encoder debounce settings"""

namespace = "settings.encoder"
debounce = NumberSetting(int, "debounce", 50, [25, 250])

def label(self, attr):
"""Returns a label for UI when given a setting name or namespace"""
return {
"debounce": t("Encoder Debounce"),
}[attr]


class TouchSettings(SettingsNamespace):
"""Touch sensitivity settings"""

Expand Down Expand Up @@ -333,6 +346,8 @@ def __init__(self):
self.appearance = ThemeSettings()
if board.config["type"].startswith("amigo"):
self.touch = TouchSettings()
if board.config["type"] == "dock":
self.encoder = EncoderSettings()

def label(self, attr):
"""Returns a label for UI when given a setting name or namespace"""
Expand All @@ -347,4 +362,6 @@ def label(self, attr):
}
if board.config["type"].startswith("amigo"):
main_menu["touchscreen"] = t("Touchscreen")
if board.config["type"] == "dock":
main_menu["encoder"] = t("Encoder")
return main_menu[attr]
3 changes: 2 additions & 1 deletion src/krux/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
ANTI_GLARE_WAIT_TIME = 500
QR_CODE_STEP_TIME = 100
CAMERA_INIT_TIME = 1000
SHUTDOWN_WAIT_TIME = 300

TOGGLE_BRIGHTNESS = (BUTTON_PAGE, BUTTON_PAGE_PREV)
PROCEED = (BUTTON_ENTER, BUTTON_TOUCH)
Expand Down Expand Up @@ -468,7 +469,7 @@ def shutdown(self):
if self.prompt(t("Are you sure?"), self.ctx.display.height() // 2):
self.ctx.display.clear()
self.ctx.display.draw_centered_text(t("Shutting down.."))
time.sleep(0.3)
time.sleep_ms(SHUTDOWN_WAIT_TIME)
return MENU_SHUTDOWN
return MENU_CONTINUE

Expand Down
22 changes: 17 additions & 5 deletions src/krux/pages/settings_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# THE SOFTWARE.
# pylint: disable=C2801

import time
from ..themes import theme, RED, GREEN, ORANGE, MAGENTA
from ..settings import (
CategorySetting,
Expand All @@ -30,7 +29,13 @@
FLASH_PATH,
Store,
)
from ..krux_settings import Settings, LoggingSettings, BitcoinSettings, TouchSettings
from ..krux_settings import (
Settings,
LoggingSettings,
BitcoinSettings,
TouchSettings,
EncoderSettings,
)
from ..input import BUTTON_ENTER, BUTTON_PAGE, BUTTON_PAGE_PREV, BUTTON_TOUCH
from ..krux_settings import t
from ..sd_card import SDHandler
Expand Down Expand Up @@ -115,8 +120,6 @@ def _display_centered_text(
"""Display a text for duration ms or until you press a button"""
self.ctx.display.clear()
self.ctx.display.draw_centered_text(message, color, bg_color)
# this sleep protect form a double input at the same time (ENTER + PAGE on a rotary encoder)
time.sleep_ms(WAIT_TO_CHECK_INPUT)
self.ctx.input.wait_for_press(block=False, wait_duration=duration)
self.ctx.display.clear()

Expand Down Expand Up @@ -237,18 +240,27 @@ def handler():
self.number_setting(settings_namespace, setting)
if settings_namespace.namespace == TouchSettings.namespace:
self._touch_threshold_exit_check()
elif settings_namespace.namespace == EncoderSettings.namespace:
self._encoder_threshold_exit_check()

return MENU_CONTINUE

return handler

def _touch_threshold_exit_check(self):
"""Handler for the 'Back' on settings screen"""
"""Handler for the 'Back' on touch settings screen"""

# Update touch detection threshold
if self.ctx.input.touch is not None:
self.ctx.input.touch.touch_driver.threshold(Settings().touch.threshold)

def _encoder_threshold_exit_check(self):
"""Handler for the 'Back' on encoder settings screen"""
from ..rotary import encoder

# Update rotary encoder debounce time
encoder.debounce = Settings().encoder.debounce

def category_setting(self, settings_namespace, setting):
"""Handler for viewing and editing a CategorySetting"""
categories = setting.categories
Expand Down
2 changes: 1 addition & 1 deletion src/krux/pages/tiny_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def scanner(self, w24=False):
self._set_camera_sensitivity()
full_screen = self._run_camera()
postcamera_ticks = time.ticks_ms()
# check how much ms camera took to retain message on the screen
# check how much time camera took to retain message on the screen
if precamera_ticks + FLASH_MSG_TIME > postcamera_ticks:
time.sleep_ms(precamera_ticks + FLASH_MSG_TIME - postcamera_ticks)
del message, precamera_ticks, postcamera_ticks
Expand Down
4 changes: 3 additions & 1 deletion src/krux/printers/thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
from ..wdt import wdt
from . import Printer

INITIALIZE_WAIT_TIME = 500


class AdafruitPrinter(Printer):
"""AdafruitPrinter is a minimal wrapper around a serial connection to
Expand Down Expand Up @@ -78,7 +80,7 @@ def setup(self):
# upon power up -- it needs a moment to cold boot
# and initialize. Allow at least 1/2 sec of uptime
# before printer can receive data.
time.sleep_ms(500)
time.sleep_ms(INITIALIZE_WAIT_TIME)

# Wake up the printer to get ready for printing
self.write_bytes(255)
Expand Down
Loading

0 comments on commit dc86469

Please sign in to comment.