A Swift library for the Nokia3310/5110 PCD8544 Monochrome LCD display

Overview

5110LCD_PCD8544.swift

A Swift library for the Nokia3310/5110 PCD8544 Monochrome LCD display

Linux-only Swift 4 compatible License: BSD

LCD with Swift logo

Summary

This library is an extended Swift port of the original PCD8544 C++ library from Limor Fried/Ladyada(Adafruit Industries). With this library you will be able to draw single pixels, display text(multiple fonts), monochrome images and transparent sprites on the Nokia 3110/5110 84x48 Monochrome LCD display.

The library need 5 GPIOs output to drive the display, two of those will act as software SPI (you don't need a hardware SPI to use this library, but if SwiftyGPIO support it for your board you can use it).

Supported Boards

Every board supported by SwiftyGPIO: Raspberries, BeagleBones, C.H.I.P., etc...

To use this library, you'll need a Linux ARM board with Swift 3.x/4.x, the old Swift 2.x version of this library is available on a specific branch.

The example below will use a CHIP board but you can easily modify the example to use one the the other supported boards, full working demo projects for both the Chip and RaspberryPi2 (using the faster hardware SPI) are available in the Examples directory.

Usage

This example uses a C.H.I.P. board and its first 5 GPIOs connected as shown below, but you can easily change the board selected with GPIOs(for:) (e.g. .RaspberryPi2) and select a different set of GPIOs.

LCD diagram

(It's highly likely that the pinout of your lcd module will be different from what is shown above, check your datasheet)

So, the first thing we need to do is configure the 7 GPIOs using SwiftyGPIO:

import SwiftyGPIO
import PCD8544

let gpios = SwiftyGPIO.GPIOs(for: .CHIP)
var sclk = gpios[.P0]!
var dnmosi = gpios[.P1]!
var dnmiso = gpios[.P5]!
var cs = gpios[.P6]!

var dc = gpios[.P2]!
var sce = gpios[.P3]!
var rst = gpios[.P4]!

Next, let's create a virtual SPI to send data to the display:

var spi = VirtualSPI(mosiGPIO: dnmosi, misoGPIO: dnmiso, clockGPIO: sclk, csGPIO: cs)

Note that if your board has an hardware SPI and SwiftyGPIO supports it (i.e. every RaspberryPi board), you can create an instance of the hardware SPI object (at least one order of magnitude faster):

//The hardware SPI has MOSI in .P10 and SCLK in .P11
let spis = SwiftyGPIO.hardwareSPIs(for: .RaspberryPi2)
var spi = spis?[0]!

Then, create the display object we'll use to interact with the LCD:

var lcd = PCD8544(spi:spi,dc:dc,rst:rst,cs:sce)

That's it, you are all set, let's see what we can do on the display:

Setting individual pixels

lcd.setPixel(x:20,y:20,color:.BLACK)
lcd.setPixel(x:30,y:30,color:.BLACK)
lcd.setPixel(x:10,y:10,color:.WHITE)
lcd.display()

The setPixel function draw a single pixel at the given coordinates. .WHITE represents the transparent background, .BLACK and opaque pixel. To update the display, always call .display() once you are done.

Clearing the display

lcd.clearDisplay()

To clean the display just call .clearDisplay().

Drawing an image

To draw an image, using a bitmap buffer (create more monochrome bitmap images here):

lcd.draw(image:swift_logo,x:0,y:0,width:LCDWIDTH,height:LCDHEIGHT)
lcd.display()

The x and y parameters specify the position inside the 84x48 grid while the last two parameters are respectively the width and height og the image. The buffer is a buffer of UInt8, with size equals to width*height, buffer with an invalid size will be ignored.

If no additional parameters are specified images are opaque, i.e. white pixels will cover what's behind them, but if you need to draw a trasparent image (e.g. game sprites) add the transparent:true parameter.

Here we are displaying the Swift logo in front of a series of horizontal lines:

let alllines=[UInt8](repeating: 0xAA, count:LCDHEIGHT*LCDWIDTH/8)
lcd.draw(image:alllines,x:0,y:0,width:LCDWIDTH,height:LCDHEIGHT)
lcd.draw(image:swift_logo,x:0,y:0,width:LCDWIDTH,height:LCDHEIGHT,transparent:true)
lcd.display()

Text and Fonts

This library allows to display strings using bitmap fonts. Two fonts are included: Tiny_Font and SinclairS (more font can be found or generated here).

To display some text, you need to load a font first and that font will be used for all the following strings:

lcd.loadFontAsDefault(font:SinclairS_Font,fontWidth:8,fontHeight:8)

To draw a string simply call draw with some text and a position:

lcd.draw(text:"HelloWorld",x:0,y:0)
lcd.display()

That covers what the library can do.

Enabling the hardware SPI

To enable the SPI on the RasperryPi check out the SwiftyGPIO Wiki.

Examples

Examples are available in the Examples directory.

Installation

Please refer to the SwiftyGPIO readme for Swift installation instructions.

Once your board runs Swift, if your version support the Swift Package Manager, you can simply add this library as a dependency of your project and compile with swift build:

let package = Package(
    name: "MyProject",
    dependencies: [
	.Package(url: "https://github.com/uraimo/5110LCD_PCD8544.swift.git", majorVersion: 2),
	...
    ]
    ...
) 

The directory Examples contains sample projects that uses SPM, compile it and run the sample with sudo ./.build/debug/Test5110.

If SPM is not supported, you'll need to manually download the library and its dependencies:

wget https://raw.githubusercontent.com/uraimo/5110lcd_pcd8544.swift/master/Sources/5110lcd_pcd8544.swift https://raw.githubusercontent.com/uraimo/5110lcd_pcd8544.swift/master/Sources/font.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/Presets.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SunXi.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SPI.swift

And once all the files have been downloaded, create an additional file that will contain the code of your application (e.g. main.swift). When your code is ready, compile it with:

swiftc *.swift

The compiler will create a main executable.

As everything interacting with GPIOs, if you are not already root, you will need to run that binary with sudo ./main.

You might also like...
A testing MQTT react native library

react-native-awesome-testing abc Installation npm install react-native-awesome-testing Usage import { multiply } from "react-native-awesome-testing";

Personal plug-in library

PPILibrary Personal plug-in library 功能介绍 Core 核心基础扩展, 不依赖于任何三方库 使用 根据subspec指定 pod 'PPILibrary', :subspecs = ['Core'], :git = 'https://github.com/Sh

🐦 An asynchronous Twitter library based on the Twitter v2 API

Twift Twift is an asynchronous Swift library for the Twitter v2 API. No external dependencies Only one callback-based method (requestUserCredentials)

Keybase Go Library, Client, Service, OS X, iOS, Android, Electron
Keybase Go Library, Client, Service, OS X, iOS, Android, Electron

Keybase Hi, and welcome to the Keybase client repo. All our client apps (macOS, Windows, Linux, iOS, and Android) are being actively developed in this

Blazing⚡️Fast BTC and ETH Wallet Generator library for React Native, Android and iOS

Blazing ⚡️ Fast BTC and ETH Wallet Generator library for React Native, Android and iOS.

Apple SMC library & tool

SMCKit An Apple System Management Controller (SMC) library & command line tool in Swift for Intel based Macs. The library works by talking to the Appl

A modern runtime (soon: modification) library

Bridge A Modern Runtime (soon: Modification) Library Setup swift package generate-xcodeproj Whenever you need to create/modify a target, please regen

A Kotlin multiplatform library for building dynamic server-driven UI
A Kotlin multiplatform library for building dynamic server-driven UI

Component Box · A Kotlin multiplatform library for building dynamic server-driven UI. Material Component-Based Interoperable Dynamic What You See Is W

React Native utility library around image and video files for getting metadata like MIME type, timestamp, duration, and dimensions. Works on iOS and Android using Java and Obj-C, instead of Node 🚀.

Qeepsake React Native File Utils Extracts information from image and video files including MIME type, duration (video), dimensions, and timestamp. The

Comments
  • strange behavior on display drawing

    strange behavior on display drawing

    Watch the attached gif, is that normal that i can see image being drawn block by block? This is my code

    let gpios = SwiftyGPIO.getGPIOsForBoard(.RaspberryPiPlus2Zero)
    
    var clk = gpios[.P17]!
    var din = gpios[.P18]!
    var dc = gpios[.P27]!
    var cs = gpios[.P22]!
    var rst = gpios[.P23]!
    
    
    var spi = VirtualSPI(dataGPIO: din, clockGPIO: clk)
    
    var lcd = PCD8544(spi: spi, dc: dc, rst: rst, cs: cs)
    lcd.clearDisplay()
    lcd.display()
    
    lcd.drawImage(swift_logo,x:0,y:0,width:LCDWIDTH,height:LCDHEIGHT,transparent:true)
    lcd.display()
    

    lcd

    opened by eugeniobaglieri 12
  • Updated package format and updated to 4.2. There were a few errors re…

    Updated package format and updated to 4.2. There were a few errors re…

    …lated to truncatedbitpattern that were fixed.

    I'm not 100% sure about my fix for the "truncatedbitpattern" errors. Would you mind taking a look? I've fixed the package format though.

    Thank you for this library!

    opened by Radagan 3
Owner
uraimo
uraimo
React Native library that implements PayPal Checkout flow using purely native code (swift).

react-native-paypal-swift React Native library that implements PayPal Checkout flow using purely native code (swift). Installation npm install react-n

Tibb 6 Nov 28, 2022
A Swift library for Panasonic AMG88 Grid-EYE thermopile sensors.

AMG88xx A Swift library for the Panasonic AMG88 family of sensors. Summary This library can read temperature data from the AMG88 thermopile sensor. Us

Emory Dunn 2 Dec 31, 2021
Swift library for parsing Extended Backus–Naur Form (EBNF) notation

Gramophone Swift library for parsing Extended Backus–Naur Form (EBNF) notation Integration Swift Package Manager dependencies: [ .package(url: "ht

Chime 5 Jul 28, 2022
KnockToReact is an iOS library written in Swift and Objective-C that brings an exclusive feature to interact with users just by receiving and recognizing "knocks" in the device.

KnockToReact is an iOS library written in Swift and Objective-C that brings an exclusive feature to interact with users just by receiving and recognizing "knocks" in the device.

Matheus Cavalca 25 Feb 10, 2022
A Swift library for hardware projects on Linux/ARM boards with support for GPIOs/SPI/I2C/PWM/UART/1Wire.

A Swift library for hardware projects on Linux/ARM boards with support for GPIOs/SPI/I2C/PWM/UART/1Wire. Summary This library provides an easy way to

uraimo 1.3k Dec 26, 2022
SwiftyXPC - a wrapper for Apple’s XPC interprocess communication library that gives it an easy-to-use, idiomatic Swift interface.

SwiftyXPC is a wrapper for Apple’s XPC interprocess communication library that gives it an easy-to-use, idiomatic Swift interface.

null 36 Jan 1, 2023
Pexels API client library for the Swift programming language.

Pexels-Swift Pexels.com API client library for the Swift programming language. Overview This Swift Package is a wrapper for Pexels API to get access t

Lukas Pistrol 4 Sep 1, 2022
A Swift library for Discord REST/Gateway API in all platforms.

swift-discord main develop A Swift library for Discord API. Package Products Discord, alias library that contains DiscordREST, DiscordGateway. Discord

swift-discord 3 Sep 30, 2022
A Swift cross-platform (Apple and Linux) networking library.

KippleNetworking A Swift library that offers cross-platform (Apple and Linux) networking support, intended for the creation of cross-platform SDKs to

Kipple 11 Sep 20, 2022
Cross-platform Swift library for accessing the public GitHub API.

GoatHerb GoatHerb is a cross-platform Swift library for accessing the GitHub API. Features General Compatible with swift-log. Full concurrency (async/

Brian Drelling 3 Oct 30, 2022