Skip to content

Commit d181870

Browse files
authored
Add files via upload
1 parent a7f8e00 commit d181870

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from micropython import const
2+
from utime import sleep_ms
3+
import framebuf
4+
5+
6+
disp_width = const(128)
7+
disp_height = const(64)
8+
disp_pages = const((disp_height >> 3))
9+
10+
# Constants
11+
SSD1306_I2C_address = const(0x3C)
12+
13+
SSD1306_SET_CONTRAST = const(0x81)
14+
SSD1306_DISPLAY_ALL_ON_RESUME = const(0xA4)
15+
SSD1306_DISPLAY_ALL_ON = const(0xA5)
16+
SSD1306_NORMAL_DISPLAY = const(0xA6)
17+
SSD1306_INVERT_DISPLAY = const(0xA7)
18+
SSD1306_DISPLAY_OFF = const(0xAE)
19+
SSD1306_DISPLAY_ON = const(0xAF)
20+
SSD1306_SET_DISPLAY_OFFSET = const(0xD3)
21+
SSD1306_SET_COM_PINS = const(0xDA)
22+
SSD1306_SET_VCOM_DETECT = const(0xDB)
23+
SSD1306_SET_DISPLAY_CLOCK_DIV = const(0xD5)
24+
SSD1306_SET_PRECHARGE = const(0xD9)
25+
SSD1306_SET_MULTIPLEX = const(0xA8)
26+
SSD1306_SET_LOW_COLUMN = const(0x00)
27+
SSD1306_SET_HIGH_COLUMN = const(0x10)
28+
SSD1306_SET_START_LINE = const(0x40)
29+
SSD1306_MEMORY_MODE = const(0x20)
30+
SSD1306_COLUMN_ADDR = const(0x21)
31+
SSD1306_PAGE_ADDR = const(0x22)
32+
SSD1306_COM_SCAN_INC = const(0xC0)
33+
SSD1306_COM_SCAN_DEC = const(0xC8)
34+
SSD1306_SEG_REMAP = const(0xA0)
35+
SSD1306_CHARGE_PUMP = const(0x8D)
36+
SSD1306_EXTERNAL_VCC = const(0x01)
37+
SSD1306_SWITCH_CAP_VCC = const(0x02)
38+
39+
# Scrolling constants
40+
SSD1306_ACTIVATE_SCROLL = const(0x2F)
41+
SSD1306_DEACTIVATE_SCROLL = const(0x2E)
42+
SSD1306_SET_VERTICAL_SCROLL_AREA = const(0xA3)
43+
SSD1306_RIGHT_HORIZONTAL_SCROLL = const(0x26)
44+
SSD1306_LEFT_HORIZONTAL_SCROLL = const(0x27)
45+
SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = const(0x29)
46+
SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = const(0x2A)
47+
48+
49+
class OLED1306(framebuf.FrameBuffer):
50+
51+
def __init__(self, _i2c, _i2c_addr = SSD1306_I2C_address):
52+
self.width = disp_width
53+
self.height = disp_height
54+
self._pages = disp_pages
55+
56+
self.WHITE = 1
57+
self.BLACK = 0
58+
59+
self.i2c = _i2c
60+
self.i2c_addr = _i2c_addr
61+
62+
self.temp = bytearray(2)
63+
self.write_list = [b"\x40", None]
64+
65+
self.buffer = bytearray(self._pages * self.width)
66+
super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
67+
self.init_display()
68+
69+
70+
def write_command(self, cmd):
71+
self.temp[0] = 0x80
72+
self.temp[1] = cmd
73+
self.i2c.writeto(self.i2c_addr, self.temp)
74+
75+
76+
def write_data(self, value):
77+
self.write_list[1] = value
78+
self.i2c.writevto(self.i2c_addr, self.write_list)
79+
80+
81+
def init_display(self):
82+
self.write_command(SSD1306_DISPLAY_OFF)
83+
self.write_command(SSD1306_SET_DISPLAY_CLOCK_DIV)
84+
self.write_command(0x80)
85+
86+
self.write_command(SSD1306_SET_MULTIPLEX)
87+
self.write_command((self.height - 1))
88+
89+
self.write_command(SSD1306_SET_DISPLAY_OFFSET)
90+
self.write_command(0x00)
91+
92+
self.write_command((SSD1306_SET_START_LINE | 0x00))
93+
94+
self.write_command(SSD1306_CHARGE_PUMP)
95+
self.write_command(0x14) # 0x10 # 0x14
96+
97+
self.write_command(SSD1306_MEMORY_MODE)
98+
self.write_command(0x00)
99+
100+
self.write_command((SSD1306_SEG_REMAP | 0x01))
101+
self.write_command(SSD1306_COM_SCAN_DEC)
102+
self.write_command(SSD1306_SET_COM_PINS)
103+
self.write_command(0x12)
104+
105+
self.write_command(SSD1306_SET_CONTRAST)
106+
self.write_command(0xCF) # 0x9F # 0xCF
107+
108+
self.write_command(SSD1306_SET_PRECHARGE)
109+
self.write_command(0xF1) # 0x22 # 0xF1
110+
111+
self.write_command(SSD1306_SET_VCOM_DETECT)
112+
self.write_command(0x40)
113+
114+
self.write_command(SSD1306_DISPLAY_ALL_ON_RESUME)
115+
self.write_command(SSD1306_NORMAL_DISPLAY)
116+
self.write_command(SSD1306_DISPLAY_ON)
117+
118+
119+
def show(self):
120+
x0 = 0
121+
x1 = (self.width - 1)
122+
if (self.width == 64):
123+
# displays with width of 64 pixels are shifted by 32
124+
x0 += 32
125+
x1 += 32
126+
127+
self.write_command(SSD1306_COLUMN_ADDR)
128+
self.write_command(x0)
129+
self.write_command(x1)
130+
self.write_command(SSD1306_PAGE_ADDR)
131+
self.write_command(0x00)
132+
self.write_command((self._pages - 1))
133+
self.write_data(self.buffer)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from pyb import LED, Pin, Timer
2+
from machine import I2C
3+
from micropython import const
4+
from SSD1306_I2C import OLED1306
5+
from utime import sleep_ms
6+
7+
8+
STEPS = const(1000)
9+
10+
count = 0
11+
step_size = 1
12+
past_value = 1
13+
14+
15+
led = LED(1)
16+
17+
18+
TWI = I2C(scl = 'PB9', sda = 'PB8', freq = 400000)
19+
20+
21+
oled = OLED1306(TWI)
22+
oled.fill(oled.BLACK)
23+
oled.show()
24+
25+
26+
args = {'pull': Pin.PULL_UP, 'af': Pin.AF2_TIM5}
27+
enc_a_pin = Pin('PA0', mode = Pin.AF_OD, **args)
28+
enc_b_pin = Pin('PA1', mode = Pin.AF_OD, **args)
29+
30+
31+
timer = Timer(5, prescaler = 2, period = ((STEPS << 1) - 1))
32+
channel = timer.channel(1, Timer.ENC_AB)
33+
timer.counter(0)
34+
35+
36+
def write_text(text, x, y, size, color):
37+
background = oled.pixel(x, y)
38+
info = []
39+
40+
oled.text(text, x, y, color)
41+
for i in range(x, x + (8 * len(text))):
42+
for j in range(y, y + 8):
43+
px_color = oled.pixel(i, j)
44+
info.append((i, j, px_color)) if px_color == color else None
45+
46+
oled.text(text, x, y, background)
47+
48+
for px_info in info:
49+
oled.fill_rect(size*px_info[0] - (size-1) * x , size * px_info[1] - (size-1) * y, size, size, px_info[2])
50+
51+
52+
while(True):
53+
count = (timer.counter() >> 1)
54+
55+
if(past_value != count):
56+
led.on()
57+
oled.fill(oled.BLACK)
58+
oled.text("Rotary Encoder", 8, 2, oled.WHITE)
59+
write_text(str("%03u" %count), 15, 20, 4, oled.WHITE)
60+
oled.show()
61+
past_value = count
62+
sleep_ms(100)
63+
led.off()
64+
sleep_ms(100)
65+

0 commit comments

Comments
 (0)