Skip to content

Commit 8755a98

Browse files
author
Gavin Lüdemann
committed
Add example files
1 parent e9ec572 commit 8755a98

File tree

10 files changed

+257
-0
lines changed

10 files changed

+257
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
A Python Lighthouse Adapter
44

55
(README WIP; please consult pyghthouse/ph.py for usage information.)
6+
7+
Example scripts can be found [here](examples)

examples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Pyghthouse Examples
2+
3+
This is a loose collection of example scripts for pyghthouse.
4+
5+
Run any script by entering `python <filename>`.
6+
7+
If you set up config.py with your username and API token, you won't have to enter them every time you run a script.
8+
Be aware that API tokens are only valid for a few days.

examples/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##### enter your username and optionally your valid API token #####
2+
3+
UNAME = ""
4+
TOKEN = ""
5+
6+
##### leave everything below unchanged #####
7+
8+
if UNAME == "":
9+
UNAME = input("Enter your username: ")
10+
if TOKEN == "":
11+
TOKEN = input("Enter your valid API token: ")

examples/huecircle.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from pyghthouse import Pyghthouse
2+
from pyghthouse.utils._color import from_hsv
3+
from config import UNAME, TOKEN
4+
import numpy as np
5+
6+
7+
def cart2pol(x, y):
8+
rho = np.sqrt(x**2 + y**2)
9+
phi = (np.arctan2(y, x) / np.pi) / 2
10+
return rho, phi
11+
12+
13+
def circle_generator():
14+
image = np.zeros((14, 28, 3))
15+
while True:
16+
for i in range(180):
17+
for x in range(28):
18+
for y in range(14):
19+
rho, phi = cart2pol(x / 28 - 0.5, y / 14 - 0.5)
20+
image[y, x, :] = from_hsv((phi + i / 180) % 1.0, 1-rho, i / 90 if i < 91 else 1 - (i - 90) / 90)
21+
yield image
22+
23+
24+
circle = circle_generator()
25+
26+
27+
def callback():
28+
return next(circle)
29+
30+
31+
if __name__ == '__main__':
32+
p = Pyghthouse(UNAME, TOKEN, image_callback=callback)
33+
print("Starting... use CTRL+C to stop.")
34+
p.start()

examples/movingdot.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from pyghthouse import Pyghthouse, VerbosityLevel
2+
from config import UNAME, TOKEN
3+
4+
5+
def clip(val, min_val, max_val):
6+
if val < min_val:
7+
return min_val
8+
if val > max_val:
9+
return max_val
10+
return val
11+
12+
13+
def main_loop():
14+
x = 0
15+
y = 0
16+
p = Pyghthouse(UNAME, TOKEN, verbosity=VerbosityLevel.NONE)
17+
p.start()
18+
while True:
19+
img = p.empty_image()
20+
img[y][x] = [255, 255, 255]
21+
p.set_image(img)
22+
s = input()
23+
for c in s.upper():
24+
if c == 'A':
25+
x -= 1
26+
elif c == 'D':
27+
x += 1
28+
elif c == 'W':
29+
y -= 1
30+
elif c == 'S':
31+
y += 1
32+
x = clip(x, 0, 27)
33+
y = clip(y, 0, 13)
34+
35+
36+
if __name__ == '__main__':
37+
main_loop()

examples/noisefill.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
from random import random
3+
4+
from config import UNAME, TOKEN
5+
from pyghthouse import Pyghthouse
6+
7+
8+
def image_gen():
9+
image = np.zeros((14, 28, 3))
10+
yield image
11+
while True:
12+
for y in range(28):
13+
for j in range(3):
14+
image[0, y, j] = int(random() * 255)
15+
yield image
16+
image = np.roll(image, 1, 0)
17+
image *= 0.85
18+
yield image
19+
20+
21+
g = image_gen()
22+
23+
if __name__ == '__main__':
24+
p = Pyghthouse(UNAME, TOKEN, image_callback=g.__next__, frame_rate=60)
25+
print("Starting... use CTRL+C to stop.")
26+
p.start()

