GY-63_MS5611/libraries/FastShiftOut/FastShiftOut.cpp

122 lines
2.9 KiB
C++
Raw Normal View History

2013-08-28 16:30:00 -04:00
//
// FILE: FastShiftOut.cpp
// AUTHOR: Rob Tillaart
2021-12-17 09:24:28 -05:00
// VERSION: 0.2.4
// PURPOSE: ShiftOut that implements the Print interface
// 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
FastShiftOut::FastShiftOut(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
2013-08-28 16:30:00 -04:00
{
2020-11-27 05:16:22 -05:00
_bitorder = bitOrder;
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
2021-01-29 06:31:58 -05:00
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/
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)
// uint8_t _datatimer = digitalPinToTimer(datapin);
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer); TODO
uint8_t _dataport = digitalPinToPort(datapin);
_dataout = portOutputRegister(_dataport);
_databit = digitalPinToBitMask(datapin);
// uint8_t _clocktimer = digitalPinToTimer(clockpin);
// if (_clocktimer != NOT_ON_TIMER) turnOffPWM(_clocktimer);
uint8_t _clockport = digitalPinToPort(clockpin);
_clockout = portOutputRegister(_clockport);
_clockbit = digitalPinToBitMask(clockpin);
#else // reference implementation
// reuse these vars as pin to save some space
_databit = datapin;
_clockbit = clockpin;
#endif
2013-08-28 16:30:00 -04:00
}
2021-05-28 07:26:27 -04:00
size_t FastShiftOut::write(const uint8_t data)
2013-08-28 16:30:00 -04:00
{
2021-01-29 06:31:58 -05:00
_value = data;
2020-11-27 05:16:22 -05:00
if (_bitorder == LSBFIRST)
{
return writeLSBFIRST(data);
}
return writeMSBFIRST(data);
2013-08-28 16:30:00 -04:00
}
2021-05-28 07:26:27 -04:00
2020-11-27 05:16:22 -05:00
size_t FastShiftOut::writeLSBFIRST(const uint8_t data)
{
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
uint8_t cbmask1 = _clockbit;
uint8_t cbmask2 = ~_clockbit;
uint8_t dbmask1 = _databit;
uint8_t dbmask2 = ~_databit;
2020-11-27 05:16:22 -05:00
for (uint8_t i = 0, m = 1; i < 8; i++)
{
uint8_t oldSREG = SREG;
2021-01-29 06:31:58 -05:00
noInterrupts();
2020-11-27 05:16:22 -05:00
if ((data & m) == 0) *_dataout &= dbmask2;
else *_dataout |= dbmask1;
*_clockout |= cbmask1;
*_clockout &= cbmask2;
SREG = oldSREG;
m <<= 1;
}
return 1;
2021-01-29 06:31:58 -05:00
#else
shiftOut(_databit, _clockbit, LSBFIRST, data);
return 1;
#endif
2020-11-27 05:16:22 -05:00
}
2021-05-28 07:26:27 -04:00
2020-11-27 05:16:22 -05:00
size_t FastShiftOut::writeMSBFIRST(const uint8_t data)
{
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
uint8_t cbmask1 = _clockbit;
uint8_t cbmask2 = ~_clockbit;
uint8_t dbmask1 = _databit;
uint8_t dbmask2 = ~_databit;
for (uint8_t i = 0, n = 128; i < 8; i++)
{
uint8_t oldSREG = SREG;
2021-01-29 06:31:58 -05:00
noInterrupts();
2020-11-27 05:16:22 -05:00
if ((data & n) == 0) *_dataout &= dbmask2;
else *_dataout |= dbmask1;
*_clockout |= cbmask1;
*_clockout &= cbmask2;
SREG = oldSREG;
n >>= 1;
}
return 1;
#else // reference implementation // note this has no cli()
2021-01-29 06:31:58 -05:00
shiftOut(_databit, _clockbit, MSBFIRST, data);
return 1;
#endif
2013-08-28 16:30:00 -04:00
}
2021-05-28 07:26:27 -04: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))
{
_bitorder = bitOrder;
return true;
};
return false;
2020-11-27 05:16:22 -05:00
}
2021-05-28 07:26:27 -04:00
2021-01-29 06:31:58 -05:00
// -- END OF FILE --