Skip to content

Commit 348f6cc

Browse files
committed
0.1.1 shiftOutSlow
1 parent 50c8a2e commit 348f6cc

File tree

7 files changed

+184
-33
lines changed

7 files changed

+184
-33
lines changed

libraries/shiftOutSlow/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,54 @@ A library for shiftInSlow also exist.
1212
## Description
1313

1414

15-
ShiftOutSlow is an experimental library that has a build in delay (in microseconds) that allows tuning the time per bit.
15+
ShiftOutSlow is an experimental library that has a build in delay (in microseconds) that allows tuning the time per bit.
1616
This allows one to improve reliability e.g. when using longer lines.
1717

18-
The datapin and clockpin are set in the constructor, the delay is settable per byte send to be able to optimize runtime.
18+
The data pin and clock pin are set in the constructor, the delay can be set per byte send to be able to optimize runtime.
1919

2020
ShiftOutSlow implements the print interface.
2121

2222

2323
## Performance
2424

25-
The performance of **write()** with a delay of 0 microseconds is slower than the default Arduino
26-
**shiftOut()** due to some overhead.
25+
The performance of **write()** with a delay of 0 microseconds is slower than the default Arduino
26+
**shiftOut()** due to some overhead.
2727

28-
The delay requested is split in two (expect rounding errors) to have "nice" looking pulses.
28+
The delay requested is divided by two to minimize disruption of the duty cycle of the clock pulse,
29+
resulting in "better" pulses.
2930

3031

3132
## Interface
3233

3334
The interface exists of the following functions:
34-
- **ShiftOutSlow(datapin, clockpin, bitorder = LSBFIRST)** constructor.
35-
- **size_t write(uint8_t data)** writes a new value
36-
- **uint8_t lastWritten()** returns last value written
37-
- **void setDelay(uint16_t microseconds)** set delay per bit from 0 .. 65535 microseconds.
35+
36+
- **ShiftOutSlow(dataPin, clockPin, bitOrder = LSBFIRST)** constructor.
37+
- **size_t write(uint8_t data)** writes a new value. Returns the bytes written.
38+
- **size_t write(const uint8_t \*buffer, size_t size)** writes an array of size over shift out. Uses **write(uint8_t)** so expect about equal performance. Returns the bytes written.
39+
- **uint8_t lastWritten()** returns last value written.
40+
- **void setDelay(uint16_t microSeconds)** set delay per bit from 0 .. 65535 microseconds.
41+
Note that the delay is not the time per bit but an additional time per bit.
42+
Note: the delay can be set runtime per write / print call.
3843
- **uint16_t getDelay()** returns the set delay in microseconds.
3944
- **bool setBitOrder(bitOrder)** set LSBFIRST or MSBFIRST. Returns false for other values.
40-
- **uint8_t getBitOrder(void)** returns LSBFIRST or MSBFIRST
45+
Note: bitorder can be changed runtime per write / print call.
46+
- **uint8_t getBitOrder(void)** returns LSBFIRST or MSBFIRST (typical 0 and 1).
47+
4148

49+
### Print interface
4250

43-
## Notes
51+
As this library implements the print interface one can use:
4452

45-
- to be tested
53+
- **size_t print(any)** print any data type.
54+
- **size_t println(any)** print any data type followed by a newline "\n".
4655

4756

4857
## Operation
4958

50-
See examples
59+
See examples.
60+
61+
62+
## Future
63+
64+
- Add a select pin to be more SPI alike?
5165

libraries/shiftOutSlow/ShiftOutSlow.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
//
22
// FILE: ShiftOutSlow.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// PURPOSE: Arduino library for shiftOut with build-in delay
66
// DATE: 2021-05-11
77
// URL: https://github.com/RobTillaart/ShiftOutSlow
88

9+
// HISTORY
10+
// 0.1.0 2021-05-11 initial version
11+
// 0.1.1 2021-08-16 add write(buffer, size) = print interface.
12+
// improve delay math; update readme.md
13+
914

1015
#include "ShiftOutSlow.h"
1116

1217

1318
ShiftOutSlow::ShiftOutSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder)
1419
{
1520
_clockPin = clockPin;
16-
_dataPin = dataPin;
21+
_dataPin = dataPin;
1722
_bitOrder = bitOrder;
1823
_value = 0;
1924
pinMode(_dataPin, OUTPUT);
2025
pinMode(_clockPin, OUTPUT);
2126
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
22-
digitalWrite(_clockPin, LOW); // assume rising pulses from clock
27+
digitalWrite(_clockPin, LOW); // assume rising pulses from clock
2328
}
2429

2530

