1
+ #Library from https://github.com/benevpi/Circuit-Python-Gauge
2
+ #I just fixed one error and a warning
3
+
4
+ from adafruit_display_shapes .triangle import Triangle
5
+ from adafruit_display_shapes .circle import Circle
6
+ from adafruit_display_shapes .rect import Rect
7
+ from adafruit_display_text .label import Label
8
+ import terminalio
9
+ import displayio
10
+ import math
11
+
12
+ class Gauge (displayio .Group ):
13
+ def __init__ (self , min_val , max_val , width , height , base_size = 8 ,
14
+ colour = 0x0000FF , outline_colour = 0x0000FF , outline = True , bg_colour = 0000000 , display_value = True ,
15
+ value_label = "" , arc_colour = 0xFF0000 , colour_fade = False ):
16
+ super ().__init__ ()
17
+ self .pivot1 = [(width // 2 )- (base_size // 2 ), 0 ]
18
+ self .pivot2 = [(width // 2 )+ (base_size // 2 ), 0 ]
19
+ self .mid = width // 2
20
+ self .min_val = min_val
21
+ self .max_val = max_val
22
+ self .colour = colour
23
+ self .height = height
24
+ self .value_label = value_label
25
+ self .outline_colour = outline_colour
26
+ self .outline = outline
27
+
28
+ self .length = int (1.4 * (width / 2 ))
29
+ if outline : self .arrow = Triangle (self .pivot1 [0 ],self .length , self .pivot2 [0 ], self .length , self .mid ,
30
+ 0 , fill = self .colour , outline = self .outline_colour )
31
+ else : self .arrow = Triangle (self .pivot1 [0 ],self .length , self .pivot2 [0 ], self .length , self .mid ,
32
+ 0 , fill = self .colour )
33
+
34
+ self .data = Label (terminalio .FONT , text = "0.0" , color = 0xFFFFFF )
35
+ self .data .x = width // 2
36
+ self .data .y = (height - self .length )// 2
37
+ if display_value : super ().append (self .data )
38
+
39
+ arc = draw_arc (width // 2 ,self .height , self .length , 0 , width , 10 , arc_colour ,
40
+ height , colour_fade = colour_fade )
41
+ for tri in arc : super ().append (tri )
42
+ super ().append (self .arrow )
43
+
44
+ def update (self , val ):
45
+ max_angle = 135
46
+ if val < self .min_val : angle = 45
47
+ elif val > self .max_val : angle = max_angle
48
+ else :
49
+ angle = ((((val - self .min_val )/ (self .max_val - self .min_val )))*
50
+ (max_angle - 45 )+ 45 )
51
+
52
+ top_point_x = self .mid - int (math .cos (math .radians (angle ))* self .length )
53
+ top_point_y = int (math .sin (math .radians (angle ))* self .length )
54
+
55
+ if self .outline : self .arrow = Triangle (self .pivot1 [0 ],self .height , self .pivot2 [0 ], self .height , top_point_x ,
56
+ self .height - top_point_y , fill = self .colour , outline = self .outline_colour )
57
+ else : self .arrow = Triangle (self .pivot1 [0 ],self .height , self .pivot2 [0 ], self .height , top_point_x ,
58
+ self .height - top_point_y , fill = self .colour )
59
+ super ().pop ()
60
+ super ().append (self .arrow )
61
+
62
+ #need a better way of formatting this
63
+ #and maybe shifting it to the right place here.
64
+ self .data .text = self .value_label + str (int (val ))
65
+
66
+ def draw_arc (centerpoint_x , centerpoint_y , length , start_x , end_x , num_sections ,
67
+ colour , height , colour_fade = False ):
68
+ triangles = []
69
+ lastpoint = [start_x , int (math .sqrt (length * length - (centerpoint_x - start_x )* (centerpoint_x - start_x )))]
70
+ increment = (end_x - start_x ) / num_sections
71
+ counter = 0
72
+ for i in range (0 ,num_sections ):
73
+ if colour_fade : this_colour = colour [counter ]
74
+ else : this_colour = colour
75
+ next_x = start_x + (i + 1 )* increment
76
+ nextpoint = [int (next_x ), int (math .sqrt (length * length - (centerpoint_x - next_x )* (centerpoint_x - next_x )
77
+ ))]
78
+ triangles .append (Triangle (lastpoint [0 ], height - lastpoint [1 ], lastpoint [0 ],
79
+ height - lastpoint [1 ], nextpoint [0 ],height - nextpoint [1 ], outline = this_colour ))
80
+ lastpoint = nextpoint
81
+ counter = counter + 1
82
+ return triangles
0 commit comments