Skip to content

Commit 70bb87f

Browse files
committed
Added isopen, open! and close! methods for Camera.
* Renamed AbstractAcquiredImage - was AcquiredImage. * Renamed AcquiredImage - was SimulatedAcquiredImage. * Moved Camera and methods to a separate file. * Fixed constructors of SimulatedCamera (omitted init of current_id). * Implemented Base.show for AcquiredImage, and SimulatedCamera.
1 parent 4254383 commit 70bb87f

File tree

6 files changed

+133
-93
lines changed

6 files changed

+133
-93
lines changed

src/Cameras.jl

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Cameras
22

33
using ResourcePools
44

5-
import Base: take!
5+
import Base: isopen, take!
66

77
import ResourcePools:
88
ref_count,
@@ -11,56 +11,23 @@ import ResourcePools:
1111
retain!
1212

1313
export Camera,
14+
open!,
15+
close!,
16+
isopen,
1417
isrunning,
15-
ref_count,
16-
release!,
17-
resource,
18-
retain!,
1918
start!,
2019
stop!,
2120
take!,
2221
trigger!
22+
include("camera.jl")
2323

24-
abstract type Camera end
25-
26-
"""
27-
isrunning(camera::Camera)
28-
29-
Return if the camera is running.
30-
"""
31-
isrunning(camera::Camera) = error("No implementation for $(typeof(camera))")
32-
33-
"""
34-
start!(camera::Camera)
35-
36-
Start camera, i.e. start image acquisition.
37-
"""
38-
start!(camera::Camera) = error("No implementation for $(typeof(camera))")
39-
40-
"""
41-
stop!(camera::Camera)
42-
43-
Stop camera, i.e. stop image acquisition.
44-
"""
45-
stop!(camera::Camera) = error("No implementation for $(typeof(camera))")
46-
47-
"""
48-
take!(camera::Camera)
49-
50-
Take an image, i.e. an [`AcquiredImage`](@ref). Blocks until an image is available.
51-
"""
52-
take!(camera::Camera)::AcquiredImage = error("No implementation for $(typeof(camera))")
53-
54-
"""
55-
trigger!(camera::Camera)
56-
57-
Trigger image acquisition.
58-
"""
59-
trigger!(camera::Camera) = error("No implementation for $(typeof(camera))")
60-
61-
export AcquiredImage,
24+
export AbstractAcquiredImage,
6225
id,
63-
timestamp
26+
timestamp,
27+
ref_count,
28+
release!,
29+
resource,
30+
retain!
6431
include("acquired_image.jl")
6532

6633
import Base: iterate, IteratorSize
@@ -69,7 +36,7 @@ export iterate,
6936
include("iteration.jl")
7037

7138
export SimulatedCamera,
72-
SimulatedAcquiredImage
39+
AcquiredImage
7340
include("simulated_camera.jl")
7441

7542
end # module

src/acquired_image.jl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1-
abstract type AcquiredImage{T,N} <: AbstractPooledDenseArray{T,N}
1+
abstract type AbstractAcquiredImage{T,N} <: AbstractPooledDenseArray{T,N}
22
end
33

44
"""
55
id(image::AcquiredImage)
66
77
Return image ID.
88
"""
9-
id(image::AcquiredImage) = error("No implementation for $(typeof(image))")
9+
id(image::AbstractAcquiredImage) = error("No implementation for $(typeof(image))")
1010

1111
"""
1212
timestamp(image::AcquiredImage)
1313
1414
Return image timestamp.
1515
"""
16-
timestamp(image::AcquiredImage) = error("No implementation for $(typeof(image))")
16+
timestamp(image::AbstractAcquiredImage) = error("No implementation for $(typeof(image))")
17+
18+
19+
mutable struct AcquiredImage{T,N} <: AbstractAcquiredImage{T,N}
20+
# Inherits behaviour of AbstractPooledDenseArray, by having the same fields
21+
array::Array{T,N}
22+
ref_count::Int
23+
dispose::Function
24+
25+
id::Int
26+
timestamp::Int
27+
function AcquiredImage(a::Array{T,N}, id, timestamp) where {T,N}
28+
function dispose(img)
29+
@debug "Disposing $img"
30+
end
31+
new{T,N}(a, 1, dispose, id, timestamp)
32+
end
33+
end
34+
35+
id(img::AcquiredImage) = img.id
36+
timestamp(img::AcquiredImage) = img.timestamp
37+
38+
function Base.show(io::IO, image::AcquiredImage)
39+
write(io, "$(nameof(AcquiredImage))(id:$(image.id), timestamp:$(image.timestamp), ref_count:$(image.ref_count), array:$(image.array))")
40+
end

