/*
 * Copyright 2009-2010  Stefan Gehn <stefan@srcbox.net>
 * 
 * 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.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef IRPACKET_H_
#define IRPACKET_H_

#include <QTime>
#include <QList>
#include <QMatrix>

class QByteArray;

namespace IrTouch
{

class RawValue
{
	qint16 mValue;
	qint16 mExtent;
public:
  RawValue() :
		mValue(0), mExtent(0)
  {
  }

	RawValue(qint16 v, qint16 e) :
		mValue(v), mExtent(e)
  {
  }

	inline qint16 start() const { return mValue; }
	inline qint16 center() const { return mValue + (mExtent/2); }
	inline qint16 end() const { return mValue + mExtent; }
	inline qint16 value() const { return mValue; }
	inline qint16 extent() const { return mExtent; }
};

typedef QList<RawValue> RawValueList;
typedef QList<RawValue>::iterator RawValueListIterator;


class Packet
{

public:
  //! invert values for X-axis (depends on hardware in use)
  static bool sFlipXPos;
  static bool sCorrectXPos;

  //! invert values for Y-axis (depends on hardware in use)
  static bool sFlipYPos;
  static bool sCorrectYPos;

  static QMatrix sCalibrationMatrix;

  Packet();
	//! For serialization
	Packet(const QTime &timestamp,
				 const QList<RawValue> &rawXValues,
				 const QList<RawValue> &rawYValues);

  void clear();

  /**
   * Returns true, if the packet has been parsed completely and
   * passed the checksum check.
   **/
	bool isValid() const { return (mState == Finished) && (mTimestamp.isValid()); }

	const QTime &timestamp() const { return mTimestamp; }
	const RawValueList &rawXValues() const { return mRawXValues; }
	const RawValueList &rawYValues() const { return mRawYValues; }

	/**
	 * Parses bytes from @p data until either @p data is empty or the end of an
	 * IrTouch packet has been detected.
	 * @return true if a full packet has been parsed,
	 *         false if more data is needed.
	 * @note The passed @p data is not necessarily empty after a call to parse().
	 *       Remaining bytes can be fed on the next call.
	 **/
	bool parse(QByteArray &data);

private:
  /**
   * calculate CRC value for a given bytearray
   **/
  static char calcCrc(const QByteArray &data, int len);
  bool parseHead(QByteArray &data);
  bool parsePacketSize(QByteArray &data);
  bool parsePayload(QByteArray &data);
  bool parseCrc(QByteArray &data);

private:
  enum State { Invalid = -1, Head = 0, PacketSize, Payload, CRC, Finished};
  State mState;
  unsigned char mCrc;
  unsigned char mRows;
  unsigned char mCols;

	QTime mTimestamp;
	RawValueList mRawXValues;
	RawValueList mRawYValues;
};

} // namespace IrTouch

QDataStream &operator<<(QDataStream &, const IrTouch::Packet &);
QDataStream &operator>>(QDataStream &, IrTouch::Packet &);

QDataStream &operator<<(QDataStream &, const IrTouch::RawValue &);
QDataStream &operator>>(QDataStream &, IrTouch::RawValue &);

#endif /*IRPACKET_H_*/
