| 1 | /* |
| 2 | Driver type: GPhoto Camera INDI Driver |
| 3 | |
| 4 | Copyright (C) 2009 Geoffrey Hausheer |
| 5 | Copyright (C) 2013 Jasem Mutlaq (mutlaqja AT ikarustech DOT com) |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU Lesser General Public License as published |
| 9 | by the Free Software Foundation; either version 2.1 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 | License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public License |
| 18 | along with this library; if not, write to the Free Software Foundation, |
| 19 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
| 25 | #include "gphoto_driver.h" |
| 26 | |
| 27 | #include <indiccd.h> |
| 28 | #include <indifocuserinterface.h> |
| 29 | |
| 30 | #include <map> |
| 31 | #include <future> |
| 32 | #include <string> |
| 33 | |
| 34 | #define MAXEXPERR 10 /* max err in exp time we allow, secs */ |
| 35 | #define OPENDT 5 /* open retry delay, secs */ |
| 36 | |
| 37 | typedef struct _Camera Camera; |
| 38 | |
| 39 | enum |
| 40 | { |
| 41 | ON_S, |
| 42 | OFF_S |
| 43 | }; |
| 44 | |
| 45 | typedef struct |
| 46 | { |
| 47 | gphoto_widget * widget; |
| 48 | union |
| 49 | { |
| 50 | INumber num; |
| 51 | ISwitch * sw; |
| 52 | IText text; |
| 53 | } item; |
| 54 | union |
| 55 | { |
| 56 | INumberVectorProperty num; |
| 57 | ISwitchVectorProperty sw; |
| 58 | ITextVectorProperty text; |
| 59 | } prop; |
| 60 | } cam_opt; |
| 61 | |
| 62 | class GPhotoCCD : public INDI::CCD, public INDI::FocuserInterface |
| 63 | { |
| 64 | public: |
| 65 | explicit GPhotoCCD(); |
| 66 | explicit GPhotoCCD(const char * model, const char * port); |
| 67 | virtual ~GPhotoCCD() override; |
| 68 | |
| 69 | const char * getDefaultName() override; |
| 70 | |
| 71 | bool initProperties() override; |
| 72 | void ISGetProperties(const char * dev) override; |
| 73 | bool updateProperties() override; |
| 74 | |
| 75 | bool Connect() override; |
| 76 | bool Disconnect() override; |
| 77 | |
| 78 | bool StartExposure(float duration) override; |
| 79 | bool AbortExposure() override; |
| 80 | bool UpdateCCDFrame(int x, int y, int w, int h) override; |
| 81 | |
| 82 | // enable binning |
| 83 | bool UpdateCCDBin(int hor, int ver) override; |
| 84 | |
| 85 | virtual bool ISNewNumber(const char * dev, const char * name, double values[], char * names[], int n) override; |
| 86 | virtual bool ISNewSwitch(const char * dev, const char * name, ISState * states, char * names[], int n) override; |
| 87 | virtual bool ISNewText(const char * dev, const char * name, char * texts[], char * names[], int n) override; |
| 88 | |
| 89 | static void ExposureUpdate(void * vp); |
| 90 | void ExposureUpdate(); |
| 91 | |
| 92 | static void UpdateExtendedOptions(void * vp); |
| 93 | void UpdateExtendedOptions(bool force = false); |
| 94 | |
| 95 | static void UpdateFocusMotionHelper(void *context); |
| 96 | void UpdateFocusMotionCallback(); |
| 97 | |
| 98 | protected: |
| 99 | // Misc. |
| 100 | bool saveConfigItems(FILE * fp) override; |
| 101 | void addFITSKeywords(INDI::CCDChip * targetChip) override; |
| 102 | void TimerHit() override; |
| 103 | |
| 104 | // Capture format |
| 105 | bool SetCaptureFormat(uint8_t index) override; |
| 106 | |
| 107 | // Simulation Triggered |
| 108 | void simulationTriggered(bool enabled) override; |
| 109 | |
| 110 | // Upload Mode |
| 111 | bool UpdateCCDUploadMode(CCD_UPLOAD_MODE mode) override; |
| 112 | |
| 113 | // Focusing |
| 114 | #if 0 |
| 115 | bool SetFocuserSpeed(int speed) override; |
| 116 | IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override; |
| 117 | #endif |
| 118 | /** |
| 119 | * \brief MoveFocuser the focuser to an relative position. |
| 120 | * \param dir Direction of focuser, either FOCUS_INWARD or FOCUS_OUTWARD. |
| 121 | * \param ticks The relative ticks to move. |
| 122 | * \return Return IPS_OK if motion is completed and focuser reached requested position. Return |
| 123 | * IPS_BUSY if focuser started motion to requested position and is in progress. |
| 124 | * Return IPS_ALERT if there is an error. |
| 125 | */ |
| 126 | virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override; |
| 127 | |
| 128 | // Streaming |
| 129 | bool StartStreaming() override; |
| 130 | bool StopStreaming() override; |
| 131 | void streamLiveView(); |
| 132 | |
| 133 | std::mutex liveStreamMutex; |
| 134 | bool m_RunLiveStream; |
| 135 | //bool stopLiveVideo(); |
| 136 | |
| 137 | // Preview |
| 138 | //bool startLivePreview(); |
| 139 | |
| 140 | |
| 141 | private: |
| 142 | ISwitch * create_switch(const char * basestr, char ** options, int max_opts, int setidx); |
| 143 | void AddWidget(gphoto_widget * widget); |
| 144 | void UpdateWidget(cam_opt * opt); |
| 145 | void ShowExtendedOptions(void); |
| 146 | void HideExtendedOptions(void); |
| 147 | |
| 148 | double CalcTimeLeft(); |
| 149 | bool grabImage(); |
| 150 | |
| 151 | char name[MAXINDIDEVICE]; |
| 152 | char model[MAXINDINAME]; |
| 153 | char port[MAXINDINAME]; |
| 154 | |
| 155 | struct timeval ExpStart; |
| 156 | double ExposureRequest; |
| 157 | |
| 158 | gphoto_driver * gphotodrv; |
| 159 | std::map<std::string, cam_opt *> CamOptions; |
| 160 | int expTID; /* exposure callback timer id, if any */ |
| 161 | int optTID; /* callback for exposure timer id */ |
| 162 | int focusSpeed {0}; |
| 163 | |
| 164 | char * on_off[2]; |
| 165 | int timerID; |
| 166 | bool frameInitialized; |
| 167 | bool isTemperatureSupported { false }; |
| 168 | |
| 169 | // Focus |
| 170 | bool m_CanFocus { false }; |
| 171 | int32_t m_TargetLargeStep {0}, m_TargetMedStep {0}, m_TargetLowStep {0}, m_FocusTimerID {-1}; |
| 172 | |
| 173 | int liveVideoWidth {-1}; |
| 174 | int liveVideoHeight {-1}; |
| 175 | |
| 176 | // binning ? |
| 177 | bool binning { false }; |
| 178 | |
| 179 | ISwitch mConnectS[2]; |
| 180 | ISwitchVectorProperty mConnectSP; |
| 181 | IText mPortT[1] {}; |
| 182 | ITextVectorProperty PortTP; |
| 183 | |
| 184 | INumber mMirrorLockN[1]; |
| 185 | INumberVectorProperty mMirrorLockNP; |
| 186 | |
| 187 | INumber mExposureN[1]; |
| 188 | INumberVectorProperty mExposureNP; |
| 189 | |
| 190 | ISwitch * mIsoS = nullptr; |
| 191 | ISwitchVectorProperty mIsoSP; |
| 192 | |
| 193 | ISwitch captureTargetS[2]; |
| 194 | ISwitchVectorProperty captureTargetSP; |
| 195 | enum |
| 196 | { |
| 197 | CAPTURE_INTERNAL_RAM, |
| 198 | CAPTURE_SD_CARD |
| 199 | }; |
| 200 | |
| 201 | ISwitch SDCardImageS[3]; |
| 202 | ISwitchVectorProperty SDCardImageSP; |
| 203 | enum |
| 204 | { |
| 205 | SD_CARD_SAVE_IMAGE, |
| 206 | SD_CARD_DELETE_IMAGE, |
| 207 | SD_CARD_IGNORE_IMAGE, |
| 208 | |
| 209 | }; |
| 210 | |
| 211 | ISwitch autoFocusS[1]; |
| 212 | ISwitchVectorProperty autoFocusSP; |
| 213 | |
| 214 | ISwitch livePreviewS[2]; |
| 215 | ISwitchVectorProperty livePreviewSP; |
| 216 | |
| 217 | ISwitch * mExposurePresetS = nullptr; |
| 218 | ISwitchVectorProperty mExposurePresetSP; |
| 219 | |
| 220 | ISwitch forceBULBS[2]; |
| 221 | ISwitchVectorProperty forceBULBSP; |
| 222 | enum |
| 223 | { |
| 224 | FORCE_BULB_ON, |
| 225 | FORCE_BULB_OFF |
| 226 | }; |
| 227 | |
| 228 | // Upload file, used for testing purposes under simulation under native mode |
| 229 | ITextVectorProperty UploadFileTP; |
| 230 | IText UploadFileT[1] {}; |
| 231 | |
| 232 | IBLOBVectorProperty * imageBP = nullptr; |
| 233 | IBLOB * imageB = nullptr; |
| 234 | |
| 235 | Camera * camera = nullptr; |
| 236 | |
| 237 | // Threading |
| 238 | std::thread liveViewThread; |
| 239 | |
| 240 | std::map <uint8_t, uint8_t> m_CaptureFormatMap; |
| 241 | |
| 242 | static constexpr double MINUMUM_CAMERA_TEMPERATURE = -60.0; |
| 243 | |
| 244 | // Ratio from far 3 to far 2 |
| 245 | static constexpr double FOCUS_HIGH_MED_RATIO = 7.33; |
| 246 | // Ratio from far 2 to far 1 |
| 247 | static constexpr double FOCUS_MED_LOW_RATIO = 6.36; |
| 248 | |
| 249 | friend void ::ISSnoopDevice(XMLEle * root); |
| 250 | friend void ::ISGetProperties(const char * dev); |
| 251 | friend void ::ISNewSwitch(const char * dev, const char * name, ISState * states, char * names[], int num); |
| 252 | friend void ::ISNewText(const char * dev, const char * name, char * texts[], char * names[], int num); |
| 253 | friend void ::ISNewNumber(const char * dev, const char * name, double values[], char * names[], int num); |
| 254 | friend void ::ISNewBLOB(const char * dev, const char * name, int sizes[], int blobsizes[], char * blobs[], |
| 255 | char * formats[], char * names[], int n); |
| 256 | }; |