Skip to content

Instantly share code, notes, and snippets.

@tef
Created August 9, 2016 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tef/7b22afad3dce80d141809fae869ec2a6 to your computer and use it in GitHub Desktop.
Save tef/7b22afad3dce80d141809fae869ec2a6 to your computer and use it in GitHub Desktop.
# tilda example
import ugfx
import buttons
import pyb
import math
from imu import IMU
ugfx.init()
buttons.init()
imu = IMU()
ugfx.backlight(50)
ugfx.enable_tear()
tear_pin = pyb.Pin("TEAR", pyb.Pin.IN)
#ugfx.set_default_font(ugfx.FONT_TITLE)
def wait_for_scanline_pass(height):
ugfx.set_tear_line(height)
while(tear_pin.value() == 0):
pass # wait for LCD to read up to line
while(tear_pin.value()):
pass # wait for LCD to read past it
def not_flipped():
ival = imu.get_acceleration()
return ival['y'] < 0
if not_flipped():
ugfx.orientation(0)
upside = False
else:
ugfx.orientation(180)
upside = True
w, h = ugfx.width(), ugfx.height()
tau = 2*math.pi
cx, cy = w//2, h//2
nlines = 4
redraw = True
points = [None]*6*nlines
oldpoints = [None] *6*nlines
angles = [(i*45)%360 for i in range(0, nlines)] # current rotation step
radius, max_radius = list(range(0, 128, 128//nlines)), 128
line_width = 9
cols = [ ugfx.RED, ugfx.BLUE, ugfx.GREEN, ugfx.WHITE ]
def hexpoints(x, y, r, angle, *, into, offset):
for n in range(offset, offset+6):
a = ((angle+n*60)%360)/360*tau
nx = x+int(math.sin(a)*2*r)
ny = y-int(math.cos(a)*2*r)
into[n] = (nx,ny)
for n in range(nlines):
hexpoints(cx, cy, radius[n], angles[n], into=oldpoints, offset=n*6)
while not buttons.is_triggered("BTN_MENU"):
# Calculate
for n in range(nlines):
radius[n] = radius[n] - 2 if radius[n] > 0 else max_radius
angles[n] = angles[n] + 3 if angles[n] < 360 else 0
hexpoints(cx, cy, radius[n], angles[n], into=points, offset=n*6)
if not_flipped():
ugfx.orientation(0)
if upside:
redraw = True
upside = False
else:
ugfx.orientation(180)
if not upside:
redraw = True
upside = True
# draw text
if redraw:
wait_for_scanline_pass(0)
print("redraw")
ugfx.clear(ugfx.BLACK)
redraw=False
for off in range(0,nlines):
for n in range(off*6,off*6+6,2):
ox1, oy1 = oldpoints[n]
ox2, oy2 = oldpoints[n+1]
x1, y1 = points[n]
x2, y2 = points[n+1]
wait_for_scanline_pass(min(oy1, oy2)-10)
ugfx.thickline(ox1, oy1, ox2, oy2, ugfx.BLACK,line_width,0)
ugfx.thickline(x1, y1, x2, y2, cols[off%4],line_width,0)
pyb.wfi()
oldpoints, points = points, oldpoints
ugfx.disable_tear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment