-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpal.cpp
More file actions
154 lines (121 loc) · 3.47 KB
/
expal.cpp
File metadata and controls
154 lines (121 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
expal.cpp - Arduino Class that provides easy interfacing to a shift register along with multiplexing via registers/latches
Copyright (C) 2015 James Vess
All Rights Reserved
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=-= Notes =-=
If you're going to increase the maximum registers from 25 ( Wow ) then you'll need to make
sure you increase the array sizes of REGDATA and REGPINS.
*/
#include "Arduino.h"
#include "Expal.h"
// Private Functions
void Expal::blinkBit(int pin) {
digitalWrite(pin, HIGH);
digitalWrite(pin, LOW);
}
void Expal::blinkBitI(int pin) {
digitalWrite(pin, LOW);
digitalWrite(pin, HIGH);
}
void Expal::shiftOutByte(byte data) {
// Reset the Shift Register
blinkBitI(SHIFTREG_R);
// Write out the bits
int pin = 0;
byte mask = B00000000;
for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
if (data & mask){ // if bitwise AND resolves to true
digitalWrite(SHIFTREG_D,HIGH); // send 1
}
else{ //if bitwise and resolves to false
digitalWrite(SHIFTREG_D,LOW); // send 0
}
// Write the bit
blinkBit(SHIFTREG_C);
pin++;
}
}
int Expal::isNumeric(int num) {
if( num >= 0 && num <= 99999 ) {
return 1;
}
return 0;
}
int Expal::isReg(int num) {
if ( num >= 0 && num <= REGINT ) {
return 1;
}
return 0;
}
// Public Functions
Expal::Expal(int shift_data, int shift_clear, int shift_clock, int registers[] ) {
// Setup Vars
SHIFTREG_D = shift_data;
SHIFTREG_R = shift_clear;
SHIFTREG_C = shift_clock;
// Set Shiftreg Pins
pinMode(SHIFTREG_D, OUTPUT);
pinMode(SHIFTREG_R, OUTPUT);
pinMode(SHIFTREG_C, OUTPUT);
// Set the clear pin to high
digitalWrite(SHIFTREG_R, HIGH);
// Iterate the max register to pull all supplied registers
for(int i = 0; i < sizeof(registers); ++i) {
if ( !isNumeric(registers[i]) ) {
continue;
}
REGDATA[REGINT] = B00000000;
REGPINS[REGINT] = registers[i];
pinMode(registers[i], OUTPUT);
++REGINT;
}
}
void Expal::clear(int registerid) {
if ( !isReg(registerid) ) {
return;
}
// Reset the Shift Register
blinkBit(REGPINS[registerid]);
}
void Expal::writeByte(int registerid, byte data) {
if ( !isReg(registerid) ) {
return;
}
shiftOutByte(data);
blinkBit(REGPINS[registerid]);
REGDATA[registerid] = data;
}
void Expal::clearAll() {
for(int i = 0; i < REGINT; ++i) {
clear(i);
}
}
void Expal::setPin(int registerid, int pin, int value) {
if ( !isReg(registerid) ) {
return;
}
if ( pin < 0 || pin > 8 ) {
return;
}
byte newval;
byte bytemask = B10000000;
bytemask = bytemask >> pin;
if ( value ) {
newval = REGDATA[registerid] | bytemask;
} else {
bytemask = ~bytemask;
newval = REGDATA[registerid] & bytemask;
}
writeByte(registerid, newval);
}