PicoLC Arduino Library
Arduino library for PicoLC hardware
Loading...
Searching...
No Matches
PicoLCSerial.h
1#pragma once
2
3#include <Arduino.h>
4
5/**
6 * @brief Provides Arduino Stream-compatible access to the PicoLC RS-485 port.
7 *
8 * PicoLCSerial wraps the hardware UART and RS-485 transmit/receive control
9 * pins. The current implementation is blocking: write operations do not
10 * return until the requested data has been transmitted and the interface has
11 * returned to receive mode.
12 *
13 * Instances are created and owned by PicoLC.
14 */
15class PicoLCSerial : public Stream {
16 friend class PicoLC;
17
18private:
19 PicoLCSerial();
20
21 bool _started = false;
22
23 /**
24 * @brief Selects the RS-485 transceiver direction.
25 *
26 * @param writing true to enable transmission; false to enable reception.
27 */
28 void setTR(bool writing);
29
30public:
31 /**
32 * @brief Initializes the RS-485 UART and transceiver-control pins.
33 *
34 * Calling begin() after end() reinitializes the UART and associated pins.
35 *
36 * @param baud Serial baud rate.
37 * @param config Arduino serial framing configuration.
38 */
39 void begin(uint32_t baud, uint16_t config = SERIAL_8N1);
40
41 /**
42 * @brief Releases PicoLC control of the UART and transceiver-control pins.
43 *
44 * Received data already present in the UART receive buffer is retained.
45 * A later call to begin() reinitializes the interface.
46 */
47 void end();
48
49 /**
50 * @brief Returns the number of received bytes available to read.
51 *
52 * @return Number of available bytes, or 0 when the interface is not
53 * started.
54 */
55 int available() override;
56
57 /**
58 * @brief Reads the next received byte.
59 *
60 * @return The next byte as an unsigned value, or -1 when no data is
61 * available or the interface is not started.
62 */
63 int read() override;
64
65 /**
66 * @brief Returns the next received byte without removing it.
67 *
68 * @return The next byte as an unsigned value, or -1 when no data is
69 * available or the interface is not started.
70 */
71 int peek() override;
72
73 /**
74 * @brief Transmits one byte over RS-485.
75 *
76 * This operation is blocking and returns after transmission completes and
77 * the transceiver has returned to receive mode.
78 *
79 * @param byte Byte to transmit.
80 * @return Number of bytes written, or 0 when the interface is not started.
81 */
82 size_t write(uint8_t byte) override;
83
84 /**
85 * @brief Transmits a buffer over RS-485.
86 *
87 * This operation is blocking and returns after the complete buffer has
88 * been transmitted and the transceiver has returned to receive mode.
89 *
90 * @param data Pointer to the data to transmit.
91 * @param length Number of bytes to transmit.
92 * @return Number of bytes written, or 0 when the interface is not started.
93 */
94 size_t write(const uint8_t* data, size_t length) override;
95
96 /**
97 * @brief Waits for pending UART transmission to complete.
98 *
99 * This method wraps the underlying Arduino serial implementation. It does
100 * nothing when the interface is not started.
101 */
102 void flush() override;
103
104 /**
105 * @brief Reports whether the RS-485 interface is initialized.
106 *
107 * @return true after begin() and until end() is called.
108 */
109 bool started() const;
110
111 using Print::write;
112};
void begin(uint32_t baud, uint16_t config=SERIAL_8N1)
Initializes the RS-485 UART and transceiver-control pins.
bool started() const
Reports whether the RS-485 interface is initialized.
size_t write(uint8_t byte) override
Transmits one byte over RS-485.
void flush() override
Waits for pending UART transmission to complete.
int available() override
Returns the number of received bytes available to read.
int read() override
Reads the next received byte.
void end()
Releases PicoLC control of the UART and transceiver-control pins.
int peek() override
Returns the next received byte without removing it.