Last active
January 14, 2022 17:26
-
-
Save nmwilk/0b8ef1f5750b6f00aef829fbffdb0f4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BytesValT unsigned int | |
#define numOfRotaryButtons 10 | |
#define numOfhc165 3 | |
#define dataWidth numOfhc165 * 8 | |
#define pulseWidthUSec 5 | |
byte previousButtonStates[dataWidth]; | |
byte currentButtonStates[dataWidth]; | |
BytesValT inputBits; | |
int ploadPin165 = 15; | |
int cePin165 = 18; | |
int dataPin165 = 5; | |
int clockPin165 = 2; | |
void setup() { | |
pinMode(ploadPin165, OUTPUT); | |
pinMode(cePin165, OUTPUT); | |
pinMode(clockPin165, OUTPUT); | |
pinMode(dataPin165, INPUT); | |
digitalWrite(clockPin165, LOW); | |
digitalWrite(ploadPin165, HIGH); | |
} | |
void loop() { | |
bool inputsChanged = processInput(); | |
if (inputsChanged) { | |
// send BLEGamePad changes | |
} | |
} | |
byte bitIsSet(BytesValT bytes, int bitNumber) { | |
return (bytes >> bitNumber) & 1; | |
} | |
bool processInput() { | |
inputBits = read165s(); | |
bool buttonsChanged = false; | |
for (int currentIndex = 0 ; currentIndex < dataWidth ; currentIndex++) { | |
currentButtonStates[currentIndex] = bitIsSet(inputBits, currentIndex); | |
if (currentButtonStates[currentIndex] != previousButtonStates[currentIndex]) { | |
buttonsChanged = true; | |
if (currentButtonStates[currentIndex] == HIGH) { | |
// button pressed e.g. press BLEGamePad button | |
} else { | |
// button not pressed e.g. release BLEGamePad button | |
} | |
} | |
} | |
if (buttonsChanged) { | |
for (int currentIndex = 0; currentIndex < dataWidth ; currentIndex++) { | |
previousButtonStates[currentIndex] = currentButtonStates[currentIndex]; | |
} | |
} | |
return buttonsChanged; | |
} | |
BytesValT read165s() { | |
long bitVal; | |
BytesValT bytesVal = 0; | |
digitalWrite(cePin165, HIGH); | |
digitalWrite(ploadPin165, LOW); | |
delayMicroseconds(pulseWidthUSec); | |
digitalWrite(ploadPin165, HIGH); | |
digitalWrite(cePin165, LOW); | |
for (int i = 0; i < dataWidth; i++) { | |
bitVal = digitalRead(dataPin165); | |
bytesVal |= (bitVal << ((dataWidth - 1) - i)); | |
digitalWrite(clockPin165, HIGH); | |
delayMicroseconds(pulseWidthUSec); | |
digitalWrite(clockPin165, LOW); | |
} | |
return bytesVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment