gxf module and examples done

pull/560/head
Gee Bartlett 2022-11-04 10:21:27 +00:00
rodzic 78101c47b5
commit eef9334805
3 zmienionych plików z 240 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,140 @@
"""
Basic Snake demo for GFX Pack
Feel free to add your own improvements :)
A = up
B = down
C = reset
D = left
E = right
"""
from picographics import PicoGraphics, DISPLAY_GFX_PACK
from gfx_pack import GfxPack
import time
import random
MOVE_UP = 0
MOVE_DOWN = 1
MOVE_LEFT = 2
MOVE_RIGHT = 3
next_move = MOVE_RIGHT
score = 0
head_possition = (30, 30)
segments = [head_possition]
ate_apple = False
apple_possition = None
display = PicoGraphics(display=DISPLAY_GFX_PACK)
display.set_backlight(1.0)
gp = GfxPack()
WIDTH, HEIGHT = display.get_bounds()
def set_new_apple():
global apple_possition
apple_possition = (random.randint(0, WIDTH), random.randint(30, HEIGHT))
def game_over():
global score, segments, head_possition, ate_apple
score = 0
head_possition = (30, 30)
segments = [head_possition]
ate_apple = False
set_new_apple()
pass
def check_button():
global next_move, ate_apple
if gp.switch_a.is_pressed:
if(next_move != MOVE_DOWN):
next_move = MOVE_UP
elif gp.switch_b.is_pressed:
if(next_move != MOVE_UP):
next_move = MOVE_DOWN
elif gp.switch_d.is_pressed:
if(next_move != MOVE_RIGHT):
next_move = MOVE_LEFT
elif gp.switch_e.is_pressed:
if(next_move != MOVE_LEFT):
next_move = MOVE_RIGHT
elif gp.switch_c.is_pressed:
game_over()
def check_eaten():
global ate_apple, head_possition, apple_possition, score
if (head_possition == apple_possition):
ate_apple = True
score += 1
set_new_apple()
def check_collision():
for index in range(len(segments) - 1):
if (head_possition == segments[index]):
game_over()
return
if (head_possition[0] >= WIDTH):
game_over()
if (head_possition[0] <= 0):
game_over()
if (head_possition[1] >= HEIGHT):
game_over()
if (head_possition[1] <= 20):
game_over()
def move():
global head_possition, segments, ate_apple
head_x, head_y = head_possition
if (next_move == MOVE_UP):
head_y -= 1
elif(next_move == MOVE_DOWN):
head_y += 1
elif(next_move == MOVE_LEFT):
head_x -= 1
elif(next_move == MOVE_RIGHT):
head_x += 1
head_possition = (head_x, head_y)
segments.append(head_possition)
if (ate_apple):
ate_apple = False
else:
segments.pop(0)
def draw():
display.set_pen(0)
display.clear()
display.set_pen(15)
display.text("score: {0}".format(score), 0, 0)
display.line(0, 20, 127, 20)
display.line(0, 63, 127, 63)
display.line(0, 63, 0, 20)
display.line(128, 63, 127, 20)
# Draw apple
display.pixel(apple_possition[0], apple_possition[1])
# Drawing snake
for segment in segments:
display.pixel(segment[0], segment[1])
display.update()
game_over()
while 1:
check_button()
check_eaten()
move()
check_collision()
draw()
time.sleep(0.2)

Wyświetl plik

@ -0,0 +1,79 @@
"""GFX temp DEMO
This demo uses a BME680 or BME688 attached to the QWST connector to measure Temperature pressure and humidity
and Display it on the GXF display
or
the internal temperature sensor can be used in place of the BME68x breakout
just change use_bme68x_breakout to False
"""
import time
from breakout_bme68x import BreakoutBME68X, STATUS_HEATER_STABLE
from pimoroni_i2c import PimoroniI2C
from picographics import PicoGraphics, DISPLAY_GFX_PACK
from gfx_pack import GfxPack
import machine
# Settings
lower_temp_bound = 15
upper_temp_bound = 30
use_bme68x_breakout = True
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535) # used for calculating a temperature from the raw sensor reading
gp = GfxPack()
display = PicoGraphics(display=DISPLAY_GFX_PACK)
display.set_backlight(0.4)
gp.rgb.set_rgb(0, 0, 0)
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
bmp = BreakoutBME68X(i2c)
# If this gives an error, try the alternative address
# bmp = BreakoutBME68X(i2c, 0x77)
display.set_pen(0)
display.clear()
display.set_font("bitmap14_outline")
while True:
# Clear display
display.set_pen(0)
display.clear()
display.set_pen(15)
display.text("GFXPack Temp demo", 0, 0, scale=0.1)
if use_bme68x_breakout:
temperature, pressure, humidity, gas, status, _, _ = bmp.read()
display.text("Temp: {:0.2f}c".format(temperature), 0, 20, scale=0.2)
display.text("Press: {:0.2f}Pa".format(pressure), 0, 35, scale=0.2)
display.text("Humid: {:0.2f}%".format(humidity), 0, 50, scale=0.2)
heater = "Stable" if status & STATUS_HEATER_STABLE else "Unstable"
print("{:0.2f}c, {:0.2f}Pa, {:0.2f}%, {:0.2f} Ohms, Heater: {}".format(
temperature, pressure, humidity, gas, heater))
else:
reading = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706) / 0.001721
display.text("Temperature", 25, 15, scale=0.2)
display.text("{:0.2f}c".format(temperature), 25, 30, scale=2)
if (temperature < lower_temp_bound):
r = 0
b = 255
elif(temperature > upper_temp_bound):
r = 255
b = 0
else:
r = ((temperature - lower_temp_bound) / (upper_temp_bound - lower_temp_bound) * 255)
b = 255 - ((temperature - lower_temp_bound) / (upper_temp_bound - lower_temp_bound) * 255)
gp.rgb.set_rgb(r, 0, b)
display.update()
time.sleep(0.2)

Wyświetl plik

@ -0,0 +1,21 @@
from pimoroni import RGBLED, Button
class GfxPack:
SW_A = 12
SW_B = 13
SW_C = 14
SW_D = 15
SW_E = 22
LED_R = 6
LED_G = 7
LED_B = 8
def __init__(self):
self.rgb = RGBLED(GfxPack.LED_R, GfxPack.LED_G, GfxPack.LED_B, invert=False)
self.switch_a = Button(GfxPack.SW_A)
self.switch_b = Button(GfxPack.SW_B)
self.switch_c = Button(GfxPack.SW_C)
self.switch_d = Button(GfxPack.SW_D)
self.switch_e = Button(GfxPack.SW_E)