Skip to content

Commit

Permalink
add default for functions (#5)
Browse files Browse the repository at this point in the history
- add default **LSBFIRST** for **setBItOrder()**
- add default **0** for **setDelay()**
- update readme.md
- move code from .h to .cpp
- update example
- update GitHub actions
- update license 2023
- minor edits
  • Loading branch information
RobTillaart authored Feb 21, 2023
1 parent a4ccfd8 commit 8c767c7
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 41 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.1.3] - 2023-02-21
- add default **LSBFIRST** for **setBItOrder()**
- add default **0** for **setDelay()**
- update readme.md
- move code from .h to .cpp
- update example
- update GitHub actions
- update license 2023
- minor edits


## [0.1.2] - 2022-11-24
- Add RP2040 support to build-CI.
- Add CHANGELOG.md


## [0.1.1] - 2021-12-28
- update library.json
- update readme.md
Expand Down
45 changes: 29 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,58 @@ The dataPin and clockPin are set in the constructor, the delay is configurable p
The performance of **read()** with a delay of 0 microseconds is slower than the default Arduino
**shiftIn()** due to some overhead.

The delay requested is split in two (expect rounding errors) to have "nice" looking pulses.
This keeps the duty cycle ~50%.
The delay requested is split in two (expect rounding errors) to have "nice" looking pulse
with the duty cycle around 50%.

Performance measurements are meaningless, as the purpose of this library is to
slow the pulse train to a working level.


## Interface

```cpp
#include "ShiftInSlow.h"
```

#### Functions

The interface exists of the following functions:
- **ShiftInSlow(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder = LSBFIRST)** constructor,
bit order is default set to LSBFIRST.
- **int read(void)** reads a new value
- **int lastRead()** returns last value read
- **void setDelay(uint16_t microseconds)** set delay per bit from 0 .. 65535 microseconds.
Note that the delay is split in two parts to keep ~ 50% duty cycle.
- **int read()** reads a new value.
- **int lastRead()** returns last value read.
- **void setDelay(uint16_t microseconds = 0)** set delay per bit from 0 .. 65535 microseconds.
Note that the delay is split in two parts to keep the duty cycle around 50%.
- **uint16_t getDelay()** returns the set delay in microseconds.
- **bool setBitOrder(uint8_t bitOrder)** set LSBFIRST or MSBFIRST. Returns false for other values.
- **uint8_t getBitOrder(void)** returns LSBFIRST or MSBFIRST
- **bool setBitOrder(uint8_t bitOrder = LSBFIRST)** set LSBFIRST or MSBFIRST. Returns false for other values.
- **uint8_t getBitOrder(void)** returns LSBFIRST or MSBFIRST.


## Operation

See examples
See examples.


## Future

#### must
#### Must

- improve documentation
- move code from .h to .cpp

#### should
#### Should

- add examples
- adaptive speed example?

#### Could

#### could
- Add a select pin to be more SPI alike?
- increase max delay uint32_t ?
- increase max delay uint32_t ?

#### Wont

- get set dutyCycle(0 .. 99%)
- set delay in terms of frequency - delay is 'wave length'
- set delay in terms of max total time the read may cost.
- set default delay = 0, is no delay ?
- get set dutyCycle(0 .. 99%)
- read16/24/32 to read more bytes is a user task.

31 changes: 27 additions & 4 deletions ShiftInSlow.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: ShiftInSlow.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for shiftIn with build-in delay
// DATE: 2021-05-11
// URL: https://github.com/RobTillaart/ShiftInSlow
Expand All @@ -14,7 +14,7 @@
ShiftInSlow::ShiftInSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder)
{
_clockPin = clockPin;
_dataPin = dataPin;
_dataPin = dataPin;
_bitOrder = bitOrder;
_value = 0;
pinMode(_dataPin, INPUT);
Expand All @@ -30,19 +30,25 @@ int ShiftInSlow::read()
for (uint8_t i = 0; i < 8; ++i)
{
digitalWrite(_clockPin, HIGH);
if (_delay > 0) delayMicroseconds(_delay/2);
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);
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))
Expand All @@ -53,6 +59,23 @@ bool ShiftInSlow::setBitOrder(const uint8_t bitOrder)
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 --

20 changes: 10 additions & 10 deletions ShiftInSlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: ShiftInSlow.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for shiftIn with build-in delay
// DATE: 2021-05-11
// URL: https://github.com/RobTillaart/ShiftInSlow
Expand All @@ -11,7 +11,7 @@
#include "Arduino.h"


