Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfixes for release 22.03.0 #116

Merged
1 commit merged into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ RUN mkdir build && \
cp -r vendor/embit/src/embit build && \
rm -rf build/embit/util/prebuilt && \
rm -f build/embit/util/ctypes_secp256k1.py && \
rm -f build/embit/util/py_secp256k1.py && \
cp -r vendor/urtypes/src/urtypes build && \
cp -r vendor/foundation-ur-py/src/ur build && \
cp -r firmware/MaixPy/projects/"${DEVICE}"/builtin_py/. build && \
Expand Down
4 changes: 2 additions & 2 deletions selfcustody.pem
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-----BEGIN PUBLIC KEY-----
MDYwEAYHKoZIzj0CAQYFK4EEAAoDIgADMc0I9lfvPOoC4/CGh95q/0mTIsNiIpQH
ZbKrI8f28KU=
MDYwEAYHKoZIzj0CAQYFK4EEAAoDIgADM56IMVfkWJHmHKnfTNO7iV7zLUdbjnk1
WeoQo2dmaJs=
-----END PUBLIC KEY-----
6 changes: 3 additions & 3 deletions src/krux/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import sensor
import lcd
from .qr import QRPartParser
from .wdt import wdt


class Camera:
"""Camera is a singleton interface for interacting with the device's camera"""

def __init__(self, ctx):
self.ctx = ctx
def __init__(self):
self.initialize_sensor()

def initialize_sensor(self):
Expand All @@ -52,7 +52,7 @@ def capture_qr_code_loop(self, callback):
prev_parsed_count = 0
new_part = False
while True:
self.ctx.wdt.feed()
wdt.feed()
stop = callback(parser.total_count(), parser.parsed_count(), new_part)
if stop:
break
Expand Down
8 changes: 2 additions & 6 deletions src/krux/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,24 @@
# THE SOFTWARE.
import gc
import board
import machine
from .logging import Logger
from .settings import settings
from .display import Display
from .input import Input
from .camera import Camera
from .light import Light

RESET_TIMEOUT = 30000


class Context:
"""Context is a singleton containing all 'global' state that lives throughout the
duration of the program, including references to all device interfaces.
"""

def __init__(self):
self.wdt = machine.WDT(timeout=RESET_TIMEOUT)
self.log = Logger(settings.log.path, settings.log.level)
self.display = Display()
self.input = Input(self)
self.camera = Camera(self)
self.input = Input()
self.camera = Camera()
self.light = Light() if "LED_W" in board.config["krux.pins"] else None
self.printer = None
self.wallet = None
Expand Down
2 changes: 2 additions & 0 deletions src/krux/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .metadata import SIGNER_PUBKEY
from .display import Display
from .i18n import t
from .wdt import wdt

MAX_FIRMWARE_SIZE = 0x300000

