From ea73d7f6c78cd38df17a4c3fcebd95fb1da31ca6 Mon Sep 17 00:00:00 2001 From: Ellipse0934 Date: Wed, 31 Oct 2018 23:38:58 +0530 Subject: [PATCH] Add orientation views to led_matrix() --- src/led.jl | 30 +++++++++++++++++++++++------- src/led_extra.jl | 16 ++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/led.jl b/src/led.jl index 9b5259d..5ae9d79 100644 --- a/src/led.jl +++ b/src/led.jl @@ -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 @@ -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]; diff --git a/src/led_extra.jl b/src/led_extra.jl index 3650650..8c90295 100644 --- a/src/led_extra.jl +++ b/src/led_extra.jl @@ -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