Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add orientation views to led_matrix() #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/led.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ ColorTypes.base_color_type(::Type{RGB565}) = RGB565


"""
led_matrix()
led_matrix(orientation::Symbol)

Returns an 8x8 matrix of `RGB565` elements that is memory-mapped to the Sense HAT LED Matrix.
Returns an 8x8 matrix view of `RGB565` elements that is memory-mapped to the Sense HAT LED Matrix.

Orientation can be set to: default, right, left, invert
See the example below for usage

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
Expand All @@ -66,18 +69,31 @@ 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[:] .= colorant"blue"

# Make the display upside-down, top points to HDMI connector
const LED2 = led_matrix(:invert) # Make the display upside-down, top points to HDMI connector
LED2[:] = SenseHat.JULIA_LOGO
sleep(3)
LED2 .= colorant"black"
```

"""
led_matrix() = Mmap.mmap(LED_FB_DEVICE[], Array{RGB565,2}, (8,8); grow=false)
led_matrix(orientation::Symbol) = led_matrix(Val{orientation})

led_matrix() = led_matrix(:default)
led_matrix(::Type{Val{:default}}) = @view PermutedDimsArray(Mmap.mmap(LED_FB_DEVICE[],
Array{RGB565,2}, (8,8); grow=false),(2,1))[:,:];
led_matrix(::Type{Val{:right}}) = @view Mmap.mmap(LED_FB_DEVICE[],
Array{RGB565,2}, (8,8); grow=false)[8:-1:1,:];
led_matrix(::Type{Val{:left}}) = @view Mmap.mmap(LED_FB_DEVICE[],
Array{RGB565,2}, (8,8); grow=false)[:,8:-1:1];
led_matrix(::Type{Val{:invert}}) = @view PermutedDimsArray(Mmap.mmap(LED_FB_DEVICE[],
Array{RGB565,2}, (8,8); grow=false),(2,1))[8:-1:1,8:-1:1];



Expand Down
16 changes: 8 additions & 8 deletions src/led_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const JULIA_LOGO = begin
p = RGB565(0.667,0.475,0.757)
z = RGB565(0.0,0.0,0.0)

permutedims([z z z z z z z z;
z z z G G z z z;
z z G g g G z z;
z z G g g G z z;
z R R G G P P z;
R r r R P p p P;
R r r R P p p P;
z R R z z P P z], (2,1)) # transpose to correct orientation
[z z z z z z z z;
z z z G G z z z;
z z G g g G z z;
z z G g g G z z;
z R R G G P P z;
R r r R P p p P;
R r r R P p p P;
z R R z z P P z]; # transpose to correct orientation
end