source: BAORadio/libindi/libindi/drivers/focuser/tcfs.cpp @ 642

Last change on this file since 642 was 642, checked in by frichard, 12 years ago

-Alignement des antennes
-Version 0.0.9 de libindi

File size: 24.9 KB
Line 
1/*
2    INDI Driver for Optec TCF-S Focuser
3
4    Copyright (C) 2010 Jasem Mutlaq (mutlaqja@ikarustech.com)
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
20*/
21
22#include <stdlib.h>
23#include <string.h>
24#include <stdarg.h>
25#include <math.h>
26#include <unistd.h>
27#include <time.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <termios.h>
33#include <memory>
34#include <indicom.h>
35
36#include "tcfs.h"
37
38#define mydev                   "Optec TCF-S"
39#define nFocusSteps             FocusStepNP->np[0].value
40#define nFocusCurrentPosition   FocusPositionNP->np[0].value
41#define nFocusTargetPosition    FocusPositionRequestNP->np[0].value
42#define nFocusTemperature       FocusTemperatureNP->np[0].value
43#define isFocusSleep            (FocusPowerSP->sp[0].s == ISS_ON)
44
45const int POLLMS = 1000;
46
47
48// We declare an auto pointer to TCFS.
49auto_ptr<TCFS> tcfs(0);
50
51void ISPoll(void *p);
52
53
54void ISInit()
55{
56   static int isInit =0;
57
58   if (isInit == 1)
59       return;
60
61    isInit = 1;
62    if(tcfs.get() == 0) tcfs.reset(new TCFS());
63    IEAddTimer(POLLMS, ISPoll, NULL);
64
65}
66
67void ISPoll(void *p)
68{
69    tcfs->ISPoll();
70    IEAddTimer(POLLMS, ISPoll, NULL);
71}
72
73void ISGetProperties(const char *dev)
74{ 
75        if(dev && strcmp(mydev, dev)) return;
76        ISInit();
77        tcfs->ISGetProperties(dev);
78}
79
80void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int num)
81{
82        if(dev && strcmp (mydev, dev)) return;
83        ISInit();
84        tcfs->ISNewSwitch(dev, name, states, names, num);
85}
86
87void ISNewText( const char *dev, const char *name, char *texts[], char *names[], int num)
88{
89        if(dev && strcmp (mydev, dev)) return;
90        ISInit();
91        tcfs->ISNewText(dev, name, texts, names, num);
92}
93
94void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int num)
95{
96        if(dev && strcmp (mydev, dev)) return;
97        ISInit();
98        tcfs->ISNewNumber(dev, name, values, names, num);
99}
100
101void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) 
102{
103  INDI_UNUSED(dev);
104  INDI_UNUSED(name);
105  INDI_UNUSED(sizes);
106  INDI_UNUSED(blobsizes);
107  INDI_UNUSED(blobs);
108  INDI_UNUSED(formats);
109  INDI_UNUSED(names);
110  INDI_UNUSED(n);
111}
112void ISSnoopDevice (XMLEle *root) 
113{
114    INDI_UNUSED(root);
115}
116
117/****************************************************************
118**
119**
120*****************************************************************/
121TCFS::TCFS()
122{
123    const char *skelFileName = "/usr/share/indi/indi_tcfs_sk.xml";
124    struct stat st;
125
126    char *skel = getenv("INDISKEL");
127    if (skel)
128      buildSkeleton(skel);
129    else if (stat(skelFileName,&st) == 0)
130       buildSkeleton(skelFileName);
131    else
132       IDLog("No skeleton file was specified. Set environment variable INDISKEL to the skeleton path and try again.\n");
133
134     // Optional: Add aux controls for configuration, debug & simulation
135     addAuxControls();
136
137     simulated_position    = 3000;
138     simulated_temperature = 25.4;
139
140}
141
142/****************************************************************
143**
144**
145*****************************************************************/
146TCFS::~TCFS()
147{
148}
149
150/****************************************************************
151**
152**
153*****************************************************************/
154void TCFS::ISGetProperties(const char *dev)
155{
156    static int propInit=0;
157
158    INDI::DefaultDriver::ISGetProperties(dev);
159
160    if (propInit == 0)
161    {
162        init_properties();
163
164        loadConfig();
165
166        isTCFS3 = false;
167
168        // Set upper limit for TCF-S3 focuser
169        if (!strcmp(me, "indi_tcfs3_focus"))
170        {
171            isTCFS3 = true;
172
173            FocusPositionRequestNP->np[0].max = 9999;
174            IUUpdateMinMax(FocusPositionRequestNP);
175            if (isDebug())
176               IDLog("TCF-S3 detected. Updating maximum position value to 9999.\n");
177        }
178
179        propInit = 1;     
180    }
181
182}
183
184/****************************************************************
185**
186**
187*****************************************************************/
188void TCFS::init_properties()
189{
190    ConnectSP = getSwitch("CONNECTION");
191    FocusStepNP = getNumber("FOCUS_STEP");
192    FocusPositionNP = getNumber("FOCUS_POSITION");
193    FocusPositionRequestNP = getNumber("FOCUS_POSITION_REQUEST");
194    FocusTemperatureNP = getNumber("FOCUS_TEMPERATURE");
195    FocusPowerSP = getSwitch("FOCUS_POWER");
196    FocusModeSP  = getSwitch("FOCUS_MODE");
197}
198
199/****************************************************************
200**
201**
202*****************************************************************/   
203bool TCFS::Connect()
204{
205    ITextVectorProperty *tProp = getText("DEVICE_PORT");
206
207   if (isConnected())
208                return true;
209
210    if (tProp == NULL)
211        return false;
212
213    if (isSimulation())
214    {
215        setConnected(true);
216
217        IDSetSwitch(ConnectSP, "TCF-S: Simulating connection to port %s.", tProp->tp[0].text);
218
219        fd=-1;
220        IUResetSwitch(FocusModeSP);
221        FocusModeSP->sp[0].s = ISS_ON;
222        FocusModeSP->s       = IPS_OK;
223        IDSetSwitch(FocusModeSP, NULL);
224
225        FocusPositionNP->s = IPS_OK;
226        IDSetNumber(FocusPositionNP, NULL);
227
228        FocusTemperatureNP->s = IPS_OK;
229        IDSetNumber(FocusTemperatureNP, NULL);
230
231        IUResetSwitch(FocusPowerSP);
232        IDSetSwitch(FocusPowerSP, NULL);
233        return true;
234    }
235
236    if (isDebug())
237        IDLog("Attempting to connect to TCF-S focuser....\n");
238
239    if (tty_connect(tProp->tp[0].text, 19200, 8, 0, 1, &fd) != TTY_OK)
240    {
241        IDMessage(deviceName(), "Error connecting to port %s. Make sure you have BOTH read and write permission to the port.", tProp->tp[0].text);
242        return false;
243    }
244
245    for (int i=0; i < TCFS_MAX_TRIES; i++)
246    {
247        dispatch_command(FMMODE);
248
249        if (read_tcfs() == true)
250        {
251            if (!strcmp(response, "!"))
252            {
253                IDMessage(deviceName(), "Successfully connected to TCF-S Focuser in Manual Mode.");
254
255                IUResetSwitch(FocusModeSP);
256                FocusModeSP->sp[0].s = ISS_ON;
257                FocusModeSP->s       = IPS_OK;
258                IDSetSwitch(FocusModeSP, NULL);
259
260                FocusPositionNP->s = IPS_OK;
261                IDSetNumber(FocusPositionNP, NULL);
262
263                FocusTemperatureNP->s = IPS_OK;
264                IDSetNumber(FocusTemperatureNP, NULL);
265
266                IUResetSwitch(FocusPowerSP);
267                IDSetSwitch(FocusPowerSP, NULL);
268
269                return true;
270             }
271        }
272
273        usleep(500000);
274    }
275
276    IDMessage(deviceName(), "Error connecting to TCF-S focuser...");
277    return false;
278
279}
280
281/****************************************************************
282**
283**
284*****************************************************************/   
285bool TCFS::Disconnect()
286{
287
288    FocusPositionNP->s = IPS_IDLE;
289    IDSetNumber(FocusPositionNP, NULL);
290
291    FocusTemperatureNP->s = IPS_IDLE;
292    IDSetNumber(FocusTemperatureNP, NULL);
293
294    dispatch_command(FFMODE);
295
296    tty_disconnect(fd);
297
298    setConnected(false, IPS_OK, "Disconnected from TCF-S.");
299
300    return true;
301}
302
303/****************************************************************
304**
305**
306*****************************************************************/
307bool TCFS::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
308{
309
310  INumberVectorProperty *nProp = getNumber(name);
311
312  if (nProp == NULL)
313      return false;
314
315  if (isConnected() == false)
316  {
317      resetProperties();
318      IDMessage(deviceName(), "TCF-S is offline. Connect before issiung any commands.");
319      return false;
320  }
321
322  if (!strcmp(nProp->name, "FOCUS_STEP"))
323  {
324      IUUpdateNumber(nProp, values, names, n);
325      nProp->s = IPS_OK;
326      IDSetNumber(nProp, NULL);
327      return true;
328  }
329
330  if (isFocusSleep)
331  {
332      nProp->s = IPS_IDLE;
333      IDSetNumber(nProp, "Focuser is still in sleep mode. Wake up in order to issue commands.");
334      return true;
335  }
336
337  if (!strcmp(nProp->name, "FOCUS_POSITION_REQUEST"))
338  {
339      int current_step = nFocusSteps;
340      IUUpdateNumber(nProp, values, names, n);
341
342      nFocusSteps = fabs(nFocusTargetPosition - nFocusCurrentPosition);
343
344      if ( (nFocusTargetPosition - nFocusCurrentPosition) > 0)
345          move_focuser(TCFS_OUTWARD);
346      else
347          move_focuser(TCFS_INWARD);
348
349      nFocusSteps = current_step;
350
351      FocusPositionNP->s = IPS_BUSY;
352      nProp->s = IPS_BUSY;
353      IDSetNumber(nProp, "Moving focuser to new position %g...", nFocusTargetPosition);
354      return true;
355  }
356
357  return DefaultDriver::ISNewNumber (dev, name, values, names, n);
358   
359}
360
361/****************************************************************
362**
363**
364*****************************************************************/
365bool TCFS::ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
366{
367    ITextVectorProperty * tProp = getText(name);
368
369    if (tProp == NULL)
370        return false;
371
372    // Device Port Text
373    if (!strcmp(tProp->name, "DEVICE_PORT"))
374    {
375        if (IUUpdateText(tProp, texts, names, n) < 0)
376                        return false;
377
378                tProp->s = IPS_OK;
379                IDSetText(tProp, "Port updated.");
380
381                return true;
382    }
383
384
385    return DefaultDriver::ISNewText(dev, name, texts, names, n);
386       
387}
388
389/****************************************************************
390**
391**
392*****************************************************************/
393bool TCFS::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
394{
395
396    ISwitch *current_active_switch = NULL, *target_active_switch = NULL;
397    // First process parent!
398    if (INDI::DefaultDriver::ISNewSwitch(deviceName(), name, states, names, n) == true)
399        return true;
400
401    ISwitchVectorProperty *sProp = getSwitch(name);
402
403    if (sProp == NULL)
404        return false;
405
406    /*if (!strcmp(sProp->name, "CONNECTION"))
407    {
408        if (!strcmp(names[0], "CONNECT"))
409            connect();
410        else
411            disconnect();
412        return true;
413    }*/
414
415
416    if (isConnected() == false)
417    {
418      resetProperties();
419      IDMessage(deviceName(), "TCF-S is offline. Connect before issiung any commands.");
420      return false;
421    }
422
423    // Which switch is CURRENTLY on?
424    current_active_switch = IUFindOnSwitch(sProp);
425
426    IUUpdateSwitch(sProp, states, names, n);
427
428    // Which switch the CLIENT wants to turn on?
429    target_active_switch = IUFindOnSwitch(sProp);
430
431    if (target_active_switch == NULL)
432    {
433        if (isDebug())
434        {
435            IDLog("Error: no ON switch found in %s property.\n", sProp->name);
436        }
437        return true;
438    }
439
440
441    if (!strcmp(sProp->name, "FOCUS_POWER"))
442    {
443        bool sleep = false;
444
445        // Sleep
446        if (!strcmp(target_active_switch->name, "FOCUS_SLEEP"))
447        {
448               dispatch_command(FSLEEP);
449               sleep = true;
450        }
451        // Wake Up
452        else
453            dispatch_command(FWAKUP);
454
455        if (read_tcfs() == false)
456        {
457              IUResetSwitch(sProp);
458              sProp->s = IPS_ALERT;
459              IDSetSwitch(sProp, "Error reading TCF-S reply.");
460              return true;
461        }
462
463            if (sleep)
464            {
465                if (isSimulation())
466                    strncpy(response, "ZZZ", TCFS_MAX_CMD);
467
468                if (!strcmp(response, "ZZZ"))
469                {
470                    sProp->s = IPS_OK;
471                    IDSetSwitch(sProp, "Focuser is set into sleep mode.");
472                    FocusPositionNP->s = IPS_IDLE;
473                    IDSetNumber(FocusPositionNP, NULL);
474                    FocusTemperatureNP->s = IPS_IDLE;
475                    IDSetNumber(FocusTemperatureNP, NULL);
476                    return true;
477                }
478                else
479                {
480                    sProp->s = IPS_ALERT;
481                    IDSetSwitch(sProp, "Focuser sleep mode operation failed. Response: %s.", response);
482                    return true;
483                }
484            }
485            else
486            {
487                if (isSimulation())
488                    strncpy(response, "WAKE", TCFS_MAX_CMD);
489
490                if (!strcmp(response, "WAKE"))
491                {
492                    sProp->s = IPS_OK;
493                    IDSetSwitch(sProp, "Focuser is awake.");
494                    FocusPositionNP->s = IPS_OK;
495                    IDSetNumber(FocusPositionNP, NULL);
496                    FocusTemperatureNP->s = IPS_OK;
497                    IDSetNumber(FocusTemperatureNP, NULL);
498                    return true;
499                }
500                else
501                {
502                    sProp->s = IPS_ALERT;
503                    IDSetSwitch(sProp, "Focuser wake up operation failed. Response: %s", response);
504                    return true;
505                }
506            }
507      }
508
509    if (isFocusSleep)
510    {
511        sProp->s = IPS_IDLE;
512        IUResetSwitch(sProp);
513
514        if (!strcmp(sProp->name, "FOCUS_MODE") && current_active_switch != NULL)
515            current_active_switch->s = ISS_ON;
516
517        IDSetSwitch(sProp, "Focuser is still in sleep mode. Wake up in order to issue commands.");
518        return true;
519    }
520
521    if (!strcmp(sProp->name, "FOCUS_MODE"))
522    {
523
524        sProp->s = IPS_OK;
525
526        if (!strcmp(target_active_switch->name, "Manual"))
527        {
528           dispatch_command(FMMODE);
529           read_tcfs();
530           if (isSimulation() == false && strcmp(response, "!"))
531           {
532               IUResetSwitch(sProp);
533               sProp->s = IPS_ALERT;
534               IDSetSwitch(sProp, "Error switching to manual mode. No reply from TCF-S. Try again.");
535               return true;
536           }
537       }
538        else if (!strcmp(target_active_switch->name, "Auto A"))
539        {
540            dispatch_command(FAMODE);
541            read_tcfs();
542            if (isSimulation() == false && strcmp(response, "A"))
543            {
544                IUResetSwitch(sProp);
545                sProp->s = IPS_ALERT;
546                IDSetSwitch(sProp, "Error switching to Auto Mode A. No reply from TCF-S. Try again.");
547                return true;
548            }
549        }
550        else
551        {
552            dispatch_command(FBMODE);
553            read_tcfs();
554            if (isSimulation() == false && strcmp(response, "B"))
555            {
556                IUResetSwitch(sProp);
557                sProp->s = IPS_ALERT;
558                IDSetSwitch(sProp, "Error switching to Auto Mode B. No reply from TCF-S. Try again.");
559                return true;
560            }
561        }
562
563        IDSetSwitch(sProp, NULL);
564        return true;
565    }
566
567    if (!strcmp(sProp->name, "FOCUS_MOTION"))
568    {
569        // Inward
570        if (!strcmp(target_active_switch->name, "FOCUS_INWARD"))
571          move_focuser(TCFS_INWARD);
572        // Outward
573        else
574            move_focuser(TCFS_OUTWARD);
575
576        return true;
577
578     }
579
580    if (!strcmp(sProp->name, "FOCUS_GOTO"))
581    {
582        int currentStep = nFocusSteps;
583
584        FocusPositionNP->s = IPS_BUSY;
585        sProp->s = IPS_OK;
586
587        // Min
588        if (!strcmp(target_active_switch->name, "FOCUS_MIN"))
589        {
590            nFocusSteps = nFocusCurrentPosition;
591            move_focuser(TCFS_INWARD);
592            nFocusSteps = currentStep;
593            IUResetSwitch(sProp);
594            IDSetSwitch(sProp, "Moving focouser to minimum position...");
595        }
596        // Center
597        else if (!strcmp(target_active_switch->name, "FOCUS_CENTER"))
598        {
599            dispatch_command(FCENTR);
600            read_tcfs();
601
602            if (isSimulation())
603                strncpy(response, "CENTER", TCFS_MAX_CMD);
604
605            if (!strcmp(response, "CENTER"))
606            {
607                IUResetSwitch(sProp);
608                sProp->s = IPS_OK;
609                FocusPositionNP->s = IPS_BUSY;
610                if (isTCFS3)
611                    nFocusTargetPosition = 5000;
612                else
613                    nFocusTargetPosition = 3500;
614
615                IDSetSwitch(sProp, "Moving focuser to center position %g...", nFocusTargetPosition);
616                return true;
617            }
618            else
619            {
620                IUResetSwitch(sProp);
621                sProp->s = IPS_ALERT;
622                IDSetSwitch(sProp, "Failed to move focuser to center position!");
623                return true;
624            }
625        }
626        // Max
627        else if (!strcmp(target_active_switch->name, "FOCUS_MAX"))
628        {
629            nFocusSteps = FocusPositionRequestNP->np[0].max - nFocusCurrentPosition;
630            move_focuser(TCFS_OUTWARD);
631            nFocusSteps = currentStep;
632            IUResetSwitch(sProp);
633            IDSetSwitch(sProp, "Moving focouser to maximum position %g...", FocusPositionRequestNP->np[0].max);
634        }
635        // Home
636        else if (!strcmp(target_active_switch->name, "FOCUS_HOME"))
637        {
638            dispatch_command(FHOME);
639            read_tcfs();
640
641            if (isSimulation())
642                strncpy(response, "DONE", TCFS_MAX_CMD);
643
644            if (!strcmp(response, "DONE"))
645            {
646                IUResetSwitch(sProp);
647                sProp->s = IPS_OK;
648                //FocusInfoNP->s = IPS_BUSY;
649                IDSetSwitch(sProp, "Moving focuser to new calculated position based on temperature...");
650                return true;
651            }
652            else
653            {
654                IUResetSwitch(sProp);
655                sProp->s = IPS_ALERT;
656                IDSetSwitch(sProp, "Failed to move focuser to home position!");
657                return true;
658            }
659        }
660
661        return true;
662    }
663
664
665
666    return DefaultDriver::ISNewSwitch(dev, name, states, names, n);
667}
668
669bool TCFS::move_focuser(TCFSMotion dir)
670{
671    ISwitchVectorProperty *sProp = getSwitch("FOCUS_MOTION");
672
673    if (sProp == NULL)
674        return false;
675
676    // Inward
677    if (dir == TCFS_INWARD)
678    {
679        dispatch_command(FIN);
680        nFocusTargetPosition = nFocusCurrentPosition - nFocusSteps;
681    }
682    // Outward
683    else
684    {
685        dispatch_command(FOUT);
686        nFocusTargetPosition = nFocusCurrentPosition + nFocusSteps;
687    }
688
689     if (read_tcfs() == false)
690     {
691          IUResetSwitch(sProp);
692          sProp->s = IPS_ALERT;
693          IDSetSwitch(sProp, "Error reading TCF-S reply.");
694          return false;
695     }
696
697        if (isSimulation())
698            strncpy(response, "*", TCFS_MAX_CMD);
699
700        if (!strcmp(response, "*"))
701        {
702            IUResetSwitch(sProp);
703            sProp->s = IPS_OK;
704            FocusPositionNP->s = IPS_BUSY;
705            IDSetSwitch(sProp, "Moving focuser %s %d steps to position %g.", (dir == TCFS_INWARD) ? "inward" : "outward", ((int) nFocusSteps), nFocusTargetPosition);
706            return true;
707        }
708        else
709        {
710            IUResetSwitch(sProp);
711            sProp->s = IPS_ALERT;
712            IDSetSwitch(sProp, "Failed to move focuser %s!", (dir == TCFS_INWARD) ? "inward" : "outward");
713            return true;
714        }
715}
716
717bool TCFS::dispatch_command(TCFSCommand command_type)
718{
719   int err_code = 0, nbytes_written=0, nbytes_read=0;
720   char tcfs_error[TCFS_ERROR_BUFFER];
721   INumberVectorProperty *nProp = NULL;
722   ISwitchVectorProperty *sProp = NULL;
723
724   // Clear string
725   command[0] = '\0';
726
727   switch (command_type)
728   {
729        // Focuser Manual Mode
730        case FMMODE:
731                strncpy(command, "FMMODE", TCFS_MAX_CMD);
732                break;
733
734        // Focuser Free Mode
735        case FFMODE:
736                strncpy(command, "FFMODE", TCFS_MAX_CMD);
737                break;
738        // Focuser Auto-A Mode
739        case FAMODE:
740                strncpy(command, "FAMODE", TCFS_MAX_CMD);
741                break;
742
743       // Focuser Auto-A Mode
744       case FBMODE:
745                 strncpy(command, "FBMODE", TCFS_MAX_CMD);
746                 break;
747
748       // Focus Center
749       case FCENTR:
750                 strncpy(command, "FCENTR", TCFS_MAX_CMD);
751                 break;
752
753       // Focuser In “nnnn”
754       case FIN:
755                 simulated_position = nFocusCurrentPosition;
756
757                    /* if ( (nFocusTargetPosition - nFocusSteps) >= FocusInfoNP->np[0].min)
758                            nFocusTargetPosition -= nFocusSteps;
759                     else
760                         return false;*/
761
762                 snprintf(command, TCFS_MAX_CMD, "FI%04d", ((int) nFocusSteps));
763                 break;
764
765      // Focuser Out “nnnn”
766      case FOUT:
767                 simulated_position = nFocusCurrentPosition;
768
769                 /*if ( (nFocusTargetPosition + nFocusSteps) <= FocusInfoNP->np[0].max)
770                    nFocusTargetPosition += nFocusSteps;
771                 else
772                     return false;*/
773
774                 snprintf(command, TCFS_MAX_CMD, "FO%04d", ((int) nFocusSteps));
775                 break;
776
777       // Focuser Position Read Out
778       case FPOSRO:
779                 strncpy(command, "FPOSRO", TCFS_MAX_CMD);
780                 break;
781
782       // Focuser Position Read Out
783       case FTMPRO:
784                strncpy(command, "FTMPRO", TCFS_MAX_CMD);
785                break;
786
787       // Focuser Sleep
788       case FSLEEP:
789                strncpy(command, "FSLEEP", TCFS_MAX_CMD);
790                break;
791       // Focuser Wake Up
792       case FWAKUP:
793               strncpy(command, "FWAKUP", TCFS_MAX_CMD);
794               break;
795       // Focuser Home Command
796       case FHOME:
797               strncpy(command, "FHOME", TCFS_MAX_CMD);
798               break;
799   }
800               
801   if (isDebug())
802        IDLog("Dispatching command #%s#\n", command);
803
804   if (isSimulation())
805       return true;
806
807  tcflush(fd, TCIOFLUSH);
808
809   if  ( (err_code = tty_write(fd, command, TCFS_MAX_CMD, &nbytes_written) != TTY_OK))
810   {
811        tty_error_msg(err_code, tcfs_error, TCFS_ERROR_BUFFER);
812        if (isDebug())
813            IDLog("TTY error detected: %s\n", tcfs_error);
814        return false;
815   }
816
817   return true;
818}
819
820
821
822void TCFS::ISPoll()
823{
824   if (!isConnected())
825       return;
826
827   int f_position=0;
828   float f_temperature=0;
829
830   if (FocusPositionNP->s != IPS_IDLE)
831   {
832       // Read Position
833       // Manual Mode
834       if (FocusModeSP->sp[0].s == ISS_ON)
835             dispatch_command(FPOSRO);
836
837       if (read_tcfs() == false)
838                   return;
839
840       if (isSimulation())
841       {
842            if (FocusPositionNP->s == IPS_BUSY)
843            {
844               if ( static_cast<int>(nFocusTargetPosition - simulated_position) > 0)
845                    simulated_position += FocusStepNP->np[0].step;
846               else if ( static_cast<int>(nFocusTargetPosition - simulated_position) < 0)
847                    simulated_position -= FocusStepNP->np[0].step;
848           }
849
850          snprintf(response, TCFS_MAX_CMD, "P=%04d", simulated_position);
851          if (isDebug())
852               IDLog("Target Position: %g -- Simulated position: #%s#\n", nFocusTargetPosition, response);
853       }
854
855        sscanf(response, "P=%d", &f_position);
856
857        nFocusCurrentPosition = f_position;
858
859        if (nFocusCurrentPosition == nFocusTargetPosition)
860        {
861             FocusPositionNP->s = IPS_OK;
862             FocusPositionRequestNP->s = IPS_OK;
863             IDSetNumber(FocusPositionRequestNP, NULL);
864         }
865
866        IDSetNumber(FocusPositionNP, NULL);
867     }
868
869
870   if (FocusTemperatureNP->s != IPS_IDLE)
871   {
872       // Read Temperature
873       // Manual Mode
874       if (FocusModeSP->sp[0].s == ISS_ON)
875            dispatch_command(FTMPRO);
876
877       if (read_tcfs() == false)
878           return;
879
880       if (isSimulation())
881       {
882           snprintf(response, TCFS_MAX_CMD, "T=%0.1f", simulated_temperature);
883           if (isDebug())
884               IDLog("Simulated temperature: #%s#\n", response);
885       }
886
887       sscanf(response, "T=%f", &f_temperature);
888
889       nFocusTemperature = f_temperature;     
890       IDSetNumber(FocusTemperatureNP, NULL);
891
892   }
893
894}
895
896bool TCFS::read_tcfs()
897{
898    int err_code = 0, nbytes_written=0, nbytes_read=0;
899    char err_msg[TCFS_ERROR_BUFFER];
900
901    // Clear string
902    response[0] = '\0';
903
904    if (isSimulation())
905    {
906         strncpy(response, "SIMULATION", TCFS_MAX_CMD);
907         return true;
908    }
909
910    // Read until encountring a CR
911    if ( (err_code = tty_read_section(fd, response, 0x0D, 15, &nbytes_read)) != TTY_OK)
912    {
913            tty_error_msg(err_code, err_msg, 32);
914            if (isDebug())
915            {
916                IDLog("TTY error detected: %s\n", err_msg);
917                IDMessage(mydev, "TTY error detected: %s\n", err_msg);
918            }
919            return false;
920    }
921
922    // Remove LF & CR
923    response[nbytes_read-2] = '\0';
924
925    if (isDebug())
926        IDLog("Bytes Read: %d - strlen(response): %d - Reponse from TCF-S: #%s#\n", nbytes_read, strlen(response), response);
927
928    return true;
929}
930
931const char * TCFS::getDefaultName()
932{
933    return mydev;
934}
Note: See TracBrowser for help on using the repository browser.