本文記錄如何用樹莓派和下圖的顯示屏展示內容。
用到的設備:
- OLED顯示屏模塊,IIC接口,128*32點陣,驅動芯片SSD1306(購買鏈接)
- 樹莓派 3 Model B+,有外接顯示屏、鼠標(如果沒有可以用個人電腦連接樹莓派,Mac 系統的可點擊參考《Mac 通過 VNC 打開樹莓派遠程桌面(不用獨立顯示屏)》)
連接設備
如上圖所示,這個 OLED 有五個 Pin,和樹莓派引腳分別對映如下:
OLED Pin | 樹莓派 GPIO Pin (Board 編碼) | 備注 |
3.3v | 1 或 17 | 3.3 v |
GND | 6 或 9/14/20/25/30/34/39 | Ground |
SCL | 5 | I2C SCL |
SDA | 3 | I2C SCA |
RES | 可以不連 | Reset,我的代碼中沒有用到這個 |
市面上大多數 OLED 有4個或6個 Pin,連接方式略有不同,4個的少一個 RES,其他都一樣,如下圖。
Enable I2C Interface
有兩種方式:
1. 樹莓派終端中執行 sudo raspi-config ,選擇 Interfacing Options > I2C ,出現如下界面,確認后重啟。
2. 桌面版樹莓派可點擊桌面左上角菜單鍵,再選擇“首選項/Preferences" > "Raspberry Pi Configuration",然后再彈出的窗口中選擇 Interfaces 標簽,I2C 選項中選擇 Enable,如下圖。
樹莓派環境准備
根據參考資料【1】中的提示,我檢查了所有需要安裝的基本庫,基本上都是最新版本的,只下載了其中一個叫 python-smbus 的庫。
安裝可以進行在 Python 2.7 或 Python 3 里,推薦使用 Python 3。
Python 3 的可以執行下面的一系列命令進行自查:
sudo apt install -y python3-dev sudo apt install -y python-imaging python-smbus i2c-tools sudo apt install -y python3-pil sudo apt install -y python3-pip sudo apt install -y python3-setuptools sudo apt install -y python3-rpi.gpio
Python 2.7 的:
sudo apt install -y python-dev sudo apt install -y python-imaging python-smbus i2c-tools sudo apt install -y python-pil sudo apt install -y python-pip sudo apt install -y python-setuptools
樹莓派下載安裝 SSD1306 屏幕驅動庫
有很多開源的 SSD1306 屏幕驅動庫,我使用的是 Adafruit 的 Python 庫,可以用於 128x32 和 128x64 的顯示屏。地址是:https://github.com/adafruit/Adafruit_Python_SSD1306。
下載安裝方式有兩種:
- 直接從 gitHub 上 clone 到本地
- git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
- cd Adafruit_Python_SSD1306
- Python 3 下: sudo python3 setup.py install ;Python 2 下:sudo python setup.py install
- 用 pip 下載
- sudo pip install Adafruit-SSD1306
我用到是第一種下載安裝方式。
測試代碼
下載的 Python 庫中有很多現成的例子,在 examples 文件夾中。終端進入該文件夾下,執行 python shapes.py 或 python3 shapes.py 顯示屏會顯示如下界面:
還有別的例子都可以嘗試一下。
其中有一個 buttons.py 例子可以配合按鍵一起測試。我就拿了四個輕觸四腳按鍵測試了一下,代碼中顯示可以用七個按鈕控制。
先展示一下效果:
具體操作如下:
用編輯器打開 buttons.py,觀察代碼,如下圖,
可以猜出 L, R, C, U, D 分別表示 Left, Right, Center, Up, Down,因為我只有四個按鈕,我就只連接了上下左右這四個按鈕,按照代碼中給的 BCM 號連接上對應的 GPIO 引腳,效果如下圖。
同時參考所有給出的例子,你就實現按鈕等元件控制顯示內容等效果。
在顯示屏上顯示文字
在 shapes.py 文件中有關於顯示字母的例子,我們可以在這個代碼上進行修改。
原代碼內容:
# Load default font. font = ImageFont.load_default() # Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script! # Some other nice fonts to try: http://www.dafont.com/bitmap.php #font = ImageFont.truetype('Minecraftia.ttf', 8)
想要顯示中文,就需要下載中文的字體,有很多免費網站可以下載到,需要是 ttf 文件。我下載了一個楷體的 ttf,然后需要將這個 ttf 文件和代碼放在同一路徑下(如果不同路徑,代碼中的路徑需要修改正確),修改代碼如下:
# Load default font. #font = ImageFont.load_default() # Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script! # Some other nice fonts to try: http://www.dafont.com/bitmap.php font = ImageFont.truetype('Kaiti.ttf', 30)
展示文字的代碼可以是 draw.text((x,top), u'測試', font=font, fill=255) ,運行結果如下。
完整的參考代碼:(第二行如果不定義 utf-8,可能會報錯)
#!/usr/bin/python # -*- coding: utf-8 -*- import RPi.GPIO as GPIO import time import Adafruit_GPIO.SPI as SPI import Adafruit_SSD1306 from PIL import Image from PIL import ImageDraw from PIL import ImageFont # Raspberry Pi pin configuration: RST = 24 # 128x32 display with hardware I2C: disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) # Initialize library. disp.begin() # Clear display. disp.clear() disp.display() # Create blank image for drawing. # Make sure to create image with mode '1' for 1-bit color. width = disp.width height = disp.height image = Image.new('1', (width, height)) # Get drawing object to draw on image. draw = ImageDraw.Draw(image) # Load default font. #font = ImageFont.load_default() # Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script! # Some other nice fonts to try: http://www.dafont.com/bitmap.php font = ImageFont.truetype('kaiti.ttf', 30) # First define some constants to allow easy resizing of shapes. padding = 0 top = padding bottom = height-padding # Move left to right keeping track of the current x position for drawing shapes. # 增加 x 值可以將文字向右移動 x = 0 draw.text((x, top), u'測試', font=font, fill=255) # 字段前加 u 表示是文字 disp.image(image) disp.display()
我的相關文章
- 樹莓派上使用 LCD1602 顯示狀態
- Python 控制樹莓派 GPIO 輸出:控制 LED 燈
- 樹莓派中添加中文輸入法
- Mac 通過 VNC 打開樹莓派遠程桌面(不用獨立顯示屏)
- 用 Mac 給樹莓派重裝系統
- 樹莓派的系統安裝,並且利用網線直連 Mac 進行配置