Skip to content

Commit

Permalink
Use memory mapped array for LED matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Apr 9, 2017
1 parent aa14a9a commit 10eb680
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ SenseHat.jl requires the Raspbian `sense-hat` package:

## LED matrix

The `led_display(X)` function will display an 8×8 matrix of colors on the LED matrix (see [ColorTypes.jl](https://github.com/JuliaGraphics/ColorTypes.jl)). `led_clear()` will set all the LEDs to black.
The main interface is the `led_matrix()` function, which creates an 8×8 array of RGB values (from [ColorTypes.jl](https://github.com/JuliaGraphics/ColorTypes.jl)) which is memory-mapped to the frame buffer of the LED matrix.

using SenseHat

led_display(SenseHat.JULIA_LOGO)
sleep(1)
led_clear()
using ColorTypes

const LED = led_matrix()

LED[:] = SenseHat.JULIA_LOGO
sleep(3)
LED[:] = RGB(0,0,0)

## Joystick

Expand Down
2 changes: 1 addition & 1 deletion src/SenseHat.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__precompile__()
module SenseHat

export led_display, led_clear,
export led_matrix, RGB565,
Stick, StickEvent, sticktask, readstick

include("led.jl")
Expand Down
32 changes: 31 additions & 1 deletion src/led.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module LED
export led_display, led_clear, RGB565
export led_matrix, RGB565

using ColorTypes, FixedPointNumbers

function _led_fb_device()
Expand Down Expand Up @@ -44,6 +45,33 @@ ColorTypes.blue(c::RGB565) = U5(c.data & 0x1f, Val{true})
ColorTypes.ccolor{Csrc<:Colorant}(::Type{RGB565}, ::Type{Csrc}) = RGB565
ColorTypes.base_color_type(::Type{RGB565}) = RGB565

"""
led_matrix()
Returns an 8x8 matrix of `RGB565` elements that is memory-mapped to the Sense HAT LED Matrix.
While it is possible to invoke the function multiple times (each returning different
arrays), it is generally preferable to assign it once into a `const` variable so as to
minimise the number of open file handlers.
# Example
```
using SenseHat
using ColorTypes
const LED = led_matrix()
LED[:] = SenseHat.JULIA_LOGO
sleep(3)
LED[:] = RGB(0,0,0)
```
"""
led_matrix() = Mmap.mmap(LED_FB_DEVICE, Array{RGB565,2}, (8,8); grow=false)




"""
led_display(X)
Expand All @@ -57,6 +85,7 @@ See also:
* `led_clear` for clearing the LED matrix.
"""
function led_display(X)
Base.depwarn("`led_display(X)` has been deprecated, use `led_matrix()[:] = X` instead.", :led_display)
size(X) == (8,8) || throw(DimensionMismatch("Can only display 8x8 images"))
open(LED_FB_DEVICE, "w") do fb
for j = 1:8
Expand All @@ -73,6 +102,7 @@ end
Sets the SenseHat LED matrix to all black.
"""
function led_clear()
Base.depwarn("`led_clear()` has been deprecated, use `led_matrix()[:] = SenseHat.RGB565(0,0,0)` instead.", :led_display)
open(LED_FB_DEVICE, "w") do fb
for j = 1:8
for i = 1:8
Expand Down
10 changes: 7 additions & 3 deletions test/led.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using SenseHat
using Base.Test

led_display(SenseHat.JULIA_LOGO)
sleep(1)
led_clear()
using ColorTypes

const LED = led_matrix()

LED[:] = SenseHat.JULIA_LOGO
sleep(3)
LED[:] = RGB(0,0,0)
1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

include("led.jl")

0 comments on commit 10eb680

Please sign in to comment.