python代码配合工具对oled取字模脚本
中文取模
def getu(_):
code = 0x00
_ = _.encode("utf-8")
code |= (_[0] << 16)
code |= (_[1] << 8)
code |= (_[2])
return hex(code)
f = open("字.txt","r") #设置文件对象
lines=f.readlines()
a=int(len(lines)/2)
for i in range(a):
line1 = getu(lines[i*2+1][79])+":"
line2 = "["+lines[i*2][:-1]+","
line3 = " "+lines[i*2+1][:79] +"],#"+lines[i*2+1][79]
print(line1)
print(line2)
print(line3)
f.close()
取模工具的使用方法同c,OLED学习笔记有记。
添加一个python的oled驱动:
from pyb import I2C
import pyb
import font
DISPLAYOFF = 0xAE
SETCONTRAST = 0x81
DISPLAYALLON_RESUME = 0xA4
DISPLAYALLON = 0xA5
NORMALDISPLAY = 0xA6
INVERTDISPLAY = 0xA7
DISPLAYON = 0xAF
SETDISPLAYOFFSET = 0xD3
SETCOMPINS = 0xDA
SETVCOMDETECT = 0xDB
SETDISPLAYCLOCKDIV = 0xD5
SETPRECHARGE = 0xD9
SETMULTIPLEX = 0xA8
SETLOWCOLUMN = 0x00
SETHIGHCOLUMN = 0x10
SETSTARTLINE = 0x40
MEMORYMODE = 0x20
COLUMNADDR = 0x21
PAGEADDR = 0x22
COMSCANINC = 0xC0
COMSCANDEC = 0xC8
SEGREMAP = 0xA0
CHARGEPUMP = 0x8D
EXTERNALVCC = 0x10
SWITCHCAPVCC = 0x20
SETPAGEADDR = 0xB0
SETCOLADDR_LOW = 0x00
SETCOLADDR_HIGH = 0x10
ACTIVATE_SCROLL = 0x2F
DEACTIVATE_SCROLL = 0x2E
SET_VERTICAL_SCROLL_AREA = 0xA3
RIGHT_HORIZONTAL_SCROLL = 0x26
LEFT_HORIZONTAL_SCROLL = 0x27
VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29
VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A
CommandLOCK = 0xFD
DEVID = 0x3c
CTL_CMD = 0x80
CTL_DAT = 0x40
class SSD1306(object):
def __init__(self, pinout, height=32, external_vcc=True, i2c_devid=DEVID):
self.external_vcc = external_vcc
self.height = 32 if height == 32 else 64
self.pages = int(self.height / 8)
self.columns = 128
self.i2c = I2C(2)
self.i2c.init(pyb.I2C.MASTER, baudrate=400000)
self.devid = i2c_devid
self.offset = 1
self.cbuffer = bytearray(2)
self.cbuffer[0] = CTL_CMD
def clear(self):
self.buffer = bytearray(self.offset + self.pages * self.columns)
if self.offset == 1:
self.buffer[0] = CTL_DAT
def write_command(self, command_byte):
if self.offset == 1:
self.cbuffer[1] = command_byte
self.i2c.send(self.cbuffer, addr=self.devid, timeout=5000)
else:
self.dc.low()
self.spi.send(command_byte)
def invert_display(self, invert):
self.write_command(INVERTDISPLAY if invert else NORMALDISPLAY)
def display(self):
self.write_command(COLUMNADDR)
self.write_command(0)
self.write_command(self.columns - 1)
self.write_command(PAGEADDR)
self.write_command(0)
self.write_command(self.pages - 1)
if self.offset == 1:
self.i2c.send(self.buffer, addr=self.devid, timeout=5000)
else:
self.dc.high()
self.spi.send(self.buffer)
def set_pixel(self, x, y, state):
index = x + (int(y / 8) * self.columns)
if state:
self.buffer[self.offset + index] |= (1 << (y & 7))
else:
self.buffer[self.offset + index] &= ~(1 << (y & 7))
def draw_chinese(self,ch_str,x_axis,y_axis):
offset_=0
y_axis=y_axis
x_axis=127-x_axis
for k in ch_str:
#print(k)
code = 0x00
data_code = k.encode("utf-8")
code |= data_code[0]<<16
code |= data_code[1]<<8
code |= data_code[2]
byte_data=font.byte2[code]
for y in range(0,16):
a_=bin(byte_data[y]).replace('0b','')
while len(a_)<8:
a_='0'+a_
b_=bin(byte_data[y+16]).replace('0b','')
while len(b_)<8:
b_='0'+b_
for x in range(0,8):
self.set_pixel(x_axis-x-offset_,y+y_axis,int(a_[x]))
self.set_pixel(x_axis-x-8-offset_,y+y_axis,int(b_[x]))
if(code==0xe7ae9c):
offset_+=8
else:
offset_+=16
def init_display(self):
chargepump = 0x10 if self.external_vcc else 0x14
precharge = 0x22 if self.external_vcc else 0xf1
multiplex = 0x1f if self.height == 32 else 0x3f
compins = 0x02 if self.height == 32 else 0x12
contrast = 0xff
data = [#CommandLOCK, 0x12,
DISPLAYOFF,
SETDISPLAYCLOCKDIV, 0x80,
SETMULTIPLEX, multiplex,
SETDISPLAYOFFSET, 0x00,
SETSTARTLINE | 0x00,
CHARGEPUMP, chargepump,
MEMORYMODE, 0x00,
#SEGREMAP | 0x10,
0xA1,
COMSCANINC,
SETCOMPINS, compins,
SETCONTRAST, contrast,
SETPRECHARGE, precharge,
SETVCOMDETECT, 0x40,
DISPLAYALLON_RESUME,
NORMALDISPLAY,
DISPLAYON]
for item in data:
self.write_command(item)
self.clear()
self.display()
def poweron(self):
if self.offset == 1:
pyb.delay(10)
else:
self.res.high()
pyb.delay(1)
self.res.low()
pyb.delay(10)
self.res.high()
pyb.delay(10)
def poweroff(self):
self.write_command(DISPLAYOFF)
def contrast(self, contrast):
self.write_command(SETCONTRAST)
self.write_command(contrast)
def draw_text(self, x, y, string, size=1, space=1):
def pixel_x(char_number, char_column, point_row):
char_offset = x + char_number * size * font.cols + space * char_number
pixel_offset = char_offset + char_column * size + point_row
return self.columns - pixel_offset
def pixel_y(char_row, point_column):
char_offset = y + char_row * size
return char_offset + point_column
def pixel_mask(char, char_column, char_row):
char_index_offset = ord(char) * font.cols
return font.bytes[char_index_offset + char_column] >> char_row & 0x1
pixels = (
(pixel_x(char_number, char_column, point_row),
pixel_y(char_row, point_column),
pixel_mask(char, char_column, char_row))
for char_number, char in enumerate(string)
for char_column in range(font.cols)
for char_row in range(font.rows)
for point_column in range(size)
for point_row in range(1, size + 1))
for pixel in pixels:
self.set_pixel(*pixel)
def draw_num(self,ch_str,x_axis,y_axis):
offset_=0
y_axis=y_axis
x_axis=127-x_axis
for k in ch_str:
byte_data=font.bytenum[k]
for y in range(0,16):
a_=bin(byte_data[y]).replace('0b','')
while len(a_)<8:
a_='0'+a_
for x in range(0,8):
self.set_pixel(x_axis-x-offset_,y+y_axis,int(a_[x]))
offset_+=8
大意和c差不多,只是在语法方面简略许多。