Skip to content

Commit 743da0d

Browse files
committed
Update snowglobe to use Gizmo Library
1 parent 4694bfd commit 743da0d

File tree

2 files changed

+29
-53
lines changed

2 files changed

+29
-53
lines changed

TFT_Gizmo_Snowglobe/snowglobe_fancy.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import board
33
import busio
44
import displayio
5-
from adafruit_st7789 import ST7789
5+
from adafruit_gizmo import tft_gizmo
66
import adafruit_imageload
77
import adafruit_lis3dh
88

@@ -24,20 +24,8 @@
2424
accelo_i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
2525
accelo = adafruit_lis3dh.LIS3DH_I2C(accelo_i2c, address=0x19)
2626

27-
# Display setup
28-
WIDTH = 240
29-
HEIGHT = 240
30-
displayio.release_displays()
31-
32-
spi = busio.SPI(board.SCL, MOSI=board.SDA)
33-
tft_cs = board.RX
34-
tft_dc = board.TX
35-
tft_backlight = board.A3
36-
37-
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
38-
39-
display = ST7789(display_bus, width=WIDTH, height=HEIGHT, rowstart=80,
40-
backlight_pin=tft_backlight, rotation=180)
27+
# Create the TFT Gizmo display
28+
display = tft_gizmo.TFT_Gizmo()
4129

4230
# Load background image
4331
try:
@@ -47,7 +35,7 @@
4735
# Or just use solid color
4836
except (OSError, TypeError):
4937
BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x000000
50-
bg_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
38+
bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
5139
bg_palette = displayio.Palette(1)
5240
bg_palette[0] = BACKGROUND
5341
background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette)
@@ -71,16 +59,16 @@
7159
height = 1,
7260
tile_width = FLAKE_WIDTH,
7361
tile_height = FLAKE_HEIGHT,
74-
x = randrange(0, WIDTH),
62+
x = randrange(0, display.width),
7563
default_tile=randrange(0, NUM_SPRITES)))
7664

7765
# Snowfield setup
78-
snow_depth = [HEIGHT] * WIDTH
66+
snow_depth = [display.height] * display.width
7967
snow_palette = displayio.Palette(2)
8068
snow_palette[0] = 0xADAF00 # transparent color
8169
snow_palette[1] = SNOW_COLOR # snow color
8270
snow_palette.make_transparent(0)
83-
snow_bitmap = displayio.Bitmap(WIDTH, HEIGHT, len(snow_palette))
71+
snow_bitmap = displayio.Bitmap(display.width, display.height, len(snow_palette))
8472
snow = displayio.TileGrid(snow_bitmap, pixel_shader=snow_palette)
8573

8674
# Add everything to display
@@ -98,13 +86,13 @@ def clear_the_snow():
9886
# set to a random sprite
9987
flake[0] = randrange(0, NUM_SPRITES)
10088
# set to a random x location
101-
flake.x = randrange(0, WIDTH)
89+
flake.x = randrange(0, display.width)
10290
# set random y locations, off screen to start
103-
flake_pos = [-1.0*randrange(0, HEIGHT) for _ in range(NUM_FLAKES)]
91+
flake_pos = [-1.0*randrange(0, display.height) for _ in range(NUM_FLAKES)]
10492
# reset snow level
105-
snow_depth = [HEIGHT] * WIDTH
93+
snow_depth = [display.height] * display.width
10694
# and snow bitmap
107-
for i in range(WIDTH*HEIGHT):
95+
for i in range(display.width*display.height):
10896
snow_bitmap[i] = 0
10997
display.auto_refresh = True
11098

@@ -117,11 +105,11 @@ def add_snow(index, amount, steepness=2):
117105
# check depth to right
118106
if snow_depth[x+1] - snow_depth[x] < steepness:
119107
add = True
120-
elif x == WIDTH - 1:
108+
elif x == display.width - 1:
121109
# check depth to left
122110
if snow_depth[x-1] - snow_depth[x] < steepness:
123111
add = True
124-
elif 0 < x < WIDTH - 1:
112+
elif 0 < x < display.width - 1:
125113
# check depth to left AND right
126114
if snow_depth[x-1] - snow_depth[x] < steepness and \
127115
snow_depth[x+1] - snow_depth[x] < steepness:
@@ -138,7 +126,7 @@ def add_snow(index, amount, steepness=2):
138126
while True:
139127
clear_the_snow()
140128
# loop until globe is full of snow
141-
while snow_depth.count(0) < WIDTH:
129+
while snow_depth.count(0) < display.width:
142130
# check for shake
143131
if accelo.shake(SHAKE_THRESHOLD, 5, 0):
144132
break
@@ -153,6 +141,6 @@ def add_snow(index, amount, steepness=2):
153141
# reset flake to top
154142
flake_pos[i] = 0
155143
# at a new x location
156-
flake.x = randrange(0, WIDTH)
144+
flake.x = randrange(0, display.width)
157145
flake.y = int(flake_pos[i])
158146
display.refresh()

