Skip to content

Commit 74fde48

Browse files
committed
Fix progress bar outline, add some assert
1 parent 055268d commit 74fde48

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

main.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,17 @@ def ScreenOn(ser: serial.Serial):
5757

5858
def SetBrightness(ser: serial.Serial, level: int):
5959
# Level : 0 (brightest) - 255 (darkest)
60+
assert 255 >= level >= 0, 'Brightness level must be [0-255]'
6061
SendReg(ser, Command.SET_BRIGHTNESS, level, 0, 0, 0)
6162

6263

6364
def DisplayPILImage(ser: serial.Serial, image: Image, x: int, y: int):
6465
image_height = image.size[1]
6566
image_width = image.size[0]
6667

68+
assert image_height > 0, 'Image width must be > 0'
69+
assert image_width > 0, 'Image height must be > 0'
70+
6771
SendReg(ser, Command.DISPLAY_BITMAP, x, y, x + image_width - 1, y + image_height - 1)
6872

6973
pix = image.load()
@@ -78,7 +82,7 @@ def DisplayPILImage(ser: serial.Serial, image: Image, x: int, y: int):
7882
line += struct.pack('H', rgb)
7983

8084
# Send image data by multiple of DISPLAY_WIDTH bytes
81-
if len(line) >= DISPLAY_WIDTH * 4:
85+
if len(line) >= DISPLAY_WIDTH * 8:
8286
ser.write(line)
8387
line = bytes()
8488

@@ -103,6 +107,9 @@ def DisplayText(ser: serial.Serial, text: str, x=0, y=0,
103107
# Convert text to bitmap using PIL and display it
104108
# Provide the background image path to display text with transparent background
105109

110+
assert len(text) > 0, 'Text must not be empty'
111+
assert font_size > 0, "Font size must be > 0"
112+
106113
if background_image is None:
107114
# A text bitmap is created with max width/height by default : text with solid background
108115
text_image = Image.new('RGB', (DISPLAY_WIDTH, DISPLAY_HEIGHT), background_color)
@@ -148,11 +155,11 @@ def DisplayProgressBar(ser: serial.Serial, x: int, y: int, width: int, height: i
148155
# Draw progress bar
149156
bar_filled_width = value / (max_value - min_value) * width
150157
draw = ImageDraw.Draw(bar_image)
151-
draw.rectangle([0, 0, bar_filled_width, height], fill=bar_color, outline=bar_color)
158+
draw.rectangle([0, 0, bar_filled_width-1, height-1], fill=bar_color, outline=bar_color)
152159

153-
# Draw outline
154160
if bar_outline:
155-
draw.rectangle([0, 0, width, height], fill=None, outline=bar_color)
161+
# Draw outline
162+
draw.rectangle([0, 0, width-1, height-1], fill=None, outline=bar_color)
156163

157164
DisplayPILImage(ser, bar_image, x, y)
158165

@@ -209,7 +216,7 @@ def sighandler(signum, frame):
209216
font_color=(255, 255, 255),
210217
background_image="res/example.png")
211218

212-
# Display the current time and a progress bar as fast as possible
219+
# Display the current time and some progress bars as fast as possible
213220
bar_value = 0
214221
while not stop:
215222
DisplayText(lcd_comm, str(datetime.now().time()), 160, 2,
@@ -218,12 +225,18 @@ def sighandler(signum, frame):
218225
font_color=(255, 0, 0),
219226
background_image="res/example.png")
220227

221-
DisplayProgressBar(lcd_comm, 0, 100,
222-
width=DISPLAY_WIDTH, height=50,
228+
DisplayProgressBar(lcd_comm, 10, 40,
229+
width=140, height=30,
223230
min_value=0, max_value=100, value=bar_value,
224231
bar_color=(255, 255, 0), bar_outline=True,
225232
background_image="res/example.png")
226233

227-
bar_value = (bar_value + 1) % 100
234+
DisplayProgressBar(lcd_comm, 160, 40,
235+
width=140, height=30,
236+
min_value=0, max_value=19, value=bar_value % 20,
237+
bar_color=(0, 255, 0), bar_outline=False,
238+
background_image="res/example.png")
239+
240+
bar_value = (bar_value + 2) % 101
228241

229242
lcd_comm.close()

0 commit comments

Comments
 (0)