# micro - OLED **Repository Path**: linghuaxue/micro-oled ## Basic Information - **Project Name**: micro - OLED - **Description**: OLED秒懂使用 - **Primary Language**: Python - **License**: MPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-12-14 - **Last Updated**: 2024-03-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # OLED秒懂教程 ## 介绍 ![](./images/6195788356_523352349.jpg "模块样图") OLED特别简单,只有两根数据线 电源正极VCC和负极GND ## 使用说明 1. [micropython 方式](#micropython) 2. [arduino 方式](./arduino) 3. [stm32 使用 stm32cubeMx 或 stm32cubeIde 方式(HAL库方式)](./stm32_stm32cube) 4. [stm32 使用keil方式](./stm32_keil5) ### micropython micropython使用方法 ``` from machine import Pin, SoftI2C from ssd1306 import SSD1306_I2C class OLED: # 初始化i2c接口 # param scl: i2c的时钟脚 # param sda: i2c的数据脚 # param width: oled屏幕的宽度像素 # param height: oled屏幕的高度像素 # param i2c: i2c口 def __init__(self, scl, sda, width, height, i2c=-1): # self._i2c = I2C(i2c, scl=Pin(scl), sda=Pin(sda)) self._i2c = SoftI2C(scl=Pin(scl), sda=Pin(sda)) self._width = width self._height = height self._oled = SSD1306_I2C(self._width, self._height, self._i2c) # 清除屏幕 def clear(self): self._oled.fill(0) # 屏幕刷新显示 def show(self): self._oled.show() # 画点 def pixel(self, x, y, v): if x < self._width and y < self._height: self._oled.pixel(x, y, v) # 显示字符串.注意字符串必须是英文或者数字 def text(self, string, x_axis, y_axis): self._oled.text(string, x_axis, y_axis) # 用字体变量,显示字符串.注意字符串必须是英文或者数字,不建议加载中文 def text_withfont(self, string, x_axis, y_axis, font, font_size): offset = 0 for k in string: data_code = k.encode("utf-8") byte_data = len(data_code) if byte_data == 1: byte_data = font[int(data_code[0])] elif byte_data == 3: code = 0x00 # 将中文转成16进制编码 code |= data_code[0] << 16 code |= data_code[1] << 8 code |= data_code[2] return byte_data = font[code] for y in range(0, font_size): a = bin(int(byte_data[y])).replace('0b', '') while len(a) < 8: a = '0' + a for x in range(0, 8): # self._oled.pixel(x_axis + offset + x, y_axis+y, int(a[x])) self.pixel(x_axis + offset + x, y_axis + y, int(a[x])) # 修改后防止出边 offset += 8 # 这个会将文件加载成变量,注意文件大小,过大会导致死机 def load_font(self, font_path): font = [] with open(font_path, 'r') as f: for l in f.readlines(): font.append(l.split(',')) return font # 显示字符串.注意字符串必须是英文或者数字,不建议加载中文 def text_with_fontfile(self, string, x_axis, y_axis, font_name, font_size,font_rowcount): offset = 0 for k in string: data_code = k.encode("utf-8") byte_data = len(data_code) if byte_data == 1: byte_data = font[int(data_code[0])] elif byte_data == 3: code = 0x00 # 将中文转成16进制编码 code |= data_code[0] << 16 code |= data_code[1] << 8 code |= data_code[2] return byte_data = font[code] for y in range(0, font_size): a = bin(int(byte_data[y])).replace('0b', '') while len(a) < 8: a = '0' + a for x in range(0, 8): # self._oled.pixel(x_axis + offset + x, y_axis+y, int(a[x])) self.pixel(x_axis + offset + x, y_axis + y, int(a[x])) # 修改后防止出边 offset += 8 o = OLED(2, 4, 128, 64) o.text('wendu', 0, 0) o.text('name title', 0, 20) font = o.load_font('/oled/ascii16.txt') o.text_withfont("0123456789abcdefghigklmnopkrstuvwxyz", 0, 0, font, 16) # 0123456789abcdefghigklmnopkrstuvwxyz中国汉语 o.show() # 必须添加,否则不显示,就是绘图的最后执行命令 print('END') pass#END ``` ## OLED 原理 无 ### 参与贡献 无