#ifndef __ACTIVYLCD_H
#define __ACTIVYLCD_H


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


///////////////////////////////////////////////////
// activy300 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>



///////////////////////////////////////////////////
// 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 DARKEST              0
#define DARK                 1
#define MEDIUM               2
#define BRIGHT               3
#define BRIGHTEST            4

#define POWERLED_GREEN_BLINK     "\x90\x80"
#define POWERLED_GREEN_ON        "\x90\x08" // or "0x90 0x01"
#define POWERLED_RED_ON          "\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 LCD_DARKEST              "\xF0\xFF"
#define LCD_DARK                 "\xF0\xC0"
#define LCD_MEDIUM               "\xF0\x7D"
#define LCD_BRIGHT               "\xF0\x3D"
#define LCD_BRIGHTEST            "\xF0\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 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];
   bool bRecStat;
   bool bInfoStat;
   bool picError;

   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 SetInfo(bool bRec, bool bInfo);
   void FormatString( char *output, const char *input, int len, bool split = false );
   bool CheckErrors(void);
};

#endif