TFT_Gizmo_Snowglobe/snowglobe_simple.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import board
33
import busio
44
import displayio
5-
from adafruit_st7789 import ST7789
5+
from adafruit_gizmo import tft_gizmo
66
import adafruit_imageload
77
import adafruit_lis3dh
88

@@ -17,20 +17,8 @@
1717
accelo_i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
1818
accelo = adafruit_lis3dh.LIS3DH_I2C(accelo_i2c, address=0x19)
1919

20-
# Display setup
21-
WIDTH = 240
22-
HEIGHT = 240
23-
displayio.release_displays()
24-
25-
spi = busio.SPI(board.SCL, MOSI=board.SDA)
26-
tft_cs = board.RX
27-
tft_dc = board.TX
28-
tft_backlight = board.A3
29-
30-
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
31-
32-
display = ST7789(display_bus, width=WIDTH, height=HEIGHT, rowstart=80,
33-
backlight_pin=tft_backlight, rotation=180)
20+
# Create the TFT Gizmo display
21+
display = tft_gizmo.TFT_Gizmo()
3422

3523
# Load background image
3624
try:
@@ -40,7 +28,7 @@
4028
# Or just use solid color
4129
except (OSError, TypeError):
4230
BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x000000
43-
bg_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
31+
bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
4432
bg_palette = displayio.Palette(1)
4533
bg_palette[0] = BACKGROUND
4634
background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette)
@@ -71,8 +59,8 @@
7159
tile_height = 4 ) )
7260

7361
# Snowfield setup
74-
snow_depth = [HEIGHT] * WIDTH
75-
snow_bmp = displayio.Bitmap(WIDTH, HEIGHT, len(palette))
62+
snow_depth = [display.height] * display.width
63+
snow_bmp = displayio.Bitmap(display.width, display.height, len(palette))
7664
snow = displayio.TileGrid(snow_bmp, pixel_shader=palette)
7765

7866
# Add everything to display
@@ -90,13 +78,13 @@ def clear_the_snow():
9078
# set to a random sprite
9179
flake[0] = randrange(0, 3)
9280
# set to a random x location
93-
flake.x = randrange(0, WIDTH)
81+
flake.x = randrange(0, display.width)
9482
# set random y locations, off screen to start
95-
flake_pos = [-1.0*randrange(0, HEIGHT) for _ in range(NUM_FLAKES)]
83+
flake_pos = [-1.0*randrange(0, display.height) for _ in range(NUM_FLAKES)]
9684
# reset snow level
97-
snow_depth = [HEIGHT] * WIDTH
85+
snow_depth = [display.height] * display.width
9886
# and snow bitmap
99-
for i in range(WIDTH*HEIGHT):
87+
for i in range(display.width * display.height):
10088
snow_bmp[i] = 0
10189
display.auto_refresh = True
10290

@@ -109,11 +97,11 @@ def add_snow(index, amount, steepness=2):
10997
# check depth to right
11098
if snow_depth[x+1] - snow_depth[x] < steepness:
11199
add = True
112-
elif x == WIDTH - 1:
100+
elif x == display.width - 1:
113101
# check depth to left
114102
if snow_depth[x-1] - snow_depth[x] < steepness:
115103
add = True
116-
elif 0 < x < WIDTH - 1:
104+
elif 0 < x < display.width - 1:
117105
# check depth to left AND right
118106
if snow_depth[x-1] - snow_depth[x] < steepness and \
119107
snow_depth[x+1] - snow_depth[x] < steepness:
@@ -130,7 +118,7 @@ def add_snow(index, amount, steepness=2):
130118
while True:
131119
clear_the_snow()
132120
# loop until globe is full of snow
133-
while snow_depth.count(0) < WIDTH:
121+
while snow_depth.count(0) < display.width:
134122
# check for shake
135123
if accelo.shake(SHAKE_THRESHOLD, 5, 0):
136124
break
@@ -145,6 +133,6 @@ def add_snow(index, amount, steepness=2):
145133
# reset flake to top
146134
flake_pos[i] = 0
147135
# at a new x location
148-
flake.x = randrange(0, WIDTH)
136+
flake.x = randrange(0, display.width)
149137
flake.y = int(flake_pos[i])
150138
display.refresh()

0 commit comments

Comments
 (0)