-
Notifications
You must be signed in to change notification settings - Fork 90
/
I2CIO.cpp
248 lines (213 loc) · 5.51 KB
/
I2CIO.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// ---------------------------------------------------------------------------
// Created by Francisco Malpartida on 20/08/11.
// Copyright (C) - 2018
//
// 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
// 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 v3.0
// along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.en.html>.
//
// ---------------------------------------------------------------------------
//
// Thread Safe: No
// Extendable: Yes
//
// @file I2CIO.h
// This file implements a basic IO library using the PCF8574 I2C IO Expander
// chip.
//
// @brief
// Implement a basic IO library to drive the PCF8574* I2C IO Expander ASIC.
// The library implements basic IO general methods to configure IO pin direction
// read and write uint8_t operations and basic pin level routines to set or read
// a particular IO port.
//
//
// @version API 1.0.0
//
// @author F. Malpartida - [email protected]
// ---------------------------------------------------------------------------
#if (ARDUINO < 100)
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#if defined(__AVR_ATtiny84__) || (__AVR_ATtiny2313__) || defined (__AVR_ATtiny85__)
#include "TinyWireM.h" // include this if ATtiny84 or ATtiny85 or ATtiny2313
#define Wire TinyWireM
#else
#if (ARDUINO < 10000)
#include <../Wire/Wire.h>
#else
#include <Wire.h>
#endif
#endif
#include <inttypes.h>
#include "I2CIO.h"
// CLASS VARIABLES
// ---------------------------------------------------------------------------
// CONSTRUCTOR
// ---------------------------------------------------------------------------
I2CIO::I2CIO ( )
{
_i2cAddr = 0x0;
_dirMask = 0xFF; // mark all as INPUTs
_shadow = 0x0; // no values set
_initialised = false;
}
// PUBLIC METHODS
// ---------------------------------------------------------------------------
//
// begin
int I2CIO::begin ( uint8_t i2cAddr )
{
_i2cAddr = i2cAddr;
Wire.begin ( );
_initialised = isAvailable ( _i2cAddr );
if (_initialised)
{
#if (ARDUINO < 100)
_shadow = Wire.receive ();
#else
_shadow = Wire.read (); // Remove the byte read don't need it.
#endif
}
return ( _initialised );
}
//
// pinMode
void I2CIO::pinMode ( uint8_t pin, uint8_t dir )
{
if ( _initialised )
{
if ( OUTPUT == dir )
{
_dirMask &= ~( 1 << pin );
}
else
{
_dirMask |= ( 1 << pin );
}
}
}
//
// portMode
void I2CIO::portMode ( uint8_t dir )
{
if ( _initialised )
{
if ( dir == INPUT )
{
_dirMask = 0xFF;
}
else
{
_dirMask = 0x00;
}
}
}
//
// read
uint8_t I2CIO::read ( void )
{
uint8_t retVal = 0;
if ( _initialised )
{
Wire.requestFrom ( _i2cAddr, (uint8_t)1 );
#if (ARDUINO < 100)
retVal = ( _dirMask & Wire.receive ( ) );
#else
retVal = ( _dirMask & Wire.read ( ) );
#endif
}
return ( retVal );
}
//
// write
int I2CIO::write ( uint8_t value )
{
int status = 0;
if ( _initialised )
{
// Only write HIGH the values of the ports that have been initialised as
// outputs updating the output shadow of the device
//
// 15-FEB-2018 - fix, all I/Os initialized as input must be written as HIGH
// _shadow = ( value & ~(_dirMask) );
_shadow = ( value | _dirMask );
Wire.beginTransmission ( _i2cAddr );
#if (ARDUINO < 100)
Wire.send ( _shadow );
#else
Wire.write ( _shadow );
#endif
status = Wire.endTransmission ();
}
return ( (status == 0) );
}
//
// digitalRead
uint8_t I2CIO::digitalRead ( uint8_t pin )
{
uint8_t pinVal = 0;
// Check if initialised and that the pin is within range of the device
// -------------------------------------------------------------------
if ( ( _initialised ) && ( pin <= 7 ) )
{
// Remove the values which are not inputs and get the value of the pin
pinVal = this->read() & _dirMask;
pinVal = ( pinVal >> pin ) & 0x01; // Get the pin value
}
return (pinVal);
}
//
// digitalWrite
int I2CIO::digitalWrite ( uint8_t pin, uint8_t level )
{
uint8_t writeVal;
int status = 0;
// Check if initialised and that the pin is within range of the device
// -------------------------------------------------------------------
if ( ( _initialised ) && ( pin <= 7 ) )
{
// Only write to HIGH the port if the port has been configured as
// an OUTPUT pin. Add the new state of the pin to the shadow
writeVal = ( 1 << pin ) & ~_dirMask;
if ( level == HIGH )
{
_shadow |= writeVal;
}
else
{
_shadow &= ~writeVal;
}
status = this->write ( _shadow );
}
return ( status );
}
//
// PRIVATE METHODS
// ---------------------------------------------------------------------------
bool I2CIO::isAvailable (uint8_t i2cAddr)
{
int error;
Wire.beginTransmission( i2cAddr );
error = Wire.endTransmission();
if (error==0)
{
return true;
}
else //Some error occured
{
return false;
}
}