Skip to content

Commit

Permalink
Merge pull request #2 from bungogood/reed-switch
Browse files Browse the repository at this point in the history
Reed switch Matrix
  • Loading branch information
mark-a-austin authored Sep 21, 2024
2 parents cf500e3 + 0535259 commit 8f3f0be
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 37 deletions.
37 changes: 28 additions & 9 deletions include/reed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@

#include <stdint.h>

class Reed {
class HC595 {
public:
const int outClk;
const int outData;
const int outLch;
const int inClk;
const int inData;
const int inLch;

Reed(int outClk, int outData, int outLch, int inClk, int inData, int inLch);
const int clockPin;
const int dataPin;
const int latchPin;

HC595(int clockPin, int dataPin, int latchPin);
void init();
void write(uint8_t data);
};

class HC165 {
public:
const int clockPin;
const int dataPin;
const int latchPin;

HC165(int clockPin, int dataPin, int latchPin);
void init();
uint8_t read();
};

class ReedMatrix {
public:
HC595* shiftOut;
HC165* shiftIn;

ReedMatrix(int outClockPin, int outDataPin, int outLatchPin, int inClockPin,
int inDataPin, int inLatchPin);
void init();

uint64_t read();
Expand Down
8 changes: 7 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
default_envs = nanoesp32

[env]
; lib_deps = wachidsusilo/Json
lib_deps = throwtheswitch/Unity@^2.6.0

[env:nanoesp32]
platform = https://github.com/platformio/platform-espressif32.git
Expand All @@ -31,3 +31,9 @@ platform = https://github.com/platformio/platform-espressif32.git
board = arduino_nano_esp32
framework = arduino
build_src_filter = +<*> -<bin/*> +<bin/ble.cpp>

[env:matrix]
platform = https://github.com/platformio/platform-espressif32.git
board = arduino_nano_esp32
framework = arduino
build_src_filter = +<*> -<bin/*> +<bin/matrix.cpp>
23 changes: 23 additions & 0 deletions src/bin/matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <Arduino.h>

#include "bluetooth.hpp"
#include "indicator.hpp"
#include "reed.hpp"

Indicator indicator(LED_RED, LED_GREEN, LED_BLUE);
BluetoothManager ble;
ReedMatrix reed(A3, A5, A4, A1, A0, A2);

void setup() {
Serial.begin(9600);
indicator.init();
indicator.set(Color::BLUE);
reed.init();
ble.begin("Gambit");
}

void loop() {
uint64_t board = reed.read();
ble.setReedSwitchValue(board);
delay(200);
}
102 changes: 75 additions & 27 deletions src/reed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,84 @@

#include <Arduino.h>

Reed::Reed(int outClk, int outData, int outLch, int inClk, int inData,
int inLch)
: outClk(outClk),
outData(outData),
outLch(outLch),
inClk(inClk),
inData(inData),
inLch(inLch) {
init();
const int clockPulseDelay = 5;
const int delayInMicroseconds = 30;

ReedMatrix::ReedMatrix(int outClockPin, int outDataPin, int outLatchPin,
int inClockPin, int inDataPin, int inLatchPin) {
shiftOut = new HC595(outClockPin, outDataPin, outLatchPin);
shiftIn = new HC165(inClockPin, inDataPin, inLatchPin);
}

void Reed::init() {
pinMode(outClk, OUTPUT);
pinMode(outData, OUTPUT);
pinMode(outLch, OUTPUT);
pinMode(inClk, OUTPUT);
pinMode(inData, INPUT);
pinMode(inLch, OUTPUT);
void ReedMatrix::init() {
shiftOut->init();
shiftIn->init();
}

uint64_t Reed::read() {
uint64_t data = 0;
digitalWrite(outLch, HIGH);
digitalWrite(outClk, LOW);
digitalWrite(outLch, LOW);
for (int i = 0; i < 64; i++) {
digitalWrite(outClk, HIGH);
digitalWrite(outClk, LOW);
data |= digitalRead(inData) << i;
uint64_t ReedMatrix::read() {
uint64_t board;
for (int row = 7; row >= 0; row--) {
shiftOut->write(1 << row);
byte data = shiftIn->read();
board = board << 8 | data;
}
digitalWrite(outLch, HIGH);
return data;
return board;
}

HC595::HC595(int clockPin, int dataPin, int latchPin)
: clockPin(clockPin), dataPin(dataPin), latchPin(latchPin) {}

void HC595::init() {
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}

void HC595::write(uint8_t data) {
digitalWrite(latchPin, LOW);
delayMicroseconds(delayInMicroseconds);
for (int i = 7; i >= 0; i--) {
digitalWrite(clockPin, LOW);
delayMicroseconds(clockPulseDelay);
if ((data >> i) & 1) {
digitalWrite(dataPin, HIGH);
} else {
digitalWrite(dataPin, LOW);
}
digitalWrite(clockPin, HIGH);
delayMicroseconds(clockPulseDelay);
}
digitalWrite(latchPin, HIGH);
delayMicroseconds(delayInMicroseconds);
}

HC165::HC165(int clockPin, int dataPin, int latchPin)
: clockPin(clockPin), dataPin(dataPin), latchPin(latchPin) {}

void HC165::init() {
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
pinMode(latchPin, OUTPUT);
}

uint8_t HC165::read() {
digitalWrite(latchPin, LOW);
delayMicroseconds(delayInMicroseconds);
digitalWrite(latchPin, HIGH);
delayMicroseconds(delayInMicroseconds);

uint8_t value = 0;
for (int i = 0; i < 8; i++) {
byte bitValue = digitalRead(dataPin);
delayMicroseconds(clockPulseDelay);

value |= (bitValue << (7 - i));

digitalWrite(clockPin, HIGH);
delayMicroseconds(clockPulseDelay);
digitalWrite(clockPin, LOW);
delayMicroseconds(clockPulseDelay);
}

return value;
}

0 comments on commit 8f3f0be

Please sign in to comment.