blinkt

package module
v0.0.0-...-06794f0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

blinkt

This library is a fork of Alex Ellis' blinkt_go that I was intending to port to my gpiocdev library, but ended up going off on a bit of a tangent and also using it as a testbed for comparing various GPIO implementations, and incorporating a merge/port of Alex's blinkt_go_examples.

Quick Start

import "github.com/warthog618/blinkt"

// create a blinkt to control the display
bl := blinkt.New()

// set the display brightness
bl.SetBrightness(23)

// write to pixels
bl.SetPixel(0, r, g, b)
...
bl.SetPixel(7, r, g, b)

// show the pixels on the display
bl.Show()

// clear the display
bl.Clear()
bl.Show()

// finish with the display
bl.Close()

Usage

The following is a short summary of the Blinkt API.

New

Create a new Blinkt object using *New":

bl := blinkt.New()
Close

If you no longer need to use the display, or your application is exiting, you should close the display:

bl.Close()
SetClearOnExit

By default the display is cleared when closed. This can be changed with the SetClearOnExit method:

bl.SetClearOnExit(false)
Whole Display

The following methods act on all pixels in the display.

Note that nothing is actually written to the physical display until Show is called.

SetAll

Set the colour of all pixels at once using SetAll:

bl.SetAll(r, g, b)

Setting the colour does not alter the brightness, which is independent.

SetBrightness

Set the brightness of all pixels at once using SetBrightness:

bl.SetBrightness(42)

The brightness value is a percent, so 0-100. By default the display brightness is set to 50.

Clear

Turn off all pixels using the Clear method:

bl.Clear()
Show

Update the display with the current pixel states using Show:

bl.Show()

The other methoss only update the pixels in memory - the Show command commits that state to the display.

The Show method performs the update in a separate goroutine using a snapshot of the pixel state, so the main goroutine can immediately begin updating the pixel state while the snapshot is being written to the display.

The Show method will block if the background goroutine is still updating the display with the previous Show.

Individual Pixels

The following methods apply to an individual pixel.

The pixel parameter which identifies the pixel, and is denoted by p in these snippets, is in the range 0-7.

ClearPixel

Turn off the pixel using the ClearPixel method:

bl.ClearPixel(p)
SetPixel

Set the colour of the pixel using SetPixel:

bl.SetPixel(p, r, g, b)

Setting the colour does not alter the brightness, which is independent.

SetPixelBrightness

Set the brightness of the pixel using SetPixelBrightness:

bl.SetPixelBrightness(68)
Goroutine Safety

The Blinkt is not safe to call from multiple goroutines, with the exception of the Close method, which is made goroutine safe so it can be called from signal handlers, e.g. when the application is closing down.

Backends

The library provides a selection of backends to drive the GPIO, with the selection being performed by a build tag. e.g.

go build -tags blinkt_wiringpi

will build an application using the WiringPi backend.

The default backend is my gpiocdev library, which makes use of the official Linux GPIO interface.

Backend Build tag Interface
gpiocdev none (default) Linux GPIO character device (/dev/gpiochip0) uAPI
gpio blinkt_gpio Raspberry Pi /dev/gpiomem direct access to the BCM hardware
wiringpi blinkt_wiringpi WiringPi cgo wrapper
sysfs blinkt_sysfs deprecated GPIO SYSFS interface

Examples

The examples directory contains a collection of examples that demonstrate the library and display.

These were originally ported from Alex's blinkt_go_examples which are themselves largely ports of some of the Pimoroni examples.

I've ported a few more of those, such as the binary clocks and rainbow.

Benchmarks

These are the results from a Raspberry Pi Zero W running Linux v5.10 and built with go1.15.6:

$ go test -test.bench=.*
goos: linux
goarch: arm
pkg: github.com/warthog618/blinkt
BenchmarkShow 	     106	  10547923 ns/op
PASS
ok  	github.com/warthog618/blinkt	1.683s

A summary of the results for each of the backends:

Backend ns/op Shows/sec
gpiocdev 10547923 94
gpio 102205 9784
wiringpi 3435945 291
sysfs 66638981 15

For comparison the Python/WiringPi implementation can perform around 9 shows/sec on the same platform.

My recommendation is to use the gpiocdev backend unless you have a serious need for speed or very low CPU utilization (and aren't concerned about conflicting with other BCM drivers), in which case the gpio library is a clear winner.

I could only recommend the Python interface for very low bandwidth applications.

Documentation

Overview

Package blinkt provides control over a Blinkt! LED display

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APA102

type APA102 struct {
	// contains filtered or unexported fields
}

APA102 implements the interface to the Blinkt! hardware

func (*APA102) Close

func (a *APA102) Close()

Close the interface to the APA102

func (*APA102) Open

func (a *APA102) Open()

Open the interface to APA102

func (*APA102) WriteBit

func (a *APA102) WriteBit(bit int)

WriteBit writes a single data bit to the APA102

func (*APA102) WriteFrame

func (a *APA102) WriteFrame(val uint)

WriteFrame writes a single LED frame to the APA102.

The frame value is passed as a 32bit uint and it clocked out MSB first.

func (*APA102) WritePixels

func (a *APA102) WritePixels(pixels []pixel)

WritePixels writes a sequence of pixels to the display.

The pixels are prefixed with a start frame and terminated with the end frame.

type Blinkt

type Blinkt struct {
	// Pixels is the number of pixels supported by the display
	Pixels int
	// contains filtered or unexported fields
}

Blinkt provides control over a Blinkt! LED display

func New

func New() Blinkt

New creates a Blinkt to control the display.

func (*Blinkt) Clear

func (bl *Blinkt) Clear()

Clear sets all the pixels to off.

Show must still be called to update the physical display.

func (*Blinkt) ClearPixel

func (bl *Blinkt) ClearPixel(p int)

ClearPixel sets the pixel to off.

Show must still be called to update the physical display.

func (*Blinkt) Close

func (bl *Blinkt) Close()

Close the Blinkt interface.

func (*Blinkt) SetAll

func (bl *Blinkt) SetAll(r, g, b int)

SetAll sets all pixels to specified r, g, b colour.

Show must be called to update the LEDs.

func (*Blinkt) SetBrightness

func (bl *Blinkt) SetBrightness(brightness float64)

SetBrightness sets the brightness of all pixels.

Brightness should be in percent, i.e. 0 to 100.0. Greater than or equal to 100 is assumed to mean full brightness. Less than or equal to 0 is assumed to mean off.

func (*Blinkt) SetClearOnExit

func (bl *Blinkt) SetClearOnExit(clearOnExit bool)

SetClearOnExit controls the blanking of the display when the Blinkt is closed.

When clearOneExit is true all pixels are turned off when Blinkt is closed. This is the default. When clearOneExit is false the display is left in its current state.

func (*Blinkt) SetPixel

func (bl *Blinkt) SetPixel(p, r, g, b int)

SetPixel sets an individual pixel to specified r, g, b colour.

Show must be called to update the LEDs.

func (*Blinkt) SetPixelBrightness

func (bl *Blinkt) SetPixelBrightness(p int, brightness float64)

SetPixelBrightness sets the brightness of pixel p.

Brightness should be in percent, i.e. 0 to 100.0. Greater than or equal to 100 is assumed to mean full brightness. Less than or equal to 0 is assumed to mean off.

func (*Blinkt) Show

func (bl *Blinkt) Show()

Show updates the physical dispolay with the values from Set/Clear.

Directories

Path Synopsis
example
1dtetris command
A blinkt demo that displays the currrent time in binary.
A blinkt demo that displays the currrent time in binary.
binary_clock command
A blinkt demo that displays the currrent time in binary.
A blinkt demo that displays the currrent time in binary.
binary_clock_meld command
A blinkt demo that displays the currrent time in binary.
A blinkt demo that displays the currrent time in binary.
cheerlights command
A blinkt demo that displays the current CheerLights colour.
A blinkt demo that displays the current CheerLights colour.
cputemp command
A blinkt demo that displays the CPU temperature.
A blinkt demo that displays the CPU temperature.
progress command
A blinkt demo that cycles a red led backwards and forwards across the display, Cylon style.
A blinkt demo that cycles a red led backwards and forwards across the display, Cylon style.
rainbow command
A blinkt demo that cycles a rainbow of colours.
A blinkt demo that cycles a rainbow of colours.
random_blink command
A blinkt demo that continually sets the led to random colours.
A blinkt demo that continually sets the led to random colours.
random_blink_colours command
A blinkt demo that continually sets the leds to random colours.
A blinkt demo that continually sets the leds to random colours.
redis_view command
A blinkt demo that sets the display using values provided by a Redis pubsub channel.
A blinkt demo that sets the display using values provided by a Redis pubsub channel.
resistor_clock command
A blinkt demo that displays the currrent time using resistor band colours.
A blinkt demo that displays the currrent time using resistor band colours.
solid_colours command
A blinkt demo that cycles the whole display through red green and blue.
A blinkt demo that cycles the whole display through red green and blue.
solid_colours_brightness command
A blinkt demo that cycles the whole display through red green and blue with varying brightness.
A blinkt demo that cycles the whole display through red green and blue with varying brightness.
spacecount command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址