10
10
*/
11
11
12
12
#include " SparkFun_Qwiic_Relay.h"
13
+ #include < stdint.h>
14
+ #include < Arduino.h>
13
15
14
16
Qwiic_Relay::Qwiic_Relay (uint8_t address)
15
17
{
@@ -66,7 +68,7 @@ uint8_t Qwiic_Relay::getState()
66
68
// This function gets the version number of the SparkFun Single Relay.
67
69
float Qwiic_Relay::singleRelayVersion ()
68
70
{
69
- float version = _readVersion (FIRMWARE_VERSION );
71
+ float version = _readVersion (SFE_SINGLE_FIRMWARE_VERSION );
70
72
return (version);
71
73
}
72
74
@@ -160,15 +162,24 @@ uint8_t Qwiic_Relay::getState(uint8_t relay)
160
162
161
163
// This function changes the I-squared-C address of the Qwiic RFID. The address
162
164
// is written to the memory location in EEPROM that determines its address.
163
- bool Qwiic_Relay::changeAddress (uint8_t newAddress)
165
+ bool Qwiic_Relay::changeAddress (uint8_t newAddress, bool singleRelay )
164
166
{
165
167
166
168
if (newAddress < 0x07 || newAddress > 0x78 ) // Range of legal addresses
167
169
return false ;
168
170
169
- _i2cPort->beginTransmission (_address);
170
- _i2cPort->write (ADDRESS_LOCATION);
171
- _i2cPort->write (newAddress);
171
+ if (singleRelay)
172
+ {
173
+ _i2cPort->beginTransmission (_address);
174
+ _i2cPort->write (SINGLE_CHANGE_ADDRESS);
175
+ _i2cPort->write (newAddress);
176
+ }
177
+ else
178
+ {
179
+ _i2cPort->beginTransmission (_address);
180
+ _i2cPort->write (QUAD_CHANGE_ADDRESS);
181
+ _i2cPort->write (newAddress);
182
+ }
172
183
173
184
if (!_i2cPort->endTransmission ())
174
185
return true ;
@@ -183,13 +194,9 @@ bool Qwiic_Relay::_writeAddress(uint8_t addressToWrite, uint8_t value)
183
194
_i2cPort->write (addressToWrite); // Toggle it on....
184
195
_i2cPort->write (value);
185
196
if (_i2cPort->endTransmission () != 0 )
186
- {
187
197
return false ; // Transaction failed
188
- } // End communcation.
189
198
else
190
- {
191
199
return true ;
192
- }
193
200
}
194
201
// This function handles I-squared-C write commands for turning the relays on.
195
202
// The quad relay relies on the current state of the relay to determine whether
@@ -203,9 +210,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
203
210
{
204
211
_status = _readCommand (RELAY_ONE_STATUS);
205
212
if (_status == QUAD_RELAY_ON)
206
- { // Is it on? Then....
207
213
return ; // Do nothing....
208
- }
209
214
else
210
215
{ // Off?
211
216
_i2cPort->beginTransmission (_address); // Start communication.
@@ -218,9 +223,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
218
223
{
219
224
_status = _readCommand (RELAY_TWO_STATUS);
220
225
if (_status == QUAD_RELAY_ON)
221
- {
222
226
return ;
223
- }
224
227
else
225
228
{
226
229
_i2cPort->beginTransmission (_address);
@@ -233,9 +236,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
233
236
{
234
237
_status = _readCommand (RELAY_THREE_STATUS);
235
238
if (_status == QUAD_RELAY_ON)
236
- {
237
239
return ;
238
- }
239
240
else
240
241
{
241
242
_i2cPort->beginTransmission (_address);
@@ -248,9 +249,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
248
249
{
249
250
_status = _readCommand (RELAY_FOUR_STATUS);
250
251
if (_status == QUAD_RELAY_ON)
251
- {
252
252
return ;
253
- }
254
253
else
255
254
{
256
255
_i2cPort->beginTransmission (_address);
@@ -289,9 +288,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
289
288
{
290
289
_status = _readCommand (RELAY_ONE_STATUS);
291
290
if (_status == QUAD_RELAY_OFF)
292
- { // Is the board off?
293
291
return ; // Do nothing...
294
- }
295
292
else
296
293
{ // Then it must be on...
297
294
_i2cPort->beginTransmission (_address); // Start communication.
@@ -304,9 +301,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
304
301
{
305
302
_status = _readCommand (RELAY_TWO_STATUS);
306
303
if (_status == QUAD_RELAY_OFF)
307
- {
308
304
return ;
309
- }
310
305
else
311
306
{
312
307
_i2cPort->beginTransmission (_address); // Start communication.
@@ -319,9 +314,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
319
314
{
320
315
_status = _readCommand (RELAY_THREE_STATUS);
321
316
if (_status == QUAD_RELAY_OFF)
322
- {
323
317
return ;
324
- }
325
318
else
326
319
{
327
320
_i2cPort->beginTransmission (_address); // Start communication.
@@ -334,9 +327,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
334
327
{
335
328
_status = _readCommand (RELAY_FOUR_STATUS);
336
329
if (_status == QUAD_RELAY_OFF)
337
- {
338
330
return ;
339
- }
340
331
else
341
332
{
342
333
_i2cPort->beginTransmission (_address); // Start communication.
@@ -362,7 +353,7 @@ uint8_t Qwiic_Relay::_readCommand(uint8_t _command)
362
353
_i2cPort->write (_command);
363
354
_i2cPort->endTransmission ();
364
355
365
- _i2cPort->requestFrom (_address, 1 );
356
+ _i2cPort->requestFrom (_address, ( uint8_t ) 1 );
366
357
uint8_t status = _i2cPort->read ();
367
358
return (status);
368
359
}
@@ -374,7 +365,7 @@ float Qwiic_Relay::_readVersion(uint8_t _command)
374
365
_i2cPort->write (_command);
375
366
_i2cPort->endTransmission ();
376
367
377
- _i2cPort->requestFrom (_address, 2 );
368
+ _i2cPort->requestFrom (_address, ( uint8_t ) 2 );
378
369
float _versValue = _i2cPort->read ();
379
370
_versValue += (float )_i2cPort->read () / 10.0 ;
380
371
return (_versValue);
0 commit comments