-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShiftInSlow.cpp
81 lines (62 loc) · 1.48 KB
/
ShiftInSlow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// FILE: ShiftInSlow.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// PURPOSE: Arduino library for shiftIn with build-in delay
// DATE: 2021-05-11
// URL: https://github.com/RobTillaart/ShiftInSlow
#include "ShiftInSlow.h"
ShiftInSlow::ShiftInSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder)
{
_clockPin = clockPin;
_dataPin = dataPin;
_bitOrder = bitOrder;
_value = 0;
pinMode(_dataPin, INPUT);
pinMode(_clockPin, OUTPUT);
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
digitalWrite(_clockPin, LOW); // assume rising pulses from clock
}
int ShiftInSlow::read()
{
_value = 0;
for (uint8_t i = 0; i < 8; ++i)
{
digitalWrite(_clockPin, HIGH);
if (_delay > 0) delayMicroseconds(_delay / 2);
yield();
if (_bitOrder == LSBFIRST)
_value |= digitalRead(_dataPin) << i;
else
_value |= digitalRead(_dataPin) << (7 - i);
digitalWrite(_clockPin, LOW);
if (_delay > 0) delayMicroseconds(_delay / 2);
}
return _value;
}
int ShiftInSlow::lastRead()
{
return _value;
}
bool ShiftInSlow::setBitOrder(const uint8_t bitOrder)
{
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
{
_bitOrder = bitOrder;
return true;
};
return false;
}
uint8_t ShiftInSlow::getBitOrder()
{
return _bitOrder;
}
void ShiftInSlow::setDelay(uint16_t microseconds)
{
_delay = microseconds;
}
uint16_t ShiftInSlow::getDelay()
{
return _delay;
}
// -- END OF FILE --