GY-63_MS5611/libraries/shiftOutSlow/ShiftOutSlow.cpp

74 lines
1.5 KiB
C++
Raw Normal View History

2021-05-30 08:25:53 -04:00
//
// FILE: ShiftOutSlow.cpp
// AUTHOR: Rob Tillaart
2022-11-24 06:51:14 -05:00
// VERSION: 0.1.3
2021-05-30 08:25:53 -04:00
// PURPOSE: Arduino library for shiftOut with build-in delay
// DATE: 2021-05-11
// URL: https://github.com/RobTillaart/ShiftOutSlow
2021-08-16 13:25:37 -04:00
2021-05-30 08:25:53 -04:00
#include "ShiftOutSlow.h"
ShiftOutSlow::ShiftOutSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder)
{
_clockPin = clockPin;
2021-08-16 13:25:37 -04:00
_dataPin = dataPin;
2021-05-30 08:25:53 -04:00
_bitOrder = bitOrder;
_value = 0;
pinMode(_dataPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
2022-11-24 06:51:14 -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:53 -04:00
}
size_t ShiftOutSlow::write(const uint8_t data)
{
2021-08-16 13:25:37 -04:00
uint8_t val = data;
uint16_t d1 = _delay/2;
uint16_t d2 = _delay - d1;
2021-05-30 08:25:53 -04:00
for (uint8_t i = 0; i < 8; ++i)
{
2021-08-16 13:25:37 -04:00
if (d1) delayMicroseconds(d1);
2021-05-30 08:25:53 -04:00
if (_bitOrder == LSBFIRST) {
digitalWrite(_dataPin, val & 0x01);
val >>= 1;
} else {
digitalWrite(_dataPin, (val & 0x80) != 0);
val <<= 1;
}
digitalWrite(_clockPin, HIGH);
2021-08-16 13:25:37 -04:00
if (d2) delayMicroseconds(d2);
2021-05-30 08:25:53 -04:00
yield();
digitalWrite(_clockPin, LOW);
}
_value = data;
return 1;
}
2021-08-16 13:25:37 -04:00
size_t ShiftOutSlow::write(const uint8_t *buffer, size_t size)
{
for (uint8_t i = 0; i < size; ++i)
{
write(buffer[i]);
}
return size;
}
2021-05-30 08:25:53 -04:00
bool ShiftOutSlow::setBitOrder(const uint8_t bitOrder)
{
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
{
2021-08-16 13:25:37 -04:00
_bitOrder = bitOrder;
2021-05-30 08:25:53 -04:00
return true;
};
return false;
}
2021-12-28 05:53:42 -05:00
2022-11-24 06:51:14 -05:00
// -- END OF FILE --
2021-12-28 05:53:42 -05:00