38#define FOCUS_SETTINGS_TAB "Settings"
39#define TEMPERATURE_THRESHOLD 0.1
59 int tty_rc = 0, nbytes_written = 0, nbytes_read = 0;
64 if (tcp && !wifi.login(
PortFD))
72 tcflush(
PortFD, TCIOFLUSH);
78 LOGF_ERROR(
"Serial write error: %s", errorMessage);
88 tcflush(
PortFD, TCIOFLUSH);
98 LOGF_ERROR(
"Serial read error: %s", errorMessage);
103 tcflush(
PortFD, TCIOFLUSH);
104 response[nbytes_read - 1] =
'\0';
109 return (strstr(response,
"FC3") !=
nullptr);
114 return "Pegasus FocusCube3";
121 TemperatureNP[0].fill(
"TEMPERATURE",
"Level",
"%.0f", -40, 40, 1, 0);
124 FirmwareVersionTP[0].fill(
"Version",
"Version",
"");
127 SpeedNP[0].fill(
"Speed",
"Value",
"%6.2f", 100, 1000., 100., 400.);
158 wifi.initProperties(
"WiFi",
159 [
this](
const char *name) {
loadConfig(
true, name); },
168 wifi.ISGetProperties();
173 if (wifi.ISNewSwitch(dev, name, states, names,
n))
181 if (wifi.ISNewText(dev, name, texts, names,
n))
190 wifi.saveConfigItems(fp);
202 std::string firmware = getFirmwareVersion();
203 FirmwareVersionTP[0].setText(firmware);
204 FirmwareVersionTP.setState(
IPS_OK);
205 FirmwareVersionTP.apply();
210 SpeedNP[0].setValue(speed);
213 wifi.updateProperties(
true,
PortFD);
217 hasTemperatureSensor =
false;
219 wifi.updateProperties(
false, -1);
230 if (wifi.ISNewNumber(dev, name, values, names,
n))
235 if(SpeedNP.isNameMatch(name))
237 SpeedNP.update(values, names,
n);
241 if(!setSpeed(values[0]))
246 SpeedNP.setState(result);
257 targetPosition = targetTicks;
259 bool rc = move(targetPosition);
264 moveStarted = std::chrono::steady_clock::now();
272 double newPosition = 0;
280 rc = move(newPosition);
285 targetPosition = newPosition;
286 moveStarted = std::chrono::steady_clock::now();
293bool PegasusFocusCube3::move(uint32_t newPosition)
296 snprintf(expectedResponse,
PEGASUS_LEN,
"%u", newPosition);
301 if(!strstr(res, expectedResponse))
309 LOGF_ERROR(
"Error on move [Position=%d]", newPosition);
318 int nbytes_read = 0, nbytes_written = 0, tty_rc = 0;
322 for (
int i = 0; i < 2; i++)
332 tcflush(
PortFD, TCIOFLUSH);
340 tcflush(
PortFD, TCIOFLUSH);
341 res[nbytes_read - 1] =
'\0';
345 const size_t skip = strspn(res,
"\r\n");
347 memmove(res, res + skip, strlen(res + skip) + 1);
365bool PegasusFocusCube3::parseReply(
const char *res,
const char *prefix,
int &value)
368 const size_t len = strlen(prefix);
369 const char *start = strncmp(res, prefix, len) == 0 ? res + len : res;
370 return sscanf(start,
"%d", &value) == 1;
373bool PegasusFocusCube3::setSpeed(uint16_t speed)
386 SpeedNP[0].setValue(actual);
389 LOGF_WARN(
"Device kept speed %d, %u is outside its range.", actual, speed);
395bool PegasusFocusCube3::getSpeed(
int &speed)
398 if (!
sendCommand(
"SP", res) || !parseReply(res,
"SP:", speed))
400 LOGF_WARN(
"Could not read the speed [Res=%s].", res);
406std::string PegasusFocusCube3::getFirmwareVersion()
424 std::regex re(regex);
425 std::sregex_token_iterator
426 first{input.begin(), input.end(), re, -1},
428 return {first, last};
456 snprintf(expectedResponse,
PEGASUS_LEN,
"%u", ticks);
460 if(!strstr(res, expectedResponse))
495 snprintf(expectedResponse,
PEGASUS_LEN,
"%u", steps);
499 if(!strstr(res, expectedResponse))
522 bool rc = updateFocusParams();
529 const bool atTarget = currentPosition == targetPosition ||
530 std::chrono::steady_clock::now() - moveStarted > MOVE_START_TIMEOUT;
531 if (isMoving ==
false && atTarget)
537 LOG_INFO(
"Focuser reached requested position.");
545bool PegasusFocusCube3::updateFocusParams()
559 std::vector<std::string> result =
split(res,
":");
562 if(result.size() < 6 || result[0] !=
"FC3")
564 LOGF_WARN(
"Unexpected status data: %s", res);
570 currentPosition = atoi(result[1].c_str());
578 isMoving = (result[2] ==
"1");
582 const double temperature = atof(result[3].c_str());
584 if (sensor != hasTemperatureSensor)
586 hasTemperatureSensor = sensor;
595 TemperatureNP.setState(
IPS_OK);
596 TemperatureNP.apply();
601 int reverseStatus = atoi(result[4].c_str());
602 if (reverseStatus >= 0 && reverseStatus <= 1)
614 int backlash = atoi(result[5].c_str());
618 LOG_WARN(
"Backlash value is zero, disabling backlash switch...");
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool loadConfig(bool silent=false, const char *property=nullptr)
Load the last saved configuration file.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
Connection::Interface * getActiveConnection()
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
void addDebugControl()
Add Debug control to the driver.
INDI::PropertyNumber FocusRelPosNP
INDI::PropertySwitch FocusReverseSP
void SetCapability(uint32_t cap)
FI::SetCapability sets the focuser capabilities. All capabilities must be initialized.
INDI::PropertyNumber FocusMaxPosNP
INDI::PropertySwitch FocusBacklashSP
INDI::PropertyNumber FocusBacklashNP
INDI::PropertyNumber FocusAbsPosNP
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Processes incoming new switch states from the client for this device. Handles updates to FocuserInter...
virtual bool saveConfigItems(FILE *fp) override
virtual void ISGetProperties(const char *dev) override
Handles client requests for property definitions for this device or all devices. Delegates to the bas...
virtual bool updateProperties() override
Updates the state and definition of properties based on the current connection status....
virtual bool initProperties() override
Initializes the default properties for the focuser device. Sets up standard INDI properties,...
Connection::TCP * tcpConnection
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Processes incoming new number values from the client for this device. Handles updates to FocuserInter...
Connection::Serial * serialConnection
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Processes incoming new text values from the client for this device. Delegates processing to the contr...
Provides generic container for INDI properties.
static constexpr int TCP_PORT
Port of the TCP command socket.
static void drain(int portFD)
Discard pending input. tcflush() only works on a serial port, so over TCP a line end left behind by a...
virtual bool initProperties() override
Initializes the default properties for the focuser device. Sets up standard INDI properties,...
virtual bool saveConfigItems(FILE *fp) override
virtual bool SetFocuserBacklash(int32_t steps) override
SetFocuserBacklash Set the focuser backlash compensation value.
static constexpr const uint8_t PEGASUS_LEN
virtual void ISGetProperties(const char *dev) override
Handles client requests for property definitions for this device or all devices. Delegates to the bas...
virtual bool ReverseFocuser(bool enabled) override
ReverseFocuser Reverse focuser motion direction.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Processes incoming new number values from the client for this device. Handles updates to FocuserInter...
virtual bool SyncFocuser(uint32_t ticks) override
SyncFocuser Set current position to ticks without moving the focuser.
static constexpr const uint8_t PEGASUS_TIMEOUT
bool sendCommand(const char *cmd, char *res)
virtual bool updateProperties() override
Updates the state and definition of properties based on the current connection status....
virtual bool AbortFocuser() override
AbortFocuser all focus motion.
virtual IPState MoveAbsFocuser(uint32_t targetTicks) override
MoveFocuser the focuser to an absolute position.
const char * getDefaultName() override
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Processes incoming new text values from the client for this device. Delegates processing to the contr...
virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override
MoveFocuser the focuser to an relative position.
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
std::vector< std::string > split(const std::string &input, const std::string ®ex)
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Processes incoming new switch states from the client for this device. Handles updates to FocuserInter...
virtual bool Handshake() override
Performs a device-specific handshake after a connection is established. This method should be overrid...
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
#define FOCUS_SETTINGS_TAB
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Implementations for common driver routines.
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_WARN(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)