-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbv4627.cpp
151 lines (131 loc) · 4.7 KB
/
bv4627.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
/*
BV4627 8 way multi-interface relay board from ByVac
Copyright (c) 2011 Jim Spence. All right reserved.
www.byvac.com - see terms and conditions for using hardware
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
*/
#include "Arduino.h"
#include "bv4627.h"
BV4627::BV4627(char i2adr)
{
_i2rlyadr=i2adr;
I2c.begin();
}
// ==============================================================
// I2C utilities
// ==============================================================
// ==============================================================
// **************************************************************
// gets a 16 bit value from the i2c bus
// **************************************************************
int BV4627::i2_16bit()
{
int rv;
I2c.read(_i2rlyadr, 2); // returns 2 bytes
rv=I2c.receive()*256; // high byte
rv+=I2c.receive(); // low byte
return rv;
}
// ==============================================================
// I2C 8 WAY RELAY
// ==============================================================
// ==============================================================
// **************************************************************
// Switches an individual relay after a given time,
// relay A=1, H=8
// **************************************************************
void BV4627::click(char rly, char on, int del)
{
if(!del) del=1; // 0 is not allowed
if(rly <1) rly = 1;
if(rly>8) rly = 8;
rly+=9; // relays range 10 to 18
// format is rly, on/off, high_del, low_del
uint8_t message[3];
message[0]=on;
message[1]=(del>>8)&0xff;
message[2]=del&0xff;
I2c.write(_i2rlyadr, rly, message, 3);
}
// **************************************************************
// gets the value of an individual relay timer
// **************************************************************
int BV4627::timer(char rly)
{
I2c.write(_i2rlyadr, 30, rly);
// The device will now send data
return i2_16bit();
}
// **************************************************************
// turns all relays off
// **************************************************************
void BV4627::off()
{
I2c.write(_i2rlyadr, 18);
}
// **************************************************************
// turns all relays on
// **************************************************************
void BV4627::on()
{
I2c.write(_i2rlyadr, 19);
}
// **************************************************************
// Binary on - switches relays in accord with the individual bits
// set in param rly. LSB is relay A
// **************************************************************
void BV4627::bin(char rly)
{
I2c.write(_i2rlyadr, 20, rly);
}
// **************************************************************
// sets I2C address - note will not respond to old I2C address
// after a reset
// **************************************************************
void BV4627::setaddress(char newAddress)
{
I2c.write(_i2rlyadr, 82, newAddress);
}
// **************************************************************
// get device id, this should be 4627
// **************************************************************
int BV4627::deviceid()
{
I2c.write(_i2rlyadr, 83);
return i2_16bit();
}
// **************************************************************
// get device firmware version, this is a string!!
// **************************************************************
void BV4627::version(char *ver)
{
char b, tmp[20];
I2c.write(_i2rlyadr, 84);
// return as a string in the format x.x
I2c.read(_i2rlyadr, 2); // returns 2 bytes
b=I2c.receive(); // major byte
itoa(b,tmp,10); // convert to string
*ver=0; // just in case
strcpy(ver,tmp);
strcat(ver,".");
b=I2c.receive(); // minor byte
itoa(b,tmp,10);
strcat(ver,tmp);
}
// **************************************************************
// reset device
// **************************************************************
void BV4627::reset()
{
I2c.write(_i2rlyadr, 85);
}