2021-05-30 08:25:11 -04:00
|
|
|
//
|
|
|
|
// FILE: ShiftInSlow.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-11-22 04:23:18 -05:00
|
|
|
// VERSION: 0.1.4
|
2021-05-30 08:25:11 -04:00
|
|
|
// PURPOSE: Arduino library for shiftIn with build-in delay
|
|
|
|
// DATE: 2021-05-11
|
|
|
|
// URL: https://github.com/RobTillaart/ShiftInSlow
|
2022-11-24 06:49:27 -05:00
|
|
|
|
2021-05-30 08:25:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "ShiftInSlow.h"
|
|
|
|
|
|
|
|
|
|
|
|
ShiftInSlow::ShiftInSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder)
|
|
|
|
{
|
|
|
|
_clockPin = clockPin;
|
2023-02-21 07:11:46 -05:00
|
|
|
_dataPin = dataPin;
|
2021-05-30 08:25:11 -04:00
|
|
|
_bitOrder = bitOrder;
|
|
|
|
_value = 0;
|
|
|
|
pinMode(_dataPin, INPUT);
|
|
|
|
pinMode(_clockPin, OUTPUT);
|
2022-11-24 06:49:27 -05:00
|
|
|
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
|
|
|
|
digitalWrite(_clockPin, LOW); // assume rising pulses from clock
|
2021-05-30 08:25:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ShiftInSlow::read()
|
|
|
|
{
|
|
|
|
_value = 0;
|
|
|
|
for (uint8_t i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
digitalWrite(_clockPin, HIGH);
|
2023-02-21 07:11:46 -05:00
|
|
|
if (_delay > 0) delayMicroseconds(_delay / 2);
|
2021-05-30 08:25:11 -04:00
|
|
|
yield();
|
|
|
|
if (_bitOrder == LSBFIRST)
|
|
|
|
_value |= digitalRead(_dataPin) << i;
|
|
|
|
else
|
|
|
|
_value |= digitalRead(_dataPin) << (7 - i);
|
|
|
|
digitalWrite(_clockPin, LOW);
|
2023-02-21 07:11:46 -05:00
|
|
|
if (_delay > 0) delayMicroseconds(_delay / 2);
|
2021-05-30 08:25:11 -04:00
|
|
|
}
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-21 07:11:46 -05:00
|
|
|
int ShiftInSlow::lastRead()
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-30 08:25:11 -04:00
|
|
|
bool ShiftInSlow::setBitOrder(const uint8_t bitOrder)
|
|
|
|
{
|
|
|
|
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
|
|
|
|
{
|
2021-12-28 05:35:20 -05:00
|
|
|
_bitOrder = bitOrder;
|
2021-05-30 08:25:11 -04:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-21 07:11:46 -05:00
|
|
|
uint8_t ShiftInSlow::getBitOrder()
|
|
|
|
{
|
|
|
|
return _bitOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ShiftInSlow::setDelay(uint16_t microseconds)
|
|
|
|
{
|
|
|
|
_delay = microseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t ShiftInSlow::getDelay()
|
|
|
|
{
|
|
|
|
return _delay;
|
|
|
|
}
|
|
|
|
|
2021-12-28 05:35:20 -05:00
|
|
|
|
2022-11-24 06:49:27 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-28 05:35:20 -05:00
|
|
|
|