Skip to content

Commit

Permalink
Merge pull request #5 from Ellipse0934/master
Browse files Browse the repository at this point in the history
Update for Julia 1.0
  • Loading branch information
simonbyrne authored Oct 19, 2018
2 parents 6d79686 + 6865209 commit dc1a38d
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 30 deletions.
94 changes: 94 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[ColorTypes]]
deps = ["FixedPointNumbers", "Random", "Test"]
git-tree-sha1 = "f73b0e10f2a5756de7019818a41654686da06b09"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.7.5"

[[Colors]]
deps = ["ColorTypes", "FixedPointNumbers", "InteractiveUtils", "Pkg", "Printf", "Reexport", "Test"]
git-tree-sha1 = "8c89e0a9a583954eae3efcf6a531e51c02b38cee"
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
version = "0.9.4"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[Distributed]]
deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[FixedPointNumbers]]
deps = ["Pkg", "Test"]
git-tree-sha1 = "b8045033701c3b10bf2324d7203404be7aef88ba"
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
version = "0.5.3"

[[InteractiveUtils]]
deps = ["LinearAlgebra", "Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[LibGit2]]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[Pkg]]
deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Reexport]]
deps = ["Pkg"]
git-tree-sha1 = "7b1d07f411bc8ddb7977ec7f377b97b158514fe0"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
version = "0.2.0"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[UUIDs]]
deps = ["Random"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
10 changes: 10 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name = "SenseHat"
uuid = "0dbe55a1-025c-5c9b-ab45-55492d058cfb"
version = "0.1.0"

[deps]
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"
6 changes: 4 additions & 2 deletions src/LPS25H.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ function LPS25H_pressure()
smbus_read(CTRL_REG2) == 0 && break # check if finished
end

pressure = (Int32(smbus_read(0x2a)) << 16 | Int16(smbus_read(0x29)) << 8 | smbus_read(0x28)) / 4096
pressure = (UInt64.(smbus_read(0x2a)) << 16 |
UInt64.(smbus_read(0x29)) << 8 |
UInt64.(smbus_read(0x28)))/4096.0

smbus_write(CTRL_REG1, 0x00) # power down
return pressure
end

"""
HTS221_temperature()
LPS25H_temperature()
The temperature (in °C) from the LPS25H sensor.
"""
Expand Down
4 changes: 2 additions & 2 deletions src/ioctl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A wrapper around the unix `ioctl` function:
- `request` is a request code
- `arg` is either an integer (which will be passed as a `Cint`), or a buffer (which will
be passed as a `Ptr{Void}`).
be passed as a `Ptr{nothing}`).
Will throw a `SystemError` if an error occurs. Otherwise returns a `Cint` (which is
typically, though not always, 0).
Expand All @@ -25,7 +25,7 @@ function ioctl(fd::Cint, request::Integer, arg::Integer)
return ret
end
function ioctl(fd::Cint, request::Integer, arg)
ret = ccall(:ioctl, Cint, (Cint, Culong, Ptr{Void}...), fd, request, arg)
ret = ccall(:ioctl, Cint, (Cint, Culong, Ptr{Cvoid}...), fd, request, arg)
if ret < 0
throw(SystemError("ioctl error"))
end
Expand Down
17 changes: 8 additions & 9 deletions src/led.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module LED

import ..ioctl

export led_matrix, RGB565, led_clear
export led_matrix, RGB565, led_clear, @colorant_str

using ColorTypes, FixedPointNumbers
using Colors, ColorTypes, FixedPointNumbers, Dates, Mmap

function _led_fb_device()
try
for devname in readdir("/sys/class/graphics")
sysfname = joinpath("/sys/class/graphics",devname,"name")
if startswith(devname, "fb") && isfile(sysfname)
if startswith(readstring(sysfname),"RPi-Sense FB")
if startswith(readline(sysfname),"RPi-Sense FB")
return joinpath("/dev",devname)
end
end
Expand All @@ -28,12 +28,10 @@ function __init__()
LED_FB_DEVICE[] = open(LED_FB_DEVICE_PATH,"w+")
end



const U5 = Normed{UInt8,5}
const U6 = Normed{UInt8,6}

immutable RGB565 <: AbstractRGB{Normed{UInt8, 8}}
struct RGB565 <: AbstractRGB{Normed{UInt8, 8}}
data::UInt16
end

Expand All @@ -43,15 +41,15 @@ function RGB565(r::U5, g::U6, b::U5)
(UInt16(reinterpret(b))) )
end

RGB565(r::Real, g::Real, b::Real) =
RGB565(r::Real, g::Real, b::Real) =
RGB565(convert(U5,r), convert(U6,g), convert(U5,b))
RGB565(c::Union{Color,Colorant}) = RGB565(red(c), green(c), blue(c))

