Created
September 30, 2024 15:53
-
-
Save odudex/daf746cc0dd1ffb2f192629536acb366 to your computer and use it in GitHub Desktop.
Creates a map of used flash bocks for Krux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flash | |
from . import Page, MENU_CONTINUE, DEFAULT_PADDING | |
from ..themes import theme, GREEN, LIGHT_PINK, PURPLE, BLUE, YELLOW | |
from ..krux_settings import t | |
from ..display import FONT_HEIGHT | |
from ..wdt import wdt | |
from ..firmware import FLASH_SIZE, SPIFFS_ADDR, FIRMWARE_SLOT_1, FIRMWARE_SLOT_2 | |
FLASH_ROWS = 64 | |
BLOCK_SIZE = 0x1000 | |
class FlashMap(Page): | |
"""Sweep the flash memory and draw a map of the used regions""" | |
def __init__(self, ctx): | |
super().__init__(ctx, None) | |
self.ctx = ctx | |
def draw(self): | |
"""Draw the flash map""" | |
# The map must contain 4096 blocks and fit the screen. | |
# The used area is a rectangle equal to width x with | |
# | |
image_block_size = self.ctx.display.width() // FLASH_ROWS | |
empty_buf = b"\xff" * BLOCK_SIZE | |
column = 0 | |
row = 0 | |
offset_x = self.ctx.display.width() | |
offset_x -= image_block_size * FLASH_ROWS | |
offset_x //= 2 | |
offset_y = DEFAULT_PADDING + 2 * FONT_HEIGHT | |
self.ctx.display.clear() | |
self.ctx.display.draw_hcentered_text(t("Flash Map")) | |
# Draw a map of the flash memory | |
for address in range(0, FLASH_SIZE, BLOCK_SIZE): | |
wdt.feed() | |
if address >= SPIFFS_ADDR: | |
color = YELLOW | |
elif address >= FIRMWARE_SLOT_2: | |
color = PURPLE | |
elif address >= FIRMWARE_SLOT_1: | |
color = LIGHT_PINK | |
elif address >= 0x4000: | |
color = GREEN | |
else: | |
color = BLUE | |
if flash.read(address, BLOCK_SIZE) == empty_buf: | |
color = theme.disabled_color | |
self.ctx.display.fill_rectangle( | |
offset_x + column * image_block_size, | |
offset_y + row * image_block_size, | |
image_block_size, | |
image_block_size, | |
color, | |
) | |
column += 1 | |
if column == FLASH_ROWS: | |
column = 0 | |
row += 1 | |
self.ctx.input.wait_for_button() | |
return MENU_CONTINUE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment