Skip to content

Commit 10eb680

Browse files
committed
Use memory mapped array for LED matrix
1 parent aa14a9a commit 10eb680

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ SenseHat.jl requires the Raspbian `sense-hat` package:
1010

1111
## LED matrix
1212

13-
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.
13+
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.
1414

1515
using SenseHat
16-
17-
led_display(SenseHat.JULIA_LOGO)
18-
sleep(1)
19-
led_clear()
16+
using ColorTypes
17+
18+
const LED = led_matrix()
19+
20+
LED[:] = SenseHat.JULIA_LOGO
21+
sleep(3)
22+
LED[:] = RGB(0,0,0)
2023

2124
## Joystick
2225

src/SenseHat.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__precompile__()
22
module SenseHat
33

4-
export led_display, led_clear,
4+
export led_matrix, RGB565,
55
Stick, StickEvent, sticktask, readstick
66

77
include("led.jl")

src/led.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module LED
2-
export led_display, led_clear, RGB565
2+
export led_matrix, RGB565
3+
34
using ColorTypes, FixedPointNumbers
45

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

48+
"""
49+
led_matrix()
50+
51+
Returns an 8x8 matrix of `RGB565` elements that is memory-mapped to the Sense HAT LED Matrix.
52+
53+
While it is possible to invoke the function multiple times (each returning different
54+
arrays), it is generally preferable to assign it once into a `const` variable so as to
55+
minimise the number of open file handlers.
56+
57+
# Example
58+
```
59+
using SenseHat
60+
using ColorTypes
61+
62+
const LED = led_matrix()
63+
64+
LED[:] = SenseHat.JULIA_LOGO
65+
sleep(3)
66+
LED[:] = RGB(0,0,0)
67+
```
68+
69+
"""
70+
led_matrix() = Mmap.mmap(LED_FB_DEVICE, Array{RGB565,2}, (8,8); grow=false)
71+
72+
73+
74+
4775
"""
4876
led_display(X)
4977
@@ -57,6 +85,7 @@ See also:
5785
* `led_clear` for clearing the LED matrix.
5886
"""
5987
function led_display(X)
88+
Base.depwarn("`led_display(X)` has been deprecated, use `led_matrix()[:] = X` instead.", :led_display)
6089
size(X) == (8,8) || throw(DimensionMismatch("Can only display 8x8 images"))
6190
open(LED_FB_DEVICE, "w") do fb
6291
for j = 1:8
@@ -73,6 +102,7 @@ end
73102
Sets the SenseHat LED matrix to all black.
74103
"""
75104
function led_clear()
105+
Base.depwarn("`led_clear()` has been deprecated, use `led_matrix()[:] = SenseHat.RGB565(0,0,0)` instead.", :led_display)
76106
open(LED_FB_DEVICE, "w") do fb
77107
for j = 1:8
78108
for i = 1:8

test/led.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using SenseHat
22
using Base.Test
33

4-
led_display(SenseHat.JULIA_LOGO)
5-
sleep(1)
6-
led_clear()
4+
using ColorTypes
5+
6+
const LED = led_matrix()
7+
8+
LED[:] = SenseHat.JULIA_LOGO
9+
sleep(3)
10+
LED[:] = RGB(0,0,0)

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
21
include("led.jl")

0 commit comments

Comments
 (0)