#define SHIFTINSLOW_LIB_VERSION (F("0.1.2"))
#define SHIFTINSLOW_LIB_VERSION (F("0.1.3"))


class ShiftInSlow
Expand All @@ -20,24 +20,24 @@ class ShiftInSlow
// bitOrder = { LSBFIRST, MSBFIRST }; defined in Arduino.h.
ShiftInSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder = LSBFIRST);

int read(void);
int lastRead(void) { return _value; };
int read();
int lastRead();

bool setBitOrder(const uint8_t bitOrder);
uint8_t getBitOrder(void) { return _bitOrder; };
bool setBitOrder(const uint8_t bitOrder = LSBFIRST);
uint8_t getBitOrder();

void setDelay(uint16_t microseconds) { _delay = microseconds; };
uint16_t getDelay() { return _delay; };
void setDelay(uint16_t microseconds = 0);
uint16_t getDelay();


private:
uint8_t _clockPin = 0 ;
uint8_t _clockPin = 0;
uint8_t _dataPin = 0;
uint8_t _bitOrder = LSBFIRST;
uint16_t _delay = 0;
uint8_t _value = 0;
};


// -- END OF FILE --
// -- END OF FILE --

8 changes: 5 additions & 3 deletions examples/shiftInSlow_demo/shiftInSlow_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void setup()
Serial.println(__FILE__);
Serial.println(SHIFTINSLOW_LIB_VERSION);

for (uint16_t d = 0; d < 1000; d += 10)
for (uint16_t d = 0; d <= 1000; d += 10)
{
SIS.setDelay(d);
uint32_t start = micros();
Expand All @@ -28,7 +28,10 @@ void setup()
float duration = stop - start;
Serial.print(stop - start);
Serial.print("\t");
Serial.println(duration / 8, 1);
Serial.print(d);
Serial.print("\t");
Serial.print(duration / 8, 1);
Serial.print("\n");
}

Serial.println("done...");
Expand All @@ -41,4 +44,3 @@ void loop()


// -- END OF FILE --

2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ ShiftInSlow KEYWORD1
# Methods and Functions (KEYWORD2)
read KEYWORD2
lastRead KEYWORD2

setBitOrder KEYWORD2
getBitOrder KEYWORD2

setDelay KEYWORD2
getDelay KEYWORD2

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/ShiftInSlow.git"
},
"version": "0.1.2",
"version": "0.1.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ShiftInSlow
version=0.1.2
version=0.1.3
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for shiftIn with build-in delay - e.g. for 74HC165
Expand Down
16 changes: 11 additions & 5 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// PATCH FOR DUE & ZERO FOR UNIT TEST - https://github.com/Arduino-CI/arduino_ci/issues/252
#if defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD)
// - due # ARDUINO_ARCH_SAM does not support shiftIn apparently
// - zero # ARDUINO_ARCH_SAMD
// - zero # ARDUINO_ARCH_SAMD
#endif


Expand All @@ -52,9 +52,12 @@ unittest(test_constructor)

assertEqual(0, SIS.lastRead());
assertEqual(LSBFIRST, SIS.getBitOrder());

SIS.setBitOrder(MSBFIRST);
assertEqual(MSBFIRST, SIS.getBitOrder());

SIS.setBitOrder();
assertEqual(LSBFIRST, SIS.getBitOrder());
}


Expand All @@ -64,7 +67,7 @@ unittest(test_constructor_LSB)

assertEqual(0, SIS.lastRead());
assertEqual(LSBFIRST, SIS.getBitOrder());

SIS.setBitOrder(MSBFIRST);
assertEqual(MSBFIRST, SIS.getBitOrder());
}
Expand All @@ -76,7 +79,7 @@ unittest(test_constructor_MSB)

assertEqual(0, SIS.lastRead());
assertEqual(MSBFIRST, SIS.getBitOrder());

SIS.setBitOrder(LSBFIRST);
assertEqual(LSBFIRST, SIS.getBitOrder());
}
Expand All @@ -91,6 +94,9 @@ unittest(test_setDelay)
SIS.setDelay(d);
assertEqual(d, SIS.getDelay());
}

SIS.setDelay();
assertEqual(0, SIS.getDelay());
}


Expand All @@ -105,4 +111,4 @@ unittest(test_read)
unittest_main()


// --------
// -- END OF FILE --

0 comments on commit 8c767c7

Please sign in to comment.