examples/rainbow.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pyghthouse import Pyghthouse
2+
from pyghthouse.utils._color import from_hsv
3+
from config import UNAME, TOKEN
4+
5+
6+
def rainbow_generator():
7+
while True:
8+
for i in range(180):
9+
yield [from_hsv((i / 180 + j / (14 * 28)) % 1.0, 1.0, 1.0) for j in range(14 * 28)]
10+
11+
12+
rainbow = rainbow_generator()
13+
14+
15+
def callback():
16+
return next(rainbow)
17+
18+
19+
if __name__ == '__main__':
20+
p = Pyghthouse(UNAME, TOKEN, image_callback=callback)
21+
print("Starting... use CTRL+C to stop.")
22+
p.start()
23+

examples/rgbfill.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
3+
from config import UNAME, TOKEN
4+
from pyghthouse import Pyghthouse
5+
6+
7+
def image_gen():
8+
image = np.zeros((14, 28, 3))
9+
yield image
10+
while True:
11+
for x in range(14):
12+
for y in range(28):
13+
for j in range(3):
14+
image[x, y, j] = 255
15+
yield image
16+
for y in range(28):
17+
for x in range(14):
18+
for j in range(3):
19+
image[x, y, j] = 0
20+
yield image
21+
22+
23+
g = image_gen()
24+
25+
if __name__ == '__main__':
26+
p = Pyghthouse(UNAME, TOKEN, image_callback=g.__next__, frame_rate=60)
27+
print("Starting... use CTRL+C to stop.")
28+
p.start()

examples/rgbscan.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
3+
from config import UNAME, TOKEN
4+
from pyghthouse import Pyghthouse
5+
6+
7+
def image_gen():
8+
image = np.zeros((14, 28, 3))
9+
yield image
10+
while True:
11+
for j in range(3):
12+
for x in range(14):
13+
for y in range(28):
14+
image[x, y, j] = 255
15+
yield image
16+
image[x, y, j] = 0
17+
18+
19+
g = image_gen()
20+
21+
if __name__ == '__main__':
22+
p = Pyghthouse(UNAME, TOKEN, image_callback=g.__next__, frame_rate=60)
23+
print("Starting... use CTRL+C to stop.")
24+
p.start()

examples/twopoints.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from random import random
2+
3+
import numpy as np
4+
from PIL import Image, ImageDraw
5+
6+
from pyghthouse.utils._color import from_hsv
7+
from pyghthouse import Pyghthouse
8+
9+
from config import UNAME, TOKEN
10+
11+
SPEED_FACTOR = 0.02
12+
COLOR_SPEED = 0.01
13+
14+
15+
class BouncyPoint:
16+
17+
def __init__(self):
18+
self.x = random()
19+
self.y = random()
20+
self.vx = SPEED_FACTOR * (random() + 0.5)
21+
self.vy = SPEED_FACTOR * (random() + 0.5)
22+
23+
def update(self):
24+
self.x += self.vx
25+
self.y += self.vy
26+
27+
if self.x < 0:
28+
self.vx *= -1
29+
self.x *= -1
30+
elif self.x > 1:
31+
self.vx *= -1
32+
self.x = 2 - self.x
33+
if self.y < 0:
34+
self.vy *= -1
35+
self.y *= -1
36+
elif self.y > 1:
37+
self.vy *= -1
38+
self.y = 2 - self.y
39+
40+
41+
class ImageMaker:
42+
43+
def __init__(self):
44+
self.p1 = BouncyPoint()
45+
self.p2 = BouncyPoint()
46+
self.img = Image.new('RGB', (280, 140))
47+
self.draw = ImageDraw.Draw(self.img)
48+
self.hue = 0.0
49+
50+
def callback(self):
51+
self.p1.update()
52+
self.p2.update()
53+
self.hue += COLOR_SPEED
54+
self.draw.line([(self.p1.x * 280, self.p1.y * 140), (self.p2.x * 280, self.p2.y * 140)], width=10,
55+
fill=tuple(from_hsv(self.hue, 1, 1)))
56+
output = self.img.copy()
57+
output.thumbnail((28, 14))
58+
return np.asarray(output)
59+
60+
61+
if __name__ == '__main__':
62+
i = ImageMaker()
63+
p = Pyghthouse(UNAME, TOKEN, image_callback=i.callback, frame_rate=60)
64+
p.start()

0 commit comments

Comments
 (0)