#ifndef _DRIBBLER_H
#define _DRIBBLER_H

#include "../../stdafx.h"

class Dribbler : public IO_Module
{
public:
	Dribbler()
	{
		this->enabled = false;
		this->curSpeed = 0;
		this->parent = NULL;
		this->moduleId = 0;
		this->hardwarePort = NULL;
		this->portPower = NULL;
		this->pinForward = 0;
		this->pinReverse = 0;
		this->pinPower = 0;
	}

	Dribbler(uint32 dribblerId)
	{
		this->enabled = false;
		this->curSpeed = 1.0f;
		this->parent = NULL;
		this->moduleId = dribblerId;
		
		switch(dribblerId)
		{
			case IO_DRIBBLER_MAIN:
				this->hardwarePort = &PORTD;
				this->portPower = &PORTA;
				this->pinForward = (1 << 6);
				this->pinReverse = (1 << 7);
				this->pinPower = (1 << 5);
				break;
			default:
				this->hardwarePort = NULL;
				this->portPower = NULL;
				this->pinForward = 0;
				this->pinReverse = 0;
				this->pinPower = 0;
				break;
		}

		UpdateDirection();
	}

protected:
	bool enabled;
	float curSpeed;

	//Hardware
	volatile uint8* hardwarePort;
	volatile uint8* portPower;
	uint8 pinForward;
	uint8 pinReverse;
	uint8 pinPower;

	void UpdateDirection()
	{
		if(enabled)
		{
			if(curSpeed > 0)
			{
				*hardwarePort |= pinForward;
				*hardwarePort &= ~pinReverse;
			}
			else if(curSpeed < 0)
			{
				*hardwarePort |= pinReverse;
				*hardwarePort &= ~pinForward;
			}
			else
			{
				*hardwarePort |= pinForward;
				*hardwarePort |= pinReverse;
			}

			*portPower |= pinPower;
		}
		else
		{
			*hardwarePort &= ~pinForward;
			*hardwarePort &= ~pinReverse;

			*portPower &= ~pinPower;
		}
	}

public:
	float GetSpeed()
	{
		return curSpeed;
	}

	void SetSpeed(float newSpeed)
	{
		curSpeed = newSpeed;

		UpdateDirection();
	}

	bool GetEnabled()
	{
		return enabled;
	}

	void SetEnabled(bool newStatus)
	{
		enabled = newStatus;

		UpdateDirection();
	}
};

#endif
