Skip to content

Commit 82aa36a

Browse files
authored
Merge pull request #3 from RobTillaart/develop
refactor documentation; fix tabs -> spaces
2 parents 40610b0 + 48e7850 commit 82aa36a

File tree

3 files changed

+100
-89
lines changed

3 files changed

+100
-89
lines changed

FastShiftIn.cpp

+63-64
Original file line numberDiff line numberDiff line change
@@ -4,109 +4,108 @@
44
// VERSION: 0.2.1
55
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
66
// DATE: 2013-09-29
7-
// URL: https://github.com/RobTillaart/FastShiftIn.git
7+
// URL: https://github.com/RobTillaart/FastShiftIn
88
//
99

1010
#include "FastShiftIn.h"
1111

1212
FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
1313
{
14-
_bitorder = bitOrder;
14+
_bitorder = bitOrder;
1515

16-
pinMode(datapin, INPUT);
17-
pinMode(clockpin, OUTPUT);
16+
pinMode(datapin, INPUT);
17+
pinMode(clockpin, OUTPUT);
1818

1919
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
2020

21-
// uint8_t _datatimer = digitalPinToTimer(datapin);
22-
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer); TODO
23-
uint8_t _dataport = digitalPinToPort(datapin);
24-
_datain = portOutputRegister(_dataport);
25-
_databit = digitalPinToBitMask(datapin);
21+
// uint8_t _datatimer = digitalPinToTimer(datapin);
22+
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer); TODO
23+
uint8_t _dataport = digitalPinToPort(datapin);
24+
_datain = portOutputRegister(_dataport);
25+
_databit = digitalPinToBitMask(datapin);
2626

27-
// uint8_t _clocktimer = digitalPinToTimer(clockpin);
28-
// if (_clocktimer != NOT_ON_TIMER) turnOffPWM(_clocktimer);
29-
uint8_t _clockport = digitalPinToPort(clockpin);
30-
_clockin = portOutputRegister(_clockport);
31-
_clockbit = digitalPinToBitMask(clockpin);
27+
// uint8_t _clocktimer = digitalPinToTimer(clockpin);
28+
// if (_clocktimer != NOT_ON_TIMER) turnOffPWM(_clocktimer);
29+
uint8_t _clockport = digitalPinToPort(clockpin);
30+
_clockin = portOutputRegister(_clockport);
31+
_clockbit = digitalPinToBitMask(clockpin);
3232

3333
#else // reference implementation
3434

35-
// reuse these vars as pin to save some space
36-
_databit = datapin;
37-
_clockbit = clockpin;
35+
// reuse these vars as pin to save some space
36+
_databit = datapin;
37+
_clockbit = clockpin;
3838

3939
#endif
4040
}
4141

4242
int FastShiftIn::read()
4343
{
44-
if (_bitorder == LSBFIRST)
45-
{
46-
return readLSBFIRST();
47-
}
48-
return readMSBFIRST();
44+
if (_bitorder == LSBFIRST)
45+
{
46+
return readLSBFIRST();
47+
}
48+
return readMSBFIRST();
4949
}
5050

5151
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
5252

5353
int FastShiftIn::readLSBFIRST()
5454
{
55-
uint8_t value = 0;
56-
uint8_t cbmask1 = _clockbit;
57-
uint8_t cbmask2 = ~_clockbit;
58-
uint8_t dbmask = _databit;
59-
60-
for (uint8_t m = 1; m > 0; m <<= 1)
61-
{
62-
uint8_t oldSREG = SREG;
63-
cli();
64-
*_clockin |= cbmask1;
65-
if ((*_datain & dbmask) > 0)
66-
{
67-
value |= m;
68-
}
69-
*_clockin &= cbmask2;
70-
SREG = oldSREG;
71-
}
72-
_value = value;
73-
return _value;
55+
uint8_t value = 0;
56+
uint8_t cbmask1 = _clockbit;
57+
uint8_t cbmask2 = ~_clockbit;
58+
uint8_t dbmask = _databit;
59+
60+
for (uint8_t m = 1; m > 0; m <<= 1)
61+
{
62+
uint8_t oldSREG = SREG;
63+
cli();
64+
*_clockin |= cbmask1;
65+
if ((*_datain & dbmask) > 0)
66+
{
67+
value |= m;
68+
}
69+
*_clockin &= cbmask2;
70+
SREG = oldSREG;
71+
}
72+
_value = value;
73+
return _value;
7474
}
7575

