Skip to content

Commit 38e621f

Browse files
authored
Add files via upload
1 parent 29cb9a4 commit 38e621f

File tree

6 files changed

+505
-0
lines changed

6 files changed

+505
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from micropython import const
2+
import framebuf
3+
4+
5+
SH1107_I2C_address = const(0x3C)
6+
7+
8+
SH1107_SET_LOWER_COLUMN_ADDRESS = const(0x00)
9+
SH1107_SET_UPPER_COLUMN_ADDRESS = const(0x10)
10+
SH1107_SET_PAGE_MEMORY_ADDRESSING_MODE = const(0x20)
11+
SH1107_SET_VERTICAL_MEMORY_ADDRESSING_MODE = const(0x21)
12+
SH1107_SET_CONSTRAST_CONTROL = const(0x81)
13+
SH1107_SET_DC_DC_OFF_MODE = const(0x8A)
14+
SH1107_SET_DC_DC_ON_MODE = const(0x8B)
15+
SH1107_SET_SEGMENT_REMAP_NORMAL = const(0xA0)
16+
SH1107_SET_SEGMENT_REMAP_REVERSE = const(0xA1)
17+
SH1107_SET_ENTIRE_DISPLAY_OFF = const(0xA4)
18+
SH1107_SET_ENTIRE_DISPLAY_ON = const(0xA5)
19+
SH1107_SET_NORMAL_DISPLAY = const(0xA6)
20+
SH1107_SET_REVERSE_DISPLAY = const(0xA7)
21+
SH1107_SET_MULTIPLEX_RATIO = const(0xA8)
22+
SH1107_SET_DC_DC_CONTROL_MODE = const(0xAD)
23+
SH1107_DISPLAY_OFF = const(0xAE)
24+
SH1107_DISPLAY_ON = const(0xAF)
25+
SH1107_SET_PAGE_ADDRESS = const(0xB0)
26+
SH1107_SET_COMMON_OUTPUT_SCAN_DIRECTION = const(0xC0)
27+
SH1107_SET_DISPLAY_OFFSET = const(0xD3)
28+
SH1107_SET_DISPLAY_CLOCK_FREQUENCY = const(0xD5)
29+
SH1107_SET_PRECHARGE_DISCHARGE_PERIOD = const(0xD9)
30+
SH1107_SET_VCOM_DESELECT_LEVEL = const(0xDB)
31+
SH1107_SET_DISPLAY_START_LINE = const(0xDC)
32+
33+
34+
disp_width = const(128)
35+
disp_height = const(64)
36+
disp_pages = const((disp_height >> 3))
37+
38+
39+
class OLED(framebuf.FrameBuffer):
40+
41+
def __init__(self, _i2c, _i2c_address = SH1107_I2C_address):
42+
self.width = disp_width
43+
self.height = disp_height
44+
self.pages = disp_pages
45+
46+
self.line_bytes = (self.width >> 3)
47+
size = (self.width * self.pages)
48+
self.curr_buffer = bytearray(b'\x00' * size)
49+
self.prev_buffer = bytearray(b'\xFF' * size)
50+
51+
self.write_list = [b"\x40", None]
52+
self.temp = bytearray(0x02)
53+
54+
self.WHITE = 1
55+
self.BLACK = 0
56+
57+
self.i2c = _i2c
58+
self.i2c_addr = _i2c_address
59+
60+
super().__init__(self.curr_buffer, self.width, self.height, framebuf.MONO_HMSB)
61+
self.init_display()
62+
63+
64+
def write_command(self, cmd):
65+
self.temp[0] = 0x80
66+
self.temp[1] = cmd
67+
self.i2c.writeto(self.i2c_addr, self.temp)
68+
69+
70+
def write_data(self, data_buffer):
71+
self.write_list[1] = data_buffer
72+
self.i2c.writevto(self.i2c_addr, self.write_list)
73+
74+
75+
def init_display(self):
76+
self.write_command(SH1107_DISPLAY_OFF)
77+
78+
self.write_command(SH1107_SET_LOWER_COLUMN_ADDRESS)
79+
self.write_command(SH1107_SET_UPPER_COLUMN_ADDRESS)
80+
81+
self.write_command(SH1107_SET_PAGE_ADDRESS)
82+
83+
self.write_command(SH1107_SET_DISPLAY_START_LINE)
84+
self.write_command(0x00)
85+
self.write_command(SH1107_SET_CONSTRAST_CONTROL)
86+
self.write_command(0x44)
87+
self.write_command(SH1107_SET_VERTICAL_MEMORY_ADDRESSING_MODE)
88+
89+
self.write_command(SH1107_SET_SEGMENT_REMAP_REVERSE)
90+
self.write_command(SH1107_SET_COMMON_OUTPUT_SCAN_DIRECTION)
91+
self.write_command(SH1107_SET_ENTIRE_DISPLAY_OFF)
92+
93+
self.write_command(SH1107_SET_NORMAL_DISPLAY)
94+
self.write_command(SH1107_SET_MULTIPLEX_RATIO)
95+
self.write_command(0x7F)
96+
97+
self.write_command(SH1107_SET_DISPLAY_OFFSET)
98+
self.write_command(0x60)
99+
100+
self.write_command(SH1107_SET_DISPLAY_CLOCK_FREQUENCY)
101+
self.write_command(0x81)
102+
103+
self.write_command(SH1107_SET_PRECHARGE_DISCHARGE_PERIOD)
104+
self.write_command(0x22)
105+
106+
self.write_command(SH1107_SET_VCOM_DESELECT_LEVEL)
107+
self.write_command(0x35)
108+
109+
self.write_command(SH1107_SET_DC_DC_CONTROL_MODE)
110+
self.write_command(SH1107_SET_DC_DC_OFF_MODE)
111+
self.write_command(SH1107_DISPLAY_ON)
112+
113+
114+
def modify_buf(self, offs, width):
115+
ptr = offs
116+
width += offs
117+
while(ptr < width):
118+
while((ptr < width) and (self.curr_buffer[ptr : (ptr + 0x08)] == self.prev_buffer[ptr : (ptr + 0x08)])):
119+
ptr += 0x08
120+
121+
if(ptr < width):
122+
first = ptr
123+
ptr += 0x08
124+
while((ptr < width) and (self.curr_buffer[ptr : (ptr + 0x08)] != self.prev_buffer[ptr : (ptr + 0x08)])):
125+
ptr += 0x08
126+
127+
yield first, ptr
128+
ptr += 0x08
129+
130+
131+
def show(self):
132+
for col in range(self.height):
133+
noffs = (col * self.line_bytes)
134+
for page1, page2 in self.modify_buf(noffs, self.line_bytes):
135+
self.write_command(SH1107_SET_PAGE_ADDRESS | (page1 - noffs))
136+
self.write_command(SH1107_SET_LOWER_COLUMN_ADDRESS | (col & 0x0F))
137+
self.write_command(SH1107_SET_UPPER_COLUMN_ADDRESS | ((col & 0x70) >> 0x04))
138+
self.write_data(self.curr_buffer[page1 : page2])
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# boot.py -- run on boot-up
2+
# can run arbitrary Python, but best to keep it minimal
3+
4+
import machine
5+
import pyb
6+
pyb.country('US') # ISO 3166-1 Alpha-2 code, eg US, GB, DE, AU
7+
#pyb.main('main.py') # main script to run after this one
8+
#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device
9+
pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Notes
3+
4+
1. Thonny or any other IDE cannot run simultaneously when a program like Putty or similar is being used for manipulating COM port
5+
6+
2. Port: Same as the COM ID of the Pyboard
7+
8+
3. Baud Rate: 115200 Parity: None Stop: 1
9+
10+
4. Must uncomment the following line
11+
12+
pyb.usb_mode('VCP+HID')
13+
14+
in the boot.py file or else USB function won't work at all
15+
"""
16+
17+
from pyb import Pin, ADCAll, USB_VCP
18+
from machine import I2C
19+
from time import sleep_ms, sleep_us
20+
from SH1107 import OLED
21+
22+
23+
SW = Pin('A0', Pin.IN, Pin.PULL_UP)
24+
click = Pin('A1', Pin.IN, Pin.PULL_UP)
25+
LED = Pin('C13', Pin.OUT)
26+
27+
adc = ADCAll(12, 0x70000)
28+
29+
TWI = I2C(scl = 'PB9', sda = 'PB8', freq = 100000)
30+
oled = OLED(TWI)
31+
32+
usb = USB_VCP()
33+
34+
oled.fill(oled.BLACK)
35+
oled.show()
36+
37+
38+
buffer = ""
39+
compensation = 0
40+
41+
42+
def get_T_avg(no_of_samples = 120):
43+
samples = no_of_samples
44+
tmp = 0
45+
46+
while(samples > 0):
47+
tmp += adc.read_core_temp()
48+
sleep_us(20)
49+
samples -= 1
50+
51+
tmp /= no_of_samples
52+
tmp += compensation
53+
54+
return tmp
55+
56+
57+
usb.setinterrupt(-1)
58+
59+
while (SW.value() == True):
60+
61+
t = get_T_avg()
62+
63+
if(usb.any()):
64+
LED.on()
65+
buffer = usb.readline()
66+
buffer = buffer[0:(len(buffer) - 2)]
67+
68+
if(buffer[0] == 45):
69+
compensation = -(((buffer[1] - 48) * 10) + (buffer[2] - 48))
70+
else:
71+
compensation = (((buffer[0] - 48) * 10) + (buffer[1] - 48))
72+
73+
usb.write(str("%4.4f" %t) + "\r\n")
74+
75+
oled.fill(oled.BLACK)
76+
77+
oled.text("PyB USB CDC", 15, 1, oled.WHITE)
78+
oled.text("Comp/'C: ", 1, 20, oled.WHITE)
79+
oled.text(str("%2d" %compensation), 80, 20, oled.WHITE)
80+
oled.text("Temp/'C: ", 1, 40, oled.WHITE)
81+
oled.text(str("%2.2f" %t), 80, 40, oled.WHITE)
82+
oled.show()
83+
LED.off()
84+
sleep_ms(400)
85+

USB HID Mouse/SH1107.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from micropython import const
2+
import framebuf
3+
4+
5+
SH1107_I2C_address = const(0x3C)
6+
7+
8+
SH1107_SET_LOWER_COLUMN_ADDRESS = const(0x00)
9+
SH1107_SET_UPPER_COLUMN_ADDRESS = const(0x10)
10+
SH1107_SET_PAGE_MEMORY_ADDRESSING_MODE = const(0x20)
11+
SH1107_SET_VERTICAL_MEMORY_ADDRESSING_MODE = const(0x21)
12+
SH1107_SET_CONSTRAST_CONTROL = const(0x81)
13+
SH1107_SET_DC_DC_OFF_MODE = const(0x8A)
14+
SH1107_SET_DC_DC_ON_MODE = const(0x8B)
15+
SH1107_SET_SEGMENT_REMAP_NORMAL = const(0xA0)
16+
SH1107_SET_SEGMENT_REMAP_REVERSE = const(0xA1)
17+
SH1107_SET_ENTIRE_DISPLAY_OFF = const(0xA4)
18+
SH1107_SET_ENTIRE_DISPLAY_ON = const(0xA5)
19+
SH1107_SET_NORMAL_DISPLAY = const(0xA6)
20+
SH1107_SET_REVERSE_DISPLAY = const(0xA7)
21+
SH1107_SET_MULTIPLEX_RATIO = const(0xA8)
22+
SH1107_SET_DC_DC_CONTROL_MODE = const(0xAD)
23+
SH1107_DISPLAY_OFF = const(0xAE)
24+
SH1107_DISPLAY_ON = const(0xAF)
25+
SH1107_SET_PAGE_ADDRESS = const(0xB0)
26+
SH1107_SET_COMMON_OUTPUT_SCAN_DIRECTION = const(0xC0)
27+
SH1107_SET_DISPLAY_OFFSET = const(0xD3)
28+
SH1107_SET_DISPLAY_CLOCK_FREQUENCY = const(0xD5)
29+
SH1107_SET_PRECHARGE_DISCHARGE_PERIOD = const(0xD9)
30+
SH1107_SET_VCOM_DESELECT_LEVEL = const(0xDB)
31+
SH1107_SET_DISPLAY_START_LINE = const(0xDC)
32+
33+
34+
disp_width = const(128)
35+
disp_height = const(64)
36+
disp_pages = const((disp_height >> 3))
37+
38+
39+
class OLED(framebuf.FrameBuffer):
40+
41+
def __init__(self, _i2c, _i2c_address = SH1107_I2C_address):
42+
self.width = disp_width
43+
self.height = disp_height
44+
self.pages = disp_pages
45+
46+
self.line_bytes = (self.width >> 3)
47+
size = (self.width * self.pages)
48+
self.curr_buffer = bytearray(b'\x00' * size)
49+
self.prev_buffer = bytearray(b'\xFF' * size)
50+
51+
self.write_list = [b"\x40", None]
52+
self.temp = bytearray(0x02)
53+
54+
self.WHITE = 1
55+
self.BLACK = 0
56+
57+
self.i2c = _i2c
58+
self.i2c_addr = _i2c_address
59+
60+
super().__init__(self.curr_buffer, self.width, self.height, framebuf.MONO_HMSB)
61+
self.init_display()
62+
63+
64+
def write_command(self, cmd):
65+
self.temp[0] = 0x80
66+
self.temp[1] = cmd
67+
self.i2c.writeto(self.i2c_addr, self.temp)
68+
69+
70+
def write_data(self, data_buffer):
71+
self.write_list[1] = data_buffer
72+
self.i2c.writevto(self.i2c_addr, self.write_list)
73+
74+
75+
def init_display(self):
76+
self.write_command(SH1107_DISPLAY_OFF)
77+
78+
self.write_command(SH1107_SET_LOWER_COLUMN_ADDRESS)
79+
self.write_command(SH1107_SET_UPPER_COLUMN_ADDRESS)
80+
81+
self.write_command(SH1107_SET_PAGE_ADDRESS)
82+
83+
self.write_command(SH1107_SET_DISPLAY_START_LINE)
84+
self.write_command(0x00)
85+
self.write_command(SH1107_SET_CONSTRAST_CONTROL)
86+
self.write_command(0x44)
87+
self.write_command(SH1107_SET_VERTICAL_MEMORY_ADDRESSING_MODE)
88+
89+
self.write_command(SH1107_SET_SEGMENT_REMAP_REVERSE)
90+
self.write_command(SH1107_SET_COMMON_OUTPUT_SCAN_DIRECTION)
91+
self.write_command(SH1107_SET_ENTIRE_DISPLAY_OFF)
92+
93+
self.write_command(SH1107_SET_NORMAL_DISPLAY)
94+
self.write_command(SH1107_SET_MULTIPLEX_RATIO)
95+
self.write_command(0x7F)
96+
97+
self.write_command(SH1107_SET_DISPLAY_OFFSET)
98+
self.write_command(0x60)
99+
100+
self.write_command(SH1107_SET_DISPLAY_CLOCK_FREQUENCY)
101+
self.write_command(0x81)
102+
103+
self.write_command(SH1107_SET_PRECHARGE_DISCHARGE_PERIOD)
104+
self.write_command(0x22)
105+
106+
self.write_command(SH1107_SET_VCOM_DESELECT_LEVEL)
107+
self.write_command(0x35)
108+
109+
self.write_command(SH1107_SET_DC_DC_CONTROL_MODE)
110+
self.write_command(SH1107_SET_DC_DC_OFF_MODE)
111+
self.write_command(SH1107_DISPLAY_ON)
112+
113+
114+
def modify_buf(self, offs, width):
115+
ptr = offs
116+
width += offs
117+
while(ptr < width):
118+
while((ptr < width) and (self.curr_buffer[ptr : (ptr + 0x08)] == self.prev_buffer[ptr : (ptr + 0x08)])):
119+
ptr += 0x08
120+
121+
if(ptr < width):
122+
first = ptr
123+
ptr += 0x08
124+
while((ptr < width) and (self.curr_buffer[ptr : (ptr + 0x08)] != self.prev_buffer[ptr : (ptr + 0x08)])):
125+
ptr += 0x08
126+
127+
yield first, ptr
128+
ptr += 0x08
129+
130+
131+
def show(self):
132+
for col in range(self.height):
133+
noffs = (col * self.line_bytes)
134+
for page1, page2 in self.modify_buf(noffs, self.line_bytes):
135+
self.write_command(SH1107_SET_PAGE_ADDRESS | (page1 - noffs))
136+
self.write_command(SH1107_SET_LOWER_COLUMN_ADDRESS | (col & 0x0F))
137+
self.write_command(SH1107_SET_UPPER_COLUMN_ADDRESS | ((col & 0x70) >> 0x04))
138+
self.write_data(self.curr_buffer[page1 : page2])

USB HID Mouse/boot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# boot.py -- run on boot-up
2+
# can run arbitrary Python, but best to keep it minimal
3+
4+
import machine
5+
import pyb
6+
pyb.country('US') # ISO 3166-1 Alpha-2 code, eg US, GB, DE, AU
7+
#pyb.main('main.py') # main script to run after this one
8+
#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device
9+
pyb.usb_mode('VCP+HID') # act as a serial device and a mouse

0 commit comments

Comments
 (0)