Instrument Neutral Distributed Interface INDI  2.0.2
v4l2driver.h
Go to the documentation of this file.
1 #if 0
2 V4L2 INDI Driver
3 INDI Interface for V4L2 devices
4 Copyright (C) 2003 - 2005 Jasem Mutlaq (mutlaqja@ikarustech.com)
5  Copyright (C) 2013 Geehalel (geehalel@gmail.com)
6 
7  This library is free software;
8 you can redistribute it and / or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation;
11 either
12 version 2.1 of the License, or (at your option) any later version.
13 
14 This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY;
16 without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library;
22 if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
24 
25 #endif
26 
27 #pragma once
28 
29 #include "indiccd.h"
30 #include "webcam/v4l2_base.h"
31 
32 #define IMAGE_CONTROL "Image Control"
33 #define IMAGE_GROUP "V4L2 Control"
34 #define IMAGE_BOOLEAN "V4L2 Options"
35 #define CAPTURE_FORMAT "Capture Options"
36 
37 #define MAX_PIXELS 4096 /* Max number of pixels in one dimension */
38 #define ERRMSGSIZ 1024
39 
40 #define TEMPFILE_LEN 16
41 
42 #ifdef __FreeBSD__
43 typedef unsigned long ulong;
44 #endif
45 
46 class Lx;
47 
48 class V4L2_Driver : public INDI::CCD
49 {
50 public:
51  V4L2_Driver();
52  explicit V4L2_Driver(std::string label, std::string path);
53  virtual ~V4L2_Driver() override;
54 
55  /* INDI Functions that must be called from indidrivermain */
56  virtual void ISGetProperties(const char *dev) override;
57  virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
58  virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
59  virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
60 
61  virtual bool initProperties() override;
62  virtual bool updateProperties() override;
63  virtual void initCamBase();
64 
65  static void newFrame(void *p);
66  void stackFrame();
67  void newFrame();
68 
69  protected:
70  virtual bool Connect() override;
71  virtual bool Disconnect() override;
72 
73  virtual const char *getDefaultName() override;
74  virtual bool StartExposure(float duration) override;
75  virtual bool AbortExposure() override;
76  virtual bool UpdateCCDFrame(int x, int y, int w, int h) override;
77  virtual bool UpdateCCDBin(int hor, int ver) override;
78  virtual bool SetCaptureFormat(uint8_t index) override;
79  virtual bool saveConfigItems(FILE *fp) override;
80 
81  virtual bool StartStreaming() override;
82  virtual bool StopStreaming() override;
83 
84  /* Structs */
85  typedef struct
86  {
87  int width;
88  int height;
89  int bpp;
90  //int expose;
91  double expose;
92  unsigned char *Y;
93  unsigned char *U;
94  unsigned char *V;
95  unsigned char *RGB24Buffer;
96  unsigned char *compressedFrame;
97  float *stackedFrame;
98  float *darkFrame;
99  } img_t;
100 
101  enum
102  {
104  IMAGE_RGB
105  };
106 
108  {
113  STACK_RESET_DARK = 4
114  };
115 
116  /* Switches */
117 
121 
122  /* Texts */
123  IText PortT[1] {};
126 
127  /* Numbers */
128  //INumber *ExposeTimeN;
131 
132  /* Switch vectors */
133  ISwitchVectorProperty ImageDepthSP; /* 8 bits or 16 bits switch */
134  ISwitchVectorProperty StackModeSP; /* StackMode switch */
135  ISwitchVectorProperty InputsSP; /* Select input switch */
136  ISwitchVectorProperty CaptureFormatsSP; /* Select Capture format switch */
137  ISwitchVectorProperty CaptureSizesSP; /* Select Capture size switch (Discrete)*/
138  ISwitchVectorProperty FrameRatesSP; /* Select Frame rate (Discrete) */
141 
142  unsigned int v4loptions;
143  unsigned int v4ladjustments;
145 
146  /* Number vectors */
147  //INumberVectorProperty *ExposeTimeNP; /* Exposure */
148  INumberVectorProperty CaptureSizesNP; /* Select Capture size switch (Step/Continuous)*/
149  INumberVectorProperty FrameRateNP; /* Frame rate (Step/Continuous) */
150  INumberVectorProperty ImageAdjustNP; /* Image controls */
151 
152  /* Text vectors */
156 
157  /* Pointers to optional properties */
160 
161  /* Initilization functions */
162  //virtual void connectCamera();
163  virtual void getBasicData();
164  bool getPixelFormat(uint32_t v4l2format, INDI_PIXEL_FORMAT &pixelFormat, uint8_t &pixelDepth);
165  void allocateBuffers();
166  void releaseBuffers();
167  void updateFrameSize();
168 
169  /* Shutter control */
170  bool setShutter(double duration);
171  bool setManualExposure(double duration);
172  bool startlongexposure(double timeinsec);
173  static void lxtimerCallback(void *userpointer);
174  static void stdtimerCallback(void *userpointer);
175 
176  /* start/stop functions */
177  bool start_capturing(bool do_stream);
178  bool stop_capturing();
179 
180  virtual void updateV4L2Controls();
181 
182  /* Variables */
184 
186 
187  int subframeCount; /* For stacking */
189  double divider; /* For limits */
190  img_t *V4LFrame; /* Video frame */
191 
192  struct timeval capture_start; /* To calculate how long a frame take */
193 
194  //struct timeval capture_end;
195 
196  struct timeval frame_duration;
197  struct timeval frame_received;
198 
199  struct timeval exposure_duration;
200  struct timeval elapsed_exposure;
201 
202  struct timeval getElapsedExposure() const;
203  float getRemainingExposure() const;
204 
205  unsigned int stackMode;
206  ulong frameBytes;
207  unsigned int non_capture_frames;
211 
212  //Long Exposure
213  Lx *lx;
214  int lxtimer;
215  int stdtimer;
216 
217  short lxstate;
218 
219  char defaultVideoPort[256] = {"/dev/video0"};
220  char configPort[256] = {0};
221 };
Class to provide general functionality of CCD cameras with a single CCD sensor, or a primary CCD sens...
Definition: indiccd.h:116
Definition: Lx.h:24
bool setManualExposure(double duration)
Definition: v4l2driver.cpp:965
ITextVectorProperty camNameTP
Definition: v4l2driver.h:154
virtual bool StopStreaming() override
StopStreaming Stop live video streaming.
unsigned int v4loptions
Definition: v4l2driver.h:142
virtual const char * getDefaultName() override
virtual bool StartStreaming() override
StartStreaming Start live video streaming.
ITextVectorProperty CaptureColorSpaceTP
Definition: v4l2driver.h:155
INumberVectorProperty FrameRateNP
Definition: v4l2driver.h:149
virtual void getBasicData()
struct timeval getElapsedExposure() const
struct timeval exposure_duration
Definition: v4l2driver.h:199
virtual bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
INumberVectorProperty CaptureSizesNP
Definition: v4l2driver.h:148
struct timeval frame_received
Definition: v4l2driver.h:197
bool setShutter(double duration)
Definition: v4l2driver.cpp:922
double divider
Definition: v4l2driver.h:189
ISwitchVectorProperty FrameRatesSP
Definition: v4l2driver.h:138
INumber FrameRateN[1]
Definition: v4l2driver.h:130
virtual void updateV4L2Controls()
virtual bool Disconnect() override
Disconnect from device.
INumberVectorProperty ImageAdjustNP
Definition: v4l2driver.h:150
ISwitchVectorProperty InputsSP
Definition: v4l2driver.h:135
bool startlongexposure(double timeinsec)
char configPort[256]
Definition: v4l2driver.h:220
bool useExtCtrl
Definition: v4l2driver.h:144
ISwitchVectorProperty CaptureFormatsSP
Definition: v4l2driver.h:136
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
Definition: v4l2driver.cpp:739
@ STACK_RESET_DARK
Definition: v4l2driver.h:113
virtual bool UpdateCCDFrame(int x, int y, int w, int h) override
CCD calls this function when CCD Frame dimension needs to be updated in the hardware....
IText camNameT[1]
Definition: v4l2driver.h:124
ITextVectorProperty PortTP
Definition: v4l2driver.h:153
void releaseBuffers()
virtual void initCamBase()
Definition: v4l2driver.cpp:252
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
Definition: v4l2driver.cpp:415
int frameCount
Definition: v4l2driver.h:188
void allocateBuffers()
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition: v4l2driver.cpp:764
virtual bool AbortExposure() override
Abort ongoing exposure.
ISwitchVectorProperty ImageDepthSP
Definition: v4l2driver.h:133
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: v4l2driver.cpp:295
virtual bool StartExposure(float duration) override
Start exposing primary CCD chip.
Definition: v4l2driver.cpp:873
ulong frameBytes
Definition: v4l2driver.h:206
char defaultVideoPort[256]
Definition: v4l2driver.h:219
bool stop_capturing()
void stackFrame()
unsigned int non_capture_frames
Definition: v4l2driver.h:207
INumber * AbsExposureN
Definition: v4l2driver.h:158
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: v4l2driver.cpp:152
struct timeval frame_duration
Definition: v4l2driver.h:196
bool v4l_capture_started
Definition: v4l2driver.h:208
struct timeval capture_start
Definition: v4l2driver.h:192
ISwitchVectorProperty CaptureSizesSP
Definition: v4l2driver.h:137
img_t * V4LFrame
Definition: v4l2driver.h:190
struct timeval elapsed_exposure
Definition: v4l2driver.h:200
int subframeCount
Definition: v4l2driver.h:187
bool is_exposing
Definition: v4l2driver.h:210
ISwitchVectorProperty ColorProcessingSP
Definition: v4l2driver.h:140
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
Definition: v4l2driver.cpp:257
ISwitchVectorProperty * ManualExposureSP
Definition: v4l2driver.h:159
ISwitch ImageDepthS[2]
Definition: v4l2driver.h:118
ISwitch StackModeS[5]
Definition: v4l2driver.h:119
void newFrame()
INumber * FrameN
Definition: v4l2driver.h:129
virtual ~V4L2_Driver() override
Definition: v4l2driver.cpp:135
virtual bool UpdateCCDBin(int hor, int ver) override
CCD calls this function when CCD Binning needs to be updated in the hardware. Derived classes should ...
IText CaptureColorSpaceT[3]
Definition: v4l2driver.h:125
bool start_capturing(bool do_stream)
unsigned int stackMode
Definition: v4l2driver.h:205
void updateFrameSize()
Definition: v4l2driver.cpp:140
IText PortT[1]
Definition: v4l2driver.h:123
short lxstate
Definition: v4l2driver.h:217
virtual bool SetCaptureFormat(uint8_t index) override
SetCaptureFormat Set Active Capture format.
INDI::V4L2_Base * v4l_base
Definition: v4l2driver.h:183
static void stdtimerCallback(void *userpointer)
unsigned int v4ladjustments
Definition: v4l2driver.h:143
bool getPixelFormat(uint32_t v4l2format, INDI_PIXEL_FORMAT &pixelFormat, uint8_t &pixelDepth)
float getRemainingExposure() const
static void lxtimerCallback(void *userpointer)
char device_name[MAXINDIDEVICE]
Definition: v4l2driver.h:185
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save configuration items in XML file.
ISwitch ColorProcessingS[3]
Definition: v4l2driver.h:120
bool is_capturing
Definition: v4l2driver.h:209
ISwitchVectorProperty * Options
Definition: v4l2driver.h:139
ISwitchVectorProperty StackModeSP
Definition: v4l2driver.h:134
ISState
Switch state.
Definition: indiapi.h:150
#define MAXINDIDEVICE
Definition: indiapi.h:193
INDI_PIXEL_FORMAT
Definition: indibasetypes.h:70
Namespace to encapsulate INDI client, drivers, and mediator classes.
One number descriptor.
One switch descriptor.
One text descriptor.
float * stackedFrame
Definition: v4l2driver.h:97
unsigned char * RGB24Buffer
Definition: v4l2driver.h:95
unsigned char * compressedFrame
Definition: v4l2driver.h:96
unsigned char * Y
Definition: v4l2driver.h:92
unsigned char * U
Definition: v4l2driver.h:93
unsigned char * V
Definition: v4l2driver.h:94
Number vector property descriptor.
Definition: indiapi.h:319
Switch vector property descriptor.
Definition: indiapi.h:367
Text vector property descriptor.
Definition: indiapi.h:246