balena-dslr-js

A js tool to run on a pi zero w to control cameras via gphoto2
git clone git://archive.git.mtrnord.blog/balena-dslr/balena-dslr-js.git
Log | Files | Refs | README | LICENSE

camera.ts (2917B)


      1 import { Camera, run } from '@typedproject/gphoto2-driver';
      2 import { sleep } from './utils';
      3 
      4 export default class CameraActions {
      5     private camera: Camera;
      6     constructor() {
      7         this.camera = Camera.listen();
      8     }
      9     /**
     10      * Do various capture actions
     11      * 
     12      * @param autoFocus Should the camera start to autofocus? (May never finish and lock up camera!)
     13      * @param preview Capture a preview image and save it to the sd card?
     14      * @param capture Save a image in full quality to the sd card
     15      */
     16     public capture(autoFocus = false, preview = false, capture = false) {
     17         if (this.camera.isClosed()) {
     18             this.camera = Camera.listen();
     19         }
     20         return run(async () => {
     21             await sleep(2000);
     22 
     23             if (autoFocus) {
     24                 console.info("Autofocus =============================");
     25                 this.camera.autoFocus();
     26 
     27                 await sleep(20000);
     28             }
     29 
     30             if (preview) {
     31                 console.info("Preview ===============================");
     32 
     33                 this.camera.widgets.apply({
     34                     "/settings/capturetarget": "Memory card",
     35                 });
     36 
     37                 const res = await this.camera.capturePreviewAsync();
     38                 res?.close();
     39 
     40                 console.info("File saved");
     41             }
     42 
     43             if (capture) {
     44                 console.info("Capture ===============================");
     45 
     46                 this.camera.widgets.apply({
     47                     "/settings/capturetarget": "Memory card",
     48                 });
     49 
     50                 const res = await this.camera.captureImageAsync();
     51                 res?.close();
     52                 console.info("File saved");
     53             }
     54         });
     55     }
     56 
     57     /**
     58      * Start a timelapse capture
     59      * 
     60      * @param interval_ms The amount of time in between a capture being done in miliseconds
     61      * @param frames The number of frames to overall take
     62      * @param autoExpose If we should try to auto adjust for changing exposure
     63      */
     64     public async run_timelapse(interval_ms: number, frames: number, autoExpose: boolean) {
     65         if (this.camera.isClosed()) {
     66             this.camera = Camera.listen();
     67         }
     68 
     69         let current_frame = 1;
     70 
     71         await sleep(2000);
     72         return run(async () => {
     73             while (current_frame <= frames) {
     74 
     75                 console.info("Capture ===============================");
     76 
     77                 this.camera.widgets.apply({
     78                     "/settings/capturetarget": "Memory card",
     79                 });
     80 
     81                 const res = await this.camera.captureImageAsync();
     82                 res?.close();
     83 
     84                 console.info("File saved ============================");
     85 
     86                 if (autoExpose) {
     87                     // TODO change exposure to the next step if needed
     88                 }
     89 
     90                 await sleep(interval_ms);
     91             }
     92         });
     93     }
     94 }