Skip to content

Commit ec5c748

Browse files
committed
small changes
updated readme, improved show_char and show_message
1 parent 0c4b278 commit ec5c748

File tree

4 files changed

+86
-51
lines changed

4 files changed

+86
-51
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function for resetting the LED matrix to black.
1717

1818
using SenseHat
1919
using ColorTypes
20-
20+
2121
const LED = led_matrix()
22-
22+
2323
LED[:] = SenseHat.JULIA_LOGO
2424
sleep(3)
2525
led_clear()
@@ -33,20 +33,19 @@ manipulated, returning a `StickEvent`:
3333

3434
e = readstick()
3535

36-
For querying within a loop, `sticktask()` will create a `Task` that produces `StickEvent`s. Use
37-
with `@schedule` for asynchronous use, e.g.
36+
For querying within a loop, use a `Channel` to create a buffer of `StickEvent`.
3837

3938
using SenseHat
4039

41-
@schedule for e in sticktask()
42-
println(e)
40+
c = Channel{StickEvent}(32)
41+
42+
@async while true
43+
put!(c, readstick())
4344
end
4445

45-
will create a new task that prints the event, then and add it to the scheduler. See the
46-
help for `StickEvent` and `sticktask` for more details.
4746

4847
## Sensors
4948

5049
`humidity()`, `temperature()` and `pressure()` will read values from the corresponding sensors.
5150

52-
The inertial measurement unit (IMU) is not yet supported, but is coming soon. In the meantime, you can use the [python library](https://pythonhosted.org/sense-hat/) via [PyCall.jl](https://github.com/JuliaPy/PyCall.jl).
51+
The inertial measurement unit (IMU) is not yet supported, but is coming soon. In the meantime, you can use the [python library](https://pythonhosted.org/sense-hat/) via [PyCall.jl](https://github.com/JuliaPy/PyCall.jl).

src/SenseHat.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module SenseHat
33

44
export led_matrix, RGB565, led_clear,
55
show_char, show_message,
6-
Stick, StickEvent, readstick,
7-
pressure, temperature, humidity
6+
Stick, StickEvent, readstick, sticktask,
7+
pressure, temperature, humidity,
8+
@colorant_str
89

910

1011
include("ioctl.jl")

src/stick.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Stick
22

3-
export StickEvent, readstick
3+
export StickEvent, readstick, sticktask
44

55
function _stick_input_device()
66
try
@@ -83,4 +83,31 @@ function readstick()
8383
end
8484
end
8585

86+
"""
87+
sticktask()
88+
This is a `Task` that produces `StickEvent`s when the joystick on the Sense HAT is
89+
manipulated. It will block until new `StickEvent`s occur.
90+
A typical usage will be to create a new task which will call this asynchronously, e.g. the
91+
following will call the function `f(::StickEvent)` for each event:
92+
```
93+
@schedule for e in sticktask()
94+
f(e)
95+
end
96+
```
97+
"""
98+
function sticktask()
99+
Base.depwarn("`sticktask()` is deprecated. Use `Channel` instead to create a buffer of `StickEvent`.", :sticktask)
100+
@task open(STICK_INPUT_DEVICE) do dev
101+
fddev = RawFD(fd(dev))
102+
while true
103+
wait(fddev, readable=true)
104+
s = read(dev, StickEventRaw)
105+
if s.ev_type == EV_KEY
106+
produce(StickEvent(s))
107+
end
108+
end
109+
end
110+
end
111+
112+
86113
end # module

src/text.jl

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,65 @@
11
module Text
22

3+
using ColorTypes, Colors.@colorant_str
4+
35
import SenseHat.LED: RGB565, led_matrix, led_clear
46

5-
export show_char, show_message
7+
export show_char, show_message, @colorant_str
68

79
"""
8-
show_char(c::Char, color::RGB565 = RGB565(1.0,1.0,1.0))
10+
show_char(c::Char, color::ColorTypes.AbstractRGB = colorant"white")
911
10-
Display a single character `c` on the `Sense-Hat` LED Matrix.
12+
Display a single character `c` on the `SenseHat` LED Matrix.
1113
"""
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
2324
end
2425

25-
2626
"""
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.
2830
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+
```
3038
"""
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
5359
end
5460

61+
show_message(s::String, color::ColorTypes.AbstractRGB) = show_message(s, 0.2, color)
62+
5563
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 ; ]),
5664
'+' => 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 ; ]),
5765
'-' => 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

Comments
 (0)