/*! \file pigpio.h
 * \ingroup IO
 * \~\brief
 * \~english GPIO
 * \~russian GPIO
*/
/*
	PIP - Platform Independent Primitives
	GPIO
	Andrey Bychkov work.a.b@yandex.ru, Ivan Pelipenko peri4ko@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 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 Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PIGPIO_H
#define PIGPIO_H

#include "pithread.h"


//! \ingroup IO
//! \~\brief
//! \~english GPIO support.
//! \~russian Поддержка GPIO.
class PIP_EXPORT PIGPIO: public PIThread
{
	PIOBJECT_SUBCLASS(PIGPIO, PIThread);
public:

	//! \~english Work mode for pin
	//! \~russian Режим работы пина
	enum Direction {
		In  /** \~english Input direction (read) \~russian Входной (чтение) */,
		Out /** \~english Output direction (write) \~russian Выходной (запись) */
	};

	//! \~english Returns singleton object of %PIGPIO
	//! \~russian Возвращает синглтон объекта %PIGPIO
	static PIGPIO * instance();

	//! \~english Initialize pin "gpio_num" for "dir" mode
	//! \~russian Инициализирует пин "gpio_num" для режима работы "dir"
	void initPin(int gpio_num, Direction dir = PIGPIO::In);

	//! \~english Set pin "gpio_num" value to "value"
	//! \~russian Устанавливает значение пина "gpio_num" в "value"
	void pinSet  (int gpio_num, bool value);

	//! \~english Set pin "gpio_num" value to \b true
	//! \~russian Устанавливает значение пина "gpio_num" в \b true
	void pinHigh (int gpio_num) {pinSet(gpio_num, true );}

	//! \~english Set pin "gpio_num" value to \b false
	//! \~russian Устанавливает значение пина "gpio_num" в \b false
	void pinLow  (int gpio_num) {pinSet(gpio_num, false);}

	//!
	//! \~english Returns pin "gpio_num" state
	//! \~russian Возвращает значение пина "gpio_num"
	bool pinState(int gpio_num);

	//! \~english Starts watch for pin "gpio_num"
	//! \~russian Начинает наблюдение за пином "gpio_num"
	void pinBeginWatch(int gpio_num);

	//! \~english End watch for pin "gpio_num"
	//! \~russian Заканчивает наблюдение за пином "gpio_num"
	void pinEndWatch  (int gpio_num);

	//! \~english End watch for all pins
	//! \~russian Заканчивает наблюдение за всеми пинами
	void clearWatch();

	EVENT2(pinChanged, int, gpio_num, bool, new_value);

	//! \events
	//! \{

		//! \fn void pinChanged(int gpio_num, bool new_value)
		//! \~english Raise on pin "gpio_num" state changes to "new_value"
		//! \~russian Вызывается по смене состояния пина "gpio_num" на "new_value"
		//! \~\details
		//! \~\warning
		//! \~english This event raised only when thread started.
		//! \~russian Это событие вызывается только при запущенном потоке.

	//! \}

private:
	PIGPIO();
	virtual ~PIGPIO();
	NO_COPY_CLASS(PIGPIO);

	struct PIP_EXPORT GPIOData {
		GPIOData() {dir = PIGPIO::In; num = fd = -1;}
		PIString name;
		int dir;
		int num;
		int fd;
	};
	void exportGPIO(int gpio_num);
	void openGPIO(GPIOData & g);
	bool getPinState(int gpio_num);
	void begin() override;
	void run() override;
	void end() override;

	static PIString GPIOName(int gpio_num);

	PIMap<int, GPIOData> gpio_;
	PIMap<int, bool> watch_state;
	PIMutex mutex;

};


#endif // PIDIR_H
