diff --git a/libraries/shiftOutSlow/.arduino-ci.yml b/libraries/shiftOutSlow/.arduino-ci.yml new file mode 100644 index 000000000..ff5659b91 --- /dev/null +++ b/libraries/shiftOutSlow/.arduino-ci.yml @@ -0,0 +1,7 @@ +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + - uno + - leonardo + - due + - zero diff --git a/libraries/shiftOutSlow/.github/workflows/arduino-lint.yml b/libraries/shiftOutSlow/.github/workflows/arduino-lint.yml new file mode 100644 index 000000000..b2ca058c1 --- /dev/null +++ b/libraries/shiftOutSlow/.github/workflows/arduino-lint.yml @@ -0,0 +1,13 @@ + +name: Arduino-lint + +on: [push, pull_request] +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: arduino/arduino-lint-action@v1 + with: + library-manager: update + compliance: strict diff --git a/libraries/shiftOutSlow/.github/workflows/arduino_test_runner.yml b/libraries/shiftOutSlow/.github/workflows/arduino_test_runner.yml new file mode 100644 index 000000000..476456bb8 --- /dev/null +++ b/libraries/shiftOutSlow/.github/workflows/arduino_test_runner.yml @@ -0,0 +1,13 @@ +--- +name: Arduino CI + +on: [push, pull_request] + +jobs: + arduino_ci: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: Arduino-CI/action@master + # Arduino-CI/action@v0.1.1 diff --git a/libraries/shiftOutSlow/.github/workflows/jsoncheck.yml b/libraries/shiftOutSlow/.github/workflows/jsoncheck.yml new file mode 100644 index 000000000..04603d081 --- /dev/null +++ b/libraries/shiftOutSlow/.github/workflows/jsoncheck.yml @@ -0,0 +1,18 @@ +name: JSON check + +on: + push: + paths: + - '**.json' + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: json-syntax-check + uses: limitusus/json-syntax-check@v1 + with: + pattern: "\\.json$" + diff --git a/libraries/shiftOutSlow/LICENSE b/libraries/shiftOutSlow/LICENSE new file mode 100644 index 000000000..a8b455d01 --- /dev/null +++ b/libraries/shiftOutSlow/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021-2021 Rob Tillaart + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/libraries/shiftOutSlow/README.md b/libraries/shiftOutSlow/README.md new file mode 100644 index 000000000..323fe6ee9 --- /dev/null +++ b/libraries/shiftOutSlow/README.md @@ -0,0 +1,51 @@ + +[![Arduino CI](https://github.com/RobTillaart/ShiftOutSlow/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/ShiftOutSlow/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/RobTillaart/ShiftOutSlow.svg?maxAge=3600)](https://github.com/RobTillaart/ShiftOutSlow/releases) + +# ShiftOutSlow + +Arduino library for shiftOut with build-in delay - e.g. for 74HC595 + +A library for shiftInSlow also exist. + +## Description + + +ShiftOutSlow is an experimental library that has a build in delay (in microseconds) that allows tuning the time per bit. +This allows one to improve reliability e.g. when using longer lines. + +The datapin and clockpin are set in the constructor, the delay is settable per byte send to be able to optimize runtime. + +ShiftOutSlow implements the print interface. + + +## Performance + +The performance of **write()** with a delay of 0 microseconds is slower than the default Arduino +**shiftOut()** due to some overhead. + +The delay requested is split in two (expect rounding errors) to have "nice" looking pulses. + + +## Interface + +The interface exists of the following functions: +- **ShiftOutSlow(datapin, clockpin, bitorder = LSBFIRST)** constructor. +- **size_t write(uint8_t data)** writes a new value +- **uint8_t lastWritten()** returns last value written +- **void setDelay(uint16_t microseconds)** set delay per bit from 0 .. 65535 microseconds. +- **uint16_t getDelay()** returns the set delay in microseconds. +- **bool setBitOrder(bitOrder)** set LSBFIRST or MSBFIRST. Returns false for other values. +- **uint8_t getBitOrder(void)** returns LSBFIRST or MSBFIRST + + +## Notes + +- to be tested + + +## Operation + +See examples + diff --git a/libraries/shiftOutSlow/ShiftOutSlow.cpp b/libraries/shiftOutSlow/ShiftOutSlow.cpp new file mode 100644 index 000000000..95b03e668 --- /dev/null +++ b/libraries/shiftOutSlow/ShiftOutSlow.cpp @@ -0,0 +1,59 @@ +// +// FILE: ShiftOutSlow.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// 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; + for (uint8_t i = 0; i < 8; ++i) + { + if (_delay > 0) delayMicroseconds(_delay/2); + if (_bitOrder == LSBFIRST) { + digitalWrite(_dataPin, val & 0x01); + val >>= 1; + } else { + digitalWrite(_dataPin, (val & 0x80) != 0); + val <<= 1; + } + digitalWrite(_clockPin, HIGH); + if (_delay > 0) delayMicroseconds(_delay/2); + yield(); + digitalWrite(_clockPin, LOW); + } + _value = data; + return 1; +} + + +bool ShiftOutSlow::setBitOrder(const uint8_t bitOrder) +{ + if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST)) + { + _bitOrder = bitOrder; + return true; + }; + return false; +} + +// -- END OF FILE -- diff --git a/libraries/shiftOutSlow/ShiftOutSlow.h b/libraries/shiftOutSlow/ShiftOutSlow.h new file mode 100644 index 000000000..2716da330 --- /dev/null +++ b/libraries/shiftOutSlow/ShiftOutSlow.h @@ -0,0 +1,42 @@ +#pragma once +// +// FILE: ShiftOutSlow.h +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: Arduino library for shiftOut with build-in delay +// DATE: 2021-05-11 +// URL: https://github.com/RobTillaart/ShiftOutSlow +// + + +#include "Arduino.h" + + +#define SHIFTOUTSLOW_LIB_VERSION (F("0.1.0")) + + +class ShiftOutSlow : public Print +{ +public: + // bitorder = { LSBFIRST, MSBFIRST }; + ShiftOutSlow(const uint8_t dataPin, const uint8_t clockPin, const uint8_t bitOrder = LSBFIRST); + + size_t write(const uint8_t data); + uint8_t lastWritten(void) { return _value; }; + + bool setBitOrder(const uint8_t bitOrder); + uint8_t getBitOrder(void) { return _bitOrder; }; + + void setDelay(uint16_t d) { _delay = d; }; + uint16_t getDelay() { return _delay; }; + + +private: + uint8_t _clockPin = 0 ; + uint8_t _dataPin = 0; + uint8_t _bitOrder = LSBFIRST; + uint16_t _delay = 0; + uint8_t _value = 0; +}; + +// -- END OF FILE -- diff --git a/libraries/shiftOutSlow/examples/shiftOutSlow_demo/shiftOutSlow_demo.ino b/libraries/shiftOutSlow/examples/shiftOutSlow_demo/shiftOutSlow_demo.ino new file mode 100644 index 000000000..797a3dc84 --- /dev/null +++ b/libraries/shiftOutSlow/examples/shiftOutSlow_demo/shiftOutSlow_demo.ino @@ -0,0 +1,42 @@ +// +// FILE: shiftOutSlow_demo.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: test sketch +// URL: https://github.com/RobTillaart/ShiftOutSlow +// + + +#include "ShiftOutSlow.h" + + +ShiftOutSlow SOS(12, 13, LSBFIRST); + +volatile int x = 0; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.println(SHIFTOUTSLOW_LIB_VERSION); + + for (uint16_t d = 0; d < 1000; d += 10) + { + SOS.setDelay(d); + uint32_t start = micros(); + x = SOS.write(0x55); + uint32_t stop = micros(); + float duration = stop - start; + Serial.print(stop - start); + Serial.print("\t"); + Serial.println(duration / 8, 1); + } + + Serial.println("done..."); +} + +void loop() +{ +} + +// -- END OF FILE -- diff --git a/libraries/shiftOutSlow/keywords.txt b/libraries/shiftOutSlow/keywords.txt new file mode 100644 index 000000000..4d303d1cb --- /dev/null +++ b/libraries/shiftOutSlow/keywords.txt @@ -0,0 +1,18 @@ +# Syntax Coloring Map For ShiftOutSlow + +# Datatypes (KEYWORD1) +ShiftOutSlow KEYWORD1 + + +# Methods and Functions (KEYWORD2) +read KEYWORD2 +lastRead KEYWORD2 +setBitOrder KEYWORD2 +getBitOrder KEYWORD2 +setDelay KEYWORD2 +getDelay KEYWORD2 + + +# Constants (LITERAL1) +SHIFTOUTSLOW_LIB_VERSION LITERAL1 + diff --git a/libraries/shiftOutSlow/library.json b/libraries/shiftOutSlow/library.json new file mode 100644 index 000000000..3293a2e90 --- /dev/null +++ b/libraries/shiftOutSlow/library.json @@ -0,0 +1,22 @@ +{ + "name": "ShiftOutSlow", + "keywords": "Shift, shiftOut, out, serial, slow, 74HC595", + "description": "Arduino library for shiftOut with build-in delay - e.g. for 74HC595", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/ShiftOutSlow.git" + }, + "version": "0.1.0", + "license": "MIT", + "frameworks": "arduino", + "platforms": "*" +} diff --git a/libraries/shiftOutSlow/library.properties b/libraries/shiftOutSlow/library.properties new file mode 100644 index 000000000..e2b54a7d7 --- /dev/null +++ b/libraries/shiftOutSlow/library.properties @@ -0,0 +1,11 @@ +name=ShiftOutSlow +version=0.1.0 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Arduino library for shiftOut with build-in delay - e.g. for 74HC165 +paragraph= +category=Signal Input/Output +url=https://github.com/RobTillaart/ShiftOutSlow +architectures=* +includes=ShiftOutSlow.h +depends= diff --git a/libraries/shiftOutSlow/test/unit_test_001.cpp b/libraries/shiftOutSlow/test/unit_test_001.cpp new file mode 100644 index 000000000..28d7fe616 --- /dev/null +++ b/libraries/shiftOutSlow/test/unit_test_001.cpp @@ -0,0 +1,103 @@ +// +// FILE: unit_test_001.cpp +// AUTHOR: Rob Tillaart +// DATE: 2021-05-11 +// PURPOSE: unit tests for the FastShiftIn library +// https://github.com/RobTillaart/ShiftOutSlow +// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md +// + +// supported assertions +// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42 +// ---------------------------- +// assertEqual(expected, actual) +// assertNotEqual(expected, actual) +// assertLess(expected, actual) +// assertMore(expected, actual) +// assertLessOrEqual(expected, actual) +// assertMoreOrEqual(expected, actual) +// assertTrue(actual) +// assertFalse(actual) +// assertNull(actual) +// assertNotNull(actual) + +#include + + +#include "Arduino.h" +#include "ShiftOutSlow.h" + + +// 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 +#endif + + +unittest_setup() +{ +} + +unittest_teardown() +{ +} + + +unittest(test_constructor) +{ + ShiftOutSlow SOS(12, 13); + + fprintf(stderr, "VERSION:\t%s\n", SHIFTOUTSLOW_LIB_VERSION); + assertEqual(1, SOS.write(65)); + assertEqual(65, SOS.lastWritten()); + assertEqual(LSBFIRST, SOS.getBitOrder()); + + SOS.setBitOrder(MSBFIRST); + assertEqual(MSBFIRST, SOS.getBitOrder()); +} + + +unittest(test_constructor_LSB) +{ + ShiftOutSlow SOS(12, 13, LSBFIRST); + + fprintf(stderr, "VERSION:\t%s\n", SHIFTOUTSLOW_LIB_VERSION); + assertEqual(1, SOS.write(65)); + assertEqual(65, SOS.lastWritten()); + assertEqual(LSBFIRST, SOS.getBitOrder()); + + SOS.setBitOrder(MSBFIRST); + assertEqual(MSBFIRST, SOS.getBitOrder()); +} + + +unittest(test_constructor_MSB) +{ + ShiftOutSlow SOS(12, 13, MSBFIRST); + + fprintf(stderr, "VERSION:\t%s\n", SHIFTOUTSLOW_LIB_VERSION); + assertEqual(1, SOS.write(65)); + assertEqual(65, SOS.lastWritten()); + assertEqual(MSBFIRST, SOS.getBitOrder()); + + SOS.setBitOrder(LSBFIRST); + assertEqual(LSBFIRST, SOS.getBitOrder()); +} + + +unittest(test_setDelay) +{ + ShiftOutSlow SOS(12, 13); + + fprintf(stderr, "VERSION:\t%s\n", SHIFTOUTSLOW_LIB_VERSION); + for (uint16_t d = 0; d < 1000; d += 100) + { + SOS.setDelay(d); + assertEqual(d, SOS.getDelay()); + } +} + +unittest_main() + +// --------