summaryrefslogtreecommitdiff
path: root/tags/firmware/Arduino/1.3/library/AnalogEncoder/AnalogEncoder.cpp
blob: acb8d22f3109fd3faf1d02ec5d4d6767e673518a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include "WConstants.h"
#include "AnalogEncoder.h"

AnalogEncoder::AnalogEncoder(int pin)
{
	this->pin = pin;
	this->current_position = -1;
	this->last_position = -1;
	this->direction = -1;
}

//our interface methods
void AnalogEncoder::readState()
{
	this->last_position = this->current_position;
	this->current_position = analogRead(this->pin);
	
	if (this->current_position < 16)
	{
		if (this->last_position > 768)
			this->direction = 1;
		else
			this->direction = 0;
	}
	else if (this->current_position > 1008)
	{
		if (this->last_position < 256)
			this->direction = 0;
		else
			this->direction = 1;
	}
	else
	{
		if (this->current_position > this->last_position)
			this->direction = 1;
		else
			this->direction = 0;
	}
}

int AnalogEncoder::getPosition()
{
	return this->current_position;
}

int AnalogEncoder::getDirection()
{
	return this->direction;
}

int AnalogEncoder::version()
{
	return 1;
}