COSA
An Object-Oriented Platform for Arduino Programming
WIO.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_WIO_HH
22 #define COSA_WIO_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/IOStream.hh"
26 #include "Cosa/Wireless.hh"
27 
34 class WIO : public IOStream::Device {
35 public:
37  static const uint8_t PAYLOAD_MAX = 30;
38 
45  WIO(Wireless::Driver* dev, uint8_t dest, uint8_t port = 0x00) :
46  IOStream::Device(),
47  m_ix(0),
48  m_dev(dev),
49  m_dest(dest),
50  m_port(port)
51  {
52  }
53 
59  virtual int room()
60  {
61  return (sizeof(m_buffer) - m_ix);
62  }
63 
72  virtual int putchar(char c)
73  {
74  m_buffer[m_ix++] = c;
75  if (UNLIKELY(m_ix == sizeof(m_buffer) || c == '\n')) flush();
76  return (c & 0xff);
77  }
78 
84  virtual int flush()
85  {
86  int res = (m_dev->send(m_dest, m_port, m_buffer, m_ix) == m_ix ? 0 : -1);
87  m_ix = 0;
88  return (res);
89  }
90 
91 private:
93  uint8_t m_buffer[PAYLOAD_MAX];
94 
96  uint8_t m_ix;
97 
99  Wireless::Driver* m_dev;
100 
102  uint8_t m_dest;
103 
105  uint8_t m_port;
106 };
107 
108 #endif
Definition: WIO.hh:34
virtual int putchar(char c)
Definition: WIO.hh:72
virtual int send(uint8_t dest, uint8_t port, const iovec_t *vec)=0
virtual int flush()
Definition: WIO.hh:84
virtual int room()
Definition: WIO.hh:59
WIO(Wireless::Driver *dev, uint8_t dest, uint8_t port=0x00)
Definition: WIO.hh:45
static const uint8_t PAYLOAD_MAX
Definition: WIO.hh:37
#define UNLIKELY(x)
Definition: Types.h:153