/*****************************************************************************

    source::worx libPiGPio
    Copyright © 2022 c.holzheuer
    chris@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Uses:

    pigpiod_if2 by joan2937, pigpio @ abyz.me.uk
    https://abyz.me.uk/rpi/pigpio

    sigslot  by 2017 Pierre-Antoine Lacaze
    https://github.com/palacaze/sigslot

***************************************************************************/


#ifndef PIGLCD_H
#define PIGLCD_H

//#include <unistd.h>
//#include <inttypes.h>

#include <pigi2cnode.h>
/*
"""
  This class provides simple functions to display text on an I2C LCD
  based on the PCF8574T I2C 8-bit port expander.

  PCF8574T P7   P6   P5   P4   P3   P2   P1   P0
  HD44780  B7   B6   B5   B4   BL   E    RW   RS

  This code defaults to working with an adapter with the above
  configuration.

  If yours is different you will have to specify the mapping
  when you instantiate the LCD.
  """

  """
  Commands

  LCD_CLEARDISPLAY = 0x01
  LCD_RETURNHOME = 0x02
  LCD_ENTRYMODESET = 0x04
  LCD_DISPLAYCONTROL = 0x08
  LCD_CURSORSHIFT = 0x10
  LCD_FUNCTIONSET = 0x20
  LCD_SETCGRAMADDR = 0x40
  LCD_SETDDRAMADDR = 0x80

  Flags for display entry mode

  LCD_ENTRYRIGHT = 0x00
  LCD_ENTRYLEFT = 0x02
  LCD_ENTRYSHIFTINCREMENT = 0x01
  LCD_ENTRYSHIFTDECREMENT = 0x00

  Flags for display on/off control

  LCD_DISPLAYON = 0x04
  LCD_DISPLAYOFF = 0x00
  LCD_CURSORON = 0x02
  LCD_CURSOROFF = 0x00
  LCD_BLINKON = 0x01
  LCD_BLINKOFF = 0x00

  Flags for display/cursor shift

  LCD_DISPLAYMOVE = 0x08
  LCD_CURSORMOVE = 0x00
  LCD_MOVERIGHT = 0x04
  LCD_MOVELEFT = 0x00

  Flags for function set

  LCD_8BITMODE = 0x10
  LCD_4BITMODE = 0x00
  LCD_2LINE = 0x08
  LCD_1LINE = 0x00
  LCD_5x10DOTS = 0x04
  LCD_5x8DOTS = 0x00

  Flags for backlight control

  LCD_BACKLIGHT = 0x08
  LCD_NOBACKLIGHT = 0x00
  """
*/


/**
 * @brief The pigLCD class für //HD44780
 * Back-Port von python, siehe: https://abyz.me.uk/rpi/pigpio/code/i2c_lcd_py.zip
 *
 */

class pigLCD : public pigI2CNode
{
//HD44780
public:

    pigLCD() = default;
    pigLCD( i2c_addr_t addr, i2c_bus_t bus = 1 );
    virtual ~pigLCD();

    void init( i2c_addr_t addr, i2c_bus_t bus=1, int width=16, bool bl=true,
               uint8_t RS=0, uint8_t RW=1, uint8_t E=2, uint8_t BL=3, uint8_t B4=4 );

    void set_backlight( bool bl );
    void move_to( int row, int col );
    void write( const char c );
    void write( const std::string text );
    void write( int row, int col, const std::string text );


    // inwieweit notwendig?
   void reset();

protected:

    void put_byte( uint8_t MSb, uint8_t LSb );
    void put_inst( uint8_t bits );
    void put_data( uint8_t bits );
 
    uint8_t _LCD_ROW[4] = { 0x80, 0xC0, 0x94, 0xD4 };

    bool _backlight = false;
    int  _width     = 16;

    //?? Type?
    uint8_t _RS;
    uint8_t _E;
    uint8_t _BL;
    uint8_t _B4;
    /*
    // GPIO chip i2c address
    uint8_t i2c=0x27;
    // Control line PINs
    uint8_t en=2;
    uint8_t rw=1;
    uint8_t rs=0;

    // Data line PINs
    uint8_t d4=4;
    uint8_t d5=5;
    uint8_t d6=6;
    uint8_t d7=7;

    // Backlight PIN
    uint8_t bl=3;

    // LCD display size (1x16, 2x16, 4x20)
    uint8_t rows=2;
    uint8_t cols=16;
    */

};

#endif // PIGLCD_H
