13.4 Use third-party driver development

RPi-Spark applications can also be developed using third-party drivers.

We use Adafruit's SSD1306 OLED driver to display the text "Hi RPi-Spark" on RPi-Spark's OLED screen.

Create a file named: 13_4_0_hello.py, and type the content below:

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306 as SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

########################################################################
# Display PINs  SPI_0
# SSD1306 OLED 128x64
#
class CONFIG_DSP:
    DSP_RESET       = None
    DSP_DC          = 9  #use MISO for DC
    DSP_SPI_PORT    = 0
    DSP_SPI_DEVICE  = 0
    DSP_SPI_MAX_SPEED_HZ = 2000000  #up to 8500000

class demo:
    _myDSP = None
    _myDSP_IMG = None
    _myCanvas = None

    def __init__(self):
        #create display
        self._myDSP = SSD1306.SSD1306_128_64(
            rst = CONFIG_DSP.DSP_RESET, 
            dc = CONFIG_DSP.DSP_DC, 
            spi = SPI.SpiDev(
                    CONFIG_DSP.DSP_SPI_PORT, 
                    CONFIG_DSP.DSP_SPI_DEVICE, 
                    max_speed_hz=CONFIG_DSP.DSP_SPI_MAX_SPEED_HZ
                )
            )
        # initialize display
        self._myDSP.begin()
        self._myDSP.clear()
        self._myDSP.display()

        # create display buffer iamge.
        self._myDSP_IMG = Image.new( '1', (self._myDSP.width, self._myDSP.height) )
        # create drawing object to draw on image.
        self._myCanvas = ImageDraw.Draw(self._myDSP_IMG)

    def _saySomething(self, x, y, fontName, fontSize, msg ):
        try:
            font = ImageFont.truetype(fontName, fontSize)
        except:
            font = ImageFont.load_default()

        self._myCanvas.text((x, y), msg, font=font, fill=1)
        self._myDSP.image(self._myDSP_IMG)
        self._myDSP.display()

    def run(self):        
        self._saySomething( 8,4, "Roboto-Light.ttf", 26, "Hi")
        self._saySomething( 8,32, "Roboto-Light.ttf", 26, "RPi-Spark")

if __name__ == "__main__":
    print("Adafruit SSD1306 drive demo ...")
    demo().run()

Save this file and execute the following command in the terminal:

$> python 13_4_0_hello.py

You should see:

Hi RPi-Spark

At this time you will find that the displayed text is upside down. Because Adafruit's SSD1306 driver cannot rotate display.

You can use the SScreen class in the RPi-Spark SDK to rotate the display. SScreen supports 0, 90, 180, 270 rotate, and is compatible with Adafruit's SSD1306 driver, you can use it directly to display.

Our improved code is as follows:

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw

from JMRPiSpark.Drives.Screen.SScreenSSD1306 import SScreenSSD1306

########################################################################
# Display PINs  SPI_0
# SSD1306 OLED 128x64
class CONFIG_DSP:
    DSP_RESET       = None
    DSP_DC          = 9  #use MISO for DC
    DSP_SPI_PORT    = 0
    DSP_SPI_DEVICE  = 0
    DSP_SPI_MAX_SPEED_HZ = 2000000  #up to 8500000

    #Screen color mode BW
    SCREEN_BUFFER_COLOR_MODE_BW = "1"
    #Screen direction [0, 90, 180, 270]
    SCREEN_ROTATING = 180

class demo:
    _myScreen = None
    _myDSP = None

    def __init__(self):
        #create display
        self._myDSP = Adafruit_SSD1306.SSD1306_128_64(
            rst = CONFIG_DSP.DSP_RESET, 
            dc = CONFIG_DSP.DSP_DC, 
            spi = SPI.SpiDev(
                    CONFIG_DSP.DSP_SPI_PORT, 
                    CONFIG_DSP.DSP_SPI_DEVICE, 
                    max_speed_hz=CONFIG_DSP.DSP_SPI_MAX_SPEED_HZ
                )
            )
        # initialize display
        self._myDSP.begin()

        #create screen
        self._myScreen = SScreenSSD1306(
            self._myDSP, 
            bufferSize=(128,64),
            bufferColorMode = CONFIG_DSP.SCREEN_BUFFER_COLOR_MODE_BW, 
            displayDirection = CONFIG_DSP.SCREEN_ROTATING
        )
        # clear up screen
        self._myScreen.clear()
        self._myScreen.refresh()

    def run(self):
        self._myScreen.write("Hi\nRPi-Spark", xy=(0,0), fontName="Roboto-Light.ttf", fontSize=26  )
        self._myScreen.refresh()

if __name__ == "__main__":
    print("Adafruit SSD1306 drive demo ...")
    demo().run()

Save as:13_4_1_hello.py and execute the following command in the terminal:

$> python 13_4_1_hello.py

You should see “Hi RPi-Spark”

Hi RPi-Spark

By comparing the 4 different development methods, you will find that the first RPiSparkModule (Spark Mode) approach is the quickest and easiest, which is our preferred method of recommendation.

Every application implemented by RPiSparkModule becomes a Spark. Each Spark can executed separately or combined with other Sparks to build more complex applications.


 

Was this article helpful?

YES     |     NO

 

Enjoying the project? Spotted a mistake? Any opinions on the website? Let us know!