Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Examples, test emulator and graphics library
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Oct 10, 2014
1 parent bd7851f commit f033f42
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build/
dist/
__pycache__
ws2812.egg-info/
.DS_Store
47 changes: 47 additions & 0 deletions python/examples/clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

import UnicornHat as unicorn
from graphics import Drawing, Color
import time, signal, threading

class UnicornDrawing(Drawing):
def __init__(self):
Drawing.__init__(self,8,8)

'''
All drawing operations use the pixel method
so we override it for UnicornHat
'''
def pixel(self, x, y, col):
if x < 0 or x > 7 or y < 0 or y > 7:
return False
self.buffer[(x,y)] = col
unicorn.set_pixel(x, y, col.r, col.g, col.b)

def show(self):
unicorn.show()

d = UnicornDrawing()

O_X = 3
O_Y = 3
R = 3

def tick():
currenthour = time.localtime().tm_hour
currentmin = time.localtime().tm_min
currentsec = time.localtime().tm_sec

d.clear()

d.circle(O_X,O_Y,R,Color(255,0,255))

d.circle_line(O_X,O_Y,R-1,360.0*(currenthour/60.0),Color(255,0,0))
d.circle_line(O_X,O_Y,R-1,360.0*(currentmin/60.0), Color(0,255,0))
d.circle_line(O_X,O_Y,R-1,360.0*(currentsec/60.0), Color(0,0,255))

d.show()

threading.Timer(1,tick).start()

tick()
158 changes: 158 additions & 0 deletions python/examples/graphics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
'''
This file is used in UnicornHat examples
for drawing things.
'''
import colorsys, math

class Color():
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b

def __str__(self):
r = hex(self.r)[2:]
g = hex(self.g)[2:]
b = hex(self.b)[2:]
return ''.join([r,g,b])

def rgb(self):
return (self.r, self.g, self.b)

def hsv(self):
return colorsys.rgb_to_hsv(self.r, self.g, self.b)

class Drawing():
def __init__(self,width=8,height=8):
self.width = width
self.height = height
self.buffer = {}
for x in range(width):
for y in range(height):
self.buffer[(x,y)] = Color(0,0,0)

def fill(self, col = Color(0,0,0)):
for x in range(self.width):
for y in range(self.height):
self.pixel(x, y, col)

def clear(self):
self.fill(Color(0,0,0))

'''
Creates a color tuple from r, g, b elements
with values 0 to 255
'''
def color_from_rgb(self, r, g, b):
return (r, g, b)

'''
Creates a color tuple from h, s, v elements
with values 0.0 to 1.0
'''
def color_from_hsv(self, h, s, v):
rgb = colorsys.hsv_to_rgb(h, s, v)
return color_from_rgb( int(rgb[0]*255), int(rgb[1]*255), int(rgb[2]*255) )

def pixel(self, x, y, col):
self.buffer[(x,y)] = col

def circle(self, x0, y0, r, col=Color(0,0,0), fill=None):
f = 1 - r
ddf_x = 1
ddf_y = -2 * r
x = 0
y = r
self.pixel(x0, y0 + r, col)
self.pixel(x0, y0 - r, col)
self.pixel(x0 + r, y0, col)
self.pixel(x0 - r, y0, col)

while x < y:
if f >= 0:
y -= 1
ddf_y += 2
f += ddf_y
x += 1
ddf_x += 2
f += ddf_x
self.pixel(x0 + x, y0 + y, col)
self.pixel(x0 - x, y0 + y, col)
self.pixel(x0 + x, y0 - y, col)
self.pixel(x0 - x, y0 - y, col)
self.pixel(x0 + y, y0 + x, col)
self.pixel(x0 - y, y0 + x, col)
self.pixel(x0 + y, y0 - x, col)
self.pixel(x0 - y, y0 - x, col)

def circle_line(self, origin_x, origin_y, radius, angle, col):
angle = (angle / 360.0) * (2*math.pi)

x = origin_x + radius * math.sin(angle)
y = origin_y + radius * math.cos(angle)

self.line( origin_x, origin_y, int(round(x)), int(round(y)), col )

def line(self, x0, y0, x1, y1, col=Color(0,0,0)):
s = abs(y1 - y0) > abs(x1 - x0)
if s:
x0, y0 = y0, x0
x1, y1 = y1, x1

if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0

dx = x1 - x0
dy = abs(y1 - y0)

err = dx / 2

ystep = 0

if y0 < y1:
ystep = 1
else:
ystep = -1

while x0<=x1:
if s:
self.pixel(y0, x0, col)
else:
self.pixel(x0, y0, col)
err -= dy

if err < 0:
y0 += ystep
err += dx

x0+=1

def test(self):
out = ''
for y in range(self.height):
for x in range(self.width):
if self.buffer[(x,y)].rgb() == (0,0,0):
out += ' '
else:
out += '##'
out += '\n'
print(out)

if __name__ == '__main__':
print("Testing color")
print(Color(255,50,20))
g = Drawing()
g.test()
print("Testing circle")
g.circle(4,4,3,Color(255,255,255))
g.test()
g.clear()
print("Testing line")
g.line(0,0,7,7,Color(255,255,255))
g.test()
g.clear()
print("Testing circle line")
g.circle_line(4,4,3,0,Color(255,255,255))
g.test()

16 changes: 16 additions & 0 deletions python/examples/rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python

import UnicornHat as unicorn
import time, colorsys

while True:
for z in range(360):
for y in range(8):
for x in range(8):
rgb = colorsys.hsv_to_rgb(z/360.0,y/7.0,x/7.0)
r = int(rgb[0]*255.0)
g = int(rgb[1]*255.0)
b = int(rgb[2]*255.0)
unicorn.set_pixel(x,y,r,g,b)
unicorn.show()
time.sleep(0.1)
2 changes: 1 addition & 1 deletion python/examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

for y in range(8):
for x in range(8):
UH.set_pixel(x,y,50,0,50)
UH.set_pixel(x,y,255,0,255)
UH.show()
time.sleep(0.05)

Expand Down
Empty file added python/test/CHANGELOG
Empty file.
5 changes: 5 additions & 0 deletions python/test/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include README
include CHANGELOG
include LICENSE.txt
include UnicornHat.py
include setup.py
Empty file added python/test/README
Empty file.
Empty file added python/test/README.md
Empty file.
48 changes: 48 additions & 0 deletions python/test/UnicornHat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import socket, atexit, time

sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print('Unicorn Started')

connected = False

port = 7676

pixels = ['000000'] * 64

while not connected:
try:
time.sleep(0.5)
print('Trying port',port)
sck.connect(('127.0.0.1',port))
connected = True
print('Connected',port)
except:
port += 1

def close():
send('stop')
sck.shutdown(socket.SHUT_RDWR)
sck.close()

atexit.register(close)

def send(cmd):
print(cmd);
sck.send(cmd + '\n')

def clear():
send('clear')

def set_pixel(x, y, r, g, b):

r = hex(r)[2:].zfill(2)
g = hex(g)[2:].zfill(2)
b = hex(b)[2:].zfill(2)

pixels[(x*8) + y] = ''.join([r,g,b])
#send( ','.join([str(x),str(y),str(r),str(g),str(b)]) )

def show():
#send('show')
send(','.join(pixels))
Loading

0 comments on commit f033f42

Please sign in to comment.