summaryrefslogtreecommitdiff
path: root/src/qhyccd/mod.rs
blob: 72a68b94c0d64800fb67a4cd765c272e43fb0312 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
pub mod QHYCCDCam;

pub use self::QHYCCDCam::Control;

use self::QHYCCDCam::*;

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 std::time::Duration;
use crossbeam_channel::unbounded;
use crossbeam_channel::{Sender, Receiver, TryRecvError};
use crossbeam_channel::select;

use png::HasParameters;

use crate::Dimensions;
use crate::Properties;

// unsafe impl Send for Camera { }
#[derive(Debug)]
pub struct Camera {
    buffer: Vec<Vec<u8>>,
    frame_size: usize,
    handle: *mut os::raw::c_void,
    settings: Settings,
}

#[derive(Debug, Default, Copy, Clone)]
pub struct Settings {
    exposure: u64,
    brightness: u32,
    contrast: u32,
    control_wbr: u32,
    control_wbb: u32,
    control_wbg: u32,
    gamma: u32,
    gain: u32,
    offset: u32,
    speed: u32,
    transfer_bit: u32,
    usb_traffic: u32,
    row_noise_re: bool,
    cur_temp: f64,
    cur_pwm: f64,
    manual_pwm: f64,
    cfw_port: f64,
    cooler: bool,
    st4_port: bool,
    color: bool,
    bin: u8,
    bpp: u8,
    channels: u8,
    // width, height
    image_roi: (u32, u32),
    image_xy: (u32, u32),
}

impl Settings {
    pub fn frame_size(&self) -> usize {
        let pixels = (self.image_roi.0 as usize) * (self.image_roi.1 as usize);
        let bytes_per_pixel = (self.bpp as usize / 8) * (self.channels as usize);
        pixels * bytes_per_pixel
    }

