Loading... # 0x00 因为之前用的Nokia5110显示有故障,所以在网友的推荐下接触到了SSD1306。 > 显示模块型号:SSD1306 OLED > > 显示范围:单色 128*64点阵 > > 测试机:树莓派zero w > > 测试机系统:[Raspbian stretch life](https://www.raspberrypi.org/downloads/raspbian/) > > 开发语言:Python3 > > 采用驱动库:[Adafruit Python SSD1306](https://github.com/adafruit/Adafruit_Python_SSD1306) # 0x01环境准备 ## 1.树莓派开启I2C接口 终端输入 ```shell sudo raspi-config ``` 选择<code>Interfacing Options</code>,开启<code>I2C</code>接口 ![Lily_Screenshot_1536654250.png][1] ![Lily_Screenshot_1536654260.png][2] ![Lily_Screenshot_1536654272.png][3] ###更改显示速率(可选,不推荐,会占用大量GPIO资源) 为了更好的显示性能,例如显示动画时如对帧率有要求,可以通过修改下面的参数将 I2C 频率从默认的 100KHz 或 400KHz 提升到 1MHz。 ```shell sudo nano /boot/config.txt ``` 在文件末添加一行: ```shell dtparam=i2c_baudrate=1000000 ``` ## 2.接线 模块共4针脚,接线为 > VCC 1 > > GND 6 > > SCL 5 > > SDA 3 ![Lily_Screenshot_1536654018.png][4] ## 3.驱动 GPIO驱动: ```shell pip install Adafruit_GPIO ``` SSD1306采用的驱动是[Adafruit Python SSD1306](https://github.com/adafruit/Adafruit_Python_SSD1306),用pip安装即可。 ```shell sudo python -m pip install --upgrade pip setuptools wheel sudo pip install Adafruit-SSD1306 ``` 或者采用python文件安装: ```shell sudo python -m pip install --upgrade pip setuptools wheel git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git cd Adafruit_Python_SSD1306 sudo python setup.py install ``` # 0x03使用 采用python文件安装的可以在<code>Adafruit_Python_SSD1306</code>文件夹中看见<code>examples</code>实例文件夹。 首先是导入驱动的库: ```python import Adafruit_GPIO.SPI as SPI import Adafruit_SSD1306 ``` 针脚配置: ```python # Raspberry Pi pin configuration: RST = 24 # Note the following are only used with SPI: DC = 23 SPI_PORT = 0 SPI_DEVICE = 0 ``` 显示配置: ```python #分辨率选择一个即可 # 128x32 disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) # 128x64 disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) # 初始化库 disp.begin() # 获取屏幕大小 width = disp.width height = disp.height # 清除屏幕内容 disp.clear() disp.display() ``` ## 1.显示内容 屏幕显示内容是先建立一张与屏幕相等的空白画布,将要显示的内容添加到其中,最后再显示出来: ```python # 创建空白画布 # 颜色为1 bit width = disp.width height = disp.height image = Image.new('1', (width, height)) # 画布绘制对象 draw = ImageDraw.Draw(image) # 绘制一个黑色内容清除画布内容 draw.rectangle((0,0,width,height), outline=0, fill=0) # 绘制内容 # 定义一些常亮控制图形大小 padding = 2 shape_width = 20 top = padding bottom = height-padding # 从左向右移动,跟踪绘制形状的当前位置 x = padding # 绘制椭圆 draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0) x += shape_width+padding # 绘制矩形 draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0) x += shape_width+padding # 绘制三角 draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=255, fill=0) x += shape_width+padding # 绘制X draw.line((x, bottom, x+shape_width, top), fill=255) draw.line((x, top, x+shape_width, bottom), fill=255) x += shape_width+padding # 加载默认字体 font = ImageFont.load_default() # 或者使用其他.ttf格式的字体,字体文件要与执行文件在同一目录 #font = ImageFont.truetype('Minecraftia.ttf', 8) # 加两行文字 draw.text((x, top), 'Hello', font=font, fill=255) draw.text((x, top+20), 'World!', font=font, fill=255) # 显示 disp.image(image) disp.display() ``` ![617739217782034705.jpg][5] ## 2.显示图片 ```python # 根据OLED高度加载图片,图片要转换成1 bit颜色 if disp.height == 64: image = Image.open('image_64.png').convert('1') else: image = Image.open('image_32.png').convert('1') ``` ![46229923031718465.jpg][6] *PS:可以把喜欢的图片改成不超过128\*64,再在Photoshop里先把模式改成灰度,然后调对比度和亮度,在不影响视觉的前提下尽可能对比度大。然后再改模式为“位图”,方法选“扩散仿色”,然后另存为PNG,选项为交错,就可以让OLED显示了。* **END** [1]: /usr/uploads/2019/10/2667507625.png [2]: /usr/uploads/2019/10/2820671452.png [3]: /usr/uploads/2019/10/4004458647.png [4]: /usr/uploads/2019/10/1395839971.png [5]: /usr/uploads/2019/10/2931856534.jpg [6]: /usr/uploads/2019/10/187501514.jpg Last modification:October 24, 2019 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 如果觉得我的文章对你有用,请随意赞赏