Skip to content

Instantly share code, notes, and snippets.

@odudex
Created September 30, 2024 15:53
Show Gist options
  • Save odudex/daf746cc0dd1ffb2f192629536acb366 to your computer and use it in GitHub Desktop.
Save odudex/daf746cc0dd1ffb2f192629536acb366 to your computer and use it in GitHub Desktop.
Creates a map of used flash bocks for Krux
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