-
Notifications
You must be signed in to change notification settings - Fork 1
/
combi_APD9960_oled.py
executable file
·227 lines (192 loc) · 6.26 KB
/
combi_APD9960_oled.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
simple test for apds9960 sensor in combination with ssd1306 oled.
requires:
adafruit-circuitpython-apds9960
adafruit-circuitpython-ssd1306
"""
import time
import datetime
import board
import busio
import digitalio
import adafruit_apds9960.apds9960
import adafruit_ssd1306
BitmapFont = adafruit_ssd1306.framebuf.BitmapFont()
print("combined test with apds9960 and ssd1306 oled display")
print("setup i2c & spi & digitalio")
i2c = busio.I2C(board.SCL, board.SDA)
spi = busio.SPI(board.SCLK, board.MOSI)
pin_cs = digitalio.DigitalInOut(board.P2_6)
pin_rst = digitalio.DigitalInOut(board.P1_2)
pin_dc = digitalio.DigitalInOut(board.P1_4)
print("setup APDS9960")
sensor = adafruit_apds9960.apds9960.APDS9960(i2c)
print("init SSD1306_SPI")
width = 128
height = 64
# width, height, spi, dc, reset, cs
display = adafruit_ssd1306.SSD1306_SPI(
width, height, spi, pin_dc, pin_rst, pin_cs)
# ------------------------------------------
gesture_name = {
0: "No",
1: "Up",
2: "Down",
3: "Left",
4: "Right",
}
text_positions = {
"gesture": (0, 0),
"proximity": (0, 0),
"color": (0, 0),
"red": (0, 0),
"green": (0, 0),
"blue": (0, 0),
"clear": (0, 0),
}
# ------------------------------------------
def clear_text(text, pos):
"""Clear area for text."""
x, y = pos
color = 0
width = BitmapFont.width(text)
height = BitmapFont.font_height
display.fill_rect(x, y, width, height, color)
return x + width
def display_text(text, pos):
"""Display text and return end position."""
x, y = pos
color = 1
width = BitmapFont.width(text)
display.text(text, x, y, color)
return x + width
# ------------------------------------------
def setup_sensor():
"""Prepare Sensor."""
print("setup Sensor")
sensor.enable_proximity = True
sensor.enable_gesture = True
# sensor.enable_color = True
def setup_display():
"""Prepare Display."""
print("setup Display")
display.fill(0)
display.show()
line_height = BitmapFont.font_height + 2
# gesture
temp_x = 0
temp_y = 0 * line_height
temp_x = display_text("gesture: ", (temp_x, temp_y))
text_positions["gesture"] = (temp_x, temp_y)
display_text("-", text_positions["gesture"])
# proximity
temp_x = 0
temp_y = 1 * line_height
temp_x = display_text("proximity: ", (temp_x, temp_y))
text_positions["proximity"] = (temp_x, temp_y)
display_text("-", text_positions["proximity"])
# color
# temp_x = 0
# temp_y = 2 * line_height
# temp_x = display_text("red: ", (temp_x, temp_y))
# text_positions["red"] = (temp_x, temp_y)
# temp_x = 0
# temp_y = 3 * line_height
# temp_x = display_text("green: ", (temp_x, temp_y))
# text_positions["green"] = (temp_x, temp_y)
# temp_x = 0
# temp_y = 4 * line_height
# temp_x = display_text("blue: ", (temp_x, temp_y))
# text_positions["blue"] = (temp_x, temp_y)
# temp_x = 0
# temp_y = 5 * line_height
# temp_x = display_text("clear: ", (temp_x, temp_y))
# text_positions["clear"] = (temp_x, temp_y)
display.show()
# ------------------------------------------
def update_gesture(gesture):
"""Update Gesture."""
# print("Saw gesture: {}: {}".format(gesture, gesture_name[gesture]))
clear_text("mmmmm", text_positions["gesture"])
display_text(gesture_name[gesture], text_positions["gesture"])
def update_proximity(proximity):
"""Update proximity."""
format_string = "{: >4}"
clear_text(format_string.format(proximity), text_positions["proximity"])
display_text(format_string.format(proximity), text_positions["proximity"])
def update_color(color_data):
"""Update Color."""
red, green, blue, clear = color_data
# format_string = "r: {: >4}, g: {: >4}, b: {: >4}, c: {: >4}"
# print(format_string.format(r, g, b, c))
# display_text(format_string.format(r, g, b, c), text_positions["color"])
format_string = "{: >4}"
clear_text(format_string.format(red), text_positions["red"])
display_text(format_string.format(red), text_positions["red"])
clear_text(format_string.format(green), text_positions["green"])
display_text(format_string.format(green), text_positions["green"])
clear_text(format_string.format(blue), text_positions["blue"])
display_text(format_string.format(blue), text_positions["blue"])
clear_text(format_string.format(clear), text_positions["clear"])
display_text(format_string.format(clear), text_positions["clear"])
# ------------------------------------------
def main():
"""Main."""
setup_sensor()
setup_display()
print("Main Loop")
print("-----")
gesture_last = None
proximity_last = None
color_last = (0, 0, 0, 0)
gesture = 0
proximity = None
color = (0, 0, 0, 0)
flag_mod = False
try:
while True:
# flag_mod = False
gesture = sensor.gesture()
# if gesture is not 0:
if gesture != gesture_last:
# print("\n gesture update")
gesture_last = gesture
update_gesture(gesture)
# flag_mod = True
display.show()
# proximity = sensor.proximity()
# if proximity != proximity_last:
# # print("\n proximity update")
# proximity_last = proximity
# update_proximity(proximity)
# flag_mod = True
# color = sensor.color_data
# if color != color_last:
# color_last = color
# update_color(color)
# flag_mod = True
# if flag_mod:
# display.show()
print(
"{} "
"gesture: {: >5}; "
# "proximity: {: >4}; "
# "color: {: >4} {: >4} {: >4} {: >4}; "
"".format(
datetime.datetime.now(),
gesture_name[gesture],
# proximity,
# *color
),
end="\r"
)
time.sleep(0.0001)
except KeyboardInterrupt as e:
print("\n → exit")
finally:
display.fill(0)
display.show()
if __name__ == "__main__":
main()