|
5 | 5 | Released into the public domain. |
6 | 6 | */ |
7 | 7 |
|
8 | | -#include "Arduino.h" |
9 | | -#include "ShiftRegister74HC595.h" |
10 | | - |
11 | | - |
12 | | -// ShiftRegister74HC595 constructor |
13 | | -ShiftRegister74HC595::ShiftRegister74HC595(int numberOfShiftRegisters, int serialDataPin, int clockPin, int latchPin) |
14 | | -{ |
15 | | - // set attributes |
16 | | - _numberOfShiftRegisters = numberOfShiftRegisters; |
17 | | - |
18 | | - _clockPin = clockPin; |
19 | | - _serialDataPin = serialDataPin; |
20 | | - _latchPin = latchPin; |
21 | | - |
22 | | - // define pins as outputs |
23 | | - pinMode(clockPin, OUTPUT); |
24 | | - pinMode(serialDataPin, OUTPUT); |
25 | | - pinMode(latchPin, OUTPUT); |
26 | | - |
27 | | - // set pins low |
28 | | - digitalWrite(clockPin, LOW); |
29 | | - digitalWrite(serialDataPin, LOW); |
30 | | - digitalWrite(latchPin, LOW); |
31 | | - |
32 | | - // allocates the specified number of bytes and initializes them to zero |
33 | | - _digitalValues = (uint8_t *)malloc(numberOfShiftRegisters * sizeof(uint8_t)); |
34 | | - memset(_digitalValues, 0, numberOfShiftRegisters * sizeof(uint8_t)); |
35 | | - |
36 | | - updateRegisters(); // reset shift register |
37 | | -} |
38 | | - |
39 | | - |
40 | | -// ShiftRegister74HC595 destructor |
41 | | -// The memory allocated in the constructor is also released. |
42 | | -ShiftRegister74HC595::~ShiftRegister74HC595() |
43 | | -{ |
44 | | - free(_digitalValues); |
45 | | -} |
46 | | - |
47 | | - |
48 | | -// Set all pins of the shift registers at once. |
49 | | -// digitalVAlues is a uint8_t array where the length is equal to the number of shift registers. |
50 | | -void ShiftRegister74HC595::setAll(const uint8_t * digitalValues) |
51 | | -{ |
52 | | - memcpy( _digitalValues, digitalValues, _numberOfShiftRegisters); // dest, src, size |
53 | | - updateRegisters(); |
54 | | -} |
55 | | - |
56 | | - |
57 | | -// Experimental |
58 | | -// The same as setAll, but the data is located in PROGMEM |
59 | | -// For example with: |
60 | | -// const uint8_t myFlashData[] PROGMEM = { 0x0F, 0x81 }; |
61 | | -#ifdef __AVR__ |
62 | | -void ShiftRegister74HC595::setAll_P(const uint8_t * digitalValuesProgmem) |
63 | | -{ |
64 | | - PGM_VOID_P p = reinterpret_cast<PGM_VOID_P>(digitalValuesProgmem); |
65 | | - memcpy_P( _digitalValues, p, _numberOfShiftRegisters); |
66 | | - updateRegisters(); |
67 | | -} |
68 | | -#endif |
| 8 | +#include <Arduino.h> |
69 | 9 |
|
70 | | - |
71 | | -// Retrieve all states of the shift registers' output pins. |
72 | | -// The returned array's length is equal to the numbe of shift registers. |
73 | | -uint8_t * ShiftRegister74HC595::getAll() |
74 | | -{ |
75 | | - return _digitalValues; |
76 | | -} |
77 | | - |
78 | | - |
79 | | -// Set a specific pin to either HIGH (1) or LOW (0). |
80 | | -// The pin parameter is a positive, zero-based integer, indicating which pin to set. |
81 | | -void ShiftRegister74HC595::set(int pin, uint8_t value) |
82 | | -{ |
83 | | - setNoUpdate(pin, value); |
84 | | - updateRegisters(); |
85 | | -} |
86 | | - |
87 | | - |
88 | | -// Updates the shift register pins to the stored output values. |
89 | | -// This is the function that actually writes data into the shift registers of the 74HC595 |
90 | | -void ShiftRegister74HC595::updateRegisters() |
91 | | -{ |
92 | | - for (int i = _numberOfShiftRegisters - 1; i >= 0; i--) { |
93 | | - shiftOut(_serialDataPin, _clockPin, MSBFIRST, _digitalValues[i]); |
94 | | - } |
95 | | - |
96 | | - digitalWrite(_latchPin, HIGH); |
97 | | - digitalWrite(_latchPin, LOW); |
98 | | -} |
99 | | - |
100 | | - |
101 | | -// Equivalent to set(int pin, uint8_t value), except the physical shift register is not updated. |
102 | | -// Should be used in combination with updateRegisters(). |
103 | | -void ShiftRegister74HC595::setNoUpdate(int pin, uint8_t value) |
104 | | -{ |
105 | | - if (value == 1) { |
106 | | - _digitalValues[pin / 8] |= 1 << (pin % 8); |
107 | | - } |
108 | | - else { |
109 | | - _digitalValues[pin / 8] &= ~(1 << (pin % 8)); |
110 | | - } |
111 | | -} |
112 | | - |
113 | | - |
114 | | -// Returns the state of the given pin. |
115 | | -// Either HIGH (1) or LOW (0) |
116 | | -uint8_t ShiftRegister74HC595::get(int pin) |
117 | | -{ |
118 | | - return (_digitalValues[pin / 8] >> (pin % 8)) & 1; |
119 | | -} |
120 | | - |
121 | | - |
122 | | -// Sets all pins of all shift registers to HIGH (1). |
123 | | -void ShiftRegister74HC595::setAllHigh() |
124 | | -{ |
125 | | - for (int i = 0; i < _numberOfShiftRegisters; i++) { |
126 | | - _digitalValues[i] = 255; |
127 | | - } |
128 | | - updateRegisters(); |
129 | | -} |
130 | | - |
131 | | - |
132 | | -// Sets all pins of all shift registers to LOW (0). |
133 | | -void ShiftRegister74HC595::setAllLow() |
134 | | -{ |
135 | | - for (int i = 0; i < _numberOfShiftRegisters; i++) { |
136 | | - _digitalValues[i] = 0; |
137 | | - } |
138 | | - updateRegisters(); |
139 | | -} |
| 10 | +#include "ShiftRegister74HC595.h" |
0 commit comments