forked from skickar/WiFi_Nugget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroOLEDPixelTest.py
More file actions
135 lines (127 loc) · 12 KB
/
MicroOLEDPixelTest.py
File metadata and controls
135 lines (127 loc) · 12 KB
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
# Neopixel Button & Screen Test for the Wi-Fi Nugget in Micropython
# By Kody Kinzie @Skicka
from machine import Pin, I2C
from neopixel import NeoPixel
import sh1106
import framebuf
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) # Assign GPIO pins 5 and 4 to i2c connection
display = sh1106.SH1106_I2C(128, 64, i2c, Pin(16), 0x3c, rotate=180) # Define display resolution and rotate it
display.sleep(False) # Turn on the display
display.fill(0) # Erase display
def dead(): # a very sad pic of a dead nug
fb = framebuf.FrameBuffer(bytearray(
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x0f\x1f\x7f\xff\xfe\xf8\xf0\xc0\x80\x00\x00\x00\x00\x80\xc0\xf0\xfc\xfe\xff\x3f\x1f'
b'\x07\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x0f\x1f\x7f\xff\xfe\xf8\xf0\xc0\x80\x00'
b'\x00\x00\x00\x80\xc0\xf0\xfc\xfe\xff\x3f\x1f\x07\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x07\x1f\xbf\xff\xfe\xf8\xf8\xfe\xff\x1f\x0f\x07\x01\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x07\x1f\xbf\xff\xfe\xf8\xf8\xfe\xff'
b'\x1f\x0f\x07\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x80\xe0\xf0\xfc\xfe\x7f\x1f\x0f\x03\x07\x1f\x7f\xff\xfe\xf8\xf0\xc0\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xe0\xf0\xfc\xfe\x7f\x1f\x0f\x03\x07\x1f\x7f\xff\xfe\xf8\xf0'
b'\xc0\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x38\x3c'
b'\x3f\x3f\x3f\x0f\x03\x01\x00\x00\x00\x00\x00\x00\x00\x00\x03\x0f\x1f\x3f\x3f\x3e\x3c\x30\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x70\xf8\xf8\xf8\xf8\xf8\xf8\xf8\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x20\x38\x3c\x3f\x3f\x3f\x0f\x03\x01\x00\x00\x00\x00\x00\x00\x00\x00\x03\x0f\x1f\x3f\x3f\x3e\x3c'
b'\x30\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18'
b'\x78\xf0\xc0\x80\x00\x00\x00\x00\x00\x01\x03\xff\x03\x01\x00\x00\x00\x00\x00\x80\xc0\xf0\x78\x18\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03'
b'\x07\x06\x0e\x0c\x0c\x0c\x0c\x0f\x0c\x08\x0c\x0c\x06\x06\x03\x03\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'),
124, 64, framebuf.MONO_VLSB)
display.fill(0)
display.blit(fb, 0, 0)
display.show()
def cutie(): # a very cute picture of an alive nug
fb = framebuf.FrameBuffer(bytearray(
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xe0\xe0'
b'\x70\x38\x3c\x1c\x1c\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x1c\x1c\x38\x78\xf0\xe0\xc0\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xe0\xf0\x78\x3c\x1c\x0e\x0e\x0e\x07\x07'
b'\x07\x07\x07\x07\x07\x06\x0e\x0e\x1c\x3c\x38\xf0\xe0\xc0\x80\x00\x00\x00\x00\x00\x00\x00\xf0\xfc\xff\x07\x01\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x20\xf8\xf8\xfc\xfc\xf8\x70\x00\x00\x01\x03\x1f\xfe\xf8\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\xfc\xff\x07\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x78'
b'\xfc\xfc\xfc\xfc\x78\x00\x00\x00\x01\x07\xff\xfe\xf0\x00\x00\x00\x00\x00\x0f\x7f\xff\xe0\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x01\x01\x01\x00\x00\x00\x00\x00\xc0\xf0\xff\x3f\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x70\xf8\xf8\xf8\xf8\xf8\xf8\xf8\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x1f\x7f\xf0\xc0\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x80\xc0\xf0\xff\x3f\x07\x00\x00\x00\x00\x00\x00\x00\x01\x03\x07\x0f\x1e\x3c\x38\x30\x70\x70\x60\x60\xe0\xe0\xe0\x60'
b'\x70\x70\x70\x38\x38\x1c\x0e\x0f\x07\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18'
b'\x78\xf0\xc0\x80\x00\x00\x00\x00\x00\x01\x03\xff\x03\x01\x00\x00\x00\x00\x00\x80\xc0\xf0\x78\x18\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x07\x0f\x1e\x1c\x38\x38\x38\x70\x70\x70\x70\x70\x70\x70\x30\x38\x38\x1c\x1e\x0e\x07'
b'\x03\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03'
b'\x07\x06\x0e\x0c\x0c\x0c\x0c\x0f\x0c\x08\x0c\x0c\x06\x06\x03\x03\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'),
124, 64, framebuf.MONO_VLSB)
display.fill(0)
display.blit(fb, 0, 0)
display.show()
counter = 0 # We'll prevent jitter from the buttons by keeping track of "off" signals with this variable
debounce = 10 # The limit for how many "off" signals we get before we assume the button is really off
numPixels = 1 # How many pixels are attached to the nugget? If just the built in display, put 1
pin = Pin(15, Pin.OUT) # set GPIO15 to output to drive NeoPixels
np = NeoPixel(pin, numPixels) # create NeoPixel driver on GPIO15 for all neopixels
pressed = 0 # The value we read when a button is pressed
green = Pin(0, Pin.IN, Pin.PULL_UP) # down is green
red = Pin(12, Pin.IN, Pin.PULL_UP) # up is red
blue = Pin(13, Pin.IN, Pin.PULL_UP) # left is blue
yellow = Pin(14, Pin.IN, Pin.PULL_UP) # right is yellow
def writepixel(r, g, b): # Function to write and display any value to our neopixels
for i in range(0, numPixels): # Loop to run over the length of however many neopixels we have
np[i] = (r, g, b) # Set the neopixel to the color
np.write() # Write the value of the neopixel
def neobutton(debounce, counter): # Main function to check buttons and do something when the button is pressed
if red.value() == pressed: # Check if the red button is pressed
writepixel(255, 0, 0) # Write pixels with a value of full red
dead() # Show a scary dead nugg
#print("red") # For debugging
if green.value() == pressed: # Check if the green button is pressed
writepixel(0, 255, 0) # Write pixels with a value of full green
dead()
#print("green") # For debugging
if blue.value() == pressed: # Check if the blue button is pressed
writepixel(0, 0, 255) # Write pixels with a value of full blue
dead() # Show a scary dead nugg
#print("blue") # For debugging
if yellow.value() == pressed: # Check if the yellow button is pressed
writepixel(255, 255, 0) # Write pixels with a value of full green and full red
dead() # Show a scary dead nugg
#print("yellow") # For debugging
else: # Runs if no buttons are pressed
try: counter = (int(counter) + 1) # Try statement protects if pressing yellow creates nonetype, adds 1 to count of "off" signals recieved
except TypeError: counter = 0 # Sets counter to 0 if it's been set to Nonetype
if counter > debounce: # If the value of the counter says we've recieved 10 off signals in a row, this triggers
writepixel(0, 0, 0) # Pixels are set to off
#print("off") # For Debugging
counter = 0 # Reset counter to 0 so it doesn't run forever
cutie() # Display a very cute Nugget
return counter # Returns the value of the counter so we can keep track outside the function
while True: # This part runs forever
counter = neobutton(debounce, counter) # We keep track of our counter to prevent jitter from bouncing on/off signals while reading buttons
#print(counter) For Debugging