2631
size_t ShiftOutSlow::write(const uint8_t data)
2732
{
28-
uint8_t val = data;
33+
uint8_t val = data;
34+
uint16_t d1 = _delay/2;
35+
uint16_t d2 = _delay - d1;
2936
for (uint8_t i = 0; i < 8; ++i)
3037
{
31-
if (_delay > 0) delayMicroseconds(_delay/2);
38+
if (d1) delayMicroseconds(d1);
3239
if (_bitOrder == LSBFIRST) {
3340
digitalWrite(_dataPin, val & 0x01);
3441
val >>= 1;
@@ -37,7 +44,7 @@ size_t ShiftOutSlow::write(const uint8_t data)
3744
val <<= 1;
3845
}
3946
digitalWrite(_clockPin, HIGH);
40-
if (_delay > 0) delayMicroseconds(_delay/2);
47+
if (d2) delayMicroseconds(d2);
4148
yield();
4249
digitalWrite(_clockPin, LOW);
4350
}
@@ -46,11 +53,21 @@ size_t ShiftOutSlow::write(const uint8_t data)
4653
}
4754

4855

56+
size_t ShiftOutSlow::write(const uint8_t *buffer, size_t size)
57+
{
58+
for (uint8_t i = 0; i < size; ++i)
59+
{
60+
write(buffer[i]);
61+
}
62+
return size;
63+
}
64+
65+
4966
bool ShiftOutSlow::setBitOrder(const uint8_t bitOrder)
5067
{
5168
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
5269
{
53-
_bitOrder = bitOrder;
70+
_bitOrder = bitOrder;
5471
return true;
5572
};
5673
return false;

libraries/shiftOutSlow/ShiftOutSlow.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: ShiftOutSlow.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.0
5+
// VERSION: 0.1.1
66
// PURPOSE: Arduino library for shiftOut with build-in delay
77
// DATE: 2021-05-11
88
// URL: https://github.com/RobTillaart/ShiftOutSlow
@@ -12,7 +12,7 @@
1212
#include "Arduino.h"
1313

1414

15-
#define SHIFTOUTSLOW_LIB_VERSION (F("0.1.0"))
15+
#define SHIFTOUTSLOW_LIB_VERSION (F("0.1.1"))
1616

1717

1818
class ShiftOutSlow : public Print
@@ -21,13 +21,17 @@ class ShiftOutSlow : public Print
2121
// bitorder = { LSBFIRST, MSBFIRST };
2222
ShiftOutSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder = LSBFIRST);
2323

24-
size_t write(const uint8_t data);
24+
// PRINT INTERFACE
25+
virtual size_t write(const uint8_t data) override;
26+
virtual size_t write(const uint8_t *buffer, size_t size) override;
27+
2528
uint8_t lastWritten(void) { return _value; };
2629

2730
bool setBitOrder(const uint8_t bitOrder);
2831
uint8_t getBitOrder(void) { return _bitOrder; };
29-
30-
void setDelay(uint16_t d) { _delay = d; };
32+
33+
// microSeconds = 0..65535
34+
void setDelay(uint16_t microSeconds) { _delay = microSeconds; };
3135
uint16_t getDelay() { return _delay; };
3236

3337

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// FILE: shiftOutSlow_print.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test sketch for print interface
6+
// URL: https://github.com/RobTillaart/ShiftOutSlow
7+
//
8+
9+
10+
#include "ShiftOutSlow.h"
11+
12+
13+
ShiftOutSlow SOS(12, 13, LSBFIRST);
14+
15+
volatile int x = 0;
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println(__FILE__);
21+
Serial.println(SHIFTOUTSLOW_LIB_VERSION);
22+
23+
Serial.println("\nwrite(byte)");
24+
Serial.println("delay\tus/byte");
25+
delay(10); // extra time to flush serial buffer
26+
for (uint16_t d = 0; d < 100; d += 10)
27+
{
28+
SOS.setDelay(d);
29+
uint32_t start = micros();
30+
x = SOS.write(0x55);
31+
uint32_t stop = micros();
32+
float duration = stop - start;
33+
Serial.print(d);
34+
Serial.print("\t");
35+
Serial.println(duration / x, 2);
36+
delay(10);
37+
}
38+
39+
char str[24] = "hello world!";
40+
Serial.println("\nwrite(buffer, size)");
41+
Serial.println("delay\tus/byte");
42+
delay(10);
43+
for (uint16_t d = 0; d < 100; d += 10)
44+
{
45+
SOS.setDelay(d);
46+
uint32_t start = micros();
47+
x = SOS.write((const uint8_t*) str, 12);
48+
uint32_t stop = micros();
49+
float duration = stop - start;
50+
Serial.print(d);
51+
Serial.print("\t");
52+
Serial.println(duration / x, 2);
53+
delay(10);
54+
}
55+
56+
Serial.println("\nprint(str)");
57+
Serial.println("delay\tus/byte");
58+
delay(10);
59+
for (uint16_t d = 0; d < 100; d += 10)
60+
{
61+
SOS.setDelay(d);
62+
uint32_t start = micros();
63+
x = SOS.print(str);
64+
uint32_t stop = micros();
65+
float duration = stop - start;
66+
Serial.print(d);
67+
Serial.print("\t");
68+
Serial.println(duration / x, 2);
69+
delay(10);
70+
}
71+
72+
Serial.println("\nprintln(str)");
73+
Serial.println("delay\tus/byte");
74+
delay(10);
75+
for (uint16_t d = 0; d < 100; d += 10)
76+
{
77+
SOS.setDelay(d);
78+
uint32_t start = micros();
79+
x = SOS.println(str);
80+
uint32_t stop = micros();
81+
float duration = stop - start;
82+
Serial.print(d);
83+
Serial.print("\t");
84+
Serial.println(duration / x, 2);
85+
delay(10);
86+
}
87+
88+
89+
Serial.println(LSBFIRST);
90+
Serial.println(MSBFIRST);
91+
Serial.println("done...");
92+
}
93+
94+
void loop()
95+
{
96+
}
97+
98+
// -- END OF FILE --

libraries/shiftOutSlow/library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ShiftOutSlow",
33
"keywords": "Shift, shiftOut, out, serial, slow, 74HC595",
4-
"description": "Arduino library for shiftOut with build-in delay - e.g. for 74HC595",
4+
"description": "Arduino library for shiftOut with build-in delay - e.g. for 74HC595.\nImplements the print interface.",
55
"authors":
66
[
77
{
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/ShiftOutSlow.git"
1717
},
18-
"version": "0.1.0",
18+
"version": "0.1.1",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*"

libraries/shiftOutSlow/library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=ShiftOutSlow
2-
version=0.1.0
2+
version=0.1.1
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for shiftOut with build-in delay - e.g. for 74HC165
6-
paragraph=
6+
paragraph=implements the print interface
77
category=Signal Input/Output
88
url=https://github.com/RobTillaart/ShiftOutSlow
99
architectures=*

libraries/shiftOutSlow/test/unit_test_001.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// PATCH FOR DUE & ZERO FOR UNIT TEST - https://github.com/Arduino-CI/arduino_ci/issues/252
3232
#if defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD)
3333
// - due # ARDUINO_ARCH_SAM does not support shiftIn apparently
34-
// - zero # ARDUINO_ARCH_SAMD
34+
// - zero # ARDUINO_ARCH_SAMD
3535
#endif
3636

3737

@@ -52,7 +52,7 @@ unittest(test_constructor)
5252
assertEqual(1, SOS.write(65));
5353
assertEqual(65, SOS.lastWritten());
5454
assertEqual(LSBFIRST, SOS.getBitOrder());
55-
55+
5656
SOS.setBitOrder(MSBFIRST);
5757
assertEqual(MSBFIRST, SOS.getBitOrder());
5858
}
@@ -66,7 +66,7 @@ unittest(test_constructor_LSB)
6666
assertEqual(1, SOS.write(65));
6767
assertEqual(65, SOS.lastWritten());
6868
assertEqual(LSBFIRST, SOS.getBitOrder());
69-
69+
7070
SOS.setBitOrder(MSBFIRST);
7171
assertEqual(MSBFIRST, SOS.getBitOrder());
7272
}
@@ -80,7 +80,7 @@ unittest(test_constructor_MSB)
8080
assertEqual(1, SOS.write(65));
8181
assertEqual(65, SOS.lastWritten());
8282
assertEqual(MSBFIRST, SOS.getBitOrder());
83-
83+
8484
SOS.setBitOrder(LSBFIRST);
8585
assertEqual(LSBFIRST, SOS.getBitOrder());
8686
}
@@ -98,6 +98,24 @@ unittest(test_setDelay)
9898
}
9999
}
100100

101+
102+
unittest(test_print_interface)
103+
{
104+
ShiftOutSlow SOS(12, 13);
105+
106+
fprintf(stderr, "VERSION:\t%s\n", SHIFTOUTSLOW_LIB_VERSION);
107+
int x = SOS.print("hello world");
108+
assertEqual(11, x);
109+
110+
int y = SOS.println("hello world");
111+
assertEqual(13, y);
112+
113+
char str[20] = "hello world";
114+
// casting needed for CI environment.
115+
int z = SOS.write((const uint8_t*) str, 8);
116+
assertEqual(8, z);
117+
}
118+
101119
unittest_main()
102120

103121
// --------

0 commit comments

Comments
 (0)