semf
i2cslavehardware.cpp
Go to the documentation of this file.
1
12
13namespace semf
14{
15void I2cSlaveHardware::write(const uint8_t data[], size_t dataSize)
16{
17 SEMF_INFO("data %p, dataSize is %u", data, dataSize);
18 if (m_isBusy)
19 {
20 SEMF_ERROR("is busy");
21 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Write_IsBusy)));
22 return;
23 }
24 if (data == nullptr)
25 {
26 SEMF_ERROR("data is nullptr");
27 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Write_DataIsNullptr)));
28 return;
29 }
30 if (dataSize == 0)
31 {
32 SEMF_ERROR("dataSize is 0");
33 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Write_DataSizeIsZero)));
34 return;
35 }
36 if (!isListening())
37 {
38 SEMF_WARNING("slave is not listening");
39 }
40
41 m_isBusy = true;
42 writeHardware(data, dataSize);
43}
44
45void I2cSlaveHardware::read(uint8_t buffer[], size_t bufferSize)
46{
47 SEMF_INFO("buffer %p, bufferSize is %u", buffer, bufferSize);
48 if (m_isBusy)
49 {
50 SEMF_ERROR("is busy");
51 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_IsBusy)));
52 return;
53 }
54 if (buffer == nullptr)
55 {
56 SEMF_ERROR("buffer is nullptr");
57 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_BufferIsNullptr)));
58 return;
59 }
60 if (bufferSize == 0)
61 {
62 SEMF_ERROR("bufferSize is 0");
63 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_BufferSizeIsZero)));
64 return;
65 }
66 if (!isListening())
67 {
68 SEMF_WARNING("slave is not listening");
69 }
70
71 m_isBusy = true;
72 readHardware(buffer, bufferSize);
73}
74
76{
77 return m_isBusy;
78}
79
81{
82 return m_isBusy;
83}
84
86{
87 m_frame = frame;
89 {
90 SEMF_INFO("frame mode: first");
91 }
92 else if (m_frame == CommunicationHardware::Frame::Next)
93 {
94 SEMF_INFO("frame mode: next");
95 }
96 else if (m_frame == CommunicationHardware::Frame::Last)
97 {
98 SEMF_INFO("frame mode: last");
99 }
100 else
101 {
102 SEMF_INFO("frame mode: first and last");
103 }
104}
105
107{
108 return m_address;
109}
110
111void I2cSlaveHardware::setAddress(uint8_t address)
112{
113 SEMF_INFO("set address to %d", address);
114 if (m_isBusy)
115 {
116 SEMF_ERROR("is busy");
117 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetAddress_IsBusy)));
118 return;
119 }
120 m_address = address;
122}
123
125{
126 SEMF_INFO("set frequency to %dhz", hz);
127 if (m_isBusy)
128 {
129 SEMF_ERROR("is busy");
130 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetFrequency_IsBusy)));
131 return;
132 }
134}
135
137{
138 SEMF_INFO("starts to listen");
139 if (m_isBusy)
140 {
141 SEMF_ERROR("is busy");
142 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::StartListening_IsBusy)));
143 return;
144 }
146}
147
149{
150 SEMF_INFO("stop listening");
151 if (m_isBusy)
152 {
153 SEMF_ERROR("is busy");
154 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::StopListening_IsBusy)));
155 return;
156 }
158}
159
161{
162 return m_listening;
163}
164
166{
167 m_isBusy = isBusy;
168}
169
171{
172 return m_frame;
173}
174
176{
177 m_listening = listening;
178}
179
181{
182 m_isBusy = false;
183 SEMF_INFO("data written");
184 dataWritten();
185}
186
188{
189 m_isBusy = false;
190 SEMF_INFO("data available");
192}
193
195{
196 m_isBusy = false;
197 SEMF_ERROR("error");
198 error(thrown);
199}
200} /* namespace semf */
@ Next
no start AND no stop condition
@ First
start condition, but no stop condition
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
void onError(Error thrown)
Is called if an error occurred by hardware read or write access. Will emit error signal.
void startListening()
Starts to react on getting addressed.
virtual void readHardware(uint8_t buffer[], size_t bufferSize)=0
Hardware will read data. After finished read operation, onDataAvailable() function will be called.
void read(uint8_t buffer[], size_t bufferSize) override
For reading data, dataAvailable signal will be emitted after successful read.
bool isBusyReading() const override
Communication hardware is busy reading at the moment.
bool isBusyWriting() const override
Communication hardware is busy writing at the moment.
void write(const uint8_t data[], size_t dataSize) override
For writing data, dataWritten signal will be emitted after successful write.
virtual void writeHardware(const uint8_t data[], size_t dataSize)=0
Hardware will write data. After finished write operation, onDataWritten() function will be called.
void onDataWritten()
Is called after data are written by the hardware. Will emit dataWritten signal.
void stopListening()
Quits reacting on getting addressed.
uint8_t address() const override
Returns the I2C slave device address.
void setFrequency(uint32_t hz) override
Sets the speed (I2C baud rate).
void setBusy(bool isBusy)
Sets the busy flag.
void setFrame(CommunicationHardware::Frame frame) override
Sets the selected usage of start and stop condition.
virtual void stopListeningHardware()=0
bool isListening() const
Returns the listening flag.
virtual void startListeningHardware()=0
CommunicationHardware::Frame frame() const
Returns the actual frame mode setting.
virtual void setFrequencyHardware(uint32_t hz)=0
Configures the frequency of the bus.
virtual void setAddressHardware(uint8_t address)=0
Sets the address of the i2c hardware.
void setListening(bool listening)
Sets the listening flag.
void onDataAvailable()
Is called after data is available in the hardware. Will emit dataAvailable signal.
void setAddress(uint8_t address) override
Sets the address of the slave.
Signal< Error > error
Definition: communication.h:64
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_WARNING(...)
Definition: debug.h:40
#define SEMF_INFO(...)
Definition: debug.h:41