OLED屏幕显示固定内容
#!/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_64(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 #获取屏幕高度
print(width,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('simfang.ttf', 16) # 引入字体文件,simfang.ttf是字体文件,如果不再同一文件夹下,需要将路径标出,30代表字显示的大小
#在黄蓝oled屏幕上,黄色像素占据16像素,蓝色像素占据48像素,比例为1:3
# 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 = 48
# x代表文字距离左边界的距离,top代表文字距离最顶端的距离
draw.text((x, top), u'测试', font=font, fill=255) # 字段前加 u 表示是文字
draw.text((16,16),u'落叶很聪明!',font=font,fill=255)
disp.image(image)
disp.display()
OLED屏幕显示动态内容
#!/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_64(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 #获取屏幕高度
print(width,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('simfang.ttf', 16) # 引入字体文件,simfang.ttf是字体文件,如果不再同一文件夹下,需要将路径标出,30代表字显示的大小
#在黄蓝oled屏幕上,黄色像素占据16像素,蓝色像素占据48像素,比例为1:3
# 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 = 48
while True:
draw.rectangle((0,0,width,height), outline=0, fill=0) #清除屏幕开始时的像素,如果不清除,那么一些像素就会堆积在一起,看不清
shijian=time.strftime("%H:%M:%S", time.localtime())
# x代表文字距离左边界的距离,top代表文字距离最顶端的距离
draw.text((0, 20), str(shijian), font=font, fill=255) # 字段前加 u 表示是文字
#draw.text((0,40),u'刘怀玉很聪明!',font=font,fill=255)
disp.image(image)
disp.display()
time.sleep(0.1)