Skip to content

Commit 71bcca3

Browse files
add button and led for PCF8574
1 parent 46dcd23 commit 71bcca3

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

Button_PCF8574/Button_PCF8574.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Button_PCF8574.cpp - Control PCF8574 connected button.
3+
*/
4+
5+
#include "Button_PCF8574.h"
6+
7+
Button_PCF8574::Button_PCF8574(
8+
int btn_pin,
9+
MultiPlexer_PCF8574* pcf,
10+
Method callback
11+
) {
12+
_btnPin = btn_pin;
13+
_pcf = pcf;
14+
_callback = callback;
15+
_lastBtnState = LOW;
16+
}
17+
18+
void Button_PCF8574::begin() {
19+
}
20+
21+
void Button_PCF8574::loop() {
22+
read();
23+
}
24+
25+
void Button_PCF8574::read() {
26+
/*
27+
int buttonReading = _mcp->getPin(_btnPin, _port);
28+
29+
// if the switch changed, due to noise or pressing
30+
if (buttonReading != _lastBtnState) {
31+
// reset the debouncing timer
32+
_lastDebounceTime = millis();
33+
}
34+
if ((millis() - _lastDebounceTime) > _debounceDelay) {
35+
// if the button state has changed
36+
if (buttonReading != _currentBtnState) {
37+
_currentBtnState = buttonReading;
38+
39+
// only toggle if the new button state is HIGH
40+
if (_currentBtnState == HIGH) {
41+
_callback.callbackIntArg(_btnPin);
42+
}
43+
}
44+
}
45+
// save the switch reading
46+
_lastBtnState = buttonReading;
47+
*/
48+
}

Button_PCF8574/Button_PCF8574.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Button_PCF8574.cpp - Control PCF8574 connected button.
3+
*/
4+
#ifndef Button_PCF8574_h
5+
#define Button_PCF8574_h
6+
7+
#include <Arduino.h>
8+
#include <Method.h>
9+
#include <MultiPlexer_PCF8574.h>
10+
11+
class Button_PCF8574
12+
{
13+
public:
14+
Button_PCF8574(
15+
int btn_pin,
16+
MultiPlexer_PCF8574* pcf,
17+
Method callback
18+
);
19+
void loop();
20+
void begin();
21+
void read();
22+
23+
private:
24+
int _btnPin;
25+
int _currentBtnState;
26+
int _lastBtnState;
27+
Method _callback;
28+
MultiPlexer_PCF8574* _pcf;
29+
unsigned long _lastDebounceTime = 0;
30+
unsigned long _debounceDelay = 5;
31+
};
32+
33+
#endif

LED_PCF8574/LED_PCF8574.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
LED_PCF8574.h - Control LED light using PCF8574 multiplexer.
3+
*/
4+
5+
#include "LED_PCF8574.h"
6+
7+
LED_PCF8574::LED_PCF8574(
8+
int pin,
9+
MultiPlexer_PCF8574* pcf,
10+
int led_state
11+
) {
12+
_pin = pin;
13+
_pcf = pcf;
14+
state = led_state;
15+
}
16+
17+
void LED_PCF8574::begin() {
18+
_pcf->pinMode(_pin, OUTPUT);
19+
}
20+
21+
void LED_PCF8574::loop() {
22+
}
23+
24+
void LED_PCF8574::update() {
25+
_pcf->digitalWrite(_pin, state);
26+
}
27+
28+
void LED_PCF8574::enable() {
29+
state = HIGH;
30+
update();
31+
}
32+
33+
void LED_PCF8574::disable() {
34+
state = LOW;
35+
update();
36+
}
37+
38+
void LED_PCF8574::toggle() {
39+
state = !state;
40+
update();
41+
}

LED_PCF8574/LED_PCF8574.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
LED_PCF8574.h - Control LED light using PCF8574 multiplexer.
3+
*/
4+
#ifndef LED_PCF8574_h
5+
#define LED_PCF8574_h
6+
7+
#include "Arduino.h"
8+
#include <MultiPlexer_PCF8574.h>
9+
10+
class LED_PCF8574
11+
{
12+
public:
13+
LED_PCF8574(
14+
int pin,
15+
MultiPlexer_PCF8574* pcf,
16+
int led_state = LOW
17+
);
18+
int state;
19+
void begin();
20+
void loop();
21+
void enable();
22+
void disable();
23+
void toggle();
24+
25+
private:
26+
void update();
27+
28+
int _pin;
29+
MultiPlexer_PCF8574* _pcf;
30+
};
31+
32+
#endif

MultiPlexer_PCF8574/MultiPlexer_PCF8574.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ MultiPlexer_PCF8574::MultiPlexer_PCF8574(uint8_t address) {
1010

1111
void MultiPlexer_PCF8574::begin() {
1212
Serial.print("Initializing PCF8574... ");
13+
14+
/*
1315
// inputs
1416
int i = 0;
1517
for (i = 0; i<4; i++) {
@@ -19,6 +21,7 @@ void MultiPlexer_PCF8574::begin() {
1921
for (i=4; i<8; i++) {
2022
_expander->pinMode(i, OUTPUT, LOW);
2123
}
24+
*/
2225

2326
if (_expander->begin()) {
2427
Serial.println("OK");
@@ -29,3 +32,11 @@ void MultiPlexer_PCF8574::begin() {
2932

3033
void MultiPlexer_PCF8574::loop() {
3134
}
35+
36+
void MultiPlexer_PCF8574::pinMode(uint8_t pin, uint8_t mode, uint8_t initialValue) {
37+
_expander->pinMode(pin, mode, initialValue);
38+
}
39+
40+
void MultiPlexer_PCF8574::digitalWrite(uint8_t pin, uint8_t value) {
41+
_expander->digitalWrite(pin, value);
42+
}

MultiPlexer_PCF8574/MultiPlexer_PCF8574.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class MultiPlexer_PCF8574
1414
MultiPlexer_PCF8574(uint8_t address);
1515
void begin();
1616
void loop();
17+
void pinMode(uint8_t pin, uint8_t mode, uint8_t initialValue = HIGH);
18+
void digitalWrite(uint8_t pin, uint8_t value);
1719

1820
private:
1921
PCF8574* _expander;

0 commit comments

Comments
 (0)