-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI2CButton.cpp
More file actions
42 lines (35 loc) · 740 Bytes
/
I2CButton.cpp
File metadata and controls
42 lines (35 loc) · 740 Bytes
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
/*
I2CButton.cpp - Support Library to manage a button with an LED over I2C.
*/
#include "I2CButton.h"
// Constructors
I2CButton::I2CButton(PCF8574 *I2CBus,
uint8_t btnPin,
uint8_t ledPin)
: _I2CBus(I2CBus),
_btnPin(btnPin),
_ledPin(ledPin),
_lastPressed(false),
_fire(false)
{
}
// Public
void I2CButton::setup()
{
_I2CBus->pinMode(_btnPin, INPUT);
_I2CBus->pinMode(_ledPin, OUTPUT);
}
void I2CButton::tick()
{
bool isPressed = _I2CBus->digitalRead(_btnPin, true);
_fire = !_lastPressed && isPressed;
_lastPressed = isPressed;
}
void I2CButton::setLed(bool state)
{
_I2CBus->digitalWrite(_ledPin, state);
}
bool I2CButton::fire()
{
return _fire;
}