    pub fn update_param(&mut self, control: Control, value: f64) {
        match control {
            Control::Brightness => { self.brightness = value as u32; }
            Control::Contrast => { self.contrast = value as u32; }
            Control::CONTROL_WBR => { self.control_wbr = value as u32; }
            Control::CONTROL_WBB => { self.control_wbb = value as u32; }
            Control::CONTROL_WBG => { self.control_wbg = value as u32; }
            Control::Gamma => { self.gamma = value as u32; }
            Control::Gain => { self.gain = value as u32; }
            Control::Offset => { self.offset = value as u32; }
            Control::Exposure => { self.exposure = value as u64; }
            Control::TransferBit => { self.transfer_bit = value as u32; }
            Control::Channels => { self.channels = value as u8; }
            Control::USBTraffic => { self.usb_traffic = value as u32; }
            Control::RowNoiseRe => { self.row_noise_re = value as u32 == 1; }
            Control::CurTemp => { self.cur_temp = value; }
            Control::CurPWM => { self.cur_pwm = value; }
            Control::ManulPwm => { self.manual_pwm = value; }
            Control::Cooler => { self.cooler = value as u32 == 1; }
            _ => { }
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub enum CameraError {
    QHYError, // unspecified error from the qhy sdk
    InvalidControl
}

type Result<T> = std::result::Result<T, CameraError>;

fn check(result: os::raw::c_int) -> Result<()> {
    match QHYResult::from(result as u32) {
        QHYResult::QHYCCD_SUCCESS => Ok(()),
        QHYResult::QHYCCD_ERROR => Err(CameraError::QHYError),
        a @ _ => {
            panic!("Unexpected result code from qhy sdk: {:?}", a);
        }
    }
}

static mut INITIALIZED: bool = false;

pub fn fix_channels_and_endianness(dataslice: &mut [u8]) {
    for i in 0..(dataslice.len() / 6) {
        let (b_low, b_high) = (dataslice[i * 6], dataslice[i * 6 + 1]);
        let tmp2 = dataslice[i * 6 + 1];
        dataslice[i * 6 + 1] = dataslice[i * 6 + 4];
        dataslice[i * 6] = dataslice[i * 6 + 5];
        dataslice[i * 6 + 4] = b_high;
        dataslice[i * 6 + 5] = b_low;

        let g_low = dataslice[i * 6 + 2];
        dataslice[i * 6 + 2] = dataslice[i * 6 + 3];
        dataslice[i * 6 + 3] = g_low;

        if false {
            for e in 0..3 {
                let el = ((dataslice[i * 6 + e * 2] as u16) << 8) | (dataslice[i * 6 + e * 2 + 1] as u16);
                let el = el.saturating_mul(16);
                dataslice[i * 6 + e * 2] = (el >> 8) as u8;
                dataslice[i * 6 + e * 2 + 1] = el as u8;
            }
        }
    }
}

pub fn fix_endianness(dataslice: &mut [u8]) {
    // yolo it
    if dataslice.len() % 2 != 0 {
        panic!("you want to fix endianness of a slice that has length {}, which is not divisible by two. are you certain the data has an endianness to reverse?", dataslice.len());
    }

    let dataslice: &mut [u16] = unsafe {
        std::slice::from_raw_parts_mut(dataslice.as_mut_ptr() as *mut u16, dataslice.len() / 2)
    };
    for i in 0..dataslice.len() {
        // TODO: simd-ize this?
        // this is a pshufb and a mul..
        dataslice[i] = dataslice[i].swap_bytes();
        if true {
//           dataslice[i] *= 16;
        }
    }
}


pub fn connect(camera_idx: i32) -> Result<(Receiver<QHYResponse>, Sender<QHYMessage>)> {
    let (response_sender, response_receiver) = unbounded();
    let (message_sender, message_receiver) = unbounded();

    std::thread::spawn(move || {
        let mut camera = match acquire(camera_idx) {
            Ok(camera) => camera,
            Err(e) => {
                response_sender.send(QHYResponse::InitializationError).unwrap();
                return;
            },
        };

        camera.set_defaults().unwrap();

        // sleep for 2ms between waiting for messages and reading frames. This introduces an
        // artificial cap of 500fps, but I don't own a camera that can hit that.
        let SLEEP_TIME = 2u64;
        let mut exposing = false;
        let mut counter = 0u64;
        let mut capture_count: Option<u64> = None;

        loop {
            match message_receiver.try_recv() {
                Ok(QHYMessage::Shutdown) => {
                    println!("Got shutdown request, closing camera...");
                    return;
                }
                Ok(QHYMessage::FrameAvailable(data)) => {
                    if data.len() == camera.settings.frame_size() {
                        camera.buffer.push(data);
                    } else {
                        // otherwise the writer finished handling a frame from an old size,
                        // and the array is incorrectly sized for current settings.
                        // so drop the buffer and free that memory.
                        drop(data);
                    }
                }
                Ok(QHYMessage::BeginCapture(count)) => {
                    counter = camera.settings.exposure;
                    capture_count = count;
                    println!("Beginning capture");
                    camera.begin_exposure().expect("can begin exposures");
                    exposing = true;
                }
                Ok(QHYMessage::StopCapture) => {
                    counter = 0;
                    capture_count = None;
                    camera.cancel_exposure().expect("can cancel exposures");
                    exposing = false;
                }
                Ok(QHYMessage::SetControl(control, value)) => {
                    if control == Control::Color {
                        // overload Control::Color to signal if we want to switch to
                        // debayer-disabled "mono"
                        let mono_mode = value == 0.0;
                        camera.set_color_mode(mono_mode);
                        // yes, this requires reset.
                        counter = 0;
                        camera.cancel_exposure().expect("can cancel exposures");
                        exposing = false;
                    } else {
                        if control.requires_reset() {
                            counter = 0;
                            camera.cancel_exposure().expect("can cancel exposures");
                            exposing = false;
                        }
                        camera.set_control(control, value).expect("can set camera controls");
                    }
                    response_sender.send(QHYResponse::UpdatedSettings(camera.settings.clone())).expect("can send responses to main thread");
                }
                Ok(QHYMessage::QueryControl(control)) => {
                    let value = camera.get_control(control);
                    camera.settings.update_param(control, value);
                    response_sender.send(QHYResponse::CurrentControlValue(control, value)).expect("can send control value");
                }
                Err(TryRecvError::Empty) => {
                    // this is fine. nothing new to do.
                },
                Err(TryRecvError::Disconnected) => {
                    // uh oh. main thread crashed? all we can do is exit.
                    return;
                }
            }

//            println!("counter: {:?}, exposing: {:?}", counter, exposing);
            if counter == 0 && exposing {
                if let Some(data) = camera.get_frame() {
                    let (data, width, height, bpp, channels) = camera.read_frame(data).expect("can read frames from camera");
                    counter = camera.settings.exposure;
                    camera.begin_exposure().expect("can begin exposures");
                    response_sender.send(QHYResponse::Data(
                        data,
                        Dimensions::new(width, height, bpp, channels),
                        Properties {
                            device: "qhy376c",
                            exposure_ms: camera.settings.exposure as u32,
                            gain: camera.settings.gain as u16,
                            offset: camera.settings.offset as u16,
                            gamma: camera.settings.gamma as u16,
                            temp: (camera.settings.cur_temp * 10.0) as u16,
                        },
                    )).unwrap();
                    match &mut capture_count {
                        Some(count) => {
                            *count -= 1;
                            if *count == 0 {
                                println!("finished capture run");
                                exposing = false;
                                camera.cancel_exposure();
                                continue;
                            }
                        },
                        None => {
                            // do nothing
                        }
                    }
                } else {
                    // no frame ready in the buffer! we can't actually read the image...
                    counter = camera.settings.exposure;
                    camera.begin_exposure().expect("can begin exposures");
                    response_sender.send(QHYResponse::DroppedFrame).unwrap();
                }
            }

            counter = counter.saturating_sub(SLEEP_TIME * 1000);
            std::thread::sleep(Duration::from_millis(SLEEP_TIME));
        }

    });

    Ok((response_receiver, message_sender))
}

pub enum QHYResponse {
    InitializationError,
    Data(Vec<u8>, Dimensions, Properties),
    DroppedFrame,
    Shutdown,
    UpdatedSettings(Settings),
    CurrentControlValue(Control, f64),
}

pub enum QHYMessage {
    FrameAvailable(Vec<u8>),
    BeginCapture(Option<u64>),
    StopCapture,
    QueryControl(Control),
    SetControl(Control, f64),
    Shutdown,
}

fn acquire(camera_idx: i32) -> Result<Camera> {
    unsafe {
        if !INITIALIZED {
            println!("Initializing QHYCCDResource");
            check(QHYCCDCam::InitQHYCCDResource())?;
            INITIALIZED = true;
        }
        let cameracount = QHYCCDCam::ScanQHYCCD();
        println!("Detected {} cameras", cameracount);
        if camera_idx >= cameracount {
            panic!("Camera id is invalid (detected {} cameras)", cameracount);
        }

        let mut id_space: [os::raw::c_char; 32] = [0; 32];
        check(QHYCCDCam::GetQHYCCDId(camera_idx, id_space.as_mut_ptr()))?;
        println!("Got camera id: {:?}", id_space);
        println!("One sec, trying again...");
        println!("How's this: {}", CStr::from_ptr(id_space.as_ptr()).to_str().unwrap());
        let handle: *mut os::raw::c_void = QHYCCDCam::OpenQHYCCD(id_space.as_mut_ptr());
        if handle == std::ptr::null_mut() {
            println!("Failed to open the device");
            return Err(CameraError::QHYError);
        }
        check(QHYCCDCam::SetQHYCCDStreamMode(handle, 0))?; // 0 means single frame mode...
        check(QHYCCDCam::InitQHYCCD(handle))?;
        check(QHYCCDCam::CancelQHYCCDExposingAndReadout(handle))?;
        Ok(Camera {
            buffer: vec![],
            frame_size: 0,
            handle: handle,
            settings: Settings::default()
        })
    }
}

impl Camera {
    pub fn get_frame(&mut self) -> Option<Vec<u8>> {
        if self.frame_size != self.image_buf_size() {
            // something has happened that requires a new buffer
            self.resize_buffer();
        }

        self.buffer.pop()
    }
    pub fn image_buf_size(&self) -> usize {
        self.settings.frame_size()
    }
    pub fn set_exposure_ms(&mut self, ms: u32) -> Result<()> {
        self.set_control(Control::Exposure, (ms as f64) * 1000.0)
    }
    pub fn set_target_temp(&self, temp: f64) -> Result<()> {
        unsafe {
            check(QHYCCDCam::ControlQHYCCDTemp(self.handle, temp))
        }
    }
    pub fn has_control(&self, control: Control) -> bool {
        unsafe {
            match QHYResult::from(QHYCCDCam::IsQHYCCDControlAvailable(self.handle, control as i32) as u32) {
                QHYResult::QHYCCD_ERROR => {
                    false
                },
                QHYResult::QHYCCD_SUCCESS => {
                    true
                }
                a @ _ => {
                    panic!("Unexpected response when querying if control '{:?}' is available: {:?}", control, a);
                }
            }
        }
    }
    pub fn set_control(&mut self, control: Control, value: f64) -> Result<()> {
        unsafe {
        if self.has_control(control) {
            check(QHYCCDCam::SetQHYCCDParam(self.handle, control as i32, value))?;
            self.settings.update_param(control, value);
            Ok(())
        } else {
            println!("Cannot set control: {:?}", control);
            Ok(())
        }
        }
    }
    pub fn get_control_limits(&self, control: Control) -> Result<(f64, f64, f64)> {
        unsafe {
            if self.has_control(control) {
                let mut min = 0f64;
                let mut max = 0f64;
                let mut step = 0f64;
                match check(QHYCCDCam::GetQHYCCDParamMinMaxStep(self.handle, control as i32, &mut min, &mut max, &mut step)) {
                    Ok(_) => {
                        Ok((min, max, step))
                    },
                    Err(_) => {
                        Err(CameraError::QHYError)
                    }
                }
            } else {
                Err(CameraError::InvalidControl)
            }
        }
    }

    pub fn get_control(&self, control: Control) -> f64 {
        unsafe {
            QHYCCDCam::GetQHYCCDParam(self.handle, control as i32)
        }
    }
    pub fn release(self) -> Result<()> {
        unsafe {
        check(QHYCCDCam::CloseQHYCCD(self.handle))
        }
    }
    pub fn set_color_mode(&mut self, mono: bool) -> Result<()> {
        // TODO: handle mono cameras correctly
        if mono {
            unsafe {
                // well, this can fail if debayering just isn't supported. so let's... not
                QHYCCDCam::SetQHYCCDDebayerOnOff(self.handle, 0);
            }
            self.set_control(Control::CONTROL_WBR, 4000.0)?;
            self.set_control(Control::CONTROL_WBG, 4000.0)?;
            self.set_control(Control::CONTROL_WBB, 4000.0)?;
            self.settings.channels = 1;
        } else {
            unsafe {
                check(QHYCCDCam::SetQHYCCDDebayerOnOff(self.handle, 1))?;
            }
            self.set_control(Control::CONTROL_WBR, 4000.0)?;
            self.set_control(Control::CONTROL_WBG, 4000.0)?;
            self.set_control(Control::CONTROL_WBB, 4000.0)?;
            self.settings.channels = 3;
        }
        Ok(())
    }
    pub fn set_defaults(&mut self) -> Result<()> {
        unsafe {
        println!("Hey wait gotta get dimensions first");
        let ((chipw, chiph), (imagew, imageh), (pixelw, pixelh), bpp) = self.get_dimensions()?;
        match QHYCCDCam::IsQHYCCDControlAvailable(self.handle, Control::Color as i32) {
            1 | 2 | 3 | 4 => {
                self.set_color_mode(false)?;
            },
            0 => {
                // no, the color control is not available. mono it is!
                self.set_color_mode(true)?;
            }
            a @ _ => {
                println!("unexpected response when querying color setting: {}", a);
                return Err(CameraError::QHYError)
            }
        }
        self.set_roi(0, 0, imagew, imageh)?;
        self.set_bin_mode(1)?;
        if self.has_control(Control::TransferBit) {
            check(QHYCCDCam::SetQHYCCDBitsMode(self.handle, 16))?;
            self.settings.bpp = 16;
            println!("set tp 16bpp");
        }
        println!("roi set to {} x {} ???", imagew, imageh);
        println!("gain limits: {:?}", self.get_control_limits(Control::Gain)?);
        println!("exposure limits: {:?}", self.get_control_limits(Control::Exposure)?);
        println!("brightness: {:?}", self.get_control_limits(Control::Brightness)?);
        println!("gamma: {:?}", self.get_control_limits(Control::Gamma)?);
        println!("contrast: {:?}", self.get_control_limits(Control::Contrast)?);
//        panic!("hi");
        Ok(())
        }
    }

    pub fn set_roi(&mut self, x: u32, y: u32, w: u32, h: u32) -> Result<()> {
        unsafe {
        check(QHYCCDCam::SetQHYCCDResolution(self.handle, x, y, w, h))?;
        self.settings.image_roi = (w, h);
        self.settings.image_xy = (x, y);
        Ok(())
        }
    }

    pub fn set_bin_mode(&mut self, bin: u8) -> Result<()> {
        match bin {
            1 => if !self.has_control(Control::Bin1x1Mode) { return Err(CameraError::InvalidControl); }
            2 => if !self.has_control(Control::Bin2x2Mode) { return Err(CameraError::InvalidControl); }
            3 => if !self.has_control(Control::Bin3x3Mode) { return Err(CameraError::InvalidControl); }
            4 => if !self.has_control(Control::Bin4x4Mode) { return Err(CameraError::InvalidControl); }
            _ => { return Err(CameraError::InvalidControl); }
        }
        unsafe {
            check(QHYCCDCam::SetQHYCCDBinMode(self.handle, bin as i32, bin as i32))?;
            self.settings.bin = bin;
            Ok(())
        }
    }

    pub fn get_exposure_remaining(&self) -> u32 {
        unsafe {
            QHYCCDCam::GetQHYCCDExposureRemaining(self.handle)
        }
    }

    pub fn display_camera_dimensions(&self) -> Result<()> {
        let (overscan_start_X, overscan_start_Y, overscan_size_X, overscan_size_Y) = self.get_overscan_area()?;
        println!("Overscan area:");
        println!("  startX x startY : {:05} x {:05}", overscan_start_X, overscan_start_Y);
        println!("  sizeX  x sizeY  : {:05} x {:05}", overscan_size_X, overscan_size_Y);
        let (effective_start_X, effective_start_Y, effective_size_X, effective_size_Y) = self.get_effective_area()?;
        println!("Effective area:");
        println!("  startX x startY : {:05} x {:05}", effective_start_X, effective_start_Y);
        println!("  sizeX  x sizeY  : {:05} x {:05}", effective_size_X, effective_size_Y);
        let ((chipw, chiph), (imagew, imageh), (pixelw, pixelh), bpp) = self.get_dimensions()?;
        println!("Chip dimensions:");
        println!("Chip size (w/h):      {:05} x {:05} [mm]", chipw, chiph);
        println!("Pixel size (w/h):     {:05} x {:05} [um]", pixelw, pixelh);
        println!("Image size (w/h):     {:05} x {:05} [pixels]", imagew, imageh);
        println!("   bpp:               {}", bpp);
        Ok(())
    }

    pub fn cancel_exposure(&self) -> Result<()> {
        unsafe {
            check(QHYCCDCam::CancelQHYCCDExposingAndReadout(self.handle))
        }
    }

    pub fn begin_exposure(&self) -> Result<()> {
        let result = unsafe { QHYCCDCam::ExpQHYCCDSingleFrame(self.handle) };
        match QHYCCDCam::QHYResult::from(result as u32) {
            QHYResult::QHYCCD_SUCCESS => {
//                println!("Didn't expect this result...");
                Ok(())
            },
            QHYResult::QHYCCD_READ_DIRECTLY => {
//                println!("Exp complete, example sleeps so i'll sleep too");
                Ok(())
            },
            QHYResult::QHYCCD_DELAY_200MS => {
                println!("Sleeping 200ms... but not actually, bout to have a bug :):)))");
                Ok(())
            },
            a @ _ =>{
                println!("exp err: {:?}", a);
                return Err(CameraError::QHYError);
            }
        }
    }

    fn resize_buffer(&mut self) {
        println!("Resizing buffer to 3x{}", self.settings.frame_size());
        self.frame_size = self.settings.frame_size();
        self.buffer = vec![
            vec![0u8; self.frame_size],
            vec![0u8; self.frame_size],
            vec![0u8; self.frame_size],
        ];
    }

    pub fn read_frame(&self, mut buf: Vec<u8>) -> Result<(Vec<u8>, u32, u32, u8, u8)> {
        let mut castediw = 0i32;
        let mut castedih = 0i32;
        let mut castedbpp = 0i32;
        let mut channels = 0i32;
        println!("Getting data...");
        unsafe {
            use std::time::{SystemTime, UNIX_EPOCH};
            let start = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
            let start = start.as_secs() * 1000 + (start.subsec_nanos() as u64) / 1000000;
            check(QHYCCDCam::GetQHYCCDSingleFrame(self.handle, &mut castediw, &mut castedih, &mut castedbpp, &mut channels, buf.as_mut_ptr()))?;
            let end = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
            let end = end.as_secs() * 1000 + (end.subsec_nanos() as u64) / 1000000;
            println!("camera record time: {}ms", end - start);
        }
        Ok((buf, castediw as u32, castedih as u32, castedbpp as u8, channels as u8))
    }
    /*
    pub fn take_image_live(&self, path: &str) -> Result<()> {
        unsafe {
        let exposure_duration = self.get_control(Control::Exposure);
        let exposure_ms = exposure_duration / 1000.0;
        println!("Exposure duration: {}", exposure_ms);

        let mut bufsize: usize = self.image_buf_size();
        println!("Ok, we'll need {} bytes...", bufsize);

        let data_layout = Layout::from_size_align(bufsize as usize, 8).unwrap();
        let data = alloc(data_layout);

        check(QHYCCDCam::BeginQHYCCDLive(self.handle))?;

        loop {
//            println!("Getting data...");

            let mut imagew: i32 = self.imagew as i32;
            let mut imageh: i32 = self.imageh as i32;
            let mut bpp: i32 = self.bpp as i32;
            let mut channels: i32 = self.channels as i32;

            let frame = match frame_rx.recv() {
                Ok(frame) => frame,
                Err(signal) => {
                    return Err(CollectionError::BufferClosed);
                }
            };
//            println!("w {} h {} bpp {} channels {}", imagew, imageh, bpp, channels);
            match check(QHYCCDCam::GetQHYCCDLiveFrame(self.handle, &mut imagew, &mut imageh, &mut bpp, &mut channels, data.as_mut_ptr())) {
                Ok(()) => {
                    processor.send(Ok(frame))
                }
                    println!("Ok, guess we got it?");

                    //fix_channels(dataslice);

                    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, imagew as u32, imageh as u32);
                    encoder.set(png::ColorType::RGB).set(png::BitDepth::Eight);
                    // crazy theory, endianness might be wrong...
                    // so flip the bytes first

                    let mut writer = encoder.write_header().unwrap();

                    writer.write_image_data(dataslice).unwrap();

                    println!("NEXT!");

                    QHYResult::QHYCCD_SUCCESS
                },
                Err(CameraError::QHYError) => {
//                    println!("Error in live capture: {:?}. sleeping..", CameraError::QHYError);
                    println!("Still waiting...");
                    std::thread::sleep(std::time::Duration::from_millis(100));
                    QHYResult::QHYCCD_ERROR
                }
                Err(_) => { unreachable!(); }
            };
        }

        dealloc(data as *mut u8, data_layout);
        Ok(())

        pub fn begin_exposure(&self) -> Result<()> {

        }
        }
    }

    pub fn take_image(&self, path: &str) -> Result<()> {
        unsafe {
        let exposure_duration = self.get_control(Control::Exposure);
        let exposure_ms = exposure_duration / 1000.0;
        println!("Exposure duration: {}", exposure_ms);
        let result = QHYCCDCam::ExpQHYCCDSingleFrame(self.handle);
        match QHYCCDCam::QHYResult::from(result as u32) {
            QHYResult::QHYCCD_SUCCESS => {
//                println!("Didn't expect this result...");
//                std::thread::sleep(std::time::Duration::from_millis(1000));
            },
            QHYResult::QHYCCD_READ_DIRECTLY => {
                println!("Exp complete, example sleeps so i'll sleep too");
//                std::thread::sleep(std::time::Duration::from_millis(1000));
            },
            QHYResult::QHYCCD_DELAY_200MS => {
                println!("Sleeping 200ms...");
            },
            a @ _ =>{
                println!("exp err: {:?}", a);
                return Err(CameraError::QHYError);
            }
        }

        let mut chipw: f64 = 0.0;
        let mut chiph: f64 = 0.0;
        let mut imagew: i32 = 0;
        let mut imageh: i32 = 0;
        let mut pixelw: f64 = 0.0;
        let mut pixelh: f64 = 0.0;
        let mut bpp: i32 = 0;
        check(QHYCCDCam::GetQHYCCDChipInfo(
            self.handle,
            &mut chipw as *mut os::raw::c_double,
            &mut chiph as *mut os::raw::c_double,
            &mut imagew as *mut os::raw::c_int,
            &mut imageh as *mut os::raw::c_int,
            &mut pixelw as *mut os::raw::c_double,
            &mut pixelh as *mut os::raw::c_double,
            &mut bpp as *mut os::raw::c_int))?;
        let mut channels: i32 = 3;

        let mut bufsize = self.image_buf_size();
        println!("Ok, we'll need {} bytes...", bufsize);
        println!("but you claim to want {} bytes...", QHYCCDCam::GetQHYCCDMemLength(self.handle));
        /*
        if self.bin != 1 {
            println!("Correcting for binning...");
            bufsize /= self.bin as i32;
            bufsize /= self.bin as i32;
            println!("you're getting {} bytes!", bufsize);
        }
        */
        let data_layout = Layout::from_size_align(bufsize as usize, 8).unwrap();
        let data = alloc(data_layout);

        let mut counter: i64 = (self.get_control(Control::Exposure) as u64 / 1000) as i64;

        while counter > 0 {
            println!("I think there's about {}ms remaining", counter);
            println!("You think there's about {}ms remaining", self.get_exposure_remaining());
            std::thread::sleep(std::time::Duration::from_millis(500));
            println!("Camera temp is currently: {}", self.get_control(Control::CurTemp));
            counter -= 500;
        }

        let mut castediw = self.imagew as i32;
        let mut castedih = self.imageh as i32;
        let mut castedbpp = self.bpp as i32;
        let mut channels = self.channels as i32;
        println!("Getting data...");
        check(QHYCCDCam::GetQHYCCDSingleFrame(self.handle, &mut castediw, &mut castedih, &mut castedbpp, &mut channels, data))?;
        println!("Ok, guess we got it?");
        println!("image: {} x {}", castediw, castedih);
        println!("bpp: {}", castedbpp);
        println!("channels: {}", channels);

        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, castediw as u32, castedih as u32);
        encoder.set(png::ColorType::RGB).set(png::BitDepth::Sixteen);
        // crazy theory, endianness might be wrong...
        // so flip the bytes first
        let dataslice: &mut [u8] = 
            unsafe {
                std::slice::from_raw_parts_mut(
                    data,
                    bufsize as usize
                )
            };

        fix_channels(dataslice);

        let mut writer = encoder.write_header().unwrap();

        if self.bin != 1 {
            let scale = self.bin as usize * self.bin as usize;
            let mut cherrypicked = vec![0; bufsize as usize / scale];
            for i in 0..cherrypicked.len() / 6 {
                for j in 0..6 {
                    cherrypicked[i * 6 + j] = dataslice[i * 6 * scale + j];
                }
            }
            writer.write_image_data(&cherrypicked).unwrap();
        } else {
            writer.write_image_data(dataslice).unwrap();
        }
        dealloc(data as *mut u8, data_layout);
        Ok(())
    }
    }
*/
    pub fn get_overscan_area(&self) -> Result<(u32, u32, u32, u32)> {
        unsafe {
        let mut startX: i32 = 0;
        let mut startY: i32 = 0;
        let mut sizeX: i32 = 0;
        let mut sizeY: i32 = 0;
        check(QHYCCDCam::GetQHYCCDOverScanArea(
            self.handle,
           &mut startX as *mut os::raw::c_int,
           &mut startY as *mut os::raw::c_int,
           &mut sizeX as *mut os::raw::c_int,
           &mut sizeY as *mut os::raw::c_int
        ))?;
        Ok((startX as u32, startY as u32, sizeX as u32, sizeY as u32))
        }
    }
    pub fn get_effective_area(&self) -> Result<(u32, u32, u32, u32)> {
        unsafe {
        let mut startX: i32 = 0;
        let mut startY: i32 = 0;
        let mut sizeX: i32 = 0;
        let mut sizeY: i32 = 0;
        check(QHYCCDCam::GetQHYCCDEffectiveArea(
            self.handle,
            &mut startX as *mut os::raw::c_int,
            &mut startY as *mut os::raw::c_int,
            &mut sizeX as *mut os::raw::c_int,
            &mut sizeY as *mut os::raw::c_int
        ))?;
        Ok((startX as u32, startY as u32, sizeX as u32, sizeY as u32))
        }
    }
    pub fn get_dimensions(&self) -> Result<((f64, f64), (u32, u32), (f64, f64), u32)> {
        unsafe {
        let mut chipw: f64 = 0.0;
        let mut chiph: f64 = 0.0;
        let mut imagew: i32 = 0;
        let mut imageh: i32 = 0;
        let mut pixelw: f64 = 0.0;
        let mut pixelh: f64 = 0.0;
        let mut bpp: i32 = 0;
        check(QHYCCDCam::GetQHYCCDChipInfo(
            self.handle,
            &mut chipw as *mut os::raw::c_double,
            &mut chiph as *mut os::raw::c_double,
            &mut imagew as *mut os::raw::c_int,
            &mut imageh as *mut os::raw::c_int,
            &mut pixelw as *mut os::raw::c_double,
            &mut pixelh as *mut os::raw::c_double,
            &mut bpp as *mut os::raw::c_int))?;
        Ok((
            (chipw, chiph),
            (imagew as u32, imageh as u32),
            (pixelw, pixelh),
            bpp as u32
        ))
        }
    }
}