forked from FAR-Lab/Interactive-Lab-Hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oled_test.py
87 lines (75 loc) · 2.43 KB
/
oled_test.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
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import busio
import adafruit_ssd1306
# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height. Change these
# to the right size for your display!
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
# Helper function to draw a circle from a given position with a given radius
# This is an implementation of the midpoint circle algorithm,
# see https://en.wikipedia.org/wiki/Midpoint_circle_algorithm#C_example for details
def draw_circle(xpos0, ypos0, rad, col=1):
x = rad - 1
y = 0
dx = 1
dy = 1
err = dx - (rad << 1)
while x >= y:
oled.pixel(xpos0 + x, ypos0 + y, col)
oled.pixel(xpos0 + y, ypos0 + x, col)
oled.pixel(xpos0 - y, ypos0 + x, col)
oled.pixel(xpos0 - x, ypos0 + y, col)
oled.pixel(xpos0 - x, ypos0 - y, col)
oled.pixel(xpos0 - y, ypos0 - x, col)
oled.pixel(xpos0 + y, ypos0 - x, col)
oled.pixel(xpos0 + x, ypos0 - y, col)
if err <= 0:
y += 1
err += dy
dy += 2
if err > 0:
x -= 1
dx += 2
err += dx - (rad << 1)
# initial center of the circle
center_x = 63
center_y = 15
# how fast does it move in each direction
x_inc = 1
y_inc = 1
# what is the starting radius of the circle
radius = 8
# start with a blank screen
oled.fill(0)
# we just blanked the framebuffer. to push the framebuffer onto the display, we call show()
oled.show()
while True:
# undraw the previous circle
draw_circle(center_x, center_y, radius, col=0)
# if bouncing off right
if center_x + radius >= oled.width:
# start moving to the left
x_inc = -1
# if bouncing off left
elif center_x - radius < 0:
# start moving to the right
x_inc = 1
# if bouncing off top
if center_y + radius >= oled.height:
# start moving down
y_inc = -1
# if bouncing off bottom
elif center_y - radius < 0:
# start moving up
y_inc = 1
# go more in the current direction
center_x += x_inc
center_y += y_inc
# draw the new circle
draw_circle(center_x, center_y, radius)
# show all the changes we just made
oled.show()