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+ */
1416LSA08::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+ */
2030LSA08::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+ */
2947LSA08::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+ */
3762void 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+ */
4786int 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+ */
57104int 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+ */
67124int 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+ */
80146int 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+ */
89165int 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+ */
102189int 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+ */
115212unsigned 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+ */
148266unsigned 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+ */
168296unsigned char LSA08::get_response (com_type type)
169297{
170298 unsigned char data[2 ] = {0 , 0 };
0 commit comments