From ead4fba1d5a5b41695f6a3ee94b0dbc33b45177d Mon Sep 17 00:00:00 2001 From: Timo Denk Date: Sun, 1 Apr 2018 13:19:52 +0200 Subject: [PATCH] Update comments Add more comments to .cpp file, update README.md file and adjust keywords to #4 --- README.md | 9 ++++-- ShiftRegister74HC595.cpp | 58 ++++++++++++++++++++++++++++-------- ShiftRegister74HC595.h | 6 ++-- examples/example/example.ino | 26 +++++++++++----- keywords.txt | 20 +++++-------- 5 files changed, 80 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index d6f6cb0..643a3e3 100644 --- a/README.md +++ b/README.md @@ -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). \ No newline at end of file diff --git a/ShiftRegister74HC595.cpp b/ShiftRegister74HC595.cpp index a7f8e8d..ae2cd84 100755 --- a/ShiftRegister74HC595.cpp +++ b/ShiftRegister74HC595.cpp @@ -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 @@ -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--) { @@ -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); } diff --git a/ShiftRegister74HC595.h b/ShiftRegister74HC595.h index 3319d96..08a3293 100755 --- a/ShiftRegister74HC595.h +++ b/ShiftRegister74HC595.h @@ -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. */ diff --git a/examples/example/example.ino b/examples/example/example.ino index 6836f65..fe81e64 100644 --- a/examples/example/example.ino +++ b/examples/example/example.ino @@ -1,13 +1,14 @@ /* - 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 -// 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() { @@ -15,13 +16,15 @@ 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 @@ -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 +} \ No newline at end of file diff --git a/keywords.txt b/keywords.txt index d4c5658..3f0f75b 100755 --- a/keywords.txt +++ b/keywords.txt @@ -1,13 +1,9 @@ -ShiftRegister74HC595 KEYWORD1 - +ShiftRegister74HC595 KEYWORD1 setAll KEYWORD2 - -set KEYWORD2 - -setAllLow KEYWORD2 - -setAllHigh KEYWORD2 - -get KEYWORD2 - -getAll KEYWORD2 \ No newline at end of file +set KEYWORD2 +setAllLow KEYWORD2 +setAllHigh KEYWORD2 +get KEYWORD2 +getAll KEYWORD2 +setNoUpdate KEYWORD2 +updateRegisters KEYWORDS2 \ No newline at end of file