Instrument Neutral Distributed Interface INDI  1.9.2
simpleccd.cpp

A simple CCD device that can capture images and control temperature. It returns a FITS image to the client. To build drivers for complex CCDs, please refer to the INDI Generic CCD driver template in INDI SVN (under 3rdparty).

/*
INDI Developers Manual
Tutorial #3
"Simple CCD Driver"
We develop a simple CCD driver.
Refer to README, which contains instruction on how to build this driver, and use it
with an INDI-compatible client.
*/
#include "simpleccd.h"
#include <memory>
/* Macro shortcut to CCD temperature value */
#define currentCCDTemperature TemperatureN[0].value
std::unique_ptr<SimpleCCD> simpleCCD(new SimpleCCD());
/**************************************************************************************
** Client is asking us to establish connection to the device
***************************************************************************************/
{
IDMessage(getDeviceName(), "Simple CCD connected successfully!");
// Let's set a timer that checks teleCCDs status every POLLMS milliseconds.
return true;
}
/**************************************************************************************
** Client is asking us to terminate connection to the device
***************************************************************************************/
{
IDMessage(getDeviceName(), "Simple CCD disconnected successfully!");
return true;
}
/**************************************************************************************
** INDI is asking us for our default device name
***************************************************************************************/
{
return "Simple CCD";
}
/**************************************************************************************
** INDI is asking us to init our properties.
***************************************************************************************/
{
// Must init parent properties first!
// We set the CCD capabilities
// Add Debug, Simulator, and Configuration controls
return true;
}
/********************************************************************************************
** INDI is asking us to update the properties because there is a change in CONNECTION status
** This fucntion is called whenever the device is connected or disconnected.
*********************************************************************************************/
{
// Call parent update properties first
if (isConnected())
{
// Let's get parameters now from CCD
setupParams();
// Start the timer
}
return true;
}
/**************************************************************************************
** Setting up CCD parameters
***************************************************************************************/
void SimpleCCD::setupParams()
{
// Our CCD is an 8 bit CCD, 1280x1024 resolution, with 5.4um square pixels.
SetCCDParams(1280, 1024, 8, 5.4, 5.4);
// Let's calculate how much memory we need for the primary CCD buffer
int nbuf;
nbuf += 512; // leave a little extra at the end
}
/**************************************************************************************
** Client is asking us to start an exposure
***************************************************************************************/
bool SimpleCCD::StartExposure(float duration)
{
ExposureRequest = duration;
// Since we have only have one CCD with one chip, we set the exposure duration of the primary CCD
ExposureTimer.start();
InExposure = true;
// We're done
return true;
}
/**************************************************************************************
** Client is asking us to abort an exposure
***************************************************************************************/
{
InExposure = false;
return true;
}
/**************************************************************************************
** Client is asking us to set a new temperature
***************************************************************************************/
int SimpleCCD::SetTemperature(double temperature)
{
TemperatureRequest = temperature;
// 0 means it will take a while to change the temperature
return 0;
}
/**************************************************************************************
** How much longer until exposure is done?
***************************************************************************************/
float SimpleCCD::CalcTimeLeft()
{
return ExposureRequest - ExposureTimer.elapsed() / 1000.0;
}
/**************************************************************************************
** Main device loop. We check for exposure and temperature progress here
***************************************************************************************/
{
long timeleft;
if (!isConnected())
return; // No need to reset timer if we are not connected anymore
if (InExposure)
{
timeleft = CalcTimeLeft();
// Less than a 0.1 second away from exposure completion
// This is an over simplified timing method, check CCDSimulator and simpleCCD for better timing checks
if (timeleft < 0.1)
{
/* We're done exposing */
IDMessage(getDeviceName(), "Exposure done, downloading image...");
// Set exposure left to zero
// We're no longer exposing...
InExposure = false;
/* grab and save image */
grabImage();
}
else
// Just update time left in client
}
// TemperatureNP is defined in INDI::CCD
switch (TemperatureNP.s)
{
case IPS_IDLE:
case IPS_OK:
break;
case IPS_BUSY:
/* If target temperature is higher, then increase current CCD temperature */
if (currentCCDTemperature < TemperatureRequest)
/* If target temperature is lower, then decrese current CCD temperature */
else if (currentCCDTemperature > TemperatureRequest)
/* If they're equal, stop updating */
else
{
IDSetNumber(&TemperatureNP, "Target temperature reached.");
break;
}
break;
case IPS_ALERT:
break;
}
}
/**************************************************************************************
** Create a random image and return it to client
***************************************************************************************/
void SimpleCCD::grabImage()
{
// Let's get a pointer to the frame buffer
uint8_t *image = PrimaryCCD.getFrameBuffer();
// Get width and height
int height = PrimaryCCD.getSubH() / PrimaryCCD.getBinY();
// Fill buffer with random pattern
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
image[i * width + j] = rand() % 255;
IDMessage(getDeviceName(), "Download complete.");
// Let INDI::CCD know we're done filling the image buffer
}
SimpleCCD::SetTemperature
int SetTemperature(double temperature) override
Set CCD temperature.
Definition: simpleccd.cpp:146
INDI::CCDChip::setFrameBufferSize
void setFrameBufferSize(uint32_t nbuf, bool allocMem=true)
setFrameBufferSize Set desired frame buffer size. The function will allocate memory accordingly....
Definition: indiccdchip.cpp:159
INDI::DefaultDevice::addAuxControls
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
Definition: defaultdevice.cpp:665
INDI::CCDChip::getBinY
int getBinY() const
getBinY Get vertical binning of the CCD chip.
Definition: indiccdchip.h:150
SimpleCCD::TimerHit
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
Definition: simpleccd.cpp:165
IPS_OK
@ IPS_OK
Definition: indiapi.h:161
_INumberVectorProperty::s
IPState s
Definition: indiapi.h:332
SimpleCCD::Connect
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition: simpleccd.cpp:35
simpleccd.h
Construct a basic INDI CCD device that simulates exposure & temperature settings. It also generates a...
INDI::ElapsedTimer::elapsed
int64_t elapsed() const
Returns the number of milliseconds since this ElapsedTimer was last started.
Definition: indielapsedtimer.cpp:51
INDI::CCD::ExposureComplete
virtual bool ExposureComplete(CCDChip *targetChip)
Uploads target Chip exposed buffer as FITS to the client. Dervied classes should class this function ...
Definition: indiccd.cpp:2034
IPS_ALERT
@ IPS_ALERT
Definition: indiapi.h:163
SimpleCCD::initProperties
bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: simpleccd.cpp:64
SimpleCCD::getDefaultName
const char * getDefaultName() override
Definition: simpleccd.cpp:56
INDI::CCDChip::getSubH
int getSubH() const
getSubH Get the height of the frame
Definition: indiccdchip.h:132
INDI::DefaultDevice::setDefaultPollingPeriod
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
Definition: defaultdevice.cpp:1157
INDI::CCD::SetCCDParams
virtual void SetCCDParams(int x, int y, int bpp, float xf, float yf)
Setup CCD paramters for primary CCD. Child classes call this function to update CCD parameters.
Definition: indiccd.cpp:2968
INDI::BaseDevice::getDeviceName
const char * getDeviceName() const
Definition: basedevice.cpp:799
INDI::CCDChip::setExposureDuration
void setExposureDuration(double duration)
setExposureDuration Set desired CCD frame exposure duration for next exposure. You must call this fun...
Definition: indiccdchip.cpp:187
INDI::CCDChip::getFrameBuffer
uint8_t * getFrameBuffer()
getFrameBuffer Get raw frame buffer of the CCD chip.
Definition: indiccdchip.h:219
INDI::CCD::PrimaryCCD
CCDChip PrimaryCCD
Definition: indiccd.h:583
INDI::CCDChip::getYRes
int getYRes() const
Get the vertical resolution in pixels of the CCD Chip.
Definition: indiccdchip.h:96
INDI::CCDChip::setExposureLeft
void setExposureLeft(double duration)
setExposureLeft Update exposure time left. Inform the client of the new exposure time left value.
Definition: indiccdchip.cpp:179
INDI::DefaultDevice::getCurrentPollingPeriod
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
Definition: defaultdevice.cpp:1139
INDI::DefaultDevice::SetTimer
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
Definition: defaultdevice.cpp:865
SimpleCCD::Disconnect
bool Disconnect() override
Disconnect from device.
Definition: simpleccd.cpp:47
INDI::CCD::initProperties
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: indiccd.cpp:133
SimpleCCD::updateProperties
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: simpleccd.cpp:85
IPS_BUSY
@ IPS_BUSY
Definition: indiapi.h:162
IPS_IDLE
@ IPS_IDLE
Definition: indiapi.h:160
INDI::CCD::TemperatureNP
INumberVectorProperty TemperatureNP
TemperatureNP Camera Temperature in Celcius.
Definition: indiccd.h:623
IDMessage
void IDMessage(const char *dev, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Function Drivers call to send log messages to Clients.
INDI::CCD::updateProperties
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: indiccd.cpp:469
INDI::CCDChip::getBPP
int getBPP() const
getBPP Get CCD Chip depth (bits per pixel).
Definition: indiccdchip.h:177
INDI::CCDChip::getXRes
int getXRes() const
getXRes Get the horizontal resolution in pixels of the CCD Chip.
Definition: indiccdchip.h:87
INDI::CCD::CCD_HAS_COOLER
@ CCD_HAS_COOLER
Definition: indiccd.h:129
INDI::BaseDevice::isConnected
bool isConnected() const
Definition: basedevice.cpp:518
INDI::CCD::CCD_CAN_BIN
@ CCD_CAN_BIN
Definition: indiccd.h:123
simpleCCD
std::unique_ptr< SimpleCCD > simpleCCD(new SimpleCCD())
INDI::CCD::CCD_CAN_SUBFRAME
@ CCD_CAN_SUBFRAME
Definition: indiccd.h:124
currentCCDTemperature
#define currentCCDTemperature
Definition: simpleccd.cpp:28
INDI::CCD::SetCCDCapability
void SetCCDCapability(uint32_t cap)
SetCCDCapability Set the CCD capabilities. Al fields must be initilized.
Definition: indiccd.cpp:119
INDI::CCD::CCD_HAS_SHUTTER
@ CCD_HAS_SHUTTER
Definition: indiccd.h:128
SimpleCCD
Definition: simpleccd.h:28
SimpleCCD::StartExposure
bool StartExposure(float duration) override
Start exposing primary CCD chip.
Definition: simpleccd.cpp:120
INDI::CCD::CCD_CAN_ABORT
@ CCD_CAN_ABORT
Definition: indiccd.h:125
INDI::CCDChip::getBinX
int getBinX() const
getBinX Get horizontal binning of the CCD chip.
Definition: indiccdchip.h:141
SimpleCCD::AbortExposure
bool AbortExposure() override
Abort ongoing exposure.
Definition: simpleccd.cpp:137
IDSetNumber
void void void IDSetNumber(const INumberVectorProperty *n, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Tell client to update an existing number vector property.
INDI::CCDChip::getSubW
int getSubW() const
getSubW Get the width of the frame
Definition: indiccdchip.h:123
INDI::ElapsedTimer::start
void start()
Starts this timer. Once started, a timer value can be checked with elapsed().
Definition: indielapsedtimer.cpp:36