///////////////////////////////////////////////////
// activy3xx LCD plugin for VDR
//
// Original written by:
//
// Meinrad Sauter <meinrad.sauter@gmx.de>
// andreas 'randy' weinberger <vdr@smue.org>
//
// rearranged, beautified and rewritten by:
//
// Markus Geisler <geisler@primusnetz.de>
//
// optimized by:
// Sascha Quint <sascha.quint@gmx.net>
//
// rearranged, beautified and rewritten by:
// Helmut Auer <helmut@helmutauer.de>
//
///////////////////////////////////////////////////

#ifndef __ACTIVYLCD_H
#define __ACTIVYLCD_H


#include <sys/time.h>
#include <sys/sem.h>
#undef LCDDEBUG

// DEFINITIONS

#define LCD_PORT "/dev/ttyS0"

#define LCD_COLUMNS 20
#define LCD_ROWS     2

#define LEDREC_RED   0
#define LEDREC_GREEN 1

#define SCROLL_BLOCK 0
#define SCROLL_CHAR  1

#define OFF                  0
#define DARKEST              1
#define BRIGHTEST           16

#define PWRLED_RED           0
#define PWRLED_GREEN         1
#define PWRLED_BLINK         2

#define ALIGN_CENTER 0
#define ALIGN_LEFT   1 
#define ALIGN_RIGHT  2

#define LED_OFF 0
#define LED_ON  1

#define POWERLED_BLINK_ON        "\x90\x80"
#define POWERLED_BLINK_OFF       "\x90\x01"
#define POWERLED_GREEN           "\x90\x08" // or "0x90 0x01"
#define POWERLED_RED             "\x90\x10"
#define BASICLED_ON              "\x90\x40"
#define BASICLED_OFF             "\x90\x04"
#define MESSAGELED_ON            "\x90\x20"
#define MESSAGELED_OFF           "\x90\x02"

#define POWERBUTTON_ON           "\x94\x12" // setkeycodes e010 xx
#define POWERBUTTON_OFF          "\x94\x21"
#define POWERBUTTON_SCANCODE_ON  "\x94\x03"
#define POWERBUTTON_SCANCODE_OFF "\x94\x30"

#define MAGIC_RESET              "\x95\x03"

#define LCD_WRITE_LINE0          "\x9A\x02" // + \x00
#define LCD_WRITE_LINE1          "\x9A\x03" // + \x00
#define LCD_CLEAR                "\x9B\x00"

#define READBUTTONS              "\x92\x00"

#define EVENTFILTER              "\x95\xF0"
#define EVENTFILTEROFF           "\x95\xFF"
#define BLAST                    "\x85\x00"

#define PICRESET                 "\x8E\x00"
#define HWREV                    "\x89\x00"
#define FWREV                    "\x8A\x00"

#define LCD_PERM_ON              "\x9c\x00"
#define LCD_PERM_OFF             "\x9c\x01"

#define PIC_RES_ACK              0x80

///////////////////////////////////////////////////
// class activyLCD

class activyLCD
{
private:

   // filedescriptor for serial port
   int fd;
   char actLine[LCD_ROWS][256];
   unsigned int actPos[LCD_ROWS];
   int actBrightness;
   unsigned int scroll[LCD_ROWS];
   int lRecStat, lInfoStat, lPowerLEDStat;
   bool picError;
   bool bPermanent;
   bool bLocked;

   void SetLCDBrightness(const int brightness);
   void SetPowerButton(bool status);
   int SerialPortOpen(void);
   void SerialPortClose();
   void SendCommand(char *command);
   void PicWrite(char *command,int length);
   void Reset();


#ifdef LCDDEBUG
   FILE *logfile;
#endif
public:

   // public member functions
   activyLCD();
   ~activyLCD();
   void SendText(char *text[LCD_ROWS]);
   void SetTextPermanent(bool bStatus);
   void SetInfo(int lRec, int lInfo, int lPwr );
   void FormatString( char *output, const char *input, int len, bool bSplit = false, bool bAlign = false );
   void Lock(bool bStatus) { bLocked = bStatus; } 
   bool CheckErrors(void);
};

#endif
