树莓派控制OLED屏幕输出文字的语法

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)

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值