-
Notifications
You must be signed in to change notification settings - Fork 8
/
EncBreakout.cpp
104 lines (84 loc) · 3.59 KB
/
EncBreakout.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "EncBreakout.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS/DESTRUCTOR
////////////////////////////////////////////////////////////////////////////////////////////////////
EncBreakout::EncBreakout(TwoWire& wire, uint8_t address, float brightness, uint32_t timeout, int8_t interruptPin, bool debug)
: _ioe(wire, address, timeout, interruptPin, debug)
, _direction(true) //clockwise
, _brightness(brightness)
, _interruptPin(interruptPin)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// METHODS
////////////////////////////////////////////////////////////////////////////////////////////////////
bool EncBreakout::initialise(bool skipChipIdCheck)
{
bool bSucceeded = true;
if(_ioe.initialise(skipChipIdCheck))
{
if(_interruptPin != -1)
_ioe.enableInterruptOut(true);
_ioe.setupRotaryEncoder(ENC_CHANNEL, PIN_ENC_A, PIN_ENC_B, PIN_ENC_C);
//Calculate a period large enough to get 0-255 steps at the desired brightness
unsigned int period = (unsigned int)(255.0f / _brightness);
_ioe.setPwmPeriod(period);
_ioe.setPwmControl(2); //PWM as fast as we can to avoid LED flicker
_ioe.setMode(PIN_RED, IOExpander::PIN_PWM, false, INVERT_OUTPUT);
_ioe.setMode(PIN_GREEN, IOExpander::PIN_PWM, false, INVERT_OUTPUT);
_ioe.setMode(PIN_BLUE, IOExpander::PIN_PWM, false, INVERT_OUTPUT);
bSucceeded = true;
}
return bSucceeded;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void EncBreakout::setAddr(uint8_t i2cAddr)
{
_ioe.setAddr(i2cAddr);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void EncBreakout::setInterruptCallback(void (*callback)())
{
_ioe.setInterruptCallback(callback);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool EncBreakout::getDirection(void)
{
return _direction;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void EncBreakout::setDirection(bool clockwise)
{
_direction = clockwise;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void EncBreakout::setBrightness(float brightness)
{
_brightness = constrain(brightness, 0.01f, 1.0f);
//Calculate a period large enough to get 0-255 steps at the desired brightness
unsigned int period = (unsigned int)(255.0f / _brightness);
_ioe.setPwmPeriod(period);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void EncBreakout::setRGB(uint8_t r, uint8_t g, uint8_t b)
{
_ioe.output(PIN_RED, r, false); //Hold off pwm load until the last
_ioe.output(PIN_GREEN, g, false); //Hold off pwm load until the last
_ioe.output(PIN_BLUE, b); //Loads all 3 pwms
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool EncBreakout::available(void)
{
return (_ioe.getInterruptFlag() > 0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int16_t EncBreakout::read(void)
{
int16_t count = _ioe.readRotaryEncoder(ENC_CHANNEL);
if(!_direction)
count = 0 - count;
_ioe.clearInterruptFlag();
return count;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////