PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
I2CSlave_Demo.cpp
Go to the documentation of this file.
1 
27 
28 using PropWare::Queue;
30 using PropWare::I2CSlave;
32 using PropWare::Port;
33 using PropWare::Pin;
34 using PropWare::Runnable;
35 
36 class MyI2CSlave: public I2CSlave {
37  public:
38  static const int ADDRESS = 42;
39  static const int SHIFTED_ADDRESS = ADDRESS << 1;
40 
41  public:
42  template<size_t I2C_BUFFER_SIZE, size_t STACK_SIZE, size_t QUEUE_BUFFER_SIZE>
43  MyI2CSlave (uint8_t (&buffer)[I2C_BUFFER_SIZE], const uint32_t (&stack)[STACK_SIZE],
44  uint8_t (&queueBuffer)[QUEUE_BUFFER_SIZE])
45  : I2CSlave(ADDRESS, buffer, stack),
46  m_queue(queueBuffer),
47  m_sum(0) {
48  }
49 
50  private:
56  void on_request () {
57  while (!this->m_queue.is_empty())
58  this->m_sum += this->m_queue.dequeue();
59  this->write(this->m_sum);
60  }
61 
67  void on_receive () {
68  int result;
69  while (-1 != (result = this->read()))
70  this->m_queue.insert(static_cast<uint8_t>(result));
71  }
72 
73  private:
74  Queue<uint8_t> m_queue;
75  uint8_t m_sum;
76 };
77 
85 int main () {
86  uint8_t buffer[32];
87  uint32_t stack[128];
88  uint8_t queueBuffer[32];
89  MyI2CSlave slave(buffer, stack, queueBuffer);
90 
91  // Start a new cog to monitor the I2C bus and respond to events
92  Runnable::invoke(slave);
93 
94  // Run the master
95  I2CMaster master;
96 
97  // Set the master to a very low frequency. The on_request() and on_receive() methods in this sample are not
98  // optimized for high speed communications, and extreme optimizations may be necessary in order to run a synchronous
99  // serial bus like I2C in slave mode. Those types of optimizations are beyond the scope of this demo, so we will
100  // simply set the frequency low enough that they are unnecessary.
101  master.set_frequency(1000);
102 
103  if (master.ping(MyI2CSlave::SHIFTED_ADDRESS)) {
104  pwOut << "ACK received!\n";
105 
106  pwOut << "Expecting 0: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(0)) << '\n';
107  pwOut << "Expecting 1: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(1)) << '\n';
108  pwOut << "Expecting 3: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(2)) << '\n';
109  pwOut << "Expecting 6: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(3)) << '\n';
110  pwOut << "Expecting 10: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint8_t>(4)) << '\n';
111  pwOut << "Expecting 80: " << master.get(MyI2CSlave::SHIFTED_ADDRESS, static_cast<uint16_t>(0x1234)) << '\n';
112  } else
113  pwOut << "No ack! :(\n I guess we're done.\n";
114 }
printer.h
PropWare::I2CMaster::set_frequency
void set_frequency(const unsigned int frequency)
Set the bus frequency.
Definition: i2cmaster.h:94
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...
PropWare::I2CMaster::get
uint8_t get(const uint8_t device, const T address) const
Get a single byte with the following format:
Definition: i2cmaster.h:327
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::I2CMaster::ping
bool ping(const uint8_t device) const
Test for the Acknowledge of a device by sending start and the slave address.
Definition: i2cmaster.h:272
PropWare::I2CSlave
Basic I2C slave driver.
Definition: i2cslave.h:47
MyI2CSlave
Definition: I2CSlave_Demo.cpp:36