-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
158 lines (135 loc) · 5.34 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# python3: CircuitPython 3.0
# Author: Gregory P. Smith (@gpshead) <[email protected]>
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import board
import digitalio
import random
import time
#from third_party.waveshare import color_epd2in13 as connected_epd
from third_party.waveshare import epd2in7 as connected_epd
#from third_party.waveshare import epd2in9 as connected_epd
#from third_party.waveshare import epd2in13 as connected_epd
try:
# Optimized version - requires a custom CircuitPython build.
from asm_thumb import fractal
from asm_thumb import monobitmap
HAVE_ASM = True
except (ImportError, SyntaxError):
import fractal
import monobitmap
HAVE_ASM = False
def sample_keys():
"""Yields four bools representing the button press state on the 2.7" EPD.
The 2.7 inch EPD rpi hat has four push buttons connected to RPI hat pins.
Follow the traces to see which pins those are and wire them up to digital
inputs D2 through D5 on your CircuitPython device. A button press grounds
the pin.
This function samples them all and yields four bools for each button.
"""
DigitalInOut = digitalio.DigitalInOut
Pull = digitalio.Pull
with DigitalInOut(board.D2) as key1, DigitalInOut(board.D3) as key2, \
DigitalInOut(board.D4) as key3, DigitalInOut(board.D5) as key4:
key1.switch_to_input(Pull.UP)
key2.switch_to_input(Pull.UP)
key3.switch_to_input(Pull.UP)
key4.switch_to_input(Pull.UP)
for k in (key1, key2, key3, key4):
yield not k.value # False is pressed
class StatusLED:
"""A simple interface to the onboard pixel."""
def __init__(self):
if hasattr(board, 'NEOPIXEL'):
import neopixel
self._led = neopixel.NeoPixel(board.NEOPIXEL, 1)
self._led.brightness = 1/16
elif hasattr(board, 'APA102_MOSI'):
import adafruit_dotstar
self._led = adafruit_dotstar.DotStar(
board.APA102_SCK, board.APA102_MOSI, 1)
self._led.brightness = 0.7
else:
self._led = [None]
def off(self):
self._led[0] = b'\0\0\0'
def busy(self):
self._led[0] = b'\x50\x10\0'
def ready(self):
self._led[0] = b'\x10\x50\0' if HAVE_ASM else b'\x10\0\x70'
def main():
led = StatusLED()
led.busy()
epd = connected_epd.EPD()
print("Initializing display...")
epd.init()
if HAVE_ASM:
print("@micropython.asm_thumb implementation loaded.")
else:
print("Pure Python implementation loaded.")
keys = [1,] # Print message on start.
while True:
if any(keys):
led.ready()
print("Awaiting key1-key4 button press.")
keys = list(sample_keys())
if any(keys):
led.busy()
if keys[0]:
print("Computing Mandlebrot fractal.")
fractal_image = fractal.get_fractal(epd.width, epd.height,
use_julia=False)
print("Displaying.")
if getattr(epd, 'colors', 2) > 2:
epd.display_frames(None, fractal_image.bit_buf)
else:
epd.display_bitmap(fractal_image, fast_ghosting=True)
del fractal_image
elif keys[1]:
print("Setting display to white.")
epd.clear_frame_memory(0xff)
epd.display_frame()
elif keys[2]:
raw_framebuf = bytearray(epd.fb_bytes)
for pos in range(len(raw_framebuf)):
raw_framebuf[pos] = random.randint(0, 255)
print("Displaying random framebuf.")
if getattr(epd, 'colors', 2) <= 2:
epd.display_frame_buf(raw_framebuf, fast_ghosting=True)
else:
raw_framebuf2 = bytearray(epd.fb_bytes)
for pos in range(len(raw_framebuf2)):
raw_framebuf2[pos] = random.randint(0, 255)
epd.display_frames(raw_framebuf, raw_framebuf2)
del raw_framebuf2
del raw_framebuf
elif keys[3]:
print("Computing Julia fractal.")
fractal_image = fractal.get_fractal(epd.width, epd.height,
use_julia=True)
print("Displaying.")
if getattr(epd, 'colors', 2) > 2:
epd.display_frames(fractal_image.bit_buf, None)
else:
epd.display_bitmap(fractal_image, fast_ghosting=True)
del fractal_image
else:
# This effectively debounces the keys, we merely sample them
# rather than treat a state change as an interrupt. If a key
# is not pressed for this duration it may not be noticed.
time.sleep(0.05) # short time between polling.
print("Done.")
if __name__ == '__main__':
main()