/*
 * icetft plugin for VDR
 *
 * IcetftSwitch.h - class for switching RX and DTR states at ttyS?
 *
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 */

#ifndef __ICETFTSWITCH_H
#define __ICETFTSWITCH_H

#include <vdr/thread.h>
#include <vdr/tools.h>

#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <errno.h>

#include "IcetftStatusMonitor.h"

// You may enable DEBUG mode for information.
//#define DEBUG


// Define the signals used to switch tft on and off
#define TFT_ON TIOCM_DTR
#define TFT_OFF TIOCM_RTS


// Define time in us on&off-signals are sent.
// The plugin is blocked during this time: Do not increase too much!
// 1000us is the lower limit for my circuit, respectively tft.
#define TFT_SLEEP_TIME  2000



/******************************* no user define ******************************/

// Define thread sleep time in us
// Must be set to avoid 100% cpu-useage. However, 0 would be right for my pc, too.

#define THREAD_SLEEP_TIME 1000

// Define timeout for wakeup after last off in ms.
// Must be set to avoid immidiate wakeup on after tft off (timeout in ms).
#define LAST_OFF_TIMEOUT 1000

// Define timeout for wakeup in ms.
#define WAKEUP_TIMEOUT 10

#ifndef TimeMs
#define TimeMs cTimeMs::Now
#endif

enum tState{error=-1, off=0, on=1, toggle=2};

class cIcetftSwitch: public cThread
{
    private:
        int  fd;
        int  state;

        int  timeLastOff;
        int  timeLastOn;
        unsigned long int poweroffTime;
        unsigned long int elapsedLastStatus;
        unsigned long int elapsedLastOff;
        unsigned long int elapsedLastOn ;

        tState tftState;
        tState tftAction;
        cIcetftStatusMonitor *IcetftStatusMonitor;
        bool Reset();

        bool active;
        virtual void Action();

        tState Off();
        tState On();
        tState Toggle();

    public:
        cIcetftSwitch();
        virtual ~cIcetftSwitch();
        tState getTftState();
        void setAction(tState action);
};

extern  cIcetftSwitch * IcetftSwitch;
#endif                                            // __ICETFTSWITCH_H
