|
1 | 1 | module Text
|
2 | 2 |
|
| 3 | +using ColorTypes, Colors.@colorant_str |
| 4 | + |
3 | 5 | import SenseHat.LED: RGB565, led_matrix, led_clear
|
4 | 6 |
|
5 |
| -export show_char, show_message |
| 7 | +export show_char, show_message, @colorant_str |
6 | 8 |
|
7 | 9 | """
|
8 |
| - show_char(c::Char, color::RGB565 = RGB565(1.0,1.0,1.0)) |
| 10 | + show_char(c::Char, color::ColorTypes.AbstractRGB = colorant"white") |
9 | 11 |
|
10 |
| -Display a single character `c` on the `Sense-Hat` LED Matrix. |
| 12 | +Display a single character `c` on the `SenseHat` LED Matrix. |
11 | 13 | """
|
12 |
| -function show_char(c::Char, color::RGB565 = RGB565(1.0,1.0,1.0)) |
13 |
| - led_clear() |
14 |
| - if haskey(font, c) |
15 |
| - convert(::Type{RGB565}, x::Bool) = x ? color : RGB565(0.0,0.0,0.0) |
16 |
| - img = hcat(font[c], Bool.(zeros(8, 3))) |
17 |
| - img = convert.(RGB565, img) |
18 |
| - led_matrix()[:] = permutedims(img, (2,1)) |
19 |
| - else |
20 |
| - error("Character font for $c not available \n") |
21 |
| - end |
22 |
| - return |
| 14 | +function show_char(c::Char, color::ColorTypes.AbstractRGB = colorant"white") |
| 15 | + if haskey(font, c) |
| 16 | + tocolor(b) = b ? RGB565(color) : RGB565(colorant"black") |
| 17 | + L = PermutedDimsArray(led_matrix(), (2, 1)) |
| 18 | + L[:] .= colorant"black" |
| 19 | + L[:,2:6] .= tocolor.(font[c]) |
| 20 | + else |
| 21 | + error("Character font for $c not available \n") |
| 22 | + end |
| 23 | + return |
23 | 24 | end
|
24 | 25 |
|
25 |
| - |
26 | 26 | """
|
27 |
| - show_message(s::String, speed::Real = 0.2, color::RGB565 = RGB565(1.0,1.0,1.0)) |
| 27 | + show_message(s::String, speed::Real = 0.2, color::ColorTypes.AbstractRGB = colorant"white") |
| 28 | +
|
| 29 | +Display a scrolling message `s` on the `SenseHat` LED Matrix. `speed` is the time in seconds per frame and `color` is the color of text. |
28 | 30 |
|
29 |
| -Display a scrolling message `s` on the `Sense-Hat` LED Matrix. `speed` is the time in seconds per frame. |
| 31 | +# Example |
| 32 | +
|
| 33 | +``` |
| 34 | +using SenseHat |
| 35 | +
|
| 36 | +show_message("Hello, World!", 0.2, colorant"purple") |
| 37 | +``` |
30 | 38 | """
|
31 |
| -function show_message(s::String, speed::Real = 0.33, color::RGB565 = RGB565(1.0,1.0,1.0)) |
32 |
| - for c in s |
33 |
| - if haskey(font, c) == false |
34 |
| - error("Character font for $c not available \n") |
35 |
| - return |
36 |
| - end |
37 |
| - end |
38 |
| - convert(::Type{RGB565}, x::Bool) = x ? color : RGB565(0.0,0.0,0.0) |
39 |
| - led_clear() |
40 |
| - img = Array{Bool}(8, 16+5*length(s)) |
41 |
| - img[1:8,1:8] = Bool.(zeros(8,8)) |
42 |
| - img[1:8,(8+5*length(s)):(15+5*length(s))] = Bool.(zeros(8,8)) |
43 |
| - for i in 1:length(s) |
44 |
| - img[1:8, (4 + 5*i):(5*i+8)] = font[s[i]] |
45 |
| - end |
46 |
| - for i in 1:(size(img)[2] - 8) |
47 |
| - frame = convert.(RGB565, img[1:8, i:(i+7)]) |
48 |
| - led_matrix()[:] = permutedims(frame, (2,1)) |
49 |
| - sleep(speed) |
50 |
| - end |
51 |
| - led_clear() |
52 |
| - return |
| 39 | +function show_message(s::String, speed::Real = 0.2, color::ColorTypes.AbstractRGB = colorant"white") |
| 40 | + for c in s |
| 41 | + if haskey(font, c) == false |
| 42 | + error("Character font for $c not available \n") |
| 43 | + return |
| 44 | + end |
| 45 | + img = Array{Bool}(8, 16 + 5*length(s)) |
| 46 | + img[1:8,1:8] = Bool.(zeros(8,8)) |
| 47 | + img[1:8,(9 + 5*length(s)):(16 + 5*length(s))] = Bool.(zeros(8,8)) |
| 48 | + for i in 1:length(s) |
| 49 | + img[1:8, (4 + 5*i):(8 + 5*i)] = font[s[i]] |
| 50 | + end |
| 51 | + tocolor(b) = b ? color : colorant"black" |
| 52 | + for i in 1:(size(img,2) - 7) |
| 53 | + frame = tocolor.(img[1:8, i:(i + 7)]) |
| 54 | + led_matrix()[:] = permutedims(frame, (2,1)) |
| 55 | + sleep(speed) |
| 56 | + end |
| 57 | + return |
| 58 | + end |
53 | 59 | end
|
54 | 60 |
|
| 61 | +show_message(s::String, color::ColorTypes.AbstractRGB) = show_message(s, 0.2, color) |
| 62 | + |
55 | 63 | font = Dict(' ' => Bool.([0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; ]),
|
56 | 64 | '+' => Bool.([0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 1 0 0 ; 0 0 1 0 0 ; 1 1 1 1 1 ; 0 0 1 0 0 ; 0 0 1 0 0 ; 0 0 0 0 0 ; ]),
|
57 | 65 | '-' => Bool.([0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 1 1 1 1 1 ; 0 0 0 0 0 ; 0 0 0 0 0 ; 0 0 0 0 0 ; ]),
|
|
0 commit comments