Expand Down Expand Up @@ -77,6 +78,7 @@ def write_data(
total_read = 0
read_attempts = 0
while True:
wdt.feed()
if sha_suffix is None:
pct_cb(total_read / data_size)
if total_read == data_size:
Expand Down
16 changes: 6 additions & 10 deletions src/krux/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import board
from Maix import GPIO
from fpioa_manager import fm
from .wdt import wdt

BUTTON_ENTER = 0
BUTTON_PAGE = 1
Expand All @@ -35,8 +36,7 @@
class Input:
"""Input is a singleton interface for interacting with the device's buttons"""

def __init__(self, ctx=None):
self.ctx = ctx
def __init__(self):
self.entropy = 0
fm.register(board.config["krux.pins"]["BUTTON_A"], fm.fpioa.GPIOHS21)
self.enter = GPIO(GPIO.GPIOHS21, GPIO.IN, GPIO.PULL_UP)
Expand All @@ -49,32 +49,28 @@ def wait_for_button(self, block=True):
"""
# Loop until all buttons are released (if currently pressed)
while self.enter.value() == PRESSED or self.page.value() == PRESSED:
if self.ctx is not None:
self.ctx.wdt.feed()
wdt.feed()
self.entropy += 1

# Wait for first button press
checks = 0
while self.enter.value() == RELEASED and self.page.value() == RELEASED:
if self.ctx is not None:
self.ctx.wdt.feed()
wdt.feed()
checks += 1
if not block and checks > NONBLOCKING_CHECKS:
break

if self.enter.value() == PRESSED:
# Wait for release
while self.enter.value() == PRESSED:
if self.ctx is not None:
self.ctx.wdt.feed()
wdt.feed()
self.entropy += 1
return BUTTON_ENTER

if self.page.value() == PRESSED:
# Wait for release
while self.page.value() == PRESSED:
if self.ctx is not None:
self.ctx.wdt.feed()
wdt.feed()
self.entropy += 1
return BUTTON_PAGE
return None
2 changes: 1 addition & 1 deletion src/krux/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
VERSION = "22.03.0"
SIGNER_PUBKEY = "0331cd08f657ef3cea02e3f08687de6aff499322c36222940765b2ab23c7f6f0a5"
SIGNER_PUBKEY = "03339e883157e45891e61ca9df4cd3bb895ef32d475b8e793559ea10a36766689b"
22 changes: 14 additions & 8 deletions src/krux/printers/thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from fpioa_manager import fm
from machine import UART
from ..settings import settings
from ..wdt import wdt
from . import Printer


Expand Down Expand Up @@ -132,6 +133,7 @@ def setup(self):
def write_bytes(self, *args):
"""Writes bytes to the printer at a stable speed"""
for arg in args:
wdt.feed()
self.uart_conn.write(arg if isinstance(arg, bytes) else bytes([arg]))
# Calculate time to issue one byte to the printer.
# 11 bits (not 8) to accommodate idle, start and
Expand Down Expand Up @@ -179,16 +181,20 @@ def clear(self):

def print_qr_code(self, qr_code):
"""Prints a QR code, scaling it up as large as possible"""
lines = qr_code.strip().split("\n")
size = 0
while qr_code[size] != "\n":
size += 1

width = len(lines)
height = len(lines)

scale = settings.printer.thermal.paper_width // width
for y in range(height):
scale = settings.printer.thermal.paper_width // size
for y in range(size):
# Scale the line (width) by scaling factor
line_y = "".join([char * scale for char in lines[y]])
line_bytes = int(line_y, 2).to_bytes((len(line_y) + 7) // 8, "big")
line = 0
for char in qr_code[y * (size + 1) : y * (size + 1) + size]:
bit = int(char)
for _ in range(scale):
line <<= 1
line |= bit
line_bytes = line.to_bytes((size * scale + 7) // 8, "big")
# Print height * scale lines out to scale by
for _ in range(scale):
self.write_bytes(18, 42, 1, len(line_bytes))
Expand Down
27 changes: 27 additions & 0 deletions src/krux/wdt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# The MIT License (MIT)

# Copyright (c) 2021 Tom J. Sun

# 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 machine

RESET_TIMEOUT = 30000

# Create a watchdog timer that resets the device if not fed for 30s
wdt = machine.WDT(timeout=RESET_TIMEOUT)
2 changes: 1 addition & 1 deletion tests/pages/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def test_capture_qr_code(mocker):
input=mock.MagicMock(
wait_for_button=mock.MagicMock(side_effect=[BUTTON_ENTER, BUTTON_PAGE])
),
camera=Camera(),
)
ctx.camera = Camera(ctx)

page = MockPage(ctx)

Expand Down
2 changes: 2 additions & 0 deletions tests/printers/test_thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def test_clear(mocker):

def test_print_qr_code(mocker):
mocker.patch("krux.printers.thermal.UART", new=MockUART)
import krux
from krux.printers.thermal import AdafruitPrinter

p = AdafruitPrinter()
Expand All @@ -96,3 +97,4 @@ def test_print_qr_code(mocker):

assert p.write_bytes.call_count == 701
p.feed.assert_called_once()
krux.printers.thermal.wdt.feed.assert_called()
31 changes: 12 additions & 19 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ def test_init(mocker):
from krux.camera import Camera

spy = mocker.spy(Camera, "initialize_sensor")
ctx = mock.MagicMock(wdt=mock.MagicMock())
c = Camera(ctx)
c = Camera()

assert isinstance(c, Camera)
spy.assert_called()
Expand All @@ -16,8 +15,7 @@ def test_initialize_sensor():
import krux
from krux.camera import Camera

ctx = mock.MagicMock(wdt=mock.MagicMock())
c = Camera(ctx)
c = Camera()

c.initialize_sensor()

Expand All @@ -42,8 +40,7 @@ def test_capture_qr_code_loop(mocker):
import krux
from krux.camera import Camera

ctx = mock.MagicMock(wdt=mock.MagicMock())
c = Camera(ctx)
c = Camera()

prev_parsed_count = -1

Expand All @@ -63,7 +60,7 @@ def progress_callback(total_count, parsed_count, is_new):
assert format == MockQRPartParser.FORMAT
assert prev_parsed_count == MockQRPartParser.TOTAL - 1
krux.camera.sensor.run.assert_called_with(0)
ctx.wdt.feed.assert_called()
krux.camera.wdt.feed.assert_called()


def test_capture_qr_code_loop_returns_early_when_requested(mocker):
Expand All @@ -74,8 +71,7 @@ def test_capture_qr_code_loop_returns_early_when_requested(mocker):
import krux
from krux.camera import Camera

ctx = mock.MagicMock(wdt=mock.MagicMock())
c = Camera(ctx)
c = Camera()

prev_parsed_count = -1

Expand All @@ -95,7 +91,7 @@ def progress_callback(total_count, parsed_count, is_new):
assert format is None
assert prev_parsed_count < MockQRPartParser.TOTAL - 1
krux.camera.sensor.run.assert_called_with(0)
ctx.wdt.feed.assert_called()
krux.camera.wdt.feed.assert_called()


def test_capture_qr_code_loop_skips_bad_histogram(mocker):
Expand All @@ -107,8 +103,7 @@ def test_capture_qr_code_loop_skips_bad_histogram(mocker):
import krux
from krux.camera import Camera

ctx = mock.MagicMock(wdt=mock.MagicMock())
c = Camera(ctx)
c = Camera()

prev_parsed_count = -1

Expand All @@ -131,7 +126,7 @@ def progress_callback(total_count, parsed_count, is_new):
assert format == MockQRPartParser.FORMAT
assert prev_parsed_count == MockQRPartParser.TOTAL - 1
krux.camera.sensor.run.assert_called_with(0)
ctx.wdt.feed.assert_called()
krux.camera.wdt.feed.assert_called()


def test_capture_qr_code_loop_skips_missing_qrcode(mocker):
Expand All @@ -143,8 +138,7 @@ def test_capture_qr_code_loop_skips_missing_qrcode(mocker):
import krux
from krux.camera import Camera

ctx = mock.MagicMock(wdt=mock.MagicMock())
c = Camera(ctx)
c = Camera()

prev_parsed_count = -1

Expand All @@ -167,7 +161,7 @@ def progress_callback(total_count, parsed_count, is_new):
assert format == MockQRPartParser.FORMAT
assert prev_parsed_count == MockQRPartParser.TOTAL - 1
krux.camera.sensor.run.assert_called_with(0)
ctx.wdt.feed.assert_called()
krux.camera.wdt.feed.assert_called()


def test_capture_qr_code_loop_skips_duplicate_qrcode(mocker):
Expand All @@ -179,8 +173,7 @@ def test_capture_qr_code_loop_skips_duplicate_qrcode(mocker):
import krux
from krux.camera import Camera

ctx = mock.MagicMock(wdt=mock.MagicMock())
c = Camera(ctx)
c = Camera()

prev_parsed_count = -1

Expand All @@ -203,4 +196,4 @@ def progress_callback(total_count, parsed_count, is_new):
assert format == MockQRPartParser.FORMAT
assert prev_parsed_count == MockQRPartParser.TOTAL - 1
krux.camera.sensor.run.assert_called_with(0)
ctx.wdt.feed.assert_called()
krux.camera.wdt.feed.assert_called()
Loading