Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
lathoub committed Nov 26, 2020
1 parent e6aa49b commit 2d22dcf
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 87 deletions.
11 changes: 6 additions & 5 deletions src/NativeDigitalIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class NativeDigitalIO
::pinMode(pin, mode);
}

uint8_t read() {
return ::digitalRead(pin);
uint8_t read()
{
return ::digitalRead(pin);
};

void write(uint8_t value) {
::digitalWrite(pin, value);
void write(uint8_t value)
{
::digitalWrite(pin, value);
};

};
48 changes: 27 additions & 21 deletions src/ShiftIn.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
#pragma once

template <class Device, uint8_t totalChipCount>
class ShiftIn {
private:
uint8_t _buffer[totalChipCount];
class ShiftIn
{
private:
uint8_t _buffer[totalChipCount];
bool invert = true;

public:
ShiftIn(Device& device)
: _device(device)
{
}
public:
ShiftIn(Device &device)
: _device(device)
{
}

uint8_t* read(uint8_t chipCount = totalChipCount) {
uint8_t *read(uint8_t chipCount = totalChipCount)
{
_device.beginRead();

_device.beginRead();
for (auto i = 0; i < chipCount; i++)
_buffer[i] = _device.read();

for (auto i = 0; i < chipCount; i++)
_buffer[i] = _device.read();
_device.endRead();

_device.endRead();
if (invert)
for (auto i = 0; i < chipCount; i++)
_buffer[i] = ~_buffer[i];

return _buffer;
}
return _buffer;
}

private:
Device& _device;
private:
Device &_device;
};

#include <hardware/IC74HC165.h>

#define CREATE_NATIVE_74HC165(Name, LatchPin, DataPin, ClockPin, MaxChipCount) \
NativeDigitalIO<LatchPin, OUTPUT> latch##Name; \
NativeDigitalIO<DataPin, INPUT> data##Name; \
NativeDigitalIO<ClockPin, OUTPUT> clock##Name; \
#define CREATE_NATIVE_74HC165(Name, LatchPin, DataPin, ClockPin, MaxChipCount) \
NativeDigitalIO<LatchPin, OUTPUT> latch##Name; \
NativeDigitalIO<DataPin, INPUT> data##Name; \
NativeDigitalIO<ClockPin, OUTPUT> clock##Name; \
IC74HC165<NativeDigitalIO<LatchPin, OUTPUT>, NativeDigitalIO<DataPin, INPUT>, NativeDigitalIO<ClockPin, OUTPUT>> chip##Name(latch##Name, data##Name, clock##Name); \
ShiftIn<IC74HC165<NativeDigitalIO<LatchPin, OUTPUT>, NativeDigitalIO<DataPin, INPUT>, NativeDigitalIO<ClockPin, OUTPUT>>, MaxChipCount> Name(chip##Name);
1 change: 0 additions & 1 deletion src/ShiftInOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@

#include <NativeDigitalIO.h>


60 changes: 42 additions & 18 deletions src/ShiftOut.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
#pragma once

template <class Device, uint8_t totalChipCount>
class ShiftOut {
public:
ShiftOut(Device& device)
: _device(device)
{
}
class ShiftOut
{
private:
uint8_t _buffer[totalChipCount];
bool _autoCommit;

void write(uint8_t* buffer, uint8_t chipCount = totalChipCount) {
_device.beginWrite();
public:
ShiftOut(Device &device)
: _device(device)
{
}

for (int i = chipCount - 1; i >= 0; i--)
_device.write(buffer[i]);
void write(uint8_t *buffer, uint8_t chipCount = totalChipCount)
{
_device.beginWrite();

_device.endWrite();
}
for (int i = chipCount - 1; i >= 0; i--)
_device.write(buffer[i]);

private:
Device& _device;
_device.endWrite();
}

ShiftOut& setAll(bool onOff)
{
for (int i = totalChipCount - 1; i >= 0; i--)
_buffer[i] = (onOff) ? 0xFF : 0x00;
return static_cast<ShiftOut &>(*this);
}

ShiftOut& set(uint8_t bit, bool onOff)
{
bitWrite(_buffer[bit / 8], bit - (8 * (bit / 8)), onOff);
return static_cast<ShiftOut &>(*this);
}

void commit()
{
write(_buffer, totalChipCount);
}

private:
Device &_device;
};

#include <hardware/IC74HC595.h>

#define CREATE_NATIVE_74HC595(Name, LatchPin, DataPin, ClockPin, MaxChipCount) \
NativeDigitalIO<LatchPin, OUTPUT> latch##Name; \
NativeDigitalIO<DataPin, OUTPUT> data##Name; \
NativeDigitalIO<ClockPin, OUTPUT> clock##Name; \
#define CREATE_NATIVE_74HC595(Name, LatchPin, DataPin, ClockPin, MaxChipCount) \
NativeDigitalIO<LatchPin, OUTPUT> latch##Name; \
NativeDigitalIO<DataPin, OUTPUT> data##Name; \
NativeDigitalIO<ClockPin, OUTPUT> clock##Name; \
IC74HC595<NativeDigitalIO<LatchPin, OUTPUT>, NativeDigitalIO<DataPin, OUTPUT>, NativeDigitalIO<ClockPin, OUTPUT>> chip##Name(latch##Name, data##Name, clock##Name); \
ShiftOut<IC74HC595<NativeDigitalIO<LatchPin, OUTPUT>, NativeDigitalIO<DataPin, OUTPUT>, NativeDigitalIO<ClockPin, OUTPUT>>, MaxChipCount> Name(chip##Name);
31 changes: 16 additions & 15 deletions src/hardware/IC74HC165.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,42 @@ to be used in HIGH-to-LOW level shifting applications.

// The chip enable (/CE) pin is connected to ground as the chip may as well always be enabled

template<class PinLoad, class PinData, class PinClock>
class IC74HC165 {
template <class PinLoad, class PinData, class PinClock>
class IC74HC165
{
private:
// PinClockEnable& CE;
PinLoad& PL;
PinData& DS;
PinClock& CP; // SCK
// PinClockEnable& CE;
PinLoad &PL;
PinData &DS;
PinClock &CP; // SCK

uint8_t _pulseWidth; // in microseconds, default is 5

public:
// Constructor
IC74HC165(PinLoad& load, PinData& data, PinClock& clock)
: PL(load), DS(data), CP(clock),
IC74HC165(PinLoad &load, PinData &data, PinClock &clock)
: PL(load), DS(data), CP(clock),
_pulseWidth(5)
{
}

void beginRead()
{
// CE.write(HIGH);
// CE.write(HIGH);
PL.write(LOW);
delayMicroseconds(_pulseWidth);
PL.write(HIGH);
// CE.write(LOW);
// CE.write(LOW);
}

uint8_t read()
{
uint8_t result = 0;
uint8_t result = 0;

for (auto i = 0; i < 8; i++) {
for (auto i = 0; i < 8; i++)
{
auto value = DS.read();
result |= (value << ((8-1) - i));
result |= (value << ((8 - 1) - i));

CP.write(HIGH);
delayMicroseconds(_pulseWidth);
Expand All @@ -59,5 +61,4 @@ class IC74HC165 {
void endRead()
{
}

};
36 changes: 19 additions & 17 deletions src/hardware/IC74HC595.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
/*
*/

template<class PinLoad, class PinData, class PinClock>
class IC74HC595 {
template <class PinLoad, class PinData, class PinClock>
class IC74HC595
{
private:
PinLoad& PL;
PinData& DS;
PinClock& CP;
PinLoad &PL;
PinData &DS;
PinClock &CP;

public:
public:
// Constructor
IC74HC595(PinLoad& load, PinData& data, PinClock& clock)
: PL(load), DS(data), CP(clock)
IC74HC595(PinLoad &load, PinData &data, PinClock &clock)
: PL(load), DS(data), CP(clock)
{
}

Expand All @@ -26,24 +27,26 @@ class IC74HC595 {
{
int pinState;

for (auto i = 0; i < 8; i++)
for (auto i = 0; i < 8; i++)
{
CP.write(LOW);

//if the value passed to myDataOut and a bitmask result
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( value & (1<<i) ) {
pinState= 1;
if (value & (1 << i))
{
pinState = 1;
}
else {
pinState= 0;
else
{
pinState = 0;
}

//Sets the pin to HIGH or LOW depending on pinState
DS.write(pinState);
//register shifts bits on upstroke of clock pin
//register shifts bits on upstroke of clock pin
CP.write(HIGH);
//zero the data pin after shift to prevent bleed through
DS.write(LOW);
Expand All @@ -57,5 +60,4 @@ class IC74HC595 {
{
PL.write(HIGH);
}

};
20 changes: 10 additions & 10 deletions src/hardware/MCP23017.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
/*
*/

template<class PinLoad, class PinData, class PinClock>
class MCP23017 {
template <class PinLoad, class PinData, class PinClock>
class MCP23017
{
private:
PinLoad& PL;
PinData& DS;
PinClock& CP; // SCK
PinLoad &PL;
PinData &DS;
PinClock &CP; // SCK

uint8_t _pulseWidth; // in microseconds, default is 5

public:
// Constructor
MCP23017(PinLoad& load, PinData& data, PinClock& clock)
: PL(load), DS(data), CP(clock),
MCP23017(PinLoad &load, PinData &data, PinClock &clock)
: PL(load), DS(data), CP(clock),
_pulseWidth(5)
{
}
Expand All @@ -26,13 +27,12 @@ class MCP23017 {

uint16_t read()
{
uint16_t result = 0;
uint16_t result = 0;

return result;
}

void endRead()
{
}

};

0 comments on commit 2d22dcf

Please sign in to comment.