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

Last change on this file since 504 was 504, checked in by frichard, 13 years ago

-Version 0.8 de libini
-Formule de Marc
-Nouvelles fonctionnalités (goto nom-de l'objet etc...)

File size: 24.7 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(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(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(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        ConnectSP->s = IPS_ALERT;
242        IDSetSwitch (ConnectSP, "Error connecting to port %s. Make sure you have BOTH read and write permission to the port.", tProp->tp[0].text);
243        return false;
244    }
245
246    for (int i=0; i < TCFS_MAX_TRIES; i++)
247    {
248        dispatch_command(FMMODE);
249
250        if (read_tcfs() == true)
251        {
252            if (!strcmp(response, "!"))
253            {
254                setConnected(true, IPS_OK, "Successfully connected to TCF-S Focuser in Manual Mode.");
255
256                IUResetSwitch(FocusModeSP);
257                FocusModeSP->sp[0].s = ISS_ON;
258                FocusModeSP->s       = IPS_OK;
259                IDSetSwitch(FocusModeSP, NULL);
260
261                FocusPositionNP->s = IPS_OK;
262                IDSetNumber(FocusPositionNP, NULL);
263
264                FocusTemperatureNP->s = IPS_OK;
265                IDSetNumber(FocusTemperatureNP, NULL);
266
267                IUResetSwitch(FocusPowerSP);
268                IDSetSwitch(FocusPowerSP, NULL);
269
270                return true;
271             }
272        }
273
274        usleep(500000);
275    }
276
277    setConnected(false, IPS_ALERT, "Error connecting to TCF-S focuser...");
278    return false;
279
280}
281
282/****************************************************************
283**
284**
285*****************************************************************/   
286bool TCFS::Disconnect()
287{
288
289    FocusPositionNP->s = IPS_IDLE;
290    IDSetNumber(FocusPositionNP, NULL);
291
292    FocusTemperatureNP->s = IPS_IDLE;
293    IDSetNumber(FocusTemperatureNP, NULL);
294
295    dispatch_command(FFMODE);
296
297    tty_disconnect(fd);
298
299    setConnected(false, IPS_OK, "Disconnected from TCF-S.");
300
301    return true;
302}
303
304/****************************************************************
305**
306**
307*****************************************************************/
308bool TCFS::ISNewNumber (const char *name, double values[], char *names[], int n)
309{
310
311  INumberVectorProperty *nProp = getNumber(name);
312
313  if (nProp == NULL)
314      return false;
315
316  if (isConnected() == false)
317  {
318      resetProperties();
319      IDMessage(deviceID, "TCF-S is offline. Connect before issiung any commands.");
320      return false;
321  }
322
323  if (!strcmp(nProp->name, "FOCUS_STEP"))
324  {
325      IUUpdateNumber(nProp, values, names, n);
326      nProp->s = IPS_OK;
327      IDSetNumber(nProp, NULL);
328      return true;
329  }
330
331  if (isFocusSleep)
332  {
333      nProp->s = IPS_IDLE;
334      IDSetNumber(nProp, "Focuser is still in sleep mode. Wake up in order to issue commands.");
335      return true;
336  }
337
338  if (!strcmp(nProp->name, "FOCUS_POSITION_REQUEST"))
339  {
340      int current_step = nFocusSteps;
341      IUUpdateNumber(nProp, values, names, n);
342
343      nFocusSteps = fabs(nFocusTargetPosition - nFocusCurrentPosition);
344
345      if ( (nFocusTargetPosition - nFocusCurrentPosition) > 0)
346          move_focuser(TCFS_OUTWARD);
347      else
348          move_focuser(TCFS_INWARD);
349
350      nFocusSteps = current_step;
351
352      FocusPositionNP->s = IPS_BUSY;
353      nProp->s = IPS_BUSY;
354      IDSetNumber(nProp, "Moving focuser to new position %g...", nFocusTargetPosition);
355      return true;
356  }
357   
358}
359
360/****************************************************************
361**
362**
363*****************************************************************/
364bool TCFS::ISNewText (const char *name, char *texts[], char *names[], int n)
365{
366    ITextVectorProperty * tProp = getText(name);
367
368    if (tProp == NULL)
369        return false;
370
371    // Device Port Text
372    if (!strcmp(tProp->name, "DEVICE_PORT"))
373    {
374        if (IUUpdateText(tProp, texts, names, n) < 0)
375                        return false;
376
377                tProp->s = IPS_OK;
378                IDSetText(tProp, "Port updated.");
379
380                return true;
381    }
382
383
384    return false;
385       
386}
387
388/****************************************************************
389**
390**
391*****************************************************************/
392bool TCFS::ISNewSwitch (const char *name, ISState *states, char *names[], int n)
393{
394
395    ISwitch *current_active_switch = NULL, *target_active_switch = NULL;
396    // First process parent!
397    if (INDI::DefaultDriver::ISNewSwitch(deviceID, name, states, names, n) == true)
398        return true;
399
400    ISwitchVectorProperty *sProp = getSwitch(name);
401
402    if (sProp == NULL)
403        return false;
404
405    /*if (!strcmp(sProp->name, "CONNECTION"))
406    {
407        if (!strcmp(names[0], "CONNECT"))
408            connect();
409        else
410            disconnect();
411        return true;
412    }*/
413
414
415    if (isConnected() == false)
416    {
417      resetProperties();
418      IDMessage(deviceID, "TCF-S is offline. Connect before issiung any commands.");
419      return false;
420    }
421
422    // Which switch is CURRENTLY on?
423    current_active_switch = IUFindOnSwitch(sProp);
424
425    IUUpdateSwitch(sProp, states, names, n);
426
427    // Which switch the CLIENT wants to turn on?
428    target_active_switch = IUFindOnSwitch(sProp);
429
430    if (target_active_switch == NULL)
431    {
432        if (isDebug())
433        {
434            IDLog("Error: no ON switch found in %s property.\n", sProp->name);
435        }
436        return true;
437    }
438
439
440    if (!strcmp(sProp->name, "FOCUS_POWER"))
441    {
442        bool sleep = false;
443
444        // Sleep
445        if (!strcmp(target_active_switch->name, "FOCUS_SLEEP"))
446        {
447               dispatch_command(FSLEEP);
448               sleep = true;
449        }
450        // Wake Up
451        else
452            dispatch_command(FWAKUP);
453
454        if (read_tcfs() == false)
455        {
456              IUResetSwitch(sProp);
457              sProp->s = IPS_ALERT;
458              IDSetSwitch(sProp, "Error reading TCF-S reply.");
459              return true;
460        }
461
462            if (sleep)
463            {
464                if (isSimulation())
465                    strncpy(response, "ZZZ", TCFS_MAX_CMD);
466
467                if (!strcmp(response, "ZZZ"))
468                {
469                    sProp->s = IPS_OK;
470                    IDSetSwitch(sProp, "Focuser is set into sleep mode.");
471                    FocusPositionNP->s = IPS_IDLE;
472                    IDSetNumber(FocusPositionNP, NULL);
473                    FocusTemperatureNP->s = IPS_IDLE;
474                    IDSetNumber(FocusTemperatureNP, NULL);
475                    return true;
476                }
477                else
478                {
479                    sProp->s = IPS_ALERT;
480                    IDSetSwitch(sProp, "Focuser sleep mode operation failed. Response: %s.", response);
481                    return true;
482                }
483            }
484            else
485            {
486                if (isSimulation())
487                    strncpy(response, "WAKE", TCFS_MAX_CMD);
488
489                if (!strcmp(response, "WAKE"))
490                {
491                    sProp->s = IPS_OK;
492                    IDSetSwitch(sProp, "Focuser is awake.");
493                    FocusPositionNP->s = IPS_OK;
494                    IDSetNumber(FocusPositionNP, NULL);
495                    FocusTemperatureNP->s = IPS_OK;
496                    IDSetNumber(FocusTemperatureNP, NULL);
497                    return true;
498                }
499                else
500                {
501                    sProp->s = IPS_ALERT;
502                    IDSetSwitch(sProp, "Focuser wake up operation failed. Response: %s", response);
503                    return true;
504                }
505            }
506      }
507
508    if (isFocusSleep)
509    {
510        sProp->s = IPS_IDLE;
511        IUResetSwitch(sProp);
512
513        if (!strcmp(sProp->name, "FOCUS_MODE") && current_active_switch != NULL)
514            current_active_switch->s = ISS_ON;
515
516        IDSetSwitch(sProp, "Focuser is still in sleep mode. Wake up in order to issue commands.");
517        return true;
518    }
519
520    if (!strcmp(sProp->name, "FOCUS_MODE"))
521    {
522
523        sProp->s = IPS_OK;
524
525        if (!strcmp(target_active_switch->name, "Manual"))
526        {
527           dispatch_command(FMMODE);
528           read_tcfs();
529           if (isSimulation() == false && strcmp(response, "!"))
530           {
531               IUResetSwitch(sProp);
532               sProp->s = IPS_ALERT;
533               IDSetSwitch(sProp, "Error switching to manual mode. No reply from TCF-S. Try again.");
534               return true;
535           }
536       }
537        else if (!strcmp(target_active_switch->name, "Auto A"))
538        {
539            dispatch_command(FAMODE);
540            read_tcfs();
541            if (isSimulation() == false && strcmp(response, "A"))
542            {
543                IUResetSwitch(sProp);
544                sProp->s = IPS_ALERT;
545                IDSetSwitch(sProp, "Error switching to Auto Mode A. No reply from TCF-S. Try again.");
546                return true;
547            }
548        }
549        else
550        {
551            dispatch_command(FBMODE);
552            read_tcfs();
553            if (isSimulation() == false && strcmp(response, "B"))
554            {
555                IUResetSwitch(sProp);
556                sProp->s = IPS_ALERT;
557                IDSetSwitch(sProp, "Error switching to Auto Mode B. No reply from TCF-S. Try again.");
558                return true;
559            }
560        }
561
562        IDSetSwitch(sProp, NULL);
563        return true;
564    }
565
566    if (!strcmp(sProp->name, "FOCUS_MOTION"))
567    {
568        // Inward
569        if (!strcmp(target_active_switch->name, "FOCUS_INWARD"))
570          move_focuser(TCFS_INWARD);
571        // Outward
572        else
573            move_focuser(TCFS_OUTWARD);
574
575        return true;
576
577     }
578
579    if (!strcmp(sProp->name, "FOCUS_GOTO"))
580    {
581        int currentStep = nFocusSteps;
582
583        FocusPositionNP->s = IPS_BUSY;
584        sProp->s = IPS_OK;
585
586        // Min
587        if (!strcmp(target_active_switch->name, "FOCUS_MIN"))
588        {
589            nFocusSteps = nFocusCurrentPosition;
590            move_focuser(TCFS_INWARD);
591            nFocusSteps = currentStep;
592            IUResetSwitch(sProp);
593            IDSetSwitch(sProp, "Moving focouser to minimum position...");
594        }
595        // Center
596        else if (!strcmp(target_active_switch->name, "FOCUS_CENTER"))
597        {
598            dispatch_command(FCENTR);
599            read_tcfs();
600
601            if (isSimulation())
602                strncpy(response, "CENTER", TCFS_MAX_CMD);
603
604            if (!strcmp(response, "CENTER"))
605            {
606                IUResetSwitch(sProp);
607                sProp->s = IPS_OK;
608                FocusPositionNP->s = IPS_BUSY;
609                if (isTCFS3)
610                    nFocusTargetPosition = 5000;
611                else
612                    nFocusTargetPosition = 3500;
613
614                IDSetSwitch(sProp, "Moving focuser to center position %g...", nFocusTargetPosition);
615                return true;
616            }
617            else
618            {
619                IUResetSwitch(sProp);
620                sProp->s = IPS_ALERT;
621                IDSetSwitch(sProp, "Failed to move focuser to center position!");
622                return true;
623            }
624        }
625        // Max
626        else if (!strcmp(target_active_switch->name, "FOCUS_MAX"))
627        {
628            nFocusSteps = FocusPositionRequestNP->np[0].max - nFocusCurrentPosition;
629            move_focuser(TCFS_OUTWARD);
630            nFocusSteps = currentStep;
631            IUResetSwitch(sProp);
632            IDSetSwitch(sProp, "Moving focouser to maximum position %g...", FocusPositionRequestNP->np[0].max);
633        }
634        // Home
635        else if (!strcmp(target_active_switch->name, "FOCUS_HOME"))
636        {
637            dispatch_command(FHOME);
638            read_tcfs();
639
640            if (isSimulation())
641                strncpy(response, "DONE", TCFS_MAX_CMD);
642
643            if (!strcmp(response, "DONE"))
644            {
645                IUResetSwitch(sProp);
646                sProp->s = IPS_OK;
647                //FocusInfoNP->s = IPS_BUSY;
648                IDSetSwitch(sProp, "Moving focuser to new calculated position based on temperature...");
649                return true;
650            }
651            else
652            {
653                IUResetSwitch(sProp);
654                sProp->s = IPS_ALERT;
655                IDSetSwitch(sProp, "Failed to move focuser to home position!");
656                return true;
657            }
658        }
659
660        return true;
661    }
662
663
664
665    return false;
666}
667
668bool TCFS::move_focuser(TCFSMotion dir)
669{
670    ISwitchVectorProperty *sProp = getSwitch("FOCUS_MOTION");
671
672    if (sProp == NULL)
673        return false;
674
675    // Inward
676    if (dir == TCFS_INWARD)
677    {
678        dispatch_command(FIN);
679        nFocusTargetPosition = nFocusCurrentPosition - nFocusSteps;
680    }
681    // Outward
682    else
683    {
684        dispatch_command(FOUT);
685        nFocusTargetPosition = nFocusCurrentPosition + nFocusSteps;
686    }
687
688     if (read_tcfs() == false)
689     {
690          IUResetSwitch(sProp);
691          sProp->s = IPS_ALERT;
692          IDSetSwitch(sProp, "Error reading TCF-S reply.");
693          return false;
694     }
695
696        if (isSimulation())
697            strncpy(response, "*", TCFS_MAX_CMD);
698
699        if (!strcmp(response, "*"))
700        {
701            IUResetSwitch(sProp);
702            sProp->s = IPS_OK;
703            FocusPositionNP->s = IPS_BUSY;
704            IDSetSwitch(sProp, "Moving focuser %s %d steps to position %g.", (dir == TCFS_INWARD) ? "inward" : "outward", ((int) nFocusSteps), nFocusTargetPosition);
705            return true;
706        }
707        else
708        {
709            IUResetSwitch(sProp);
710            sProp->s = IPS_ALERT;
711            IDSetSwitch(sProp, "Failed to move focuser %s!", (dir == TCFS_INWARD) ? "inward" : "outward");
712            return true;
713        }
714}
715
716bool TCFS::dispatch_command(TCFSCommand command_type)
717{
718   int err_code = 0, nbytes_written=0, nbytes_read=0;
719   char tcfs_error[TCFS_ERROR_BUFFER];
720   INumberVectorProperty *nProp = NULL;
721   ISwitchVectorProperty *sProp = NULL;
722
723   // Clear string
724   command[0] = '\0';
725
726   switch (command_type)
727   {
728        // Focuser Manual Mode
729        case FMMODE:
730                strncpy(command, "FMMODE", TCFS_MAX_CMD);
731                break;
732
733        // Focuser Free Mode
734        case FFMODE:
735                strncpy(command, "FFMODE", TCFS_MAX_CMD);
736                break;
737        // Focuser Auto-A Mode
738        case FAMODE:
739                strncpy(command, "FAMODE", TCFS_MAX_CMD);
740                break;
741
742       // Focuser Auto-A Mode
743       case FBMODE:
744                 strncpy(command, "FBMODE", TCFS_MAX_CMD);
745                 break;
746
747       // Focus Center
748       case FCENTR:
749                 strncpy(command, "FCENTR", TCFS_MAX_CMD);
750                 break;
751
752       // Focuser In “nnnn”
753       case FIN:
754                 simulated_position = nFocusCurrentPosition;
755
756                    /* if ( (nFocusTargetPosition - nFocusSteps) >= FocusInfoNP->np[0].min)
757                            nFocusTargetPosition -= nFocusSteps;
758                     else
759                         return false;*/
760
761                 snprintf(command, TCFS_MAX_CMD, "FI%04d", ((int) nFocusSteps));
762                 break;
763
764      // Focuser Out “nnnn”
765      case FOUT:
766                 simulated_position = nFocusCurrentPosition;
767
768                 /*if ( (nFocusTargetPosition + nFocusSteps) <= FocusInfoNP->np[0].max)
769                    nFocusTargetPosition += nFocusSteps;
770                 else
771                     return false;*/
772
773                 snprintf(command, TCFS_MAX_CMD, "FO%04d", ((int) nFocusSteps));
774                 break;
775
776       // Focuser Position Read Out
777       case FPOSRO:
778                 strncpy(command, "FPOSRO", TCFS_MAX_CMD);
779                 break;
780
781       // Focuser Position Read Out
782       case FTMPRO:
783                strncpy(command, "FTMPRO", TCFS_MAX_CMD);
784                break;
785
786       // Focuser Sleep
787       case FSLEEP:
788                strncpy(command, "FSLEEP", TCFS_MAX_CMD);
789                break;
790       // Focuser Wake Up
791       case FWAKUP:
792               strncpy(command, "FWAKUP", TCFS_MAX_CMD);
793               break;
794       // Focuser Home Command
795       case FHOME:
796               strncpy(command, "FHOME", TCFS_MAX_CMD);
797               break;
798   }
799               
800   if (isDebug())
801        IDLog("Dispatching command #%s#\n", command);
802
803   if (isSimulation())
804       return true;
805
806  tcflush(fd, TCIOFLUSH);
807
808   if  ( (err_code = tty_write(fd, command, TCFS_MAX_CMD, &nbytes_written) != TTY_OK))
809   {
810        tty_error_msg(err_code, tcfs_error, TCFS_ERROR_BUFFER);
811        if (isDebug())
812            IDLog("TTY error detected: %s\n", tcfs_error);
813        return false;
814   }
815
816   return true;
817}
818
819
820
821void TCFS::ISPoll()
822{
823   if (!isConnected())
824       return;
825
826   int f_position=0;
827   float f_temperature=0;
828
829   if (FocusPositionNP->s != IPS_IDLE)
830   {
831       // Read Position
832       // Manual Mode
833       if (FocusModeSP->sp[0].s == ISS_ON)
834             dispatch_command(FPOSRO);
835
836       if (read_tcfs() == false)
837                   return;
838
839       if (isSimulation())
840       {
841            if (FocusPositionNP->s == IPS_BUSY)
842            {
843               if ( static_cast<int>(nFocusTargetPosition - simulated_position) > 0)
844                    simulated_position += FocusStepNP->np[0].step;
845               else if ( static_cast<int>(nFocusTargetPosition - simulated_position) < 0)
846                    simulated_position -= FocusStepNP->np[0].step;
847           }
848
849          snprintf(response, TCFS_MAX_CMD, "P=%04d", simulated_position);
850          if (isDebug())
851               IDLog("Target Position: %g -- Simulated position: #%s#\n", nFocusTargetPosition, response);
852       }
853
854        sscanf(response, "P=%d", &f_position);
855
856        nFocusCurrentPosition = f_position;
857
858        if (nFocusCurrentPosition == nFocusTargetPosition)
859        {
860             FocusPositionNP->s = IPS_OK;
861             FocusPositionRequestNP->s = IPS_OK;
862             IDSetNumber(FocusPositionRequestNP, NULL);
863         }
864
865        IDSetNumber(FocusPositionNP, NULL);
866     }
867
868
869   if (FocusTemperatureNP->s != IPS_IDLE)
870   {
871       // Read Temperature
872       // Manual Mode
873       if (FocusModeSP->sp[0].s == ISS_ON)
874            dispatch_command(FTMPRO);
875
876       if (read_tcfs() == false)
877           return;
878
879       if (isSimulation())
880       {
881           snprintf(response, TCFS_MAX_CMD, "T=%0.1f", simulated_temperature);
882           if (isDebug())
883               IDLog("Simulated temperature: #%s#\n", response);
884       }
885
886       sscanf(response, "T=%f", &f_temperature);
887
888       nFocusTemperature = f_temperature;     
889       IDSetNumber(FocusTemperatureNP, NULL);
890
891   }
892
893}
894
895bool TCFS::read_tcfs()
896{
897    int err_code = 0, nbytes_written=0, nbytes_read=0;
898    char err_msg[TCFS_ERROR_BUFFER];
899
900    // Clear string
901    response[0] = '\0';
902
903    if (isSimulation())
904    {
905         strncpy(response, "SIMULATION", TCFS_MAX_CMD);
906         return true;
907    }
908
909    // Read until encountring a CR
910    if ( (err_code = tty_read_section(fd, response, 0x0D, 15, &nbytes_read)) != TTY_OK)
911    {
912            tty_error_msg(err_code, err_msg, 32);
913            if (isDebug())
914            {
915                IDLog("TTY error detected: %s\n", err_msg);
916                IDMessage(mydev, "TTY error detected: %s\n", err_msg);
917            }
918            return false;
919    }
920
921    // Remove LF & CR
922    response[nbytes_read-2] = '\0';
923
924    if (isDebug())
925        IDLog("Bytes Read: %d - strlen(response): %d - Reponse from TCF-S: #%s#\n", nbytes_read, strlen(response), response);
926
927    return true;
928}
929
930const char * TCFS::getDefaultName()
931{
932    return "TCFS";
933}
Note: See TracBrowser for help on using the repository browser.