2013-08-28 16:30:00 -04:00
|
|
|
//
|
|
|
|
// FILE: FastShiftOut.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-11-01 07:28:08 -04:00
|
|
|
// VERSION: 0.3.2
|
2021-12-17 09:24:28 -05:00
|
|
|
// PURPOSE: ShiftOut that implements the Print interface
|
2013-09-29 06:38:05 -04:00
|
|
|
// DATE: 2013-08-22
|
2020-11-27 05:16:22 -05:00
|
|
|
// URL: https://github.com/RobTillaart/FastShiftOut
|
2013-08-28 16:30:00 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2013-08-28 16:30:00 -04:00
|
|
|
#include "FastShiftOut.h"
|
|
|
|
|
2021-05-28 07:26:27 -04:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
FastShiftOut::FastShiftOut(uint8_t dataOut, uint8_t clockPin, uint8_t bitOrder)
|
2013-08-28 16:30:00 -04:00
|
|
|
{
|
2022-11-06 14:23:19 -05:00
|
|
|
_bitOrder = bitOrder;
|
|
|
|
pinMode(dataOut, OUTPUT);
|
|
|
|
pinMode(clockPin, OUTPUT);
|
2022-11-05 07:39:40 -04:00
|
|
|
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/
|
2022-11-06 14:23:19 -05:00
|
|
|
digitalWrite(clockPin, LOW); // assume rising pulses from clock
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
uint8_t _port = digitalPinToPort(dataOut);
|
|
|
|
_dataOutRegister = portOutputRegister(_port);
|
|
|
|
_dataOutBit = digitalPinToBitMask(dataOut);
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
_port = digitalPinToPort(clockPin);
|
|
|
|
_clockRegister = portOutputRegister(_port);
|
|
|
|
_clockBit = digitalPinToBitMask(clockPin);
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-11-05 07:39:40 -04:00
|
|
|
#else // reference implementation
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
_dataPinOut = dataOut;
|
2023-02-20 11:37:06 -05:00
|
|
|
_clockPin = clockPin;
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
#endif
|
2013-08-28 16:30:00 -04:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:26:27 -04:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
size_t FastShiftOut::write(uint8_t data)
|
2013-08-28 16:30:00 -04:00
|
|
|
{
|
2022-11-06 14:23:19 -05:00
|
|
|
if (_bitOrder == LSBFIRST)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
|
|
|
return writeLSBFIRST(data);
|
|
|
|
}
|
|
|
|
return writeMSBFIRST(data);
|
2013-08-28 16:30:00 -04:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:26:27 -04:00
|
|
|
|
2023-02-20 11:37:06 -05:00
|
|
|
/* experimental
|
|
|
|
size_t write(const uint8_t \*buffer, size_t size)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
if (_bitOrder == LSBFIRST)
|
|
|
|
{
|
|
|
|
for (size_t i = size; i > 0; ) // from end to begin ????
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
n += writeLSBFIRST(buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; i++) // from begin to end..
|
|
|
|
{
|
|
|
|
n += writeMSBFIRST(buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
size_t FastShiftOut::writeLSBFIRST(uint8_t data)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
2022-11-06 14:23:19 -05:00
|
|
|
uint8_t value = data;
|
2023-02-20 11:37:06 -05:00
|
|
|
_lastValue = value;
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
2015-03-06 09:37:48 -05:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
uint8_t cbmask1 = _clockBit;
|
|
|
|
uint8_t cbmask2 = ~_clockBit;
|
|
|
|
uint8_t outmask1 = _dataOutBit;
|
|
|
|
uint8_t outmask2 = ~_dataOutBit;
|
|
|
|
|
|
|
|
for (uint8_t m = 1; m > 0; m <<= 1)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
|
|
|
uint8_t oldSREG = SREG;
|
2021-01-29 06:31:58 -05:00
|
|
|
noInterrupts();
|
2022-11-06 14:23:19 -05:00
|
|
|
if ((value & m) == 0) *_dataOutRegister &= outmask2;
|
|
|
|
else *_dataOutRegister |= outmask1;
|
|
|
|
*_clockRegister |= cbmask1;
|
|
|
|
*_clockRegister &= cbmask2;
|
2020-11-27 05:16:22 -05:00
|
|
|
SREG = oldSREG;
|
|
|
|
}
|
2022-11-06 14:23:19 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#else
|
2022-11-06 14:23:19 -05:00
|
|
|
|
|
|
|
shiftOut(_dataPinOut, _clockPin, LSBFIRST, value);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#endif
|
2022-11-06 14:23:19 -05:00
|
|
|
|
|
|
|
return 1;
|
2020-11-27 05:16:22 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:26:27 -04:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
size_t FastShiftOut::writeMSBFIRST(uint8_t data)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
2022-11-06 14:23:19 -05:00
|
|
|
uint8_t value = data;
|
2023-02-20 11:37:06 -05:00
|
|
|
_lastValue = value;
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
uint8_t cbmask1 = _clockBit;
|
|
|
|
uint8_t cbmask2 = ~_clockBit;
|
|
|
|
uint8_t outmask1 = _dataOutBit;
|
|
|
|
uint8_t outmask2 = ~_dataOutBit;
|
|
|
|
|
|
|
|
for (uint8_t m = 0x80; m > 0; m >>= 1)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
|
|
|
uint8_t oldSREG = SREG;
|
2021-01-29 06:31:58 -05:00
|
|
|
noInterrupts();
|
2022-11-06 14:23:19 -05:00
|
|
|
if ((value & m) == 0) *_dataOutRegister &= outmask2;
|
|
|
|
else *_dataOutRegister |= outmask1;
|
|
|
|
*_clockRegister |= cbmask1;
|
|
|
|
*_clockRegister &= cbmask2;
|
2020-11-27 05:16:22 -05:00
|
|
|
SREG = oldSREG;
|
|
|
|
}
|
2022-11-06 14:23:19 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
shiftOut(_dataPinOut, _clockPin, MSBFIRST, value);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#endif
|
2022-11-06 14:23:19 -05:00
|
|
|
|
|
|
|
return 1;
|
2013-08-28 16:30:00 -04:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:26:27 -04:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
uint8_t FastShiftOut::lastWritten(void)
|
|
|
|
{
|
2023-02-20 11:37:06 -05:00
|
|
|
return _lastValue;
|
|
|
|
}
|
2022-11-06 14:23:19 -05:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
bool FastShiftOut::setBitOrder(const uint8_t bitOrder)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
|
|
|
|
{
|
2022-11-06 14:23:19 -05:00
|
|
|
_bitOrder = bitOrder;
|
2021-01-29 06:31:58 -05:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
return false;
|
2020-11-27 05:16:22 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:26:27 -04:00
|
|
|
|
2022-11-06 14:23:19 -05:00
|
|
|
uint8_t FastShiftOut::getBitOrder(void)
|
|
|
|
{
|
|
|
|
return _bitOrder;
|
2023-02-20 11:37:06 -05:00
|
|
|
}
|
2022-11-06 14:23:19 -05:00
|
|
|
|
|
|
|
|
2023-02-20 11:37:06 -05:00
|
|
|
// -- END OF FILE --
|
2022-11-06 14:23:19 -05:00
|
|
|
|