-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShiftOutSlow.cpp
97 lines (76 loc) · 1.76 KB
/
ShiftOutSlow.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// FILE: ShiftOutSlow.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// PURPOSE: Arduino library for shiftOut with build-in delay
// DATE: 2021-05-11
// URL: https://github.com/RobTillaart/ShiftOutSlow
#include "ShiftOutSlow.h"
ShiftOutSlow::ShiftOutSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder)
{
_clockPin = clockPin;
_dataPin = dataPin;
_bitOrder = bitOrder;
_value = 0;
pinMode(_dataPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
digitalWrite(_clockPin, LOW); // assume rising pulses from clock
}
size_t ShiftOutSlow::write(const uint8_t data)
{
uint8_t val = data;
uint16_t d1 = _delay/2;
uint16_t d2 = _delay - d1;
for (uint8_t i = 0; i < 8; ++i)
{
if (d1) delayMicroseconds(d1);
if (_bitOrder == LSBFIRST) {
digitalWrite(_dataPin, val & 0x01);
val >>= 1;
} else {
digitalWrite(_dataPin, (val & 0x80) != 0);
val <<= 1;
}
digitalWrite(_clockPin, HIGH);
if (d2) delayMicroseconds(d2);
yield();
digitalWrite(_clockPin, LOW);
}
_value = data;
return 1;
}
size_t ShiftOutSlow::write(const uint8_t *buffer, size_t size)
{
for (uint8_t i = 0; i < size; ++i)
{
write(buffer[i]);
}
return size;
}
uint8_t ShiftOutSlow::lastWritten()
{
return _value;
}
bool ShiftOutSlow::setBitOrder(const uint8_t bitOrder)
{
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
{
_bitOrder = bitOrder;
return true;
};
return false;
}
uint8_t ShiftOutSlow::getBitOrder()
{
return _bitOrder;
}
void ShiftOutSlow::setDelay(uint16_t microSeconds)
{
_delay = microSeconds;
}
uint16_t ShiftOutSlow::getDelay()
{
return _delay;
}
// -- END OF FILE --