7676
int FastShiftIn::readMSBFIRST()
7777
{
78-
uint8_t value = 0;
79-
uint8_t cbmask1 = _clockbit;
80-
uint8_t cbmask2 = ~cbmask1;
81-
uint8_t dbmask = _databit;
82-
83-
for (uint8_t n = 128; n > 0; n >>= 1)
84-
{
85-
uint8_t oldSREG = SREG;
86-
cli();
87-
*_clockin |= cbmask1;
88-
89-
if ((*_datain & dbmask) > 0)
90-
{
91-
value |= n;
92-
}
93-
*_clockin &= cbmask2;
94-
SREG = oldSREG;
95-
}
96-
_value = value;
97-
return _value;
78+
uint8_t value = 0;
79+
uint8_t cbmask1 = _clockbit;
80+
uint8_t cbmask2 = ~cbmask1;
81+
uint8_t dbmask = _databit;
82+
83+
for (uint8_t n = 128; n > 0; n >>= 1)
84+
{
85+
uint8_t oldSREG = SREG;
86+
cli();
87+
*_clockin |= cbmask1;
88+
if ((*_datain & dbmask) > 0)
89+
{
90+
value |= n;
91+
}
92+
*_clockin &= cbmask2;
93+
SREG = oldSREG;
94+
}
95+
_value = value;
96+
return _value;
9897
}
9998

100-
#else // reference implementation // note this has no cli()
99+
#else // reference implementation - note this has no cli()
101100

102101
int FastShiftIn::readLSBFIRST()
103102
{
104-
return shiftIn(_databit, _clockbit, LSBFIRST);
103+
return shiftIn(_databit, _clockbit, LSBFIRST);
105104
}
106105

107106
int FastShiftIn::readMSBFIRST()
108107
{
109-
return shiftIn(_databit, _clockbit, MSBFIRST);
108+
return shiftIn(_databit, _clockbit, MSBFIRST);
110109
}
111110

112111
#endif

FastShiftIn.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// VERSION: 0.2.1
66
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
77
// DATE: 2013-09-29
8-
// URL: https://github.com/RobTillaart/FastShiftIn.git
8+
// URL: https://github.com/RobTillaart/FastShiftIn
99
//
1010

1111
#include "Arduino.h"
@@ -15,23 +15,23 @@
1515
class FastShiftIn
1616
{
1717
public:
18-
// bitorder = { LSBFIRST, MSBFIRST };
19-
FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder = LSBFIRST);
20-
int read(void);
21-
22-
// overrule bitorder (most optimized).
23-
int readLSBFIRST(void);
24-
int readMSBFIRST(void);
18+
// bitorder = { LSBFIRST, MSBFIRST };
19+
FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder = LSBFIRST);
20+
int read(void);
21+
22+
// overrule bitorder (most optimized).
23+
int readLSBFIRST(void);
24+
int readMSBFIRST(void);
2525

2626
private:
27-
uint8_t _bitorder;
28-
int _value;
27+
uint8_t _bitorder;
28+
int _value;
2929

30-
uint8_t _databit;
31-
volatile uint8_t *_datain;
30+
uint8_t _databit;
31+
volatile uint8_t *_datain;
3232

33-
uint8_t _clockbit;
34-
volatile uint8_t *_clockin;
33+
uint8_t _clockbit;
34+
volatile uint8_t *_clockin;
3535
};
3636

3737
// -- END OF FILE --

README.md

+23-11
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,34 @@ A library for FastShiftOut also exist.
66

77
## Description
88

9-
FastShiftIn is a class that speeds up the shifting of the bits by using predetermined ports and masks.
10-
These are predetermined inthe constructor of the FastShiftIN object.
9+
FastShiftIn is a class that has optimized code (AVR only) to shift in data faster
10+
than the normal shiftIn() function.
11+
It speeds up the shift using low level ports and masks. These are predetermined
12+
in the constructor of the FastShiftIn object.
1113

12-
The performance of **read()** is substantially faster than the default Arduino **shiftIn()**,
13-
but not as fast as HW SPI. Exact how big the performance gain is can be seen with the example sketch.
14+
If not an **ARDUINO_ARCH_AVR** or **ARDUINO_ARCH_MEGAAVR** the class falls back
15+
to the default shiftIn() implementation.
16+
17+
## Performance
18+
19+
The performance of **read()** is substantially faster than the default Arduino
20+
**shiftIn()**, but not as fast as HW SPI.
21+
Exact how big the performance gain is can be seen with the example sketch.
1422
It does a comparison and shows how the class is to be used.
1523

16-
Version 0.2.0 added **readLSBFIRST()** and **readMSBFIRST()** to squeeze max performance.
17-
The next stop might be assembly.
24+
## Interface
25+
26+
The interface exists of the following functions:
27+
28+
- **int read(void);**
29+
- **int readLSBFIRST(void);** most optimized
30+
- **int readMSBFIRST(void);** most optimized
1831

19-
**Note**
20-
The optimizations are AVR only for now, other platforms may follow.
32+
## Notes
2133

22-
**Note**
23-
The 74HC165 needs 0.1uF caps and the data and clock lines
24-
may need pull up resistors, especially if wires are exceeding 10 cm (4").
34+
- The optimizations are AVR only for now, other platforms may follow.
35+
- The 74HC165 needs 0.1uF caps and the data and clock lines may need
36+
pull up resistors, especially if wires are exceeding 10 cm (4").
2537

2638
## Operation
2739

0 commit comments

Comments
 (0)