Created
February 7, 2020 05:51
-
-
Save paulwinex/a65f0a36485ae1dea3de4a82f647949a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from machine import Pin, SPI | |
import st7789py as st7789 | |
from random import randrange, choice | |
def hsv_to_rgb(h, s, v): | |
if s == 0.0: | |
return v, v, v | |
i = int(h * 6.0) # XXX assume int() truncates! | |
f = (h * 6.0) - i | |
p = v * (1.0 - s) | |
q = v * (1.0 - s * f) | |
t = v * (1.0 - s * (1.0 - f)) | |
i = i % 6 | |
if i == 0: | |
return int(v * 255), int(t * 255), int(p * 255) | |
if i == 1: | |
return int(q * 255), int(v * 255), int(p * 255) | |
if i == 2: | |
return int(p * 255), int(v * 255), int(t * 255) | |
if i == 3: | |
return int(p * 255), int(q * 255), int(v * 255) | |
if i == 4: | |
return int(t * 255), int(p * 255), int(v * 255) | |
if i == 5: | |
return int(v * 255), int(p * 255), int(q * 255) | |
# Cannot get here | |
def rainbow(): | |
print('RAINBOW') | |
for i in range(240): | |
color = st7789.color565(*hsv_to_rgb(i / 240, 1.0, 1.0)) | |
tft.hline(0, i, 135, color) | |
def fill(): | |
print('FILL') | |
tft.fill_rect(0, 0, 135, 240, st7789.color565( | |
randrange(0, 255), randrange(0, 255), randrange(0, 255) | |
)) | |
# ##################################################################### | |
p1 = Pin(0, Pin.IN) | |
p2 = Pin(35, Pin.IN) | |
time.sleep(0.5) | |
tft = st7789.ST7789( | |
SPI(2, baudrate=30000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19)), | |
135, | |
240, | |
reset=Pin(23, Pin.OUT), | |
cs=Pin(5, Pin.OUT), | |
dc=Pin(16, Pin.OUT), | |
backlight=Pin(4, Pin.OUT), | |
rotation=0) | |
time.sleep(0.5) | |
fill() | |
while True: | |
if not p1.value(): | |
rainbow() | |
elif not p2.value(): | |
fill() | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment