CircuitPython Usage

It's easy to use the Sharp Memory Display with CircuitPython and the Adafruit CircuitPython SharpMemoryDisplay module. This module allows you to easily write Python code to control the display.

You can use this display with any CircuitPython microcontroller board.

To demonstrate the usage, we'll initialize the library and use Python code to control the LCD from the board's Python REPL.

Initialization

First need to initialize the SPI bus. To do that, run the following commands:

Download: file
import board
import busio
import digitalio
import adafruit_sharpmemorydisplay

spi = busio.SPI(board.SCK, MOSI=board.MOSI)
scs = digitalio.DigitalInOut(board.D6) # inverted chip select

display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168)

The last three parameters to the initializer are the pins connected to the display's CS line, width and height in that order.  Again make sure to use the right pin names as you have wired up to your board!

Drawing

The SharpMemoryDisplay module currently supports a basic set of commands to draw on the display.  You can set individual pixels, fill the screen, and write lines of text. 

To fill or clear the entire screen use the fill function.  This function takes a parameter which specifies the color to fill with, either 0 for black or 1 for white.  For example to fill the screen black:

Download: file
display.fill(0)
display.show()

Notice the fill function doesn't actually change the display.  You must call show after making drawing commands to send the updated pixel data to the display!

To clear the screen to white just call fill again but with the color 1:

Download: file
display.fill(1)
display.show()

To set a pixel use the pixel function.  This function takes the following parameters:

  • Pixel X position
  • Pixel Y position
  • Pixel color (0 = black, 1 = white)

For example to set the pixel at (10, 10) black:

Download: file
display.pixel(10, 10, 0)
display.show()

Try setting other pixels white by changing the X and Y position.  Remember you have to call show after setting pixels to see them appear!

Text

To write text to your display, you must download a font file and copy it to your CIRCUITPY drive. Click the button below to download the file, and then copy font5x8.bin to your CIRCUITPY drive.

You can write a line of text with the text function.  This function takes the following parameters:

  • String of text
  • Text X position
  • Text Y position
  • Text color (0 = black, 1 = white)

For example to clear the screen and then write two lines of text:

Download: file
display.fill(1)
display.text('Hello', 0, 0, 0)
display.text('World', 0, 10, 0)
display.show()

Notice the second line of text starts at Y position 10, this moves it down the display 10 pixels so it's below the first line of text.  The font used by the text function is 8 pixels tall so a size of 10 gives a bit of room between the lines.

That's all there is to drawing on the Sharp Memory Display with CircuitPython! The drawing functions are basic but provide building blocks for more advanced usage.  For example you can display text with sensor readings or other state, or even program a simple game like pong!

Full Example Code

import time
import board
import busio
import digitalio

import adafruit_sharpmemorydisplay

# Initialize SPI bus and control pins
spi = busio.SPI(board.SCK, MOSI=board.MOSI)
scs = digitalio.DigitalInOut(board.D6) # inverted chip select

# pass in the display size, width and height, as well
# display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 96, 96)
display = adafruit_sharpmemorydisplay.SharpMemoryDisplay(spi, scs, 144, 168)

print("Pixel test")

# Clear the display.  Always call show after changing pixels to make the display
# update visible!
display.fill(1)
display.show()

# Set a pixel in the origin 0,0 position.
display.pixel(0, 0, 0)
# Set a pixel in the middle position.
display.pixel(display.width//2, display.width//2, 0)
# Set a pixel in the opposite corner position.
display.pixel(display.width-1, display.height-1, 0)
display.show()
time.sleep(2)

print("Lines test")
# we'll draw from corner to corner, lets define all the pair coordinates here
corners = ((0, 0), (0, display.height-1), (display.width-1, 0),
           (display.width-1, display.height-1))

display.fill(1)
for corner_from in corners:
    for corner_to in corners:
        display.line(corner_from[0], corner_from[1],
                     corner_to[0], corner_to[1], 0)
display.show()
time.sleep(2)

print("Rectangle test")
display.fill(1)
w_delta = display.width / 10
h_delta = display.height / 10
for i in range(11):
    display.rect(0, 0, int(w_delta*i), int(h_delta*i), 0)
display.show()
time.sleep(2)

print("Text test")
display.fill(1)
display.text(' hello world!', 0, 0, 0)
display.text(' This is the', 0, 8, 0)
display.text(' CircuitPython', 0, 16, 0)
display.text('adafruit library', 0, 24, 0)
display.text(' for the SHARP', 0, 32, 0)
display.text(' Memory Display :) ', 0, 40, 0)
display.show()
This guide was first published on Jun 22, 2013. It was last updated on Jun 22, 2013. This page (CircuitPython Usage) was last updated on Jan 21, 2020.