Initial commit

This commit is contained in:
2023-02-09 11:17:03 +01:00
commit 713cc5cb0a
3 changed files with 111 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include <Arduino.h>
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;
};
+23
View File
@@ -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": "*"
}
+64
View File
@@ -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);
}