Skip to content

Commit

Permalink
creade dedicated module for diag. tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDeadman committed Dec 23, 2021
1 parent 43fa58e commit ae2b43f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 35 deletions.
4 changes: 1 addition & 3 deletions src/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
from machine import PWM, SPI, Pin

from diagnostics import Diagnostics
from drivers import Display, Touch, color565
from extensions import input
from utils import COMMON_COLORS
from drivers import Display, Touch


def main():
Expand Down
30 changes: 8 additions & 22 deletions src/diagnostics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import gc
import os

from extensions import input
from utils import COMMON_COLORS

from diagnostics.draw_test import DrawTest
from diagnostics.font_test import FontTest
from diagnostics.tools import Tools
from diagnostics.touch_test import TouchTest


Expand All @@ -17,22 +15,21 @@ def __init__(self, display, touch):
self.bg_color_idx = 0

def run(self):
tools = Tools(self.display)
draw_test = DrawTest(self.display)
font_test = FontTest(self.display)
touch_test = TouchTest(self.display, self.touch)

while True:
bg_color = COMMON_COLORS[self.bg_color_idx]
print('Diagnostics menu:')
tests = [
['Free RAM', self._get_free_ram],
['Free Disk Space', lambda: self._get_free_diskspace('/')],
['Display info', lambda: print(self.display.get_info())],
['Clear display', lambda: self.display.fill(COMMON_COLORS[self.bg_color_idx])],
['Change brightness', lambda: self.display.set_brightness(input.read_int('brightness>'))],
['Clear display', lambda: self.display.fill(bg_color)],
['Cycle background color', self._cylce_bg_color],
['Draw test', lambda: draw_test.execute(COMMON_COLORS[self.bg_color_idx])],
['Font test', lambda: font_test.execute(COMMON_COLORS[self.bg_color_idx])],
['Touch test', lambda: touch_test.execute(COMMON_COLORS[self.bg_color_idx])],
['Tools', lambda: tools.execute()],
['Draw tests', lambda: draw_test.execute(bg_color)],
['Font tests', lambda: font_test.execute(bg_color)],
['Touch tests', lambda: touch_test.execute(bg_color)],
['Exit', None],
]
for idx, test in enumerate(tests):
Expand All @@ -49,14 +46,3 @@ def _cylce_bg_color(self):
if self.bg_color_idx >= len(COMMON_COLORS):
self.bg_color_idx = 0
self.display.fill(COMMON_COLORS[self.bg_color_idx])

def _get_free_ram(self):
free_space_kb = gc.mem_free() // 1024
print(f'Free RAM: {free_space_kb}kB')

def _get_free_diskspace(self, path):
fs_stats = os.statvfs(path)
f_bsize = fs_stats[0]
f_bavail = fs_stats[4]
free_space_kb = (f_bsize * f_bavail) // 1024
print(f'Free disk space: {free_space_kb}kB')
16 changes: 7 additions & 9 deletions src/diagnostics/font_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os

from drivers import color565
from extensions import input, re
from extensions import input
from fonts import FONTS
from utils import COMMON_COLORS

Expand Down Expand Up @@ -39,23 +36,24 @@ def execute(self, bg_color):

def test_normal(self, bg_color):
self.display.fill(bg_color)

x = 0
y = 0
for color_idx in range(len(COMMON_COLORS)):
color = COMMON_COLORS[color_idx]
for color in COMMON_COLORS:
self.display.text('Colored text', x, y, color, font=self.font)
y += self.font.height() + 1

def test_background(self, bg_color):
self.display.fill(bg_color)

x = 0
y = 0
for color_idx in range(len(COMMON_COLORS)):
bg_color_idx = len(COMMON_COLORS) - color_idx - 1
color_idx = len(COMMON_COLORS) - 1
for bg_color in COMMON_COLORS:
color = COMMON_COLORS[color_idx]
bg_color = COMMON_COLORS[bg_color_idx]
self.display.text('Text with background', x, y, color, bg=bg_color, font=self.font)
y += self.font.height() + 1
color_idx -= 1

def load_font(self):
print('Select font:')
Expand Down
40 changes: 40 additions & 0 deletions src/diagnostics/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import gc
import os

from extensions import input


class Tools(object):

def __init__(self, display):
self.display = display

def execute(self):
while True:
print('Tools:')
tools = [
['Show free RAM', self._get_free_ram],
['Show free disk space', lambda: self._get_free_diskspace('/')],
['Show display info', lambda: print(self.display.get_info())],
['Change brightness', lambda: self.display.set_brightness(input.read_int('brightness>'))],
['Return', None]
]
for idx, tool in enumerate(tools):
print(f'{idx}: {tool[0]}')

tool_idx = input.read_int('tool>')
if tool_idx >= len(tools) - 1:
break

tools[tool_idx][1]()

def _get_free_ram(self):
free_space_kb = gc.mem_free() // 1024
print(f'Free RAM: {free_space_kb}kB')

def _get_free_diskspace(self, path):
fs_stats = os.statvfs(path)
f_bsize = fs_stats[0]
f_bavail = fs_stats[4]
free_space_kb = (f_bsize * f_bavail) // 1024
print(f'Free disk space: {free_space_kb}kB')
2 changes: 1 addition & 1 deletion src/extensions/input_extensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def read_int(prompt):
while True:
value = input(prompt)
if value.isdigit():
if value.replace('-', '').isdigit():
return int(value)

0 comments on commit ae2b43f

Please sign in to comment.