InputEvents v1.5.2
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
HC165ExpanderAdapter.h
1#ifndef HC165ExpanderAdapter_h
2#define HC165ExpanderAdapter_h
3
4#include "Arduino.h"
5#include "GpioExpanderAdapter/GpioExpanderAdapter.h"
6#include "PinAdapter/PinAdapter.h"
7
19 static const int cascadeDefaultLength = 1; // single 74HC164 is default
20 static const int cascadeMaxLength = 4; // max number of 74HC164 supported by this implementation
21 static const int piPinCount = 8; // number of parallel input pins
22 public:
23
32 HC165ExpanderAdapter(byte dataPin, byte clockPin, byte shldPin, byte cascadeLength = cascadeDefaultLength)
33 : dataPin(dataPin),
34 clockPin(clockPin),
35 shldPin(shldPin),
36 cascadeLength(cascadeLength)
37 {
38 cascadeLength = cascadeLength > cascadeMaxLength ? cascadeMaxLength : cascadeLength;
39 }
40
45 void begin() override {
46 pinMode(dataPin, INPUT);
47 pinMode(clockPin, OUTPUT);
48 pinMode(shldPin, OUTPUT);
49 }
50
55 void update() override {
56 // Step 1: Sample
57 digitalWrite(shldPin, LOW);
58 digitalWrite(shldPin, HIGH);
59
60 // Step 2: Read & Shift
61 for (int i = 0; i < cascadeLength * piPinCount; i++) {
62 int pin = digitalRead(dataPin);
63 if (pin == HIGH)
64 bitSet(pins, i);
65 else
66 bitClear(pins, i);
67
68 // Shift out the next bit
69 digitalWrite(clockPin, HIGH);
70 digitalWrite(clockPin, LOW);
71 }
72 }
73
80 bool read(byte pin) override {
81 return bitRead(pins, pin);
82 }
83
90 void attachPin(byte pin, int mode = INPUT_PULLUP) override {}
91
93
94 private:
95 byte dataPin;
96 byte clockPin;
97 byte shldPin;
98 int cascadeLength;
99 uint32_t pins = 0;
100};
101
102#endif
The base class/interface specification for GPIO expanders.
Definition: GpioExpanderAdapter.h:10
An implementation of the GpioExpanderAdapter using 74HC165 shift register(s)
Definition: HC165ExpanderAdapter.h:18
void update() override
Read the state of pins on the 74HC165 cascade.
Definition: HC165ExpanderAdapter.h:55
bool read(byte pin) override
Returns the state of a pin on the expander.
Definition: HC165ExpanderAdapter.h:80
void begin() override
Initialize the pin expander.
Definition: HC165ExpanderAdapter.h:45
HC165ExpanderAdapter(byte dataPin, byte clockPin, byte shldPin, byte cascadeLength=cascadeDefaultLength)
Construct a new HC165ExpanderAdapter.
Definition: HC165ExpanderAdapter.h:32
void attachPin(byte pin, int mode=INPUT_PULLUP) override
pinMode not supported by 74HC165 so do nothing
Definition: HC165ExpanderAdapter.h:90