src/camera.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
abstract type Camera end
2+
3+
"""
4+
open!(camera::Camera)
5+
6+
Open camera.
7+
"""
8+
open!(camera::Camera) = error("No implementation for $(typeof(camera))")
9+
10+
"""
11+
close!(camera::Camera)
12+
13+
Close camera.
14+
"""
15+
close!(camera::Camera) = error("No implementation for $(typeof(camera))")
16+
17+
"""
18+
isopen(camera::Camera)
19+
20+
Return if the camera is open.
21+
"""
22+
isopen(camera::Camera) = error("No implementation for $(typeof(camera))")
23+
24+
"""
25+
isrunning(camera::Camera)
26+
27+
Return if the camera is running.
28+
"""
29+
isrunning(camera::Camera) = error("No implementation for $(typeof(camera))")
30+
31+
"""
32+
start!(camera::Camera)
33+
34+
Start camera, i.e. start image acquisition.
35+
"""
36+
start!(camera::Camera) = error("No implementation for $(typeof(camera))")
37+
38+
"""
39+
stop!(camera::Camera)
40+
41+
Stop camera, i.e. stop image acquisition.
42+
"""
43+
stop!(camera::Camera) = error("No implementation for $(typeof(camera))")
44+
45+
"""
46+
take!(camera::Camera)
47+
48+
Take an image, i.e. an [`AbstractAcquiredImage`](@ref). Blocks until an image is available.
49+
"""
50+
take!(camera::Camera)::AbstractAcquiredImage = error("No implementation for $(typeof(camera))")
51+
52+
"""
53+
trigger!(camera::Camera)
54+
55+
Trigger image acquisition.
56+
"""
57+
trigger!(camera::Camera) = error("No implementation for $(typeof(camera))")

src/simulated_camera.jl

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,44 @@
11
mutable struct SimulatedCamera <: Camera
2-
isrunning::Bool
3-
trigger_source::Channel
2+
trigger_source::Channel{UInt64}
43
image_source::Channel
5-
next_id::Int
6-
SimulatedCamera(image_source::Channel) = new(false, Channel(Inf), image_source)
7-
SimulatedCamera(trigger_source::Channel, image_source::Channel) = new(false, trigger_source, image_source)
4+
isopen::Bool
5+
isrunning::Bool
6+
current_id::Int
7+
SimulatedCamera(image_source::Channel) = new(Channel{UInt64}(Inf), image_source, false, false, 0)
8+
SimulatedCamera(trigger_source::Channel{UInt64}, image_source::Channel) = new(trigger_source, image_source, false, false, 0)
89
function SimulatedCamera(trigger_period::Real, image_source::Channel)
9-
function produce_triggers!(trigger_source::Channel)
10+
function produce_triggers!(trigger_source::Channel{UInt64})
1011
while true
1112
sleep(trigger_period)
1213
put!(trigger_source, time_ns())
1314
end
1415
end
15-
trigger_source = Channel(produce_triggers!)
16-
new(false, trigger_source, image_source, 0)
17-
end
18-
end
19-
20-
mutable struct SimulatedAcquiredImage{T,N} <: AcquiredImage{T,N}
21-
# Inherits behaviour of AbstractPooledDenseArray, by having the same fields
22-
array::Array{T,N}
23-
ref_count::Int
24-
dispose::Function
25-
26-
is_disposed::Bool
27-
id::Int
28-
timestamp::Int
29-
function SimulatedAcquiredImage(a::Array{T,N}, id, timestamp) where {T,N}
30-
function dispose(img)
31-
@debug "Disposing $img"
32-
img.is_disposed = true
33-
end
34-
new{T,N}(a, 1, dispose, false, id, timestamp)
16+
trigger_source = Channel(produce_triggers!; ctype = UInt64)
17+
new(trigger_source, image_source, false, false, 0)
3518
end
3619
end
3720

