Skip to content

Commit 38f4ff1

Browse files
authored
Add files via upload
1 parent 0afaf28 commit 38f4ff1

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
Lines changed: 133 additions & 0 deletions
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)

125kHz RFID Reader (EXTI)/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from pyb import LED, Pin, ExtInt
2+
from machine import I2C
3+
from SSD1306_I2C import OLED1306
4+
from utime import sleep_ms
5+
6+
7+
raw_card_data = 0
8+
count = 30
9+
10+
11+
led = LED(1)
12+
13+
14+
TWI = I2C(scl = 'PB9', sda = 'PB8', freq = 400000)
15+
16+
17+
oled = OLED1306(TWI)
18+
oled.fill(oled.BLACK)
19+
oled.show()
20+
21+
22+
def D0_EXTI_handler(pin):
23+
global raw_card_data, count
24+
25+
led.off()
26+
raw_card_data <<= 1
27+
count += 1
28+
29+
30+
ext_d0 = ExtInt(Pin('PA0'), ExtInt.IRQ_FALLING, Pin.PULL_UP, callback = D0_EXTI_handler)
31+
32+
33+
def D1_EXTI_handler(pin):
34+
global raw_card_data, count
35+
36+
led.on()
37+
raw_card_data <<= 1
38+
raw_card_data |= 1
39+
count += 1
40+
41+
42+
ext_d1 = ExtInt(Pin('PA1'), ExtInt.IRQ_FALLING, Pin.PULL_UP, callback = D1_EXTI_handler)
43+
44+
45+
while(True):
46+
if(count >= 25):
47+
led.on()
48+
card_number = (raw_card_data & 0xFFFF)
49+
facility_code = (0xFF & (raw_card_data >> 0x10))
50+
oled.fill(oled.BLACK)
51+
oled.text("PYB RFID Reader", 1, 4, oled.WHITE)
52+
oled.text("Facility Code:", 1, 20, oled.WHITE)
53+
oled.text(str("%04u" %facility_code), 1, 30, oled.WHITE)
54+
oled.text("Serial Number:", 1, 40, oled.WHITE)
55+
oled.text(str("%04u" %card_number), 1, 50, oled.WHITE)
56+
oled.show()
57+
raw_card_data = 0
58+
count = 0
59+
sleep_ms(400)
60+
led.off()
61+
62+

0 commit comments

Comments
 (0)