-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi_spigot.py
73 lines (59 loc) · 2.1 KB
/
pi_spigot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Stolen from: https://www.instructables.com/A-Spigot-That-Streams-Digits-of-Pi/
import re
import time
import argparse
import RPi.GPIO as GPIO
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.render import canvas
from luma.core.virtual import viewport
from luma.core.legacy import text, show_message
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
def pi_digits():
"Generate n digits of Pi."
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while True:
p, q, k = k * k, 2 * k + 1, k + 1
a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
d, d1 = a / b, a1 / b1
while d == d1:
yield int(d)
# n -= 1
a, a1 = 10 * (a % b), 10 * (a1 % b1)
d, d1 = a / b, a1 / b1
# for n, i in enumerate(pi_digits()):
# if n % 80 == 0:
# print()
# print(i, end="", flush=True)
# time.sleep(0.6)
nDigits = 8
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, cascaded=nDigits, block_orientation=90,
rotate=0, blocks_arranged_in_reverse_order=True)
# start message
def startMessage():
msg = "#PiDay"
show_message(device, msg, fill="white", font=proportional(CP437_FONT))
startMessage()
piGen = pi_digits()
digitWindow = " " * nDigits
nPiDigits = 0
while True:
virtual = viewport(device, width=device.width + 8, height=8)
with canvas(virtual) as draw:
for i in range(nDigits):
text(draw, (i*8, 0), digitWindow[i], fill="white", font=CP437_FONT)
if nPiDigits != 1 or nextDigit == ".":
nextDigit = "{}".format(next(piGen))
nPiDigits += 1
else:
nextDigit = "."
digitWindow = digitWindow[1:] + nextDigit
if nextDigit != ".":
x = nDigits * 8
for r in [3, 2, 1]:
with canvas(virtual) as draw:
text(draw, (8, 0), digitWindow, fill="white", font=CP437_FONT)
for j in range(0, r):
draw.rectangle((x+j, j, x+7-j, 7-j), outline = "black")
time.sleep(0.3)