38-
id(img::SimulatedAcquiredImage) = img.id
39-
timestamp(img::SimulatedAcquiredImage) = img.timestamp
21+
isopen(camera::SimulatedCamera) = camera.isopen
22+
open!(camera::SimulatedCamera) = camera.isopen = true
23+
close!(camera::SimulatedCamera) = camera.isopen = false
4024

4125
isrunning(camera::SimulatedCamera) = camera.isrunning
42-
43-
function start!(camera::SimulatedCamera)
44-
camera.isrunning = true
45-
end
46-
47-
function stop!(camera::SimulatedCamera)
48-
camera.isrunning = false
49-
end
26+
start!(camera::SimulatedCamera) = camera.isrunning = true
27+
stop!(camera::SimulatedCamera) = camera.isrunning = false
5028

5129
function take!(camera::SimulatedCamera)
30+
@debug "Taking image from $(camera.image_source)"
5231
image = take!(camera.image_source)
53-
id = camera.next_id += 1
32+
id = camera.current_id += 1
33+
@debug "Taking trigger from $(camera.trigger_source)"
5434
timestamp = take!(camera.trigger_source)
55-
return SimulatedAcquiredImage(image, id, timestamp)
35+
return AcquiredImage(image, id, timestamp)
5636
end
5737

5838
function trigger!(camera::SimulatedCamera)
5939
put!(camera.trigger_source, time_ns())
6040
end
41+
42+
function Base.show(io::IO, camera::SimulatedCamera)
43+
write(io, "$(nameof(SimulatedCamera))(trigger_source:$(camera.trigger_source), image_source:$(camera.image_source), current_id:$(camera.current_id))")
44+
end

test/acquired_image.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using Test
1616
t_1 = time_ns()
1717
trigger!(camera)
1818
img = take!(camera)
19-
@test typeof(img) <: AcquiredImage
19+
@test typeof(img) <: AbstractAcquiredImage
2020
@assert size(img) == image_size
2121

2222
@test ref_count(img) == 1
@@ -27,10 +27,6 @@ using Test
2727
release!(img)
2828
@test ref_count(img) == 1
2929

30-
@assert !img.is_disposed
31-
release!(img)
32-
@test img.is_disposed
33-
3430
@test id(img) == 1
3531
@test timestamp(img) >= t_1
3632

test/acquisition.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ using Test
1414
@testset "Manually triggered" begin
1515
camera = SimulatedCamera(image_source)
1616

17+
@test !isopen(camera)
18+
open!(camera)
19+
@test isopen(camera)
20+
1721
@test !isrunning(camera)
1822
start!(camera)
1923
@test isrunning(camera)
@@ -33,18 +37,26 @@ using Test
3337
@test size(img) == image_size
3438
end
3539
end
40+
41+
@assert isrunning(camera)
42+
stop!(camera)
43+
@test !isrunning(camera)
44+
45+
@assert isopen(camera)
46+
close!(camera)
47+
@test !isopen(camera)
3648
end
3749

3850
@testset "Continuously triggered" begin
3951
period = 0.04 # seconds
4052

41-
function produce_triggers!(trigger_source::Channel)
53+
function produce_triggers!(trigger_source::Channel{UInt64})
4254
while true
4355
put!(trigger_source, time_ns())
4456
sleep(period)
4557
end
4658
end
47-
trigger_source = Channel(produce_triggers!)
59+
trigger_source = Channel(produce_triggers!; ctype = UInt64)
4860

4961
continuous_camera = SimulatedCamera(trigger_source, image_source)
5062

0 commit comments

Comments
 (0)