1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
pub mod ASICamera2;
use self::ASICamera2::{CameraInfo, ControlCaps, ControlType, ExposureStatus, ImageType};
use std::alloc::{alloc, dealloc, Layout};
use std::collections::HashMap;
use std::ffi::CStr;
use std::os;
use std::fs::File;
use std::io::BufWriter;
use std::path::Path;
use png::HasParameters;
#[derive(Debug)]
pub struct Control {
pub name: String,
pub description: String,
pub max: i64,
pub min: i64,
pub default: i64,
pub can_auto: bool,
pub is_writable: bool,
pub control_type: ASICamera2::ControlType
}
#[derive(Debug)]
pub struct Camera {
id: i32,
pub width: u32,
pub height: u32,
curr_width: u32,
curr_height: u32,
bin: u8,
color_format: ASICamera2::ImageType,
image_buffer: *mut u8,
controls: HashMap<ASICamera2::ControlType, Control>
}
impl Camera {
pub fn new(id: i32) -> Camera {
Camera {
id: id,
controls: HashMap::new(),
width: 0,
height: 0,
curr_width: 0,
curr_height: 0,
bin: 1,
image_buffer: std::ptr::null_mut(),
color_format: ASICamera2::ImageType::END
}
}
pub fn get_control_value(&self, control: ASICamera2::ControlType) -> Result<i64> {
let mut current: os::raw::c_long = 0;
let mut is_auto: os::raw::c_int = 0;
let res =
unsafe {
ASICamera2::ASIGetControlValue(
self.id,
control as i32,
&mut current as *mut os::raw::c_long,
&mut is_auto as *mut os::raw::c_int
)
};
build_result(current, res)
}
pub fn set_control_value(&mut self, control: ASICamera2::ControlType, value: i64) -> Result<()> {
let res =
unsafe {
ASICamera2::ASISetControlValue(
self.id,
control as i32,
value,
0
)
};
build_result((), res)?;
match control {
ControlType::HardwareBin => {
if value == 0 {
self.curr_width *= 2;
self.curr_height *= 2;
Ok(())
} else if value == 1 {
self.curr_width /= 2;
self.curr_height /= 2;
Ok(())
} else {
// pretty sure this is unreachable,
// would be an out of band value and fail in `build_result`
unreachable!();
}
}
_ => Ok(())
}
}
pub fn set_exposure_ms(&mut self, ms: u64) -> Result<()> {
self.set_control_value(ControlType::Exposure, ms as i64 * 1000)
}
pub fn take_image(&self, path: &str) -> Result<()> {
let exposure_duration = self.get_control_value(ControlType::Exposure).unwrap();
let exposure_ms = exposure_duration / 1000;
unsafe {
let res = ASICamera2::ASIStartExposure(self.id, 0); // isDark == false, doesnt matter really
build_result((), res)?;
}
println!("Sleeping {}ms", exposure_ms + 700);
std::thread::sleep(std::time::Duration::from_millis(exposure_ms as u64 + 700));
let res = unsafe {
ASICamera2::ASIGetDataAfterExp(
self.id,
self.image_buffer,
self.curr_width as i64 * self.curr_height as i64 * 2
)
};
build_result((), res)?;
let mut real_image = vec![0; self.curr_width as usize * self.curr_height as usize * 2 * 3];
for i in 0..(self.curr_width * self.curr_height) {
unsafe {
real_image[i as usize * 6 + 0] = *self.image_buffer.offset(i as isize * 2 + 1);
real_image[i as usize * 6 + 2] = *self.image_buffer.offset(i as isize * 2 + 1);
real_image[i as usize * 6 + 4] = *self.image_buffer.offset(i as isize * 2 + 1);
real_image[i as usize * 6 + 1] = *self.image_buffer.offset(i as isize * 2);
real_image[i as usize * 6 + 3] = *self.image_buffer.offset(i as isize * 2);
real_image[i as usize * 6 + 5] = *self.image_buffer.offset(i as isize * 2);
}
}
let dest = Path::new(path);
let file = File::create(dest).unwrap();
let ref mut w = BufWriter::new(file);
let mut encoder = png::Encoder::new(w, self.curr_width, self.curr_height);
encoder.set(png::ColorType::RGB).set(png::BitDepth::Sixteen);
let mut writer = encoder.write_header().unwrap();
writer.write_image_data(&real_image).unwrap();
Ok(())
}
pub fn exposure_status(&self) -> Result<ExposureStatus> {
let mut exposure_status = ExposureStatus::Failed;
let res = unsafe {
ASICamera2::ASIGetExpStatus(self.id, &mut exposure_status as *mut ExposureStatus)
};
build_result(exposure_status, res)
}
pub fn set_roi_format(&mut self, width: u32, height: u32, binning: u8, image_type: ImageType) -> Result<()> {
self.curr_width = width - (width % 8);
self.curr_height = height - (height % 8);
self.bin = binning;
let res = unsafe {
ASICamera2::ASISetROIFormat(
self.id,
self.curr_width as i32,
self.curr_height as i32,
self.bin as i32,
image_type as i32)
};
build_result((), res)
}
}
#[derive(Copy, Clone, Debug)]
pub enum CameraError {
InvalidIndex = 1,
InvalidId = 2,
InvalidControlType = 3,
CameraClosed = 4,
CameraRemoved = 5,
InvalidPath = 6,
InvalidFileformat = 7,
InvalidSize = 8,
InvalidImgtype = 9,
OutofBoundary = 10,
Timeout = 11,
InvalidSequence = 12,
BufferTooSmall = 13,
VideoModeActive = 14,
ExposureInProgress = 15,
GeneralError = 16,
InvalidMode = 17,
End = 18
}
fn build_result<T>(value: T, err: ASICamera2::ErrorCode) -> Result<T> {
match err {
ASICamera2::ErrorCode::Success => { Ok(value) }
ASICamera2::ErrorCode::InvalidIndex => { Err(CameraError::InvalidIndex) }
ASICamera2::ErrorCode::InvalidId => { Err(CameraError::InvalidId) }
ASICamera2::ErrorCode::InvalidControlType => { Err(CameraError::InvalidControlType) }
ASICamera2::ErrorCode::CameraClosed => { Err(CameraError::CameraClosed) }
ASICamera2::ErrorCode::CameraRemoved => { Err(CameraError::CameraRemoved) }
ASICamera2::ErrorCode::InvalidPath => { Err(CameraError::InvalidPath) }
ASICamera2::ErrorCode::InvalidFileformat => { Err(CameraError::InvalidFileformat) }
ASICamera2::ErrorCode::InvalidSize => { Err(CameraError::InvalidSize) }
ASICamera2::ErrorCode::InvalidImgtype => { Err(CameraError::InvalidImgtype) }
ASICamera2::ErrorCode::OutofBoundary => { Err(CameraError::OutofBoundary) }
ASICamera2::ErrorCode::Timeout => { Err(CameraError::Timeout) }
ASICamera2::ErrorCode::InvalidSequence => { Err(CameraError::InvalidSequence) }
ASICamera2::ErrorCode::BufferTooSmall => { Err(CameraError::BufferTooSmall) }
ASICamera2::ErrorCode::VideoModeActive => { Err(CameraError::VideoModeActive) }
ASICamera2::ErrorCode::ExposureInProgress => { Err(CameraError::ExposureInProgress) }
ASICamera2::ErrorCode::GeneralError => { Err(CameraError::GeneralError) }
ASICamera2::ErrorCode::InvalidMode => { Err(CameraError::InvalidMode) }
ASICamera2::ErrorCode::End => { Err(CameraError::End) }
}
}
type Result<T> = std::result::Result<T, CameraError>;
pub fn acquire(camera_id: i32) -> Result<Camera> {
unsafe {
let cameracount = ASICamera2::ASIGetNumOfConnectedCameras();
if camera_id >= cameracount {
panic!("Camera id is invalid (detected {} cameras)", camera_id);
}
let props_layout = Layout::array::<CameraInfo>(cameracount as usize).unwrap();
let props = alloc(props_layout) as *mut CameraInfo;
let res = ASICamera2::ASIGetCameraProperty(props, camera_id);
build_result((), res)?;
println!("Got properties");
let res = ASICamera2::ASIOpenCamera(camera_id);
build_result((), res)?;
println!("Opened camera");
let res = ASICamera2::ASIInitCamera(camera_id);
build_result((), res)?;
println!("Init'd camera");
let mut control_count: i32 = 0;
let res = ASICamera2::ASIGetNumOfControls(camera_id, &mut control_count as *mut os::raw::c_int);
build_result((), res)?;
println!("Got control count");
let control_layout = Layout::array::<ControlCaps>(1).unwrap();
let control = alloc(control_layout) as *mut ControlCaps;
let mut camera = Camera::new(camera_id);
let camera_props: CameraInfo = *props.offset(camera_id as isize);
camera.width = camera_props.max_width as u32;
camera.height = camera_props.max_height as u32;
camera.curr_width = camera_props.max_width as u32;
camera.curr_height = camera_props.max_height as u32;
camera.color_format = ImageType::RGB24;
camera.image_buffer = alloc(
Layout::from_size_align(camera.curr_width as usize * camera.curr_height as usize * 2, 8).unwrap()
);
let res = ASICamera2::ASISetROIFormat(camera_id, camera.width as i32, camera.height as i32, 1, ImageType::RAW16 as i32);
build_result((), res)?;
println!("Set ROI/Format");
for c in 0..control_count {
let res = ASICamera2::ASIGetControlCaps(0, c, control);
build_result((), res)?;
println!("Got control {:?}", c);
let control = Control {
name: CStr::from_ptr((*control).name.as_ptr()).to_str().unwrap().to_owned(),
description: CStr::from_ptr((*control).description.as_ptr()).to_str().unwrap().to_owned(),
max: (*control).max_value,
min: (*control).min_value,
default: (*control).default_value,
can_auto: bool::from((*control).is_auto_supported),
is_writable: bool::from((*control).is_writable),
control_type: (*control).control_type
};
camera.controls.insert(control.control_type, control);
}
dealloc(control as *mut u8, control_layout);
dealloc(props as *mut u8, props_layout);
Ok(camera)
}
}
|