semf
printer.cpp
Go to the documentation of this file.
1
11
12namespace semf::esh
13{
15: m_uart(uart)
16{
17 m_uart.error.connect(m_uartErrorSlot);
18}
19
20void Printer::print(char character, size_t count)
21{
22 if (m_busy)
23 {
24 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Print_IsBusy)));
25 return;
26 }
27
28 m_busy = true;
29 m_char = character;
30 m_count = count;
31 m_uart.dataWritten.clear();
32 m_uart.dataWritten.connect(m_onCharWrittenSlot);
33 m_uart.write(reinterpret_cast<uint8_t*>(&m_char), 1);
34}
35
36void Printer::print(std::string_view text, size_t count)
37{
38 if (m_busy)
39 {
40 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Print_IsBusy)));
41 return;
42 }
43
44 if (count == 0 || text.empty())
45 {
46 dataWritten();
47 return;
48 }
49
50 m_busy = true;
51 m_string = text;
52 m_count = count;
53 m_uart.dataWritten.clear();
54 m_uart.dataWritten.connect(m_onStringWrittenSlot);
55 m_uart.write(reinterpret_cast<const uint8_t*>(m_string.data()), m_string.size());
56}
57
59{
60 m_uart.dataAvailable.connect(m_onDataAvailableSlot);
61 m_uart.read(&m_availableChar, sizeof(m_availableChar));
62}
63
64bool Printer::isBusy() const
65{
66 return m_busy;
67}
68
69void Printer::onCharWritten()
70{
71 if (--m_count == 0)
72 {
73 onDataWritten();
74 return;
75 }
76 m_uart.write(reinterpret_cast<uint8_t*>(&m_char), 1);
77}
78
79void Printer::onStringWritten()
80{
81 if (--m_count == 0)
82 {
83 onDataWritten();
84 return;
85 }
86 m_uart.write(reinterpret_cast<const uint8_t*>(m_string.data()), m_string.size());
87}
88
89void Printer::onDataAvailable()
90{
91 m_uart.dataAvailable.disconnect(m_onDataAvailableSlot);
92 dataAvailable(m_availableChar);
93}
94
95void Printer::onDataWritten()
96{
97 m_uart.dataWritten.disconnect(m_onStringWrittenSlot);
98 m_busy = false;
99 dataWritten();
100}
101} // namespace semf::esh
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
void clear()
Disconnect all functions or methods.
Definition: signal.h:81
void connect(SlotBase< Arguments... > &slot)
Connect a method to the signal.
Definition: signal.h:94
void disconnect(SlotBase< Arguments... > &slot)
Disonnect a method from the signal.
Definition: signal.h:107
Class for using UART hardware.
Definition: uarthardware.h:22
void write(const uint8_t data[], size_t dataSize) override
For writing data, dataWritten signal will be emitted after successful write.
void read(uint8_t buffer[], size_t bufferSize) override
For reading data, dataAvailable signal will be emitted after successful read.
Signal< Error > error
Definition: communication.h:64
void print(char character, size_t count)
Prints a single character count times.
Definition: printer.cpp:20
Printer(semf::UartHardware &uart)
Constructor.
Definition: printer.cpp:14
bool isBusy() const
Indicates wether the printer is busy.
Definition: printer.cpp:64
void readCharacter()
Triggers a read cyle for reading a single character.
Definition: printer.cpp:58