#ifndef _EADISPLAY_H_
#define _EADISPLAY_H_
//----------------------------------------------------------------------------
///
/// \brief  "Electronic Assembly" Display
///
///         See implementation files for further information.
///
/// \date   20141217
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


//---Own------------------------------

#include <biwak/output.hpp>
#include <biwak/spi.hpp>

#define ARROW_RIGHT     0b01111110
#define ARROW_LEFT      0b01111111


//---Declaration--------------------------------------------------------------
typedef COutput COutputArray[];

enum EEaDisplayType
{
   eEaDisplayTypeW162Oled5VSpi,
   eEaDisplayTypeDogm163W5VSpi,
   eEaDisplayTypeDogm163W3VSpi,
   eEaDisplayTypeQt,
};

class CEaDisplay
{

   private:
      EEaDisplayType eEaDisplayType;
      CSpi    &spi;
      COutput outputCS;
      COutput outputRS;
#if 0
      int busyCounter;
#endif
      int yPos, xPos;

#if 0
      static const int byteDelay;
      static const int csEnableDelay;
      static const int csDisableDelay;
      static const int busyDelay;
      static const int clearDelay;
#endif

      static constexpr int byteDelay=0;
      // csEnableDelay:
      // Testet Dogm163W3VSpi with nucleo STM32f303: 1 needed
      // Testet EA W162-X3LG 5VSpi with nucleo STM32f303: 1 needed
#if 0
      static constexpr int csEnableDelay=250; //*2;
      static constexpr int csDisableDelay=0;
      static constexpr int busyDelay=0;
      static constexpr int clearDelay=250*2;
#endif
      static constexpr int csEnableDelay=1;
      static constexpr int csDisableDelay=0;
      static constexpr int busyDelay=0;
      // at least Dogm163W3VSpi neededs "1"; 64...128
      // 64 notenough for EA DOGM163W; 256 not enough in mui
      // Todo: check busy flag?
      static constexpr int clearDelay=256*4;
   public:

      CEaDisplay( EEaDisplayType _eEaDisplayType,
                  CSpi &_spi, EGpioPeripheral eGpioPeripheralCS, EGpioPin eGpioPinCS
                            , EGpioPeripheral eGpioPeripheralRS, EGpioPin eGpioPinRS
                  );
      ~CEaDisplay();
      void resetDisplay();
      //void sendNibble (char data);
      void checkBusy();

      void writeIns (const uint32_t data, int delayValue=0);
      void writeInsArray (const uint8_t *pData, int size);
      void writeData (const uint32_t data);
      void enableCs();
      void disableCs();

      void puts(const char *string);
      void putch(const char ch);
      void gotoxy(int x, int y);
      void clear();
      COutput &getOutputCS()
      {
         return(outputCS);
      }
#if 0
      int getBusyCounter()
      {
         return(busyCounter);
      }
#endif
      int lines()
      {
         int lines=0;
         switch(eEaDisplayType)
         {
            case eEaDisplayTypeW162Oled5VSpi:
               lines=2;
               break;
            case eEaDisplayTypeDogm163W5VSpi:
               lines=3;
               break;
            case eEaDisplayTypeQt:
               lines=3;
               break;
            default:
               break;
         }

         return(lines);
      }
      int width()
      {
         return(16);
      }
      EEaDisplayType getDisplayType()
      {
         return(eEaDisplayType);
      }
      void enableCursor(bool enable)
      {
         int value=0xF;
         value&=~3;
         value|=enable?3:0;
         writeIns(value);
      }

};


//---fin----------------------------------------------------------------------
#endif // ? ! _EADISPLAY_H_