ColorTypes.red(c::RGB565) = U5(c.data >> 11, Val{true})
ColorTypes.green(c::RGB565) = U6((c.data >> 5) & 0x3f, Val{true})
ColorTypes.blue(c::RGB565) = U5(c.data & 0x1f, Val{true})

ColorTypes.ccolor{Csrc<:Colorant}(::Type{RGB565}, ::Type{Csrc}) = RGB565
ColorTypes.ccolor(::Type{RGB565}, ::Type{Csrc}) where {Csrc <: Colorant} = RGB565
ColorTypes.base_color_type(::Type{RGB565}) = RGB565


Expand Down Expand Up @@ -138,7 +136,8 @@ end
Sets the SenseHat LED matrix to all black.
"""
function led_clear()
led_matrix()[:] = RGB565(0,0,0)
led_matrix()[:] .= RGB565(0,0,0)
return
end

end # module
2 changes: 1 addition & 1 deletion src/smbus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const I2C_SMBUS_BYTE_DATA = 0x02
const I2C_SLAVE = 0x0703
const I2C_SMBUS = 0x0720

immutable SMBusData
struct SMBusData
read_write ::UInt8
command ::UInt8
size ::UInt32
Expand Down
18 changes: 10 additions & 8 deletions src/stick.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module Stick

export StickEvent, readstick, sticktask

using Dates

function _stick_input_device()
try
for devname in readdir("/sys/class/input")
sysfname = joinpath("/sys/class/input",devname,"device","name")
if startswith(devname, "event") && isfile(sysfname)
if startswith(readstring(sysfname),"Raspberry Pi Sense HAT Joystick")
if startswith(readline(sysfname),"Raspberry Pi Sense HAT Joystick")
return joinpath("/dev/input",devname)
end
end
Expand All @@ -30,7 +32,7 @@ const EV_KEY = Cushort(0x01)
The raw data format from the input device. This is based on the struct specification from https://github.com/RPi-Distro/python-sense-hat/blob/ec8c96118d5eb58488c4ff6091cd373117e6ce08/sense_hat/stick.py#L41.
"""
immutable StickEventRaw
struct StickEventRaw
tv_sec::Clong
tv_usec::Clong
ev_type::Cushort
Expand All @@ -53,15 +55,15 @@ The data from a joystick event. The type contains the following fields:
* `state` : the state of the event (`PRESS`, `RELEASE`, `HOLD`).
"""
immutable StickEvent
struct StickEvent
timestamp::DateTime
direc::Direc
state::State
end

Base.convert(::Type{StickEvent}, r::StickEventRaw) =
StickEvent(Dates.unix2datetime(r.tv_sec + r.tv_usec/1_000_000),
Direc(r.ev_direc), State(r.ev_state))
Direc(r.ev_direc), State(r.ev_state))

"""
readstick()
Expand All @@ -75,9 +77,9 @@ function readstick()
fddev = RawFD(fd(dev))
while true
wait(fddev, readable=true)
s = read(dev, StickEventRaw)
s = read!(dev, Ref{StickEventRaw}())[]
if s.ev_type == EV_KEY
return StickEvent(s)
return convert(StickEvent,s)
end
end
end
Expand All @@ -101,9 +103,9 @@ function sticktask()
fddev = RawFD(fd(dev))
while true
wait(fddev, readable=true)
s = read(dev, StickEventRaw)
s = read(dev, Ref{StickEventRaw}())[]
if s.ev_type == EV_KEY
produce(StickEvent(s))
produce(convert(StickEvent, s))
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions src/text.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Text

using ColorTypes, Colors.@colorant_str
using ColorTypes, Colors

import SenseHat.LED: RGB565, led_matrix, led_clear
import Colors.@colorant_str

export show_char, show_message, @colorant_str
export show_char, show_message

"""
show_char(c::Char, color::ColorTypes.AbstractRGB = colorant"white")
Expand Down Expand Up @@ -42,9 +43,8 @@ function show_message(s::String, speed::Real = 0.2, color::ColorTypes.AbstractRG
error("Character font for $c not available \n")
return
end
img = Array{Bool}(8, 16 + 5*length(s))
img[1:8,1:8] = Bool.(zeros(8,8))
img[1:8,(9 + 5*length(s)):(16 + 5*length(s))] = Bool.(zeros(8,8))
img = Array{Bool, 2}(undef, 8, 16 + 5*length(s))
img[:] .= 0
for i in 1:length(s)
img[1:8, (4 + 5*i):(8 + 5*i)] = font[s[i]]
end
Expand Down
2 changes: 1 addition & 1 deletion test/led.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SenseHat
using Base.Test
using Test

using ColorTypes

Expand Down
4 changes: 2 additions & 2 deletions test/sensors.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Base.Test
using Test
using SenseHat

@test -40 <= temperature() <= 60
@test 0 <= humidity() <= 100
@test 950 <= pressure() <= 1100
@test 900 <= pressure() <= 1200

0 comments on commit dc1a38d

Please sign in to comment.