semf
i2cslavehardware.h
Go to the documentation of this file.
1
10#ifndef SEMF_COMMUNICATION_I2CSLAVEHARDWARE_H_
11#define SEMF_COMMUNICATION_I2CSLAVEHARDWARE_H_
12
15
16namespace semf
17{
23{
24public:
28 enum class ErrorCode : uint8_t
29 {
30 Write_IsBusy = 0,
31 Write_DataIsNullptr,
32 Write_DataSizeIsZero,
33 Read_IsBusy,
34 Read_BufferIsNullptr,
35 Read_BufferSizeIsZero,
36 SetAddress_IsBusy,
37 SetFrequency_IsBusy,
38 StartListening_IsBusy,
39 StopListening_IsBusy
40 };
41
42 virtual ~I2cSlaveHardware() = default;
43
51 void write(const uint8_t data[], size_t dataSize) override;
59 void read(uint8_t buffer[], size_t bufferSize) override;
60 bool isBusyReading() const override;
61 bool isBusyWriting() const override;
63 uint8_t address() const override;
69 void setAddress(uint8_t address) override;
74 void setFrequency(uint32_t hz) override;
79 void startListening();
84 void stopListening();
89 bool isListening() const;
90
95
96protected:
103 void setBusy(bool isBusy);
114 virtual void writeHardware(const uint8_t data[], size_t dataSize) = 0;
120 virtual void readHardware(uint8_t buffer[], size_t bufferSize) = 0;
122 virtual void startListeningHardware() = 0;
124 virtual void stopListeningHardware() = 0;
129 virtual void setAddressHardware(uint8_t address) = 0;
134 void setListening(bool listening);
139 virtual void setFrequencyHardware(uint32_t hz) = 0;
144 void onDataWritten();
149 void onDataAvailable();
155 void onError(Error thrown);
156
157private:
159 bool m_isBusy = false;
163 bool m_listening = false;
165 uint8_t m_address = 0;
167 static constexpr Error::ClassID kSemfClassId = Error::ClassID::I2cSlaveHardware;
168};
169} /* namespace semf */
170#endif // SEMF_COMMUNICATION_I2CSLAVEHARDWARE_H_
@ FirstAndLast
start AND stop condition
This interface standardized the read and write interface for synchronous slave hardware,...
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
ClassID
Semf class IDs.
Definition: error.h:28
Interface for I2C specific functionalities, which are not solved in a generic way in CommunicationHar...
Definition: i2c.h:25
This class can be used for using the I2C bus as a slave. Call startListening() for receiving I/O-requ...
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.
ErrorCode
Error codes for this class. Error ID identify a unique error() / onError call (excluding transferring...
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
virtual ~I2cSlaveHardware()=default
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 for lightweight signal/slot implementation. One signal can be connected to multiple slots and ...
Definition: signal.h:41