semf
i2cmasterhardware.cpp
Go to the documentation of this file.
1
12
13namespace semf
14{
15void I2cMasterHardware::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
37 m_isBusy = true;
38 writeHardware(data, dataSize);
39}
40
41void I2cMasterHardware::read(uint8_t buffer[], size_t bufferSize)
42{
43 SEMF_INFO("buffer %p, bufferSize is %u", buffer, bufferSize);
44 if (m_isBusy)
45 {
46 SEMF_ERROR("is busy");
47 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_IsBusy)));
48 return;
49 }
50 if (buffer == nullptr)
51 {
52 SEMF_ERROR("buffer is nullptr");
53 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_BufferIsNullptr)));
54 return;
55 }
56 if (bufferSize == 0)
57 {
58 SEMF_ERROR("bufferSize is 0");
59 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_BufferSizeIsZero)));
60 return;
61 }
62
63 m_isBusy = true;
64 readHardware(buffer, bufferSize);
65}
66
68{
69 return m_isBusy;
70}
71
73{
74 return m_isBusy;
75}
76
78{
79 return m_address;
80}
81
82void I2cMasterHardware::setAddress(uint8_t address)
83{
84 SEMF_ERROR("set address %u", address);
85 if (m_isBusy)
86 {
87 SEMF_ERROR("set address, is busy");
88 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetAddress_IsBusy)));
89 return;
90 }
91 m_address = address;
92}
93
95{
96 m_frame = frame;
98 {
99 SEMF_INFO("frame mode: first");
100 }
101 else if (m_frame == CommunicationHardware::Frame::Next)
102 {
103 SEMF_INFO("frame mode: next");
104 }
105 else if (m_frame == CommunicationHardware::Frame::Last)
106 {
107 SEMF_INFO("frame mode: last");
108 }
109 else
110 {
111 SEMF_INFO("frame mode: first and last");
112 }
113}
114
116{
117 m_isBusy = isBusy;
118}
119
121{
122 return m_frame;
123}
124
126{
127 m_isBusy = false;
128 SEMF_INFO("data written");
129 dataWritten();
130}
131
133{
134 m_isBusy = false;
135 SEMF_INFO("data available");
137}
138
140{
141 m_isBusy = false;
142 SEMF_ERROR("error");
143 error(thrown);
144}
145} /* 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 setAddress(uint8_t address) override
void write(const uint8_t data[], size_t dataSize) override
For writing data, dataWritten signal will be emitted after successful write.
void setFrame(CommunicationHardware::Frame frame) override
Sets the selected usage of start and stop condition.
bool isBusyReading() const override
Communication hardware is busy reading at the moment.
CommunicationHardware::Frame frame() const
Returns the actual frame mode setting.
uint8_t address() const override
Returns the I2C slave device address.
void onError(Error thrown)
Is called if an error occurred by hardware read or write access. Will emit error signal.
virtual void writeHardware(const uint8_t data[], size_t dataSize)=0
Hardware will write data. After finished write operation, onDataWritten() function will be called.
bool isBusyWriting() const override
Communication hardware is busy writing at the moment.
virtual void readHardware(uint8_t buffer[], size_t bufferSize)=0
Hardware will read data. After finished read operation, onDataAvailable() function will be called.
void setBusy(bool isBusy)
Sets the busy flag.
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
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41