-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlibIO.cpp
More file actions
219 lines (192 loc) · 5.59 KB
/
Copy pathlibIO.cpp
File metadata and controls
219 lines (192 loc) · 5.59 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
libIO - An I2C Extended IO Library for chipKit.
Texas Instrurments PCF8574 over I2C serial communications.
Created by Skyler Brandt on May 2013 for chipKit.
Copyright 2013 Skyler Brandt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include <libIO.h>
/******************************************************************************
* Constructors
******************************************************************************/
libIO::libIO(int initAddress)
{
this->address = initAddress; //address of the device
this->outReg = 0xFF;
}
/******************************************************************************
* Global Functions
******************************************************************************/
/**********************************************************
*setup the device to use as an output (1) or input (0)
**********************************************************/
void libIO::init(uint8_t inOut)
{
//set up device to be used as an inputs
//must write highs to all the outputs to minimize current
if (inOut == INPUT)
{
allOn();
}
else if (inOut == OUTPUT) //set up as output
{
allOff();
}
else
{
return;
}
}
/**********************************************************
*write either a low or high to a specfic bit
**********************************************************/
void libIO::writeOut(uint8_t tBit, uint8_t state)
{
//write a low (0)
if (state == LOW)
{
setOutOff(tBit);
}
//write a high (1)
else if (state == HIGH)
{
setOutOn(tBit);
}
else
{
return;
}
}
/**********************************************************
*turn all outputs off
**********************************************************/
void libIO::allOff()
{
this->outReg = 0x00;
setIO(this->outReg);
}
/**********************************************************
*turn all outputs on
**********************************************************/
void libIO::allOn()
{
this->outReg = 0xFF;
setIO(this->outReg);
}
/**********************************************************
*Turns on outputs corresponding to the bit mask
**********************************************************/
void libIO::outputMaskOn(uint8_t outputMask)
{
this->outReg |= outputMask;
setIO(this->outReg);
}
/**********************************************************
*Turns off outputs corresponding to the bit mask
**********************************************************/
void libIO::outputMaskOff(uint8_t outputMask)
{
this->outReg &= ~outputMask;
setIO(this->outReg);
}
/**********************************************************
*check specific bit status
**********************************************************/
uint8_t libIO::bitStat(uint8_t tBit)
{
uint8_t tByte = inputReg(); //get input register from device
tByte >>= tBit; //shift off bits
tByte &= 0x01; //determine if the first bit is a 1 or 0
if (tByte == 0x01)
{
return HIGH;
}
else
{
return LOW;
}
}
/**********************************************************
*returns pointer to an array of HIGH/LOW on the status of the input register
**********************************************************/
uint8_t* libIO::inputArray()
{
static uint8_t in[8];
uint8_t tempByte = 0;
uint8_t tByte = inputReg(); //get input register from device
for (uint8_t i=0 ; i<8 ; i++)
{
tempByte = tByte;
tempByte >>= i;
tempByte &= 0x01;
if (tempByte == 0x01)
{
in[i] = HIGH;
}
else
{
in[i] = LOW;
}
}
return in;
}
/******************************************************************************
*Private Functions
******************************************************************************/
/**********************************************************
*turn single bit output on
**********************************************************/
void libIO::setOutOn(uint8_t tBit)
{
uint8_t tByte = powerTwo(tBit); //convert integer to bit mask
this->outReg |= tByte;
setIO(this->outReg);
}
/**********************************************************
*turn single bit output off
**********************************************************/
void libIO::setOutOff(uint8_t tBit)
{
uint_t tByte = powerTwo(tBit); //convert integer to bit mask
this->outReg &= ~tByte;
setIO(this->outReg); //send output register
}
/**********************************************************
*send output Registers via I2C to device
**********************************************************/
void libIO::setIO(uint8_t outputRegister)
{
uint8_t sendByte[2] = {0, outputRegister};
Wire.beginTransmission(this->address);
Wire.send(sendByte, 2);
Wire.endTransmission();
}
/**********************************************************
*read input register
**********************************************************/
uint8_t libIO::inputReg()
{
uint8_t registerByte = 0;
Wire.requestFrom(this->address,2);
while(Wire.available())
{
registerByte = Wire.receive();
}
return registerByte;
}
/**********************************************************
*Power of Two Calculation
**********************************************************/
uint8_t libIO::powerTwo(double n)
{
return pow(2, n);
}