/****************************************************************************/
/* Copyright (c) 2014 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Interface for the Modtronix SBCEC65 controller                */
/* Filename : Modtronix.h                                                   */
/* Author   : Henthorn                                                      */
/* Project  : i2MAP                                                         */
/* Version  : 1.0                                                           */
/* Created  : 12/03/2014                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _MODTRONIX_H
#define _MODTRONIX_H

#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <errno.h>
#include <unix.h>
#include <math.h>

#include "CARL_IF.h"
#include "PeriodicTask.h"
#include "ModtronixMsg.h"

class ModtronixOutput;
class ModtronixLog;
class epiBattOutput;
class LayeredControlIF;
class EZ17;
class KiPro;

// Driver interface to the Modtronix for use by other components.
//
class Modtronix : public PeriodicTask
{

   friend class ModtronixLog;

public:
   Modtronix(const char* name, const char* server_ip, unsigned port,
      unsigned interval_msec);
   ~Modtronix();

   int initialize();
   int init_socket(int port, long recv_timeout = 0L);

//protected:

   /* Socket send/read functions */
   int read_msg(int index);
   int send_msg(int index, const char* msg);
   Boolean set_pin(char bank, char pin, char state);
   Boolean check_pin(char bank, char pin, char state);
   int get_ctd_data();
   
   void recorder_switch(int on);   // off = 0, otherwise on
   void power_switch(int on);      // off = 0, otherwise on
   int  power_state(int on);       // Returns 1 if on == 0 and the power switch output is 0
                                   // Returns 1 if on != 0 and the power switch output is 1
                                   // Returns 0 in all other cases

   void ctd_switch(int on);        // off = 0, otherwise on
   int  ctd_state(int on);         // Returns 1 if on == 0 and the ctd switch output is 0

   void light_switch(int on);                   // off = 0, otherwise on
   int  light_state(int light, int state);      // Returns 1 if state == 0 and the light switche output is 0
                                                // Returns 1 if state != 0 and the light switche output is 1
                                                // Returns 0 in all other cases
   void light_level(char level);   // Adjust brightness of the lights (0 to 100)

   // KiPro functions
   void and_action();
   void cut();
   void record_mode(int mode);
   void timecode();
   void available_media();
   void sync();
   void clipname(char *str);
   
   // Called periodically to get CTD data and log evironmental data
   //
   void mod_callback(void);

   void abort();

   // Called when subscribed-to events occur
   void action(TaskInterface *taskInterface, EventCode eventCode);

   /* IF */
   CARL_IF  *_carlIF;

   pid_t _ppid;

   /* Modtronix UDP settings */
   char* _name;
   int                _mod_fds[3];
   char               _mod_bufs[3][256];
   char*              _mod_ip;
   struct sockaddr_in _mod_addr;
   struct timeval     _mod_tv;

   unsigned _interval_msec;

   /* Modtronix control (flip switches, analog inputs, etc) */
   char     _mod_buf[100];
   unsigned _mod_port;

   /* Modtronix serial 1 */
   char     _ctd_buf[200];
   unsigned _ctd_port;
   int      _ctdfd;

   /* Modtronix serial 2 */
   char  _ser2_buf[100];
   unsigned _ser2_port;
   int      _emfd;

   ModtronixMsg    *_msgQ;
   ModtronixOutput *_output;
   ModtronixLog    *_log;

   LayeredControlIF *_layeredControl;
   int _alarmCounts;
   Boolean _aborted;

   epiBattOutput  *_battery;

   // KiPro AJA stuff
   KiPro   *_aja;
   Boolean _recordReq;
   Boolean _clipnameReq;
   long _bootTimer;

   EZ17    *_ez17;
   Boolean _ezHome;
   Boolean _ezMove;
   int     _ezPos;
   int     _ezCmd;

   Boolean EZOn();
   int     EZ17Handler(ModtronixMsg::Message *modmsg);
   int     EZ17Watch();
   int     EZ17FS2Steps(int coded_fstop);  // 43 => third spot in fstop 4
   float   EZ17FSLookup();
};

#endif
