zoukankan      html  css  js  c++  java
  • 树莓派python OLED使用

    Python有两个可以用的OLED库

    i2c接线

    树莓派引脚图

    OLED引脚 树莓派物理BOARD引脚
    VCC 1号
    GND 6号
    SCL 5号
    SDA 3号

    接好线后就是像一个L型的。

    接线方式如下图,按颜色对应:

    开启i2c功能

    sudo apt-get install -y python-smbus
    sudo apt-get install -y i2c-tools
    sudo raspi-config

    打开树莓派配置选择5 Interfacing Options。

    选择P5 I2C回车激活I2C。

    按回车启动就得了。

    查看i2c地址

    sudo i2cdetect -y 1
    
    

    然后你能看到下面的地址,3c就是oled屏的i2c地址了,说明已经成功开启i2c啦

    – 安装Luma.oled库

    终端输入下面命令。

    sudo apt-get install python-dev python-pip libfreetype6-dev libjpeg-dev
    
    pip install --upgrade luma.oled


    如果安装Luma.oled库时出现红字错误,请继续执行命令重试,那是因为网络问题下载一个叫Pillow的库不成功。注:如果你需要安装Python3的Luma.oled库的则按下面对应的Python3版本修改上面的命令进行安装。
    pip ⇒ pip3
    python ⇒ python3
    python-dev ⇒ python3-dev
    python-pip ⇒ python3-pip

    安装好Luma.oled库后新建文件命名为oled.py,复制粘贴下面代码。参考这里使用说明

    from luma.core.interface.serial import i2c, spi
    
    from luma.core.render import canvas
    
    from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106
    
    
    
    # rev.1 users set port=0
    
    # substitute spi(device=0, port=0) below if using that interface
    
    serial = i2c(port=1, address=0x3C)
    
    
    
    # substitute ssd1331(...) or sh1106(...) below if using that device
    
    device = sh1106(serial)#这里改ssd1306, ssd1325, ssd1331, sh1106
    
    
    
    with canvas(device) as draw:
    
        draw.rectangle(device.bounding_box, outline="white", fill="black")
    
        draw.text((30, 40), "Hello World", fill="white")

    如果你的oled驱动芯片是其它型号找到device = sh1106(serial),把sh1106改成库支持的其它型号。
    树莓派上用Python2打开oled.py运行就能看到下图的Hello World。

    能驱动成功后我们去下载Luma.oled的examples代码
    然后是examples里面的例子怎么用呢?如果是非ssd1306芯片直接运行还是花屏的,因为那个examples的代码需要修改。
    下面以pi_logo.py为例参考上面那个Hello World的例子修改成自己OLED芯片型号的(文件放在在examples内)。

    #!/usr/bin/env python
    
    # -*- coding: utf-8 -*-
    
    # Copyright (c) 2014-17 Richard Hull and contributors
    
    # See LICENSE.rst for details.
    
    # PYTHON_ARGCOMPLETE_OK
    
    
    
    """
    
    Display the Raspberry Pi logo (loads image as .png).
    
    """
    
    
    
    import os.path
    
    from PIL import Image
    
    
    
    from luma.core.interface.serial import i2c, spi
    
    from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106
    
    
    
    def main():
    
        img_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
    
            'images', 'pi_logo.png'))
    
        logo = Image.open(img_path).convert("RGBA")
    
        fff = Image.new(logo.mode, logo.size, (255,) * 4)
    
    
    
        background = Image.new("RGBA", device.size, "white")
    
        posn = ((device.width - logo.width) // 2, 0)
    
    
    
        while True:
    
            for angle in range(0, 360, 2):
    
                rot = logo.rotate(angle, resample=Image.BILINEAR)
    
                img = Image.composite(rot, fff, rot)
    
                background.paste(img, posn)
    
                device.display(background.convert(device.mode))
    
    
    
    if __name__ == "__main__":
    
        try:
    
            serial = i2c(port=1, address=0x3C)
    
            device = sh1106(serial)
    
            main()
    
        except KeyboardInterrupt:
    
            pass

    Python运行上面的程序oled屏会出现一个能旋转的树莓派LOGO。


    参考文章:

    https://shumeipai.nxez.com/2017/09/13/solve-the-raspberry-pi-drive-oled-problem.html

    https://www.jianshu.com/p/570978ea7cfd

  • 相关阅读:
    ld -l选项注意事项
    linux下创建用户(转)
    delete void *
    __attribute__机制介绍(转)
    正常断开连接情况下,判断非阻塞模式socket连接是否断开
    std::thread “terminate called without an active exception”
    Android 开发手记二 C可执行程序编译实例(转帖)
    c++11 on Android
    由一段小程序看算法复杂度
    Linux守护进程的编程实现(转)
  • 原文地址:https://www.cnblogs.com/Archger/p/12774636.html
Copyright © 2011-2022 走看看