Skip to content

Commit a7f8e00

Browse files
authored
Add files via upload
1 parent 38f4ff1 commit a7f8e00

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-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,91 @@
1+
from pyb import LED, Pin, ExtInt
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+
MAX_count = const(1000)
9+
MIN_count = const(-1000)
10+
11+
count = 0
12+
step_size = 1
13+
past_value = 1
14+
15+
16+
led = LED(1)
17+
18+
19+
TWI = I2C(scl = 'PB9', sda = 'PB8', freq = 400000)
20+
21+
22+
oled = OLED1306(TWI)
23+
oled.fill(oled.BLACK)
24+
oled.show()
25+
26+
27+
sw_pin = Pin('PA0', Pin.IN, Pin.PULL_UP)
28+
enc_a_pin = Pin('PA2', Pin.IN, Pin.PULL_NONE)
29+
enc_b_pin = Pin('PA3', Pin.IN, Pin.PULL_NONE)
30+
31+
32+
def EXTI_ENC_A(pin):
33+
global count
34+
35+
state_of_A = enc_a_pin.value()
36+
state_of_B = enc_b_pin.value()
37+
38+
if((state_of_A == True) and (state_of_B == False)):
39+
count -= step_size
40+
41+
if(count > MAX_count):
42+
count = MIN_count
43+
44+
45+
def EXTI_ENC_B(pin):
46+
global count
47+
48+
state_of_A = enc_a_pin.value()
49+
state_of_B = enc_b_pin.value()
50+
51+
if((state_of_A == False) and (state_of_B == True)):
52+
count += step_size
53+
54+
if(count < MIN_count):
55+
count = MAX_count
56+
57+
58+
EXTI_A = ExtInt(enc_a_pin,
59+
ExtInt.IRQ_RISING,
60+
Pin.PULL_NONE,
61+
EXTI_ENC_A)
62+
63+
EXTI_B = ExtInt(enc_b_pin,
64+
ExtInt.IRQ_RISING,
65+
Pin.PULL_NONE,
66+
EXTI_ENC_B)
67+
68+
69+
while(True):
70+
if(sw_pin.value() == False):
71+
led.on()
72+
sleep_ms(400)
73+
led.off()
74+
while(sw_pin.value() == False):
75+
pass
76+
step_size *= 10
77+
78+
if(step_size > 1000):
79+
step_size = 1
80+
81+
if(past_value != count):
82+
led.on()
83+
oled.fill(oled.BLACK)
84+
oled.text("AEDR8300 Encoder", 1, 2, oled.WHITE)
85+
oled.text("Value: " + str("%03u" %count), 1, 20, oled.WHITE)
86+
oled.show()
87+
past_value = count
88+
sleep_ms(100)
89+
led.off()
90+
sleep_ms(100)
91+

0 commit comments

Comments
 (0)