Skip to content

Commit eba68f4

Browse files
authored
Merge pull request #1 from csd-robocon-nitk/dev
Add changes for v1,2.0
2 parents 370ab39 + 9dc16ab commit eba68f4

File tree

5 files changed

+206
-33
lines changed

5 files changed

+206
-33
lines changed

.github/workflows/libraryci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ jobs:
99
- uses: actions/checkout@v3
1010
- uses: arduino/[email protected]
1111
with:
12-
library-manager: submit
12+
library-manager: update
1313
compliance: specification
1414
project-type: library

LSA08_Simplified.cpp

Lines changed: 146 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
/**
2-
* Source code of the LSA08_Simplified library
3-
* Developed by CSD Robocon NITK
4-
*
5-
* This is free software. You can redistribute it and/or modify it under
6-
* the terms of MIT Licence.
7-
* To view a copy of this license, visit http://opensource.org/licenses/mit-license.php
8-
*
9-
* version: 1.0.0
2+
* @file LSA08_Simplified.cpp
3+
* @author Joel Jojo Painuthara ([email protected])
4+
* @brief Source code of the LSA08_Simplified library
5+
* @version 1.2.0
6+
* @date 2025-05-16
107
*/
118

129
#include "LSA08_Simplified.h"
1310

11+
/**
12+
* @brief Construct a new LSA08 object set to analog mode
13+
*
14+
* @param pin The analog pin to which the LSA08 is connected
15+
*/
1416
LSA08::LSA08(unsigned int pin)
1517
{
1618
this->mode = LSA08_MODE_ANALOG;
1719
this->pin = pin;
1820
}
1921

22+
/**
23+
* @brief Construct a new LSA08 object set to hardware serial mode
24+
*
25+
* @param port The hardware serial port to which the LSA08 is connected
26+
* @param baudrate The baud rate for the serial communication
27+
* @param addr The address of the LSA08
28+
* @param en_pin The pin to which UART enable pin of the LSA08 is connected
29+
*/
2030
LSA08::LSA08(HardwareSerial *port, int baudrate, unsigned int addr, unsigned int en_pin)
2131
{
2232
this->mode = LSA08_MODE_SERIAL;
@@ -26,6 +36,14 @@ LSA08::LSA08(HardwareSerial *port, int baudrate, unsigned int addr, unsigned int
2636
this->baudrate = baudrate;
2737
}
2838

39+
/**
40+
* @brief Construct a new LSA08 object set to software serial mode
41+
*
42+
* @param port The software serial port to which the LSA08 is connected
43+
* @param baudrate The baud rate for the serial communication
44+
* @param addr The address of the LSA08
45+
* @param en_pin The pin to which the UART enable pin of the LSA08 is connected
46+
*/
2947
LSA08::LSA08(SoftwareSerial *port, int baudrate, unsigned int addr, unsigned int en_pin)
3048
{
3149
this->mode = LSA08_MODE_SOFT_SERIAL;
@@ -34,16 +52,37 @@ LSA08::LSA08(SoftwareSerial *port, int baudrate, unsigned int addr, unsigned int
3452
this->pin = en_pin;
3553
}
3654

55+
/**
56+
* @brief Initialize the LSA08 sensor
57+
*
58+
* This function sets the pin mode and initializes the serial communication.
59+
* It should be called in the setup() function of the Arduino sketch when using
60+
* the LSA08 in serial mode (both hardware and software serial).
61+
*/
3762
void LSA08::init()
3863
{
39-
pinMode(this->pin, OUTPUT);
40-
digitalWrite(this->pin, HIGH);
41-
if(this->mode == LSA08_MODE_SERIAL)
42-
this->port->begin(baudrate);
64+
if (this->mode == LSA08_MODE_SERIAL)
65+
{
66+
pinMode(this->pin, OUTPUT);
67+
digitalWrite(this->pin, HIGH);
68+
this->port->begin(this->baudrate);
69+
}
4370
else if (this->mode == LSA08_MODE_SOFT_SERIAL)
44-
this->soft_port->begin(9600);
71+
{
72+
pinMode(this->pin, OUTPUT);
73+
digitalWrite(this->pin, HIGH);
74+
this->soft_port->begin(this->baudrate);
75+
}
4576
}
4677

78+
/**
79+
* @brief Disable line sensor reading from the LSA08
80+
*
81+
* This function sets the UART enable pin to HIGH, disabling the stream of line sensor reading from the LSA08.
82+
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
83+
*
84+
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, otherwise @ref SYS_OK
85+
*/
4786
int LSA08::disable_stream()
4887
{
4988
if (mode != LSA08_MODE_SERIAL && mode != LSA08_MODE_SOFT_SERIAL)
@@ -54,6 +93,14 @@ int LSA08::disable_stream()
5493
return SYS_OK;
5594
}
5695

96+
/**
97+
* @brief Enable line sensor reading from the LSA08
98+
*
99+
* This function sets the UART enable pin to LOW, enabling the stream of line sensor reading from the LSA08.
100+
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
101+
*
102+
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, otherwise @ref SYS_OK
103+
*/
57104
int LSA08::enable_stream()
58105
{
59106
if (mode != LSA08_MODE_SERIAL && mode != LSA08_MODE_SOFT_SERIAL)
@@ -64,6 +111,16 @@ int LSA08::enable_stream()
64111
return SYS_OK;
65112
}
66113

114+
/**
115+
* @brief Set the UART mode of the LSA08
116+
*
117+
* This function sets the UART mode of the LSA08 to either none, digital, analog, or raw.
118+
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
119+
*
120+
* @param mode The UART mode to set (refer to @ref uart_mode for valid modes)
121+
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET if the
122+
* UART mode is invalid, otherwise @ref SYS_OK
123+
*/
67124
int LSA08::set_uart_mode(uart_mode mode)
68125
{
69126
if (this->mode != LSA08_MODE_SERIAL && this->mode != LSA08_MODE_SOFT_SERIAL)
@@ -77,6 +134,15 @@ int LSA08::set_uart_mode(uart_mode mode)
77134
return send_packet('D', (unsigned char)mode, COM_TYPE_COMMAND);
78135
}
79136

137+
/**
138+
* @brief Calibrate the LSA08 sensor
139+
*
140+
* This function sends a command to the LSA08 to calibrate the sensor.
141+
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
142+
*
143+
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET
144+
* if request failed, otherwise @ref SYS_OK
145+
*/
80146
int LSA08::calibrate()
81147
{
82148
if (mode != LSA08_MODE_SERIAL && mode != LSA08_MODE_SOFT_SERIAL)
@@ -86,6 +152,16 @@ int LSA08::calibrate()
86152
return send_packet('C', 0, COM_TYPE_COMMAND);
87153
}
88154

155+
/**
156+
* @brief Set the line mode of the LSA08 sensor
157+
*
158+
* This function sets the line mode of the LSA08 sensor to either light or dark.
159+
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
160+
*
161+
* @param mode The line mode to set (refer to @ref line_mode for valid modes)
162+
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET if the
163+
* line mode is invalid, otherwise @ref SYS_OK
164+
*/
89165
int LSA08::set_line_mode(line_mode mode)
90166
{
91167
if (this->mode != LSA08_MODE_SERIAL && this->mode != LSA08_MODE_SOFT_SERIAL)
@@ -99,6 +175,17 @@ int LSA08::set_line_mode(line_mode mode)
99175
return send_packet('L', (unsigned char)mode, COM_TYPE_COMMAND);
100176
}
101177

178+
/**
179+
* @brief Set the threshold for the LSA08 sensor
180+
*
181+
* This function sets the threshold for the LSA08 sensor to detect lines.
182+
* The threshold value should be between 0 and 7.
183+
* To be used only when the LSA08 is set to serial mode (both hardware and software serial).
184+
*
185+
* @param threshold The threshold value to set
186+
* @return @ref MODE_ERROR if the LSA08 is not set to serial mode, @ref BAD_PACKET if the
187+
* threshold value is invalid, otherwise @ref SYS_OK
188+
*/
102189
int LSA08::set_threshold(unsigned int threshold)
103190
{
104191
if (this->mode != LSA08_MODE_SERIAL && this->mode != LSA08_MODE_SOFT_SERIAL)
@@ -112,6 +199,16 @@ int LSA08::set_threshold(unsigned int threshold)
112199
return send_packet('T', (unsigned char)threshold, COM_TYPE_COMMAND);
113200
}
114201

202+
/**
203+
* @brief Read the line sensor data from the LSA08
204+
*
205+
* This function reads the line sensor data from the LSA08.
206+
* The data is read from the analog pin if the LSA08 is set to analog mode,
207+
* or from the serial port if it is set to serial mode (both hardware and
208+
* software serial).
209+
*
210+
* @return The line sensor data
211+
*/
115212
unsigned int LSA08::read_line()
116213
{
117214
if (this->mode == LSA08_MODE_SERIAL || this->mode == LSA08_MODE_SOFT_SERIAL)
@@ -126,25 +223,46 @@ unsigned int LSA08::read_line()
126223
return 0;
127224
}
128225

129-
unsigned char LSA08::send_packet(unsigned char command, unsigned char value, com_type type)
226+
/**
227+
* @brief Send a packet to the LSA08 sensor
228+
*
229+
* This function sends a request and value (is applicable) to the LSA08 sensor.
230+
* The request and value are sent over the serial port if the LSA08 is set
231+
* to serial mode (both hardware and software serial).
232+
*
233+
* @param request The request to send (refer to the LSA08 documentation for valid requests)
234+
* @param value The value to send with the request (if applicable)
235+
* @param type The type of request - command or data
236+
* @return The response from the LSA08 sensor
237+
*/
238+
unsigned char LSA08::send_packet(unsigned char request, unsigned char value, com_type type)
130239
{
131240
if (mode == LSA08_MODE_SERIAL)
132241
{
133242
port->write(addr);
134-
port->write(command);
243+
port->write(request);
135244
port->write(value);
136-
port->write(addr + command + value);
245+
port->write(addr + request + value);
137246
}
138247
else if (mode == LSA08_MODE_SOFT_SERIAL)
139248
{
140249
soft_port->write(addr);
141-
soft_port->write(command);
250+
soft_port->write(request);
142251
soft_port->write(value);
143-
soft_port->write(addr + command + value);
252+
soft_port->write(addr + request + value);
144253
}
145254
return get_response(type);
146255
}
147256

257+
/**
258+
* @brief Get the data from the LSA08 sensor
259+
*
260+
* This function reads the data from the LSA08 sensor.
261+
* The data is read from the serial port if the LSA08 is set to serial mode
262+
* (both hardware and software serial).
263+
*
264+
* @return The data read from the LSA08 sensor
265+
*/
148266
unsigned char LSA08::get_data()
149267
{
150268
unsigned char data = 0;
@@ -165,6 +283,16 @@ unsigned char LSA08::get_data()
165283
return data;
166284
}
167285

286+
/**
287+
* @brief Get the response from the LSA08 sensor
288+
*
289+
* This function reads the response from the LSA08 sensor.
290+
* The response is read from the serial port if the LSA08 is set to serial mode
291+
* (both hardware and software serial).
292+
*
293+
* @param type The type of request for which response is awaited (command or data)
294+
* @return The response from the LSA08 sensor
295+
*/
168296
unsigned char LSA08::get_response(com_type type)
169297
{
170298
unsigned char data[2] = {0, 0};

LSA08_Simplified.h

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,95 @@
11
/**
2-
* Header for LSA08_Simplified library
3-
* Developed by CSD Robocon NITK
4-
*
5-
* This is free software. You can redistribute it and/or modify it under
6-
* the terms of MIT Licence.
7-
* To view a copy of this license, visit http://opensource.org/licenses/mit-license.php
8-
*
9-
* version: 1.0.0
2+
* @file LSA08_Simplified.h
3+
* @author Joel Jojo Painuthara ([email protected])
4+
* @brief Header file of the LSA08_Simplified library
5+
* @version 1.2.0
6+
* @date 2025-05-16
107
*/
118

129
#include <Arduino.h>
1310
#include <SoftwareSerial.h>
1411

12+
/**
13+
* @brief Error code when using LSA08 in the wrong mode
14+
*
15+
*/
1516
#define MODE_ERROR -1
17+
/**
18+
* @brief Error code when data being sent to LSA08 is invalid or connection
19+
* is not established
20+
*
21+
*/
1622
#define BAD_PACKET 1
23+
/**
24+
* @brief Code when no error occurs
25+
*
26+
*/
1727
#define SYS_OK 0
1828

29+
/**
30+
* @brief Mode of communication with the LSA08 sensor
31+
*
32+
*/
1933
typedef enum
2034
{
21-
LSA08_MODE_ANALOG,
22-
LSA08_MODE_DIGITAL,
35+
/** Analog mode via analog pin */
36+
LSA08_MODE_ANALOG,
37+
/** Digital mode via 8 digital pins */
38+
LSA08_MODE_DIGITAL,
39+
/** Hardware serial mode via UART pins*/
2340
LSA08_MODE_SERIAL,
41+
/** Software serial mode */
2442
LSA08_MODE_SOFT_SERIAL
2543
} lsa08_mode;
2644

45+
/**
46+
* @brief UART mode of the LSA08 sensor
47+
*
48+
*/
2749
typedef enum
2850
{
51+
/** No UART mode (No data output)*/
2952
UART_MODE_NONE,
53+
/** Digital UART mode (1 byte digital value from sensor)*/
3054
UART_MODE_DIGITAL,
55+
/** Analog UART mode (1 byte line position value. Value between 0-70 of
56+
* line is detected, 255 otherwise)*/
3157
UART_MODE_ANALOG,
58+
/** Raw UART mode (9 bytes of raw analog value from sensor)*/
3259
UART_MODE_RAW
3360
} uart_mode;
3461

62+
/**
63+
* @brief Line mode of the LSA08 sensor
64+
*
65+
*/
3566
typedef enum
3667
{
68+
/** Detect dark line */
3769
DARK_LINE,
70+
/** Detect light line */
3871
LIGHT_LINE
3972
} line_mode;
4073

74+
/**
75+
* @brief Type of request sent to LSA08 sensor
76+
*
77+
*/
4178
typedef enum
4279
{
80+
/** Command request (Response is OK if command is valid) */
4381
COM_TYPE_COMMAND,
82+
/** Data request (Response is the data from the sensor) */
4483
COM_TYPE_DATA
4584
} com_type;
4685

86+
/**
87+
* @brief Definition of LSA08 object
88+
*
89+
* This class provides methods to initialize, configure, and read data from
90+
* the LSA08 line sensor. It currently supports analog, hardware and software serial
91+
* communication modes.
92+
*/
4793
class LSA08
4894
{
4995
private:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LSA08_Simplified
22

3-
![v1.1.0](https://img.shields.io/badge/Version-1.0.0-blue)
3+
![v1.2.0](https://img.shields.io/badge/Version-1.2.0-blue)
44
![Library CI](https://github.com/csd-robocon-nitk/LSA08_Simplified/actions/workflows/libraryci.yml/badge.svg)
55

66
An easy to use Arduino library for interfacing LSA08 line sensor.

0 commit comments

Comments
 (0)