Skip to content

Commit

Permalink
Update comments
Browse files Browse the repository at this point in the history
Add more comments to .cpp file, update README.md file and adjust keywords to #4
  • Loading branch information
Simsso committed Apr 1, 2018
1 parent e34c7ef commit ead4fba
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 39 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Shift-Register-74HC595-Arduino-Library
The Shift Register 74HC595 Arduino Library makes the use of shift registers much easier.
For detailed information visit http://shiftregister.simsso.de/.
# ShiftRegister 74HC595 Arduino Library
This library simplifies shift registers usage. It allows, for instance, to set shift register pins just like normal Arduino pins: `sr.set(1, HIGH)`.

The **documentation** is available at http://shiftregister.simsso.de/.

An **example** sketch can be found in this repository at [/examples/example/example.ino](https://github.com/Simsso/ShiftRegister74HC595/blob/master/examples/example/example.ino).
58 changes: 45 additions & 13 deletions ShiftRegister74HC595.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/*
ShiftRegister74HC595.cpp - Library for simplified control of 74HC595 shift registers.
Created by Timo Denk (www.timodenk.com), Nov 2014.
Additional information is available at http://shiftregister.simsso.de/
Released into the public domain.
*/

#include "Arduino.h"
#include "ShiftRegister74HC595.h"


// constructor
// ShiftRegister74HC595 constructor
ShiftRegister74HC595::ShiftRegister74HC595(int numberOfShiftRegisters, int serialDataPin, int clockPin, int latchPin)
{
// set attributes
Expand Down Expand Up @@ -30,7 +37,10 @@ ShiftRegister74HC595::ShiftRegister74HC595(int numberOfShiftRegisters, int seria
}


void ShiftRegister74HC595::setAll(uint8_t * digitalValues) {
// Set all pins of the shift registers at once.
// digitalVAlues is a uint8_t array where the length is equal to the number of shift registers.
void ShiftRegister74HC595::setAll(uint8_t * digitalValues)
{
int byte;

for (byte = _numberOfShiftRegisters - 1; byte >= 0; byte--) {
Expand All @@ -44,45 +54,67 @@ void ShiftRegister74HC595::setAll(uint8_t * digitalValues) {
}


uint8_t * ShiftRegister74HC595::getAll() {
// Retrieve all states of the shift registers' output pins.
// The returned array's length is equal to the numbe of shift registers.
uint8_t * ShiftRegister74HC595::getAll()
{
return _digitalValues;
}


void ShiftRegister74HC595::set(int pin, uint8_t value) {
// Set a specific pin to either HIGH (1) or LOW (0).
// The pin parameter is a positive, zero-based integer, indicating which pin to set.
void ShiftRegister74HC595::set(int pin, uint8_t value)
{
setNoUpdate(pin, value);
updateRegisters();
}

// Updates the shift register pins to the stored output values.
void ShiftRegister74HC595::updateRegisters()
{
setAll(_digitalValues);
}

void ShiftRegister74HC595::setNoUpdate(int pin, uint8_t value) {
if (value == 1)

// Equivalent to set(int pin, uint8_t value), except the physical shift register is not updated.
// Should be used in combination with updateRegisters().
void ShiftRegister74HC595::setNoUpdate(int pin, uint8_t value)
{
if (value == 1) {
_digitalValues[pin / 8] |= 1 << (pin % 8);
else
}
else {
_digitalValues[pin / 8] &= ~(1 << (pin % 8));
}
}


uint8_t ShiftRegister74HC595::get(int pin) {
// Returns the state of the given pin.
// Either HIGH (1) or LOW (0)
uint8_t ShiftRegister74HC595::get(int pin)
{
return (_digitalValues[pin / 8] >> (pin % 8)) & 1;
}


void ShiftRegister74HC595::setAllHigh() {
// Sets all pins of all shift registers to HIGH (1).
void ShiftRegister74HC595::setAllHigh()
{
int i;
for (i = 0; i < _numberOfShiftRegisters; i++)
for (i = 0; i < _numberOfShiftRegisters; i++) {
_digitalValues[i] = 255;
}
setAll(_digitalValues);
}


void ShiftRegister74HC595::setAllLow() {
// Sets all pins of all shift registers to LOW (0).
void ShiftRegister74HC595::setAllLow()
{
int i;
for (i = 0; i < _numberOfShiftRegisters; i++)
_digitalValues[i] = 0;
for (i = 0; i < _numberOfShiftRegisters; i++) {
_digitalValues[i] = 0;
}
setAll(_digitalValues);
}
6 changes: 3 additions & 3 deletions ShiftRegister74HC595.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
ShiftRegister74HC595.h - Library for easy control of the 74HC595 shift register.
Created by Timo Denk (www.simsso.de), Nov 2014.
Additional information are available on http://shiftregister.simsso.de/
ShiftRegister74HC595.h - Library for simplified control of 74HC595 shift registers.
Created by Timo Denk (www.timodenk.com), Nov 2014.
Additional information is available at http://shiftregister.simsso.de/
Released into the public domain.
*/

Expand Down
26 changes: 18 additions & 8 deletions examples/example/example.ino
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
/*
ShiftRegister74HC595.h - Library for easy control of the 74HC595 shift register.
Created by Timo Denk (www.simsso.de), Nov 2014.
Additional information are available on http://shiftregister.simsso.de/
ShiftRegister74HC595 - Library for simplified control of 74HC595 shift registers.
Created by Timo Denk (www.timodenk.com), Nov 2014.
Additional information is available at http://shiftregister.simsso.de/
Released into the public domain.
*/

#include <ShiftRegister74HC595.h>

// create shift register object (number of shift registers, data pin, clock pin, latch pin)
// create a global shift register object
// parameters: (number of shift registers, data pin, clock pin, latch pin)
ShiftRegister74HC595 sr (1, 0, 1, 2);

void setup() {
}

void loop() {

// setting all pins at the same time to either HIGH or LOW
sr.setAllHigh(); // set all pins HIGH
delay(500);

sr.setAllLow(); // set all pins LOW
delay(500);



// setting single pins
for (int i = 0; i < 8; i++) {

sr.set(i, HIGH); // set single pin HIGH
Expand All @@ -33,8 +36,15 @@ void loop() {
uint8_t pinValues[] = { B10101010 };
sr.setAll(pinValues);
delay(1000);


// read pin (zero based)
// read pin (zero based, i.e. 6th pin)
uint8_t stateOfPin5 = sr.get(5);

}


// set pins without immediate update
sr.setNoUpdate(0, HIGH);
sr.setNoUpdate(1, LOW);
// at this point of time, pin 0 and 1 did not change yet
sr.updateRegisters(); // update the pins to the set values
}
20 changes: 8 additions & 12 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
ShiftRegister74HC595 KEYWORD1

ShiftRegister74HC595 KEYWORD1
setAll KEYWORD2

set KEYWORD2

setAllLow KEYWORD2

setAllHigh KEYWORD2

get KEYWORD2

getAll KEYWORD2
set KEYWORD2
setAllLow KEYWORD2
setAllHigh KEYWORD2
get KEYWORD2
getAll KEYWORD2
setNoUpdate KEYWORD2
updateRegisters KEYWORDS2

0 comments on commit ead4fba

Please sign in to comment.