Beaglebone black(BBB)使用spi接口oled:基于python

上一篇博客介绍了树莓派(raspberry)上使用spi接口的oled屏幕,今天就再把那个屏幕用到beaglebone black(BBB)上面,用它来使beaglebone black有个显示提示功能,可以用来调试等.屏幕还是那个:0.98寸的oled屏幕,ssd1306主控芯片,128*64的分辨率。 

个人原创,版权所有,转载请注明原文出处,并保留原文链接:

http://www.embbnux.com/2014/08/10/beaglebone_black_use_spi_oled_ssd1306/

beaglebone和树莓派一样也有很多gpio口提供给用户,而且更多,资源也很丰富。

 

cape-headers-spi

开始进入正题吧:

一 首先还是进行连线:

spi oled的接口和之前的一样:

spioled

连线:

GND >> Beaglebone black的P9_1管脚也就是GND

VCC >> Beaglebone black的P9_3即VDD_3V3

CS     >> Beaglebone black的P9_17(SPI0_CS0)

DC    >>  Beaglebone black的 P9_15 

RST  >>   Beaglebone black的 P9_12

D1(MOSI) >> Beaglebone black的SPI0_D1(P9_18)

D0(SCLK) >> Beaglebone black的SPI0_SCLK(P9_22)

二 在beaglebone black上开启spi接口

电脑用usb线连接上beaglebone black后,电脑上会多出一个磁盘分区:BEAGLEBONE,这个是beaglebone black上emmc上的一个分区,里面的文件就是beaglebone black的配置文件。

用文本编辑器打开uEnv.txt

在里面最后一行加入下面的语句:

optargs=capemgr.enable_partno=BB-SPIDEV0

重新启动beaglebone black后会在/dev目录下新增下面两个文件: spidev1.0 spidev1.1

对应的是beaglebone black的spi接口

这样就开启了

三 使用python操作oled准备

先是安装相应的库和程序:

我beaglebone black的系统目前是angstrom,如果是ubuntu的看再下面

angstrom:

使用ssh登陆到板子上:ssh root@192.168.7.2


/usr/bin/ntpdate -b -s -u pool.ntp.org
opkg update
opkg install task-native-sdk
opkg install python-pip python-setuptools
opkg install python-dev
opkg install python-misc #不安装的话使用python提示找不到contextlib:No module named contextlib

opkg install python-imaging python-smbus
pip install Adafruit_BBIO
opkg install git
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git

git的时候出现了一下问题:

error:  while accessing https://github.com/adafruit/Adafruit_Python_SSD1306.git/info/refs
fatal: HTTP request failed

原因是git上使用https需要验证,解决:


git config --global http.sslverify false
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git

继续:


cd Adafruit_Python_SSD1306

python setup.py install

如果提示错误:
subprocess.CalledProcessError: Command ‘[‘curl’, ‘https://pypi.python.org/packages/source/s/setuptools/setuptools-3.5.1.zip’, ‘–silent’, ‘–output’, ‘/home/root/python/Adafruit_Python_SSD1306/setuptools-3.5.1.zip’]’ returned non-zero exit status 77

则下载https://pypi.python.org/packages/source/s/setuptools/setuptools-3.5.1.zip,放到Adafruit_Python_SSD1306目录下,再执行 python setup.py install

ubuntu的操作如下:


sudo apt-get update
sudo apt-get install build-essential python-dev python-pip
sudo pip install Adafruit_BBIO

sudo apt-get install python-imaging python-smbus

sudo apt-get install git

git clone   https://github.com/adafruit/Adafruit_Python_SSD1306.git
cd Adafruit_Python_SSD1306
sudo python setup.py install

四 编写python程序测试显示屏

新建spioled.py:


import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

import Image
import ImageDraw
import ImageFont

# Beaglebone Black pin configuration:
RST = 'P9_12'
# Note the following are only used with SPI:
DC = 'P9_15'
SPI_PORT = 1
SPI_DEVICE = 0

disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))

# 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)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = padding
# Draw an ellipse.
draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0)
x += shape_width+padding
# Draw a rectangle.
draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
x += shape_width+padding
# Draw a triangle.
draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width, bottom)], outline=255, fill=0)
x += shape_width+padding
# Draw an 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

# Load default font.
font = ImageFont.load_default()

# Alternatively load a TTF font.
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
#font = ImageFont.truetype('Minecraftia.ttf', 8)

# Write two lines of text.
draw.text((x, top),    'Hello',  font=font, fill=255)
draw.text((x, top+20), 'World!', font=font, fill=255)

# Display image.
disp.image(image)
disp.display()

如果没任何问题的话执行该程序,应该就是执行了:

python spioled.py 或者 sudo python spioled.py

显示如下:
bbb_spi_oled_embbnux

参考文章:

https://learn.adafruit.com/ssd1306-oled-displays-with-raspberry-pi-and-beaglebone-black/

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Time limit is exhausted. Please reload the CAPTCHA.