commit 81e507f29501dbef6e3e642d1dc6a9c16046ad40 Author: Alexander Klein Date: Thu Feb 9 11:17:03 2023 +0100 Initial commit diff --git a/include/shiftregister.h b/include/shiftregister.h new file mode 100644 index 0000000..74dd5ef --- /dev/null +++ b/include/shiftregister.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +class ShiftRegister { + public: + ShiftRegister(uint8_t latch, uint8_t clock, uint8_t data, uint8_t numberOfRegister = 1); + ~ShiftRegister(); + + bool writePin(uint8_t pin, uint8_t state); + bool writeByte(uint8_t registerNumber, byte data); + + private: + void write(); + + uint8_t latchPin; + uint8_t clockPin; + uint8_t dataPin; + uint8_t numberOfRegister; + uint8_t numberOfPins; + + byte* data; + +}; \ No newline at end of file diff --git a/library.json b/library.json new file mode 100644 index 0000000..860e7ac --- /dev/null +++ b/library.json @@ -0,0 +1,23 @@ +{ + "name": "Shiftregister", + "version": "1.0.0", + "description": "Control shift register with simple Class.", + "keywords": "shift, register, multiple", + "repository": + { + "type": "git", + "url": "https://git.kleiax.de/PlatformIO-Libs/Shiftregister.git" + }, + "authors": + [ + { + "name": "Alexander Klein", + "email": "alex@kleiax.de", + "url": "https://www.kleiax.de/contact" + } + ], + "license": "MIT", + "homepage": "https://www.kleiax.de/", + "frameworks": "*", + "platforms": "*" + } \ No newline at end of file diff --git a/src/shiftregister.cpp b/src/shiftregister.cpp new file mode 100644 index 0000000..ac50626 --- /dev/null +++ b/src/shiftregister.cpp @@ -0,0 +1,64 @@ +#include "shiftregister.h" + +ShiftRegister::ShiftRegister(uint8_t latch, uint8_t clock, uint8_t data, uint8_t numberOfRegister) { + this->latchPin = latch; + this->clockPin = clock; + this->dataPin = data; + this->numberOfPins = numberOfRegister * 8; + + pinMode(this->latchPin, OUTPUT); + pinMode(this->clockPin, OUTPUT); + pinMode(this->dataPin, OUTPUT); + + digitalWrite(this->latchPin, LOW); + digitalWrite(this->clockPin, LOW); + digitalWrite(this->dataPin, LOW); + + this->data = new byte[numberOfRegister]; + + for (uint8_t i = 0; i < numberOfRegister; i++) + this->data[i] = 0; + + this->numberOfRegister = numberOfRegister; + this->numberOfPins = this->numberOfRegister * 8; + + this->write(); +} + +ShiftRegister::~ShiftRegister() { + delete[] data; +} + +bool ShiftRegister::writePin(uint8_t pin, uint8_t state) { + if (!(pin < this->numberOfPins)) + return false; + + uint8_t registerNumber = pin / 8; + if (registerNumber) + pin -= 8; + + if (state) + this->data[registerNumber] |= (1 << pin); + else + this->data[registerNumber] &= ~(1 << pin); + + this->write(); + return true; +} + +bool ShiftRegister::writeByte(uint8_t registerNumber, byte data) { + if (registerNumber < this->numberOfRegister) { + this->data[registerNumber] = data; + this->write(); + return true; + } + return false; +} + +void ShiftRegister::write() { + for (int8_t i = this->numberOfRegister - 1; i >= 0; i--) + shiftOut(this->dataPin, this->clockPin, MSBFIRST, this->data[i]); + + digitalWrite(this->latchPin, HIGH); + digitalWrite(this->latchPin, LOW); +} \ No newline at end of file