semf
i2cscanner.cpp
Go to the documentation of this file.
1
12
13namespace semf
14{
16: m_hardware(hardware)
17{
18}
19
20void I2cScanner::startScan(uint16_t firstAddress, uint16_t lastAddress)
21{
22 SEMF_INFO("I2c Scanner - Start");
23 if (m_hardware.isBusyWriting())
24 {
25 SEMF_ERROR("is busy");
26 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::StartScan_IsBusy)));
27 return;
28 }
29
30 m_addressBegin = firstAddress;
31 m_addressEnd = lastAddress + 1;
32 m_addressIterator = firstAddress;
33
34 m_hardware.error.connect(m_onI2cNotAcknowledgeSlot);
35 m_hardware.addressFound.connect(m_onI2cAcknowledgeSlot);
36
37 testAddress();
38}
39
40void I2cScanner::testAddress()
41{
42 if (m_addressIterator >= m_addressEnd)
43 {
44 m_hardware.error.disconnect(m_onI2cNotAcknowledgeSlot);
45 m_hardware.addressFound.disconnect(m_onI2cAcknowledgeSlot);
46 SEMF_INFO("I2c Scanner - Finished\n");
47 finished();
48 }
49 else
50 {
51 m_hardware.checkAddress(static_cast<uint8_t>(m_addressIterator));
52 }
53}
54
55void I2cScanner::onI2cAcknowledge()
56{
57 SEMF_INFO("I2c device found on: 0x%02x\n", m_addressIterator);
58 deviceFound(m_addressIterator++);
59 testAddress();
60}
61
62void I2cScanner::onI2cNotAcknowledge(Error thrown)
63{
64 (void)thrown;
65 m_addressIterator++;
66 testAddress();
67}
68} /* namespace semf */
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
For using the I2C in master mode.
virtual void checkAddress(uint8_t address)=0
Checks if the given address is available on the bus. Emits address found if successful (ACK)....
bool isBusyWriting() const override
Communication hardware is busy writing at the moment.
I2cScanner(I2cMasterHardware &hardware)
Constructor.
Definition: i2cscanner.cpp:15
Signal< uint16_t > deviceFound
Definition: i2cscanner.h:61
void startScan(uint16_t firstAddress=0, uint16_t lastAddress=0x7F)
Starts scan process for the address range from firstAddress to lastAddress. For all found devices,...
Definition: i2cscanner.cpp:20
Signal< Error > error
Definition: i2cscanner.h:65
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
Signal< Error > error
Definition: communication.h:64
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41