-
Notifications
You must be signed in to change notification settings - Fork 0
/
indi_lumix.cpp
431 lines (350 loc) · 12.5 KB
/
indi_lumix.cpp
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
#include "config.h"
#include "indi_lumix.h"
#include "indidevapi.h"
// declare an auto pointer to LumixS5IIX
static std::unique_ptr<LumixCameraDriver> lumix_s5iix_driver(new LumixCameraDriver());
LumixCameraDriver::LumixCameraDriver()
{
setVersion(INDI_LUMIX_VERSION_MAJOR, INDI_LUMIX_VERSION_MINOR);
setDeviceName("Lumix S5IIX");
}
LumixCameraDriver::~LumixCameraDriver()
{
}
const char * LumixCameraDriver::getDefaultName()
{
return "Lumix S5IIX";
}
bool LumixCameraDriver::initProperties()
{
INDI::CCD::initProperties();
// Add the camera IP address property (and load the default value from the config file)
char defaultCameraIP[256] = {""};
IUGetConfigText(getDeviceName(), "CAMERA_IP", "CAMERA_IP", defaultCameraIP, sizeof(defaultCameraIP));
CameraIPAddressTP[0].fill("CAMERA_IP", "Camera IP", defaultCameraIP);
CameraIPAddressTP.fill(getDeviceName(), "CAMERA_IP", "Camera IP", MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
defineProperty(CameraIPAddressTP);
CameraIPAddressTP.onUpdate([this]
{
CameraIPAddressTP.setState(IPS_OK);
CameraIPAddressTP.apply();
});
// set which capabilities the camera has
uint32_t cap = CCD_HAS_SHUTTER;
SetCCDCapability(cap);
// Add configuration for debug
addDebugControl();
return true;
}
bool LumixCameraDriver::updateProperties()
{
INDI::CCD::updateProperties();
if (isConnected()) {
setupParams();
// if connected, disable camera IP address write
CameraIPAddressTP.setPermission(IP_RO);
SetTimer(getCurrentPollingPeriod());
} else {
// if not connected, enable camera IP address write
CameraIPAddressTP.setPermission(IP_RW);
}
return true;
}
bool LumixCameraDriver::Connect()
{
// Connect to the camera
try {
camera = std::make_unique<Lumix::Camera>(CameraIPAddressTP[0].getText(), "INDI Lumix Driver");
} catch (const std::exception& e) {
LOG_ERROR("Error connecting to camera");
return false;
}
LOG_INFO("Connected to camera");
return true;
}
bool LumixCameraDriver::Disconnect()
{
// Disconnect from the camera
camera.reset();
LOG_INFO("Disconnected from camera");
return true;
}
bool LumixCameraDriver::saveConfigItems(FILE *fp) {
INDI::CCD::saveConfigItems(fp);
CameraIPAddressTP.save(fp);
return true;
}
bool LumixCameraDriver::setupParams()
{
float x_pixel_size, y_pixel_size;
int bit_depth = 8; // valid values are 8, 16, 32
int x_1, y_1, x_2, y_2;
int channels = 3;
// TODO: Actually get the pixel size from the camera
x_pixel_size = 5.95;
y_pixel_size = 5.95;
// TODO: Actually get the image size from the camera
x_1 = y_1 = 0;
x_2 = 6008;
y_2 = 4008;
// Set the pixel size
SetCCDParams(x_2 - x_1, y_2 - y_1, bit_depth, x_pixel_size, y_pixel_size);
// Set the channels
PrimaryCCD.setNAxis(channels);
// TODO: Now we usually do the following in the hardware
// Set Frame to LIGHT or NORMAL
// Set Binning to 1x1
/* Default frame type is NORMAL */
// Calculate the required buffer
int nbuf;
nbuf = PrimaryCCD.getXRes() * PrimaryCCD.getYRes() * ((PrimaryCCD.getBPP() * PrimaryCCD.getNAxis()) / 8); // this is the pixel count
nbuf += 512; // add some extra buffer
PrimaryCCD.setFrameBufferSize(nbuf);
return true;
}
bool LumixCameraDriver::StartExposure(float duration)
{
// Set the exposure request
PrimaryCCD.setExposureDuration(duration);
ExposureRequest = duration;
// start the takePhoto process in a separate thread
std::thread([this, duration] {
isExposing = true;
camera->TakePhoto(duration);
isExposing = false;
}).detach();
m_ElapsedTimer.start();
InExposure = true;
return true;
}
bool LumixCameraDriver::AbortExposure()
{
// TODO: actually abort the exposure
InExposure = false;
return true;
}
bool LumixCameraDriver::UpdateCCDFrameType(INDI::CCDChip::CCD_FRAME fType) {
INDI::CCDChip::CCD_FRAME imageFrameType = PrimaryCCD.getFrameType();
if (fType == imageFrameType) {
return true;
}
// TODO: Actually implement this
switch (imageFrameType)
{
case INDI::CCDChip::BIAS_FRAME:
case INDI::CCDChip::DARK_FRAME:
/**********************************************************
*
*
*
* IMPORRANT: Put here your CCD Frame type here
* BIAS and DARK are taken with shutter closed, so _usually_
* most CCD this is a call to let the CCD know next exposure shutter
* must be closed. Customize as appropiate for the hardware
* If there is an error, report it back to client
* e.g.
* LOG_INFO( "Error, unable to set frame type to ...");
* return false;
*
*
**********************************************************/
break;
case INDI::CCDChip::LIGHT_FRAME:
case INDI::CCDChip::FLAT_FRAME:
/**********************************************************
*
*
*
* IMPORRANT: Put here your CCD Frame type here
* LIGHT and FLAT are taken with shutter open, so _usually_
* most CCD this is a call to let the CCD know next exposure shutter
* must be open. Customize as appropiate for the hardware
* If there is an error, report it back to client
* e.g.
* LOG_INFO( "Error, unable to set frame type to ...");
* return false;
*
*
**********************************************************/
break;
}
PrimaryCCD.setFrameType(fType);
return true;
}
bool LumixCameraDriver::UpdateCCDFrame(int x, int y, int w, int h) {
// add the x and y offsets
long x_1 = x;
long y_1 = y;
long bin_width = x_1 + (w / PrimaryCCD.getBinX());
long bin_height = y_1 + (h / PrimaryCCD.getBinY());
if (bin_width > PrimaryCCD.getXRes() / PrimaryCCD.getBinX()) {
LOG_INFO("Error: X offset + width is greater than the CCD width");
return false;
} else if (bin_height > PrimaryCCD.getYRes() / PrimaryCCD.getBinY()) {
LOG_INFO("Error: Y offset + height is greater than the CCD height");
return false;
}
/**********************************************************
*
*
*
* TODO: Put here your CCD Frame dimension call
* The values calculated above are BINNED width and height
* which is what most CCD APIs require, but in case your
* CCD API implementation is different, don't forget to change
* the above calculations.
* If there is an error, report it back to client
* e.g.
* LOG_INFO( "Error, unable to set frame to ...");
* return false;
*
*
**********************************************************/
// set UNBINNED coords
PrimaryCCD.setFrame(x_1, y_1, bin_width, bin_height);
int nbuf;
nbuf = (bin_width * bin_height * PrimaryCCD.getNAxis() * PrimaryCCD.getBPP() / 8); // this is the pixel count
nbuf += 512; // add some extra buffer
PrimaryCCD.setFrameBufferSize(nbuf);
return true;
}
bool LumixCameraDriver::UpdateCCDBin(int binx, int biny)
{
/**********************************************************
*
*
*
* TODO: Put here your CCD Binning call
* If there is an error, report it back to client
* e.g.
* LOG_INFO( "Error, unable to set binning to ...");
* return false;
*
*
**********************************************************/
PrimaryCCD.setBin(binx, biny);
return UpdateCCDFrame(PrimaryCCD.getSubX(), PrimaryCCD.getSubY(), PrimaryCCD.getSubW(), PrimaryCCD.getSubH());
}
int LumixCameraDriver::downloadImage()
{
uint8_t *image = PrimaryCCD.getFrameBuffer();
int width = PrimaryCCD.getSubW() / PrimaryCCD.getBinX();
int height = PrimaryCCD.getSubH() / PrimaryCCD.getBinY();
int bpp = PrimaryCCD.getBPP();
int channels = PrimaryCCD.getNAxis();
/**********************************************************
*
*
* TODO: Put here your CCD Get Image routine here
* use the image, width, and height variables above
* If there is an error, report it back to client
*
*
**********************************************************/
// For now, just fill the image with random data
/*for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
image[i * width + j] = rand() % 255;*/
LOG_INFO("Starting Copy...");
Lumix::ImageData imageData;
if (!camera->DownloadLatestPhoto(imageData)) {
LOG_ERROR("Error downloading image");
return -1;
}
if (!camera->GetRawPixelData(imageData)) {
LOG_ERROR("Error getting raw pixel data");
return -1;
}
if (imageData.pixelBuffer.size() != (width * height * channels * (PrimaryCCD.getBPP() / 8))) {
LOG_ERROR("Error: Image size does not match expected size");
return -1;
}
// Copy rgbrgb... to rrr...ggg...bbb...
std::vector<uint8_t> r(width * height * bpp / 8);
std::vector<uint8_t> g(width * height * bpp / 8);
std::vector<uint8_t> b(width * height * bpp / 8);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
r[i * width + j] = imageData.pixelBuffer[(i * width + j) * channels + 0];
g[i * width + j] = imageData.pixelBuffer[(i * width + j) * channels + 1];
b[i * width + j] = imageData.pixelBuffer[(i * width + j) * channels + 2];
}
}
// Copy the data to the frame buffer
std::copy(r.begin(), r.end(), image);
std::copy(g.begin(), g.end(), image + r.size());
std::copy(b.begin(), b.end(), image + r.size() + g.size());
LOG_INFO("Download complete.");
ExposureComplete(&PrimaryCCD);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
/// TimerHit is the main loop of the driver where it gets called every 1 second
/// by default. Here you perform checks on any ongoing operations and perhaps query some
/// status updates like temperature.
///////////////////////////////////////////////////////////////////////////////////////
void LumixCameraDriver::TimerHit()
{
if (isConnected() == false)
return;
// Are we in exposure? Let's check if we're done!
if (InExposure)
{
// Seconds elapsed
double timeLeft = ExposureRequest - m_ElapsedTimer.elapsed() / 1000.0;
if (timeLeft <= 0 && !isExposing)
{
/* We're done exposing */
LOG_INFO("Exposure done, downloading image...");
PrimaryCCD.setExposureLeft(0);
InExposure = false;
// Download Image
downloadImage();
}
else
// set the remaining exposure time (make sure it's not negative)
PrimaryCCD.setExposureLeft(std::max(0.0, timeLeft));
}
// TODO: use this syntax to handle ISO, shutter speed, and aperture
switch (TemperatureNP.s)
{
case IPS_IDLE:
case IPS_OK:
/**********************************************************
*
*
*
* IMPORRANT: Put here your CCD Get temperature call here
* If there is an error, report it back to client
* e.g.
* LOG_INFO( "Error, unable to get temp due to ...");
* return false;
*
*
**********************************************************/
break;
case IPS_BUSY:
/**********************************************************
*
*
*
* IMPORRANT: Put here your CCD Get temperature call here
* If there is an error, report it back to client
* e.g.
* LOG_INFO( "Error, unable to get temp due to ...");
* return false;
*
*
**********************************************************/
//TemperatureN[0].value = TemperatureRequest;
// If we're within threshold, let's make it BUSY ---> OK
//if (fabs(TemperatureRequest - TemperatureN[0].value) <= TEMP_THRESHOLD)
// TemperatureNP.s = IPS_OK;
//IDSetNumber(&TemperatureNP, nullptr);
break;
case IPS_ALERT:
break;
}
SetTimer(getCurrentPollingPeriod());
return;
}