Skip to content

Commit

Permalink
various bugfixes (#175)
Browse files Browse the repository at this point in the history
* Moved sdcard.py

* Add sdcard driver

* Fix sdcard on TDeck

* Fix missing display instance

* Add default to terminal input

* Fix tdeck quit and Config save
  • Loading branch information
echo-lalia authored Nov 16, 2024
1 parent d57c6a4 commit 80b4e55
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 67 deletions.
3 changes: 2 additions & 1 deletion devices/TDECK/definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constants:
_MH_SDCARD_MISO: '38'
_MH_SDCARD_MOSI: '41'
_MH_SDCARD_SCK: '40'
_MH_SDCARD_SLOT: '2'
_MH_SDCARD_SLOT: '1'

features:
- keyboard
Expand All @@ -36,6 +36,7 @@ features:
- bluetooth
- kb_light
- spi_ram
- shared_sdcard_spi

mpy_arch: xtensawin
source_board: MICROHYDRA_GENERIC_S3
10 changes: 6 additions & 4 deletions devices/TDECK/lib/userinput/_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Keys:
secondary_action = "ENT"
aux_action = "SPC"

ext_dir_dict = {'i':'UP', 'j':'LEFT', 'k':'DOWN', 'l':'RIGHT', 'q':'ESC'}
ext_dir_dict = {'i':'UP', 'j':'LEFT', 'k':'DOWN', 'l':'RIGHT'}

def __init__(self, tb_repeat_ms=60, **kwargs): # noqa: ARG002
# turn on keyboard
Expand Down Expand Up @@ -201,7 +201,6 @@ def _special_mod_keys(self, keylist: list):
and "SHIFT" in keylist:
keylist.remove("SHIFT")
keylist.remove("FN")
keylist.append("OPT")
# shortcut for "ESC"
if "ALT" in keylist \
and "e" in keylist:
Expand Down Expand Up @@ -281,10 +280,13 @@ def get_pressed_keys(self, *, force_fn=False, force_shift=False) -> list:


# process special keys before converting to readable format
if (_KC_FN in codes and tb_val) \
if _KC_FN in codes and _KC_SHIFT in codes:
keymap = KEYMAP
keys.append('OPT')
elif (_KC_FN in codes) \
or force_fn:
keymap = KEYMAP_FN
elif _KC_SHIFT in codes or _KC_LEFT_SHIFT in codes \
elif (_KC_SHIFT in codes or _KC_LEFT_SHIFT in codes) \
or force_shift:
keymap = KEYMAP_SHIFT
else:
Expand Down
1 change: 1 addition & 0 deletions devices/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ features:
- bluetooth
- kb_light
- keyboard
- shared_sdcard_spi

mpy_arch: xtensawin
source_board: MICROHYDRA_GENERIC_S3
2 changes: 1 addition & 1 deletion src/launcher/terminal/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def print(self, *args, skip_none=False, **kwargs): # noqa: ARG002
self.draw()
self.display.show()

def input(self, prmpt: str) -> str:
def input(self, prmpt: str = '') -> str:
"""Get user input (Override the `input` built-in)."""

current_text = ''
Expand Down
2 changes: 1 addition & 1 deletion src/lib/display/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Display(st7789.ST7789):

def __new__(cls, **kwargs): # noqa: ARG003, D102
if not hasattr(cls, 'instance'):
cls.instance = super().__new__(cls)
Display.instance = super().__new__(cls)
return cls.instance


Expand Down
33 changes: 16 additions & 17 deletions src/lib/display/st7789.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
# mh_end_if


_MH_DISPLAY_BAUDRATE = const(40_000_000)


# ST7789 commands
_ST7789_SWRESET = b"\x01"
Expand Down Expand Up @@ -258,6 +260,10 @@ def init(self, commands: tuple):

def _write(self, command=None, data=None):
"""SPI write to the device: commands and data."""
# mh_if shared_sdcard_spi:
# # TDeck shares SPI with SDCard
# self.spi.init(baudrate=_MH_DISPLAY_BAUDRATE)
# mh_end_if
if self.cs:
self.cs.off()
if command is not None:
Expand All @@ -278,6 +284,10 @@ def _write_tiny_buf(self, y_min: int, y_max: int):
converts the 4bit data to 16bit RGB565 format,
and sends the data over SPI.
"""
# mh_if shared_sdcard_spi:
# # TDeck shares SPI with SDCard
# self.spi.init(baudrate=_MH_DISPLAY_BAUDRATE)
# mh_end_if
if self.cs:
self.cs.off()
self.dc.on()
Expand Down Expand Up @@ -365,17 +375,14 @@ def sleep_mode(self, value: bool):
value (bool): if True enable sleep mode. if False disable sleep
mode
"""
# mh_if TDECK:
# mh_if shared_sdcard_spi:
# # TDeck shares SPI with SDCard
# self.spi.init()
# self.spi.init(baudrate=_MH_DISPLAY_BAUDRATE)
# mh_end_if
if value:
self._write(_ST7789_SLPIN)
else:
self._write(_ST7789_SLPOUT)
# mh_if TDECK:
# self.spi.deinit()
# mh_end_if


def inversion_mode(self, value: bool):
Expand All @@ -386,17 +393,14 @@ def inversion_mode(self, value: bool):
value (bool): if True enable inversion mode. if False disable
inversion mode
"""
# mh_if TDECK:
# mh_if shared_sdcard_spi:
# # TDeck shares SPI with SDCard
# self.spi.init()
# self.spi.init(baudrate=_MH_DISPLAY_BAUDRATE)
# mh_end_if
if value:
self._write(_ST7789_INVON)
else:
self._write(_ST7789_INVOFF)
# mh_if TDECK:
# self.spi.deinit()
# mh_end_if


def rotation(self, rotation):
Expand Down Expand Up @@ -454,9 +458,9 @@ def _set_window(self, x0, y0, x1, y1):

def show(self):
"""Write the current framebuf to the display."""
# mh_if TDECK:
# mh_if shared_sdcard_spi:
# # TDeck shares SPI with SDCard
# self.spi.init()
# self.spi.init(baudrate=_MH_DISPLAY_BAUDRATE)
# mh_end_if

# Reset and clamp min/max vals
Expand All @@ -477,8 +481,3 @@ def show(self):
self._write_tiny_buf(y_min, y_max)
else:
self._write_normal_buf(y_min, y_max)

# mh_if TDECK:
# # TDeck shares SPI with SDCard
# self.spi.deinit()
# mh_end_if
43 changes: 0 additions & 43 deletions src/lib/sdcard.py

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib/sdcard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""This simple module configures and mounts an SDCard."""

from .mhsdcard import SDCard
61 changes: 61 additions & 0 deletions src/lib/sdcard/mhsdcard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""This simple module configures and mounts an SDCard."""

# mh_if shared_sdcard_spi:
from .sdcard import _SDCard
# mh_end_if

import machine
import os



_MH_SDCARD_SLOT = const(1)
_MH_SDCARD_SCK = const(40)
_MH_SDCARD_MISO = const(38)
_MH_SDCARD_MOSI = const(41)
_MH_SDCARD_CS = const(39)



class SDCard:
"""SDCard control."""

def __init__(self):
"""Initialize the SDCard."""
# mh_if shared_sdcard_spi:
self.sd = _SDCard(
machine.SPI(
_MH_SDCARD_SLOT, # actually SPI id
sck=machine.Pin(_MH_SDCARD_SCK),
miso=machine.Pin(_MH_SDCARD_MISO),
mosi=machine.Pin(_MH_SDCARD_MOSI),
),
cs=machine.Pin(_MH_SDCARD_CS),
)
# mh_else:
# self.sd = machine.SDCard(
# slot=_MH_SDCARD_SLOT,
# sck=machine.Pin(_MH_SDCARD_SCK),
# miso=machine.Pin(_MH_SDCARD_MISO),
# mosi=machine.Pin(_MH_SDCARD_MOSI),
# cs=machine.Pin(_MH_SDCARD_CS)
# )
# mh_end_if


def mount(self):
"""Mount the SDCard."""
if "sd" in os.listdir("/"):
return
try:
os.mount(self.sd, '/sd')
except (OSError, NameError, AttributeError) as e:
print(f"Could not mount SDCard: {e}")


def deinit(self):
"""Unmount and deinit the SDCard."""
os.umount('/sd')
# mh_if not shared_sdcard_spi:
# self.sd.deinit()
# mh_end_if
Loading

0 comments on commit 80b4e55

Please sign in to comment.