Skip to content

Commit e0baedd

Browse files
authored
Merge pull request #10 from FoamyGuy/master
adding thermometer example
2 parents 1eef8b8 + ac7d767 commit e0baedd

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

examples/gizmo_tft_termometer.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
This example will initialize the display using displayio and draw a bmp image
3+
background, and overlay text containing the value read from the on-board temperature sensor.
4+
User may press the A button to switch between celsius and fahrenheit units.
5+
6+
Required libraries:
7+
* Adafruit_CircuitPython_Gizmo
8+
* Adafruit_CircuitPython_ST7789
9+
* Adafruit_CircuitPython_Display_Text
10+
* Adafruit_CircuitPython_CircuitPlayground
11+
"""
12+
import time
13+
from adafruit_circuitplayground import cp
14+
import displayio
15+
import terminalio
16+
from adafruit_display_text import label
17+
from adafruit_gizmo import tft_gizmo
18+
display = tft_gizmo.TFT_Gizmo()
19+
20+
21+
# text scaling factor
22+
TEXT_SCALE = 2
23+
24+
# previous iteration button value
25+
old_a_val = cp.button_a
26+
27+
# boolean for current unit type
28+
show_c_units = True
29+
30+
31+
# function to convert celsius degrees to fahrenheit
32+
def c_to_f(c_val):
33+
return (c_val * 9/5) + 32
34+
35+
36+
# Open the background image file
37+
with open("/thermometer_background.bmp", "rb") as bitmap_file:
38+
39+
# Setup the file as the bitmap data source
40+
bitmap = displayio.OnDiskBitmap(bitmap_file)
41+
42+
# Create a TileGrid to hold the bitmap
43+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
44+
45+
# Create a Group to hold the TileGrid
46+
group = displayio.Group()
47+
48+
# Add the TileGrid to the Group
49+
group.append(tile_grid)
50+
51+
# variable with initial text value, temperature rounded to 2 places
52+
text = "%.2f C" % (round(cp.temperature, 2))
53+
54+
# Create a Group for the text so we can scale it
55+
text_group = displayio.Group(max_size=1, scale=TEXT_SCALE, x=0, y=0)
56+
57+
# Create a Label to show the initial temperature value
58+
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
59+
60+
# Set the anchor_point for center,top
61+
text_area.anchor_point = (0.5, 0.0)
62+
63+
# Set the location to center of display, accounting for text_scale
64+
text_area.anchored_position = (240/(2*TEXT_SCALE), 240/(2*TEXT_SCALE))
65+
66+
# Subgroup for text scaling
67+
text_group.append(text_area)
68+
69+
# Add the text_group to main Group
70+
group.append(text_group)
71+
72+
# Add the main Group to the Display
73+
display.show(group)
74+
75+
# Loop forever
76+
while True:
77+
# set current button state to variable
78+
cur_a_val = cp.button_a
79+
if cur_a_val and not old_a_val: # if the button was released
80+
print('Just released')
81+
# flip the units boolean to the opposite value
82+
show_c_units = not show_c_units
83+
84+
if show_c_units:
85+
# Update the text
86+
text_area.text = "%.2f C" % (round(cp.temperature, 2))
87+
else: # show f units
88+
# Update the text
89+
text_area.text = "%.2f F" % (round(c_to_f(cp.temperature), 2))
90+
91+
# set previous button value for next time
92+
old_a_val = cur_a_val
93+
# Wait a little bit
94+
time.sleep(0.2)

examples/thermometer_background.bmp

57.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)