-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSA08_Simplified.cpp
More file actions
320 lines (305 loc) · 9.06 KB
/
LSA08_Simplified.cpp
File metadata and controls
320 lines (305 loc) · 9.06 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* @file LSA08_Simplified.cpp
* @author Joel Jojo Painuthara (joeljojop@gmail.com)
* @brief Source code of the LSA08_Simplified library
* @version 1.2.0
* @date 2025-05-16
*/
#include "LSA08_Simplified.h"
/**
* @brief Construct a new LSA08 object set to analog mode
*
* @param pin The analog pin to which the LSA08 is connected
*/
LSA08::LSA08(unsigned int pin)
{
this->mode = LSA08_MODE_ANALOG;
this->pin = pin;
}
/**
* @brief Construct a new LSA08 object set to hardware serial mode
*
* @param port The hardware serial port to which the LSA08 is connected
* @param baudrate The baud rate for the serial communication
* @param addr The address of the LSA08
* @param en_pin The pin to which UART enable pin of the LSA08 is connected
*/
LSA08::LSA08(HardwareSerial *port, int baudrate, unsigned int addr, unsigned int en_pin)
{
this->mode = LSA08_MODE_SERIAL;
this->port = port;
this->addr = addr;
this->pin = en_pin;
this->baudrate = baudrate;
}
/**
* @brief Construct a new LSA08 object set to software serial mode
*
* @param port The software serial port to which the LSA08 is connected
* @param baudrate The baud rate for the serial communication
* @param addr The address of the LSA08
* @param en_pin The pin to which the UART enable pin of the LSA08 is connected
*/
LSA08::LSA08(SoftwareSerial *port, int baudrate, unsigned int addr, unsigned int en_pin)
{
this->mode = LSA08_MODE_SOFT_SERIAL;
this->soft_port = port;
this->addr = addr;
this->pin = en_pin;
}
/**
* @brief Initialize the LSA08 sensor
*
* This function sets the pin mode and initializes the serial communication.
* It should be called in the setup() function of the Arduino sketch when using
* the LSA08 in serial mode (both hardware and software serial).
*/
void LSA08::init()
{
if (this->mode == LSA08_MODE_SERIAL)
{
pinMode(this->pin, OUTPUT);
digitalWrite(this->pin, HIGH);
this->port->begin(this->baudrate);
}
else if (this->mode == LSA08_MODE_SOFT_SERIAL)
{
pinMode(this->pin, OUTPUT);
digitalWrite(this->pin, HIGH);
this->soft_port->begin(this->baudrate);
}
}
/**
* @brief Disable line sensor reading from the LSA08
*
* This function sets the UART enable pin to HIGH, disabling the stream of line sensor reading from the LSA08.
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
*
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, otherwise @ref SYS_OK
*/
int LSA08::disable_stream()
{
if (mode != LSA08_MODE_SERIAL && mode != LSA08_MODE_SOFT_SERIAL)
{
return MODE_ERROR;
}
digitalWrite(this->pin, HIGH);
return SYS_OK;
}
/**
* @brief Enable line sensor reading from the LSA08
*
* This function sets the UART enable pin to LOW, enabling the stream of line sensor reading from the LSA08.
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
*
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, otherwise @ref SYS_OK
*/
int LSA08::enable_stream()
{
if (mode != LSA08_MODE_SERIAL && mode != LSA08_MODE_SOFT_SERIAL)
{
return MODE_ERROR;
}
digitalWrite(this->pin, LOW);
return SYS_OK;
}
/**
* @brief Set the UART mode of the LSA08
*
* This function sets the UART mode of the LSA08 to either none, digital, analog, or raw.
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
*
* @param mode The UART mode to set (refer to @ref uart_mode for valid modes)
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET if the
* UART mode is invalid, otherwise @ref SYS_OK
*/
int LSA08::set_uart_mode(uart_mode mode)
{
if (this->mode != LSA08_MODE_SERIAL && this->mode != LSA08_MODE_SOFT_SERIAL)
{
return MODE_ERROR;
}
if (mode > 3 && mode < 0)
{
return BAD_PACKET;
}
return send_packet('D', (unsigned char)mode, COM_TYPE_COMMAND);
}
/**
* @brief Calibrate the LSA08 sensor
*
* This function sends a command to the LSA08 to calibrate the sensor.
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
*
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET
* if request failed, otherwise @ref SYS_OK
*/
int LSA08::calibrate()
{
if (mode != LSA08_MODE_SERIAL && mode != LSA08_MODE_SOFT_SERIAL)
{
return MODE_ERROR;
}
return send_packet('C', 0, COM_TYPE_COMMAND);
}
/**
* @brief Set the line mode of the LSA08 sensor
*
* This function sets the line mode of the LSA08 sensor to either light or dark.
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
*
* @param mode The line mode to set (refer to @ref line_mode for valid modes)
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET if the
* line mode is invalid, otherwise @ref SYS_OK
*/
int LSA08::set_line_mode(line_mode mode)
{
if (this->mode != LSA08_MODE_SERIAL && this->mode != LSA08_MODE_SOFT_SERIAL)
{
return MODE_ERROR;
}
if (mode != LIGHT_LINE && mode != DARK_LINE)
{
return BAD_PACKET;
}
return send_packet('L', (unsigned char)mode, COM_TYPE_COMMAND);
}
/**
* @brief Set the threshold for the LSA08 sensor
*
* This function sets the threshold for the LSA08 sensor to detect lines.
* The threshold value should be between 0 and 7.
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
*
* @param threshold The threshold value to set
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET if the
* threshold value is invalid, otherwise @ref SYS_OK
*/
int LSA08::set_threshold(unsigned int threshold)
{
if (this->mode != LSA08_MODE_SERIAL && this->mode != LSA08_MODE_SOFT_SERIAL)
{
return MODE_ERROR;
}
if (threshold > 7)
{
return BAD_PACKET;
}
return send_packet('T', (unsigned char)threshold, COM_TYPE_COMMAND);
}
/**
* @brief Read the line sensor data from the LSA08
*
* This function reads the line sensor data from the LSA08.
* The data is read from the analog pin if the LSA08 is set to analog mode,
* or from the serial port if it is set to serial mode (both hardware and
* software serial).
*
* @return The line sensor data
*/
unsigned int LSA08::read_line()
{
if (this->mode == LSA08_MODE_SERIAL || this->mode == LSA08_MODE_SOFT_SERIAL)
{
return get_data();
}
else if (this->mode == LSA08_MODE_ANALOG)
{
return analogRead(pin);
}
else
return 0;
}
/**
* @brief Send a packet to the LSA08 sensor
*
* This function sends a request and value (is applicable) to the LSA08 sensor.
* The request and value are sent over the serial port if the LSA08 is set
* to serial mode (both hardware and software serial).
*
* @param request The request to send (refer to the LSA08 documentation for valid requests)
* @param value The value to send with the request (if applicable)
* @param type The type of request - command or data
* @return The response from the LSA08 sensor
*/
unsigned char LSA08::send_packet(unsigned char request, unsigned char value, com_type type)
{
if (mode == LSA08_MODE_SERIAL)
{
port->write(addr);
port->write(request);
port->write(value);
port->write(addr + request + value);
}
else if (mode == LSA08_MODE_SOFT_SERIAL)
{
soft_port->write(addr);
soft_port->write(request);
soft_port->write(value);
soft_port->write(addr + request + value);
}
return get_response(type);
}
/**
* @brief Get the data from the LSA08 sensor
*
* This function reads the data from the LSA08 sensor.
* The data is read from the serial port if the LSA08 is set to serial mode
* (both hardware and software serial).
*
* @return The data read from the LSA08 sensor
*/
unsigned char LSA08::get_data()
{
unsigned char data = 0;
if (mode == LSA08_MODE_SERIAL)
{
while (port->available() > 0)
{
data = port->read();
}
}
else if (mode == LSA08_MODE_SOFT_SERIAL)
{
while (soft_port->available() > 0)
{
data = soft_port->read();
}
}
return data;
}
/**
* @brief Get the response from the LSA08 sensor
*
* This function reads the response from the LSA08 sensor.
* The response is read from the serial port if the LSA08 is set to serial mode
* (both hardware and software serial).
*
* @param type The type of request for which response is awaited (command or data)
* @return The response from the LSA08 sensor
*/
unsigned char LSA08::get_response(com_type type)
{
unsigned char data[2] = {0, 0};
data[0] = get_data();
if (type == COM_TYPE_DATA)
{
return data[0];
}
else if (type == COM_TYPE_COMMAND)
{
data[1] = get_data();
if (data[0] == 'O' && data[1] == 'K')
{
return SYS_OK;
}
else
{
return BAD_PACKET;
}
}
else
{
return 0;
}
}