Skip to content

Commit cb023de

Browse files
authored
add read16() read24() read32() (#12)
- add (experimental) read16(), read24(), read32() functions. - changed return types of some functions. - update keywords.txt - update readme.md - update GitHub actions - update license 2023 - minor edits
1 parent 611e020 commit cb023de

File tree

10 files changed

+311
-58
lines changed

10 files changed

+311
-58
lines changed

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.2] - 2023-02-20
10+
- add (experimental) read16(), read24(), read32() functions.
11+
- changed return types of some functions.
12+
- update keywords.txt
13+
- update readme.md
14+
- update GitHub actions
15+
- update license 2023
16+
- minor edits
17+
18+
919
## [0.3.1] - 2022-11-06
1020
- redo clock pulse
1121

12-
1322
## [0.3.0] - 2022-11-05
1423
- fix inputRegister bug
1524
- refactor for code size
1625

26+
----
1727

1828
## [0.2.4] - 2022-11-05
1929
- add changelog.md
@@ -27,10 +37,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2737

2838
## [0.2.2] - 2020-12-22
2939

40+
----
3041

3142
## No history ...
3243

33-
3444
## [0.1.0 - 2013-09-29
3545
- initial version
3646

FastShiftIn.cpp

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: FastShiftIn.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.0
4+
// VERSION: 0.3.2
55
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
66
// DATE: 2013-09-29
77
// URL: https://github.com/RobTillaart/FastShiftIn
@@ -14,8 +14,8 @@
1414

1515
FastShiftIn::FastShiftIn(uint8_t dataIn, uint8_t clockPin, uint8_t bitOrder)
1616
{
17-
_bitOrder = bitOrder;
18-
_value = 0;
17+
_bitOrder = bitOrder;
18+
_lastValue = 0;
1919
pinMode(dataIn, INPUT);
2020
pinMode(clockPin, OUTPUT);
2121
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
@@ -41,8 +41,7 @@ FastShiftIn::FastShiftIn(uint8_t dataIn, uint8_t clockPin, uint8_t bitOrder)
4141
}
4242

4343

44-
45-
int FastShiftIn::read()
44+
uint16_t FastShiftIn::read()
4645
{
4746
if (_bitOrder == LSBFIRST)
4847
{
@@ -52,8 +51,65 @@ int FastShiftIn::read()
5251
}
5352

5453

54+
uint16_t FastShiftIn::read16()
55+
{
56+
uint16_t rv;
57+
if (_bitOrder == LSBFIRST)
58+
{
59+
rv = readLSBFIRST();
60+
rv += uint16_t(readLSBFIRST()) << 8;
61+
return rv;
62+
}
63+
rv = readMSBFIRST();
64+
rv <<= 8;
65+
rv += readMSBFIRST();
66+
return rv;
67+
}
68+
69+
70+
uint32_t FastShiftIn::read24()
71+
{
72+
uint32_t rv;
73+
if (_bitOrder == LSBFIRST)
74+
{
75+
rv = readLSBFIRST();
76+
rv += uint32_t(readLSBFIRST()) << 8;
77+
rv += uint32_t(readLSBFIRST()) << 16;
78+
return rv;
79+
}
80+
rv = readMSBFIRST();
81+
rv <<= 8;
82+
rv += readMSBFIRST();
83+
rv <<= 8;
84+
rv += readMSBFIRST();
85+
return rv;
86+
}
87+
88+
89+
uint32_t FastShiftIn::read32()
90+
{
91+
uint32_t rv;
92+
if (_bitOrder == LSBFIRST)
93+
{
94+
rv = readLSBFIRST();
95+
rv += uint32_t(readLSBFIRST()) << 8;
96+
rv += uint32_t(readLSBFIRST()) << 16;
97+
rv += uint32_t(readLSBFIRST()) << 24;
98+
return rv;
99+
}
100+
rv = readMSBFIRST();
101+
rv <<= 8;
102+
rv += readMSBFIRST();
103+
rv <<= 8;
104+
rv += readMSBFIRST();
105+
rv <<= 8;
106+
rv += readMSBFIRST();
107+
return rv;
108+
}
109+
110+
55111

56-
int FastShiftIn::readLSBFIRST()
112+
uint8_t FastShiftIn::readLSBFIRST()
57113
{
58114
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
59115

@@ -74,20 +130,20 @@ int FastShiftIn::readLSBFIRST()
74130
*_clockRegister &= cbmask2;
75131
SREG = oldSREG;
76132
}
77-
_value = rv;
133+
_lastValue = rv;
78134
return rv;
79135

80136
#else
81137

82-
// reference implementation
83-
_value = shiftIn(_dataPinIn, _clockPin, LSBFIRST);
84-
return _value;
138+
// reference implementation
139+
_lastValue = shiftIn(_dataPinIn, _clockPin, LSBFIRST);
140+
return _lastValue;
85141

86142
#endif
87143
}
88144

89145

90-
int FastShiftIn::readMSBFIRST()
146+
uint8_t FastShiftIn::readMSBFIRST()
91147
{
92148
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
93149

@@ -108,23 +164,23 @@ int FastShiftIn::readMSBFIRST()
108164
*_clockRegister &= cbmask2;
109165
SREG = oldSREG;
110166
}
111-
_value = rv;
167+
_lastValue = rv;
112168
return rv;
113169

114170
#else
115171

116-
// reference implementation
117-
_value = shiftIn(_dataPinIn, _clockPin, MSBFIRST);
118-
return _value;
172+
// reference implementation
173+
_lastValue = shiftIn(_dataPinIn, _clockPin, MSBFIRST);
174+
return _lastValue;
119175

120176
#endif
121177

122178
}
123179

124180

125-
int FastShiftIn::lastRead(void)
181+
uint32_t FastShiftIn::lastRead(void)
126182
{
127-
return _value;
183+
return _lastValue;
128184
};
129185

130186

@@ -145,5 +201,5 @@ uint8_t FastShiftIn::getBitOrder(void)
145201
};
146202

147203

148-
// -- END OF FILE --
204+
// -- END OF FILE --
149205

FastShiftIn.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: FastShiftIn.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.3.1
5+
// VERSION: 0.3.2
66
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
77
// DATE: 2013-09-29
88
// URL: https://github.com/RobTillaart/FastShiftIn
@@ -11,7 +11,7 @@
1111
#include "Arduino.h"
1212

1313

14-
#define FASTSHIFTIN_LIB_VERSION (F("0.3.1"))
14+
#define FASTSHIFTIN_LIB_VERSION (F("0.3.2"))
1515

1616

1717
class FastShiftIn
@@ -20,36 +20,40 @@ class FastShiftIn
2020
// bitOrder = { LSBFIRST, MSBFIRST };
2121
FastShiftIn(uint8_t dataIn, uint8_t clockPin, uint8_t bitOrder = LSBFIRST);
2222

23-
int read(void);
24-
int lastRead(void);
23+
uint16_t read(void);
24+
uint16_t read16(void);
25+
uint32_t read24(void);
26+
uint32_t read32(void);
27+
uint32_t lastRead(void);
2528

26-
bool setBitOrder(uint8_t bitOrder);
27-
uint8_t getBitOrder(void);
29+
// returns false if bitOrder out of range.
30+
bool setBitOrder(uint8_t bitOrder);
31+
uint8_t getBitOrder(void);
2832

2933
// overrule bitOrder (most optimized).
30-
int readLSBFIRST(void);
31-
int readMSBFIRST(void);
34+
uint8_t readLSBFIRST(void);
35+
uint8_t readMSBFIRST(void);
3236

3337
private:
34-
uint8_t _bitOrder;
35-
int _value;
38+
uint8_t _bitOrder;
39+
uint32_t _lastValue;
3640

3741
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
3842

3943
volatile uint8_t *_dataInRegister;
40-
uint8_t _dataInBit;
44+
uint8_t _dataInBit;
4145

4246
volatile uint8_t *_clockRegister;
43-
uint8_t _clockBit;
47+
uint8_t _clockBit;
4448

4549
#else
4650

47-
uint8_t _dataPinIn;
48-
uint8_t _clockPin;
51+
uint8_t _dataPinIn;
52+
uint8_t _clockPin;
4953

5054
#endif
5155
};
5256

5357

54-
// -- END OF FILE --
58+
// -- END OF FILE --
5559

README.md

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ in the constructor of the FastShiftIn object.
2727
If not an **ARDUINO_ARCH_AVR** or **ARDUINO_ARCH_MEGAAVR** the class falls back
2828
to the default shiftIn() implementation.
2929

30+
Since 0.3.2 read16(), read24() and read32() are added.
31+
These are experimental and not fully tested yet.
32+
3033

3134
## Performance
3235

@@ -35,26 +38,50 @@ The performance of **read()** is substantially faster than the default Arduino
3538
Exact how big the performance gain is can be seen with the example sketch.
3639
It does a comparison and shows how the class is to be used.
3740

38-
test 0.2.3 Arduino UNO
41+
Time in microseconds, Arduino UNO
42+
43+
| function | 0.2.3 | 0.3.2 |
44+
|:---------------------|---------:|---------:|
45+
| read() | 19.30 | 20.49 |
46+
| read16() | | 41.04 |
47+
| read24() | | 62.91 |
48+
| read32() | | 83.95 |
49+
| readLSBFIRST() | 19.04 | 19.92 |
50+
| readMSBFIRST() | 19.04 | 19.92 |
51+
| reference shiftIn() | 107.82 | 108.20 |
52+
53+
54+
0.3.2 is a bit slower (incl. reference) than 0.2.3 but still much
55+
faster than the reference.
3956

40-
| function | time (us) |
41-
|:---------------------|----------:|
42-
| read() | 19.30 |
43-
| readLSBFIRST() | 19.04 |
44-
| readMSBFIRST() | 19.04 |
45-
| reference shiftIn() | 107.82 |
4657

4758

4859
## Interface
4960

50-
The interface exists of the following functions:
61+
```cpp
62+
#include "FastShiftIn.h"
63+
```
64+
65+
#### Functions
66+
67+
- **FastShiftIn(uint8_t dataIn, uint8_t clockPin, uint8_t bitOrder = LSBFIRST)** Constructor
68+
- **uint16_t read(void)** reads a new value, 8 bit.
69+
- **uint16_t read16(void)** reads a new value, 16 bit.
70+
- **uint32_t read24(void)** reads a new value, 24 bit.
71+
- **uint32_t read32(void)** reads a new value, 32 bit.
72+
- **uint32_t lastRead()** returns last value read.
73+
- **bool setBitOrder(uint8_t bitOrder)** set LSBFIRST or MSBFIRST.
74+
Returns false for other values.
75+
- **uint8_t getBitOrder(void)** returns LSBFIRST or MSBFIRST.
76+
- **uint16_t readLSBFIRST(void)** optimized LSB read(), 8 bit.
77+
- **uint16_t readMSBFIRST(void)** optimized MSB read(), 8 bit.
78+
5179

52-
- **int read(void)** reads a new value.
53-
- **int lastRead()** returns last value read.
54-
- **bool setBitOrder(uint8_t bitOrder)** set LSBFIRST or MSBFIRST. Returns false for other values.
55-
- **uint8_t getBitOrder(void)** returns LSBFIRST or MSBFIRST
56-
- **int readLSBFIRST(void)** optimized LSB read().
57-
- **int readMSBFIRST(void)** optimized MSB read().
80+
#### Byte order
81+
82+
It might be that **read16/24/32** has bytes not in the right order.
83+
Then you should use multiple calls to **read()** and merge these
84+
bytes in the order you want them.
5885

5986

6087
## Notes
@@ -64,15 +91,22 @@ The interface exists of the following functions:
6491
pull up resistors, especially if wires are exceeding 10 cm (4").
6592

6693

67-
## Operation
94+
## Future
6895

69-
See examples
96+
#### Must
7097

98+
#### Should
7199

72-
## Future
100+
- extend unit tests
101+
102+
#### Could
73103

74104
- esp32 optimization readLSBFIRST readMSBFIRST
75-
- read8() read16(), read24(), read32()
76105
- **read(uint8_t \* arr, uint8_t nr)** ??
77106
- example schema
107+
- would it be interesting to make a fastShiftIn16() etc?
108+
- squeeze performance but more maintenance.?
109+
110+
#### Wont
111+
78112

0 commit comments

Comments
 (0)