PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
I2CSlave_Demo.cpp

Use an easy I2CSlave class to imitate an I2C slave with address 42

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(I2CSlave_Demo)
create_simple_executable(${PROJECT_NAME} I2CSlave_Demo.cpp)
class MyI2CSlave: public I2CSlave {
public:
static const int ADDRESS = 42;
static const int SHIFTED_ADDRESS = ADDRESS << 1;
public:
template<size_t I2C_BUFFER_SIZE, size_t STACK_SIZE, size_t QUEUE_BUFFER_SIZE>
MyI2CSlave (uint8_t (&buffer)[I2C_BUFFER_SIZE], const uint32_t (&stack)[STACK_SIZE],
uint8_t (&queueBuffer)[QUEUE_BUFFER_SIZE])
: I2CSlave(ADDRESS, buffer, stack),
m_queue(queueBuffer),
m_sum(0) {
}
private:
void on_request () {
while (!this->m_queue.is_empty())
this->m_sum += this->m_queue.dequeue();
this->write(this->m_sum);
}
void on_receive () {
int result;
while (-1 != (result = this->read()))
this->m_queue.insert(static_cast<uint8_t>(result));
}
private:
Queue<uint8_t> m_queue;
uint8_t m_sum;
};
int main () {
uint8_t buffer[32];
uint32_t stack[128];
uint8_t queueBuffer[32];
MyI2CSlave slave(buffer, stack, queueBuffer);
// Start a new cog to monitor the I2C bus and respond to events
Runnable::invoke(slave);
// Run the master
I2CMaster master;
// Set the master to a very low frequency. The on_request() and on_receive() methods in this sample are not
// optimized for high speed communications, and extreme optimizations may be necessary in order to run a synchronous
// serial bus like I2C in slave mode. Those types of optimizations are beyond the scope of this demo, so we will
// simply set the frequency low enough that they are unnecessary.
master.set_frequency(1000);
if (master.ping(MyI2CSlave::SHIFTED_ADDRESS)) {
pwOut << "ACK received!\n";
pwOut << "Expecting 0: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(0)) << '\n';
pwOut << "Expecting 1: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(1)) << '\n';
pwOut << "Expecting 3: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(2)) << '\n';
pwOut << "Expecting 6: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(3)) << '\n';
pwOut << "Expecting 10: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(4)) << '\n';
pwOut << "Expecting 80: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint16_t>(0x1234)) << '\n';
} else
pwOut << "No ack! :(\n I guess we're done.\n";
}
printer.h
PropWare::Queue
A basic first-in, first-out queue implementation. The queue will overwrite itself when the maximum si...
Definition: queue.h:44
i2cslave.h
PropWare::Queue::is_empty
bool is_empty() const
Determine if any elements exist.
Definition: queue.h:100
queue.h
PropWare::I2CSlave::read
int read()
Read the next byte from the receiveBuffer.
Definition: i2cslave.h:153
PropWare::I2CSlave::write
void write(const uint8_t data)
Send the given byte of data on the bus during a request from the bus master.
Definition: i2cslave.h:167
PropWare::I2CSlave::I2CSlave
I2CSlave(const uint8_t address, uint8_t(&buffer)[BUFFER_SIZE], const uint32_t(&stack)[STACK_SIZE], const Pin::Mask sclMask=DEFAULT_SCL_MASK, const Pin::Mask sdaMask=DEFAULT_SDA_MASK)
Create an I2CSlave object (requires static allocation of buffer and stack)
Definition: i2cslave.h:67
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
simpleport.h
main
int main(void)
Definition: GraphicsTest.c:20
PropWare::Runnable
Helper class for creating easy parallel applications.
Definition: runnable.h:75
PropWare::I2CMaster
Basic I2C driver.
Definition: i2cmaster.h:64
PropWare::Queue::dequeue
virtual T dequeue()
Return and remove the oldest value in the buffer.
Definition: queue.h:181
pwOut
PropWare::Printer pwOut
Most common use of printing in PropWare applications (not thread safe; see PropWare::pwSyncOut for mu...
i2cmaster.h
PropWare::SimplePort
The PropWare::SimplePort is the recommended way to use data ports on the Propeller....
Definition: simpleport.h:36
PropWare::Queue::insert
Queue & insert(const T &value)
Definition: queue.h:170
PropWare::I2CSlave
Basic I2C slave driver.
Definition: i2cslave.h:47
MyI2CSlave
Definition: I2CSlave_Demo.cpp:36
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33