Skip to content

Commit d90560d

Browse files
authored
Merge pull request #500 from caerffili/advanced_radials
2 parents 164bb2a + a2575ad commit d90560d

File tree

6 files changed

+390
-4
lines changed

6 files changed

+390
-4
lines changed

library/lcd/lcd_comm.py

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,29 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
445445

446446
self.DisplayPILImage(graph_image, x, y)
447447

448+
def DrawRadialDecoration(self, draw: ImageDraw, angle: float, radius: float, width: float, color: Tuple[int, int, int] = (0, 0, 0)):
449+
i_cos = math.cos(angle*math.pi/180)
450+
i_sin = math.sin(angle*math.pi/180)
451+
x_f = (i_cos * (radius - width/2)) + radius
452+
if math.modf(x_f) == 0.5:
453+
if i_cos > 0:
454+
x_f = math.floor(x_f)
455+
else:
456+
x_f = math.ceil(x_f)
457+
else:
458+
x_f = math.floor(x_f + 0.5)
459+
460+
y_f = (i_sin * (radius - width/2)) + radius
461+
if math.modf(y_f) == 0.5:
462+
if i_sin > 0:
463+
y_f = math.floor(y_f)
464+
else:
465+
y_f = math.ceil(y_f)
466+
else:
467+
y_f = math.floor(y_f + 0.5)
468+
draw.ellipse([x_f - width/2, y_f - width/2, x_f + width/2, y_f - 1 + width/2 - 1], outline=color, fill=color, width=1)
469+
470+
448471
def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int,
449472
min_value: int = 0,
450473
max_value: int = 100,
@@ -461,7 +484,12 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
461484
font_color: Tuple[int, int, int] = (0, 0, 0),
462485
bar_color: Tuple[int, int, int] = (0, 0, 0),
463486
background_color: Tuple[int, int, int] = (255, 255, 255),
464-
background_image: str = None):
487+
background_image: str = None,
488+
custom_bbox: Tuple[int, int, int, int] = (0, 0, 0, 0),
489+
text_offset: Tuple[int, int] = (0,0),
490+
bar_background_color: Tuple[int, int, int] = (0, 0, 0),
491+
draw_bar_background: bool = False,
492+
bar_decoration: str = ""):
465493
# Generate a radial progress bar and display it
466494
# Provide the background image path to display progress bar with transparent background
467495

@@ -474,6 +502,9 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
474502
if isinstance(font_color, str):
475503
font_color = tuple(map(int, font_color.split(', ')))
476504

505+
if isinstance(bar_background_color, str):
506+
bar_background_color = tuple(map(int, bar_background_color.split(', ')))
507+
477508
if angle_start % 361 == angle_end % 361:
478509
if clockwise:
479510
angle_start += 0.1
@@ -525,6 +556,23 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
525556
ecart = 360 - angle_start + angle_end
526557
else:
527558
ecart = angle_end - angle_start
559+
560+
# draw bar background
561+
if draw_bar_background:
562+
if angle_end < angle_start:
563+
angleE = angle_start + ecart
564+
angleS = angle_start
565+
else:
566+
angleS = angle_start
567+
angleE = angle_start + ecart
568+
draw.arc([0, 0, diameter - 1, diameter - 1], angleS, angleE, fill=bar_background_color, width=bar_width)
569+
570+
# draw bar decoration
571+
if bar_decoration == "Ellipse":
572+
self.DrawRadialDecoration(draw = draw, angle = angle_end, radius = radius, width = bar_width, color = bar_background_color)
573+
self.DrawRadialDecoration(draw = draw, angle = angle_start, radius = radius, width = bar_width, color = bar_color)
574+
self.DrawRadialDecoration(draw = draw, angle = angle_start + pct * ecart, radius = radius, width = bar_width, color = bar_color)
575+
528576
#
529577
# solid bar case
530578
if angle_sep == 0:
@@ -558,6 +606,25 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
558606
ecart = angle_start - angle_end
559607
else:
560608
ecart = 360 - angle_end + angle_start
609+
610+
# draw bar background
611+
if draw_bar_background:
612+
if angle_end < angle_start:
613+
angleE = angle_start
614+
angleS = angle_start - ecart
615+
else:
616+
angleS = angle_start - ecart
617+
angleE = angle_start
618+
draw.arc([0, 0, diameter - 1, diameter - 1], angleS, angleE, fill=bar_background_color, width=bar_width)
619+
620+
621+
# draw bar decoration
622+
if bar_decoration == "Ellipse":
623+
self.DrawRadialDecoration(draw = draw, angle = angle_end, radius = radius, width = bar_width, color = bar_background_color)
624+
self.DrawRadialDecoration(draw = draw, angle = angle_start, radius = radius, width = bar_width, color = bar_color)
625+
self.DrawRadialDecoration(draw = draw, angle = angle_start - pct * ecart, radius = radius, width = bar_width, color = bar_color)
626+
627+
#
561628
# solid bar case
562629
if angle_sep == 0:
563630
if angle_end < angle_start:
@@ -593,10 +660,14 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
593660
font = ImageFont.truetype("./res/fonts/" + font, font_size)
594661
left, top, right, bottom = font.getbbox(text)
595662
w, h = right - left, bottom - top
596-
draw.text((radius - w / 2, radius - top - h / 2), text,
663+
draw.text((radius - w / 2 + text_offset[0], radius - top - h / 2 + text_offset[1]), text,
597664
font=font, fill=font_color)
598665

599-
self.DisplayPILImage(bar_image, xc - radius, yc - radius)
666+
if custom_bbox[0] != 0 or custom_bbox[1] != 0 or custom_bbox[2] != 0 or custom_bbox[3] != 0:
667+
bar_image = bar_image.crop(box=custom_bbox)
668+
669+
self.DisplayPILImage(bar_image, xc - radius + custom_bbox[0], yc - radius + custom_bbox[1])
670+
# self.DisplayPILImage(bar_image, xc - radius, yc - radius)
600671

601672
# Load image from the filesystem, or get from the cache if it has already been loaded previously
602673
def open_image(self, bitmap_path: str) -> Image:

library/stats.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ def display_themed_radial_bar(theme_data, value, min_size=0, unit='', custom_tex
188188
font_size=theme_data.get("FONT_SIZE", 10),
189189
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
190190
background_color=theme_data.get("BACKGROUND_COLOR", (0, 0, 0)),
191-
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None))
191+
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None)),
192+
custom_bbox=theme_data.get("CUSTOM_BBOX", (0, 0, 0, 0)),
193+
text_offset=theme_data.get("TEXT_OFFSET", (0, 0)),
194+
bar_background_color = theme_data.get("BAR_BACKGROUND_COLOR", (0, 0, 0)),
195+
draw_bar_background = theme_data.get("DRAW_BAR_BACKGROUND", False),
196+
bar_decoration = theme_data.get("BAR_DECORATION", "")
192197
)
193198

194199

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Credits for graphical resources used from freepik
2+
3+
Acknowledgements for resources :
4+
1.35 KB
Loading
9.56 KB
Loading

0 commit comments

Comments
 (0)