// PZEMSerial.h

#ifndef _PZEMSERIAL_h
#define _PZEMSERIAL_h

#include "Arduino.h"
#include <inttypes.h>
#include <Stream.h>

class PZEMSerial : public Stream {
public:
  PZEMSerial(int receivePin, int transmitPin, bool inverse_logic = false, unsigned int buffSize = 64);
  ~PZEMSerial();

  void begin(long speed);
  void setTransmitEnablePin(int transmitEnablePin);

  int peek();

  virtual size_t write(uint8_t byte);
  virtual int read();
  virtual int available();
  virtual void flush();
  operator bool() { return m_rxValid || m_txValid; }

  // Disable or enable interrupts on the rx pin
  void enableRx(bool on);

  void rxRead();

  using Print::write;

private:
  bool isValidGPIOpin(int pin);

  // Member variables
  int m_rxPin, m_txPin, m_txEnablePin;
  bool m_rxValid, m_txValid, m_txEnableValid;
  bool m_invert;
  unsigned long m_bitTime;
  unsigned int m_inPos, m_outPos;
  int m_buffSize;
  uint8_t *m_buffer;

};

// If only one tx or rx wanted then use this as parameter for the unused pin
#define SW_SERIAL_UNUSED_PIN -1

#endif

