/
SM125.cpp
223 lines (195 loc) · 4.35 KB
/
SM125.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
/*
Arduino Library for Sonmicro SM125 RFID reader (over I2C protocol)
go to http://www.sonmicro.com/en/index.php?option=com_content&view=article&id=48&Itemid=64 for more information
License: CC BY-SA 3.0: Creative Commons Share-alike 3.0. Feel free
to use and abuse this code however you'd like. If you find it useful
please attribute, and SHARE-ALIKE!
Created July 2013
by Jonathan Ruiz de Garibay
*/
#include "Arduino.h"
#include "SM125.h"
#include <Wire.h>
//#define SM125_DEBUG
//
// begin
//
// This library use I2C protocol so, it is necesary to specified an I2C address.
boolean SM125::begin(uint8_t address){
//save address, init outputs and I2C
_address = address;
_dreadyPin = 0xFF;
_outputValues = 0x00;
Wire.begin();
}
boolean SM125::begin(uint8_t address, uint8_t dreadyPin){
//save address, init outputs and I2C
_address = address;
_dreadyPin = dreadyPin;
pinMode(_dreadyPin, INPUT);
_outputValues = 0x00;
Wire.begin();
}
//
// anyTag
//
// Check Dready pin from sm125 module and return true if this pin is active
// This function need pin Dready is declared
boolean SM125::anyTag() {
if (_dreadyPin == 0xFF)
return false;
return digitalRead(_dreadyPin);
}
//
// readTag
//
// Read a RFID tag and return true or false if there is a tag or not, respectively.
String SM125::readTag(){
String tagId = "";
byte data;
//first, read SM125 state
Wire.beginTransmission(_address);
Wire.write(READ_TAG);
Wire.requestFrom(_address, 1);
Wire.endTransmission();
delay(1);
if ((Wire.available() == 1) && (Wire.read() == DATA_READY)){
//read 5 bytes; the first and the last are unused
Wire.requestFrom(_address, 5);
Wire.endTransmission();
delay(1);
if (Wire.available() == 5){
Wire.read();
data = Wire.read();
tagId += toHexString(data);
#ifdef SM125_DEBUG
Serial.print("Data 0: ");
Serial.println(data, HEX);
#endif
data = Wire.read();
tagId += toHexString(data);
#ifdef SM125_DEBUG
Serial.print("Data 1: ");
Serial.println(data, HEX);
#endif
data = Wire.read();
tagId += toHexString(data);
#ifdef SM125_DEBUG
Serial.print("Data 2: ");
Serial.println(data, HEX);
#endif
Wire.read();
}
//read 5 bytes; only the first one is needed
Wire.requestFrom(_address, 5);
Wire.endTransmission();
delay(1);
if (Wire.available() == 5){
data = Wire.read();
tagId += toHexString(data);
#ifdef SM125_DEBUG
Serial.print("Data 3: ");
Serial.println(data, HEX);
#endif
//el resto de bytes no se usan
Wire.read();
Wire.read();
Wire.read();
Wire.read();
}
}
else{
#ifdef SM125_DEBUG
Serial.println("No tag available");
#endif
}
return tagId;
}
//
// writeOutput
//
// Set sm125 outputs pin to high or low.
void SM125::writeOutput(uint8_t output, uint8_t value){
//get the new output status
uint8_t tempValues = 0x00;
if (output == 0){
if (value == HIGH)
tempValues = 0x01;
}
else{
if (value == HIGH)
tempValues = 0x02;
}
if (value == HIGH)
tempValues = tempValues | _outputValues;
else
tempValues = tempValues & _outputValues;
//calculate the checksum
uint8_t checksum = WRITE_OUTPUT + tempValues;
//send command
Wire.beginTransmission(_address);
Wire.write(WRITE_OUTPUT);
Wire.write(tempValues);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(checksum);
Wire.requestFrom(_address, 1);
Wire.endTransmission();
delay(1);
if (Wire.available() == 1){
byte request = Wire.read();
if (request == COMMAND_OK){
#ifdef SM125_DEBUG
Serial.println("Output Updated");
#endif
_outputValues = tempValues;
}
}
}
//
// readInput
//
// Get the state of the sm125 input pin.
uint8_t SM125::readInput(){
//Send command
Wire.beginTransmission(_address);
Wire.write(READ_INPUT);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(READ_INPUT);
Wire.requestFrom(_address, 1);
Wire.endTransmission();
//read reply and evaluate
delay(1);
if (Wire.available() == 1){
byte request = Wire.read();
if (request == INPUT_LOW){
#ifdef SM125_DEBUG
Serial.println("Input LOW");
#endif
return 0x00;
}
else{
#ifdef SM125_DEBUG
Serial.println("Input HIGH");
#endif
return 0x01;
}
}
return 0xFF;
}
String SM125::toHexString(byte value) {
String data = "";
if (value < 16)
data += "0";
data += String(value, HEX);
data.toUpperCase();
return data;
}