Skip to content

Commit c28a804

Browse files
committed
Add Pico support
1 parent cff9d33 commit c28a804

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

raspberry pi pico/gauge/code.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import board, busio
2+
from adafruit_st7735r import ST7735R
3+
import displayio
4+
import time
5+
import random
6+
from gauge import Gauge
7+
8+
mosi_pin = board.GP11
9+
clk_pin = board.GP10
10+
reset_pin = board.GP17
11+
cs_pin = board.GP18
12+
dc_pin = board.GP16
13+
14+
displayio.release_displays()
15+
16+
spi = busio.SPI(clock=clk_pin, MOSI=mosi_pin)
17+
18+
display_bus = displayio.FourWire(spi, command=dc_pin, chip_select=cs_pin, reset=reset_pin)
19+
20+
display = ST7735R(display_bus, width=128, height=160, bgr = 1)
21+
22+
gauge = Gauge(0,100, 64, 80, value_label="x:", arc_colour=0xFF0000, colour=0xFFFF00, outline_colour=0xFFFF00)
23+
gauge.x = 32
24+
gauge.y = 0
25+
26+
gauge2 = Gauge(0,100, 64, 80, value_label="y:", arc_colour=0xFF0000, colour=0xFFFF00, outline_colour=0xFFFF00)
27+
gauge2.x = 32
28+
gauge2.y = 81
29+
30+
group = displayio.Group(scale=1)
31+
32+
group.append(gauge)
33+
group.append(gauge2)
34+
35+
display.show(group)
36+
display.auto_refresh = True
37+
38+
x = 0
39+
y = 100
40+
41+
while True:
42+
while x < 100:
43+
x += 2
44+
y -= 2
45+
gauge.update(x)
46+
gauge2.update(y)
47+
48+
while x > 0:
49+
x -= 2
50+
y += 2
51+
gauge.update(x)
52+
gauge2.update(y)
53+
54+
while x < 100:
55+
x += 5
56+
y -= 5
57+
gauge.update(x)
58+
gauge2.update(y)
59+
60+
while x > 0:
61+
x -= 5
62+
y += 5
63+
gauge.update(x)
64+
gauge2.update(y)

raspberry pi pico/gauge/gauge.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)