COSA
An Object-Oriented Platform for Arduino Programming
OWI.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_OWI_HH
22 #define COSA_OWI_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/IOPin.hh"
26 #include "Cosa/IOStream.hh"
27 
36 class OWI : private IOPin {
37 public:
41  enum {
42  SEARCH_ROM = 0xF0,
43  READ_ROM = 0x33,
44  MATCH_ROM = 0x55,
45  SKIP_ROM = 0xCC,
46  ALARM_SEARCH = 0xEC
47  } __attribute__((packed));
48 
50  static const uint8_t ROM_MAX = 8;
51 
53  static const uint8_t ROMBITS = ROM_MAX * CHARBITS;
54 
58  class Driver {
59  public:
60  enum {
61  FIRST = -1,
62  ERROR = -1,
63  LAST = ROMBITS
64  } __attribute__((packed));
65 
68 
74  Driver(OWI* pin, const char* name = NULL) :
75  NAME((str_P) name),
76  ROM(NULL),
77  m_next(NULL),
78  m_pin(pin)
79  {
80  memset(m_rom, 0, sizeof(m_rom));
81  }
82 
90  Driver(OWI* pin, const uint8_t* rom, const char* name = NULL);
91 
96  uint8_t* rom()
97  {
98  return (m_rom);
99  }
100 
107  bool update_rom();
108 
115  int8_t search_rom(int8_t last = FIRST);
116 
122  bool read_rom();
123 
130  bool match_rom();
131 
137  bool skip_rom();
138 
145  int8_t alarm_search(int8_t last = FIRST);
146 
153  bool connect(uint8_t family, uint8_t index);
154 
159  virtual void on_alarm() {}
160 
161  protected:
162  uint8_t m_rom[ROM_MAX];
163  const uint8_t* ROM;
166 
173  int8_t search(int8_t last = FIRST);
174 
175  friend class OWI;
176  friend IOStream& operator<<(IOStream& outs, OWI& owi);
177  friend IOStream& operator<<(IOStream& outs, Driver& dev);
178  };
179 
183  class Search : protected Driver {
184  public:
191  Search(OWI* owi, uint8_t family = 0) :
192  Driver(owi),
193  m_family(family),
194  m_last(FIRST)
195  {}
196 
201  Driver* next();
202 
206  void reset()
207  {
208  m_last = FIRST;
209  }
210 
211  protected:
212  uint8_t m_family;
213  int8_t m_last;
214  };
215 
216 public:
222  IOPin(pin),
223  m_devices(0),
224  m_device(NULL),
225  m_crc(0)
226  {}
227 
233  bool reset();
234 
241  uint8_t read(uint8_t bits = CHARBITS);
242 
250  bool read(void* buf, uint8_t size);
251 
260  void write(uint8_t value, uint8_t bits = CHARBITS, bool power = false);
261 
269  void write(uint8_t value, void* buf, uint8_t size);
270 
274  void power_off()
275  __attribute__((always_inline))
276  {
277  mode(INPUT_MODE);
278  clear();
279  }
280 
285  Driver* lookup(uint8_t* rom);
286 
293  bool alarm_dispatch();
294 
295 private:
297  uint8_t m_devices;
298 
300  Driver* m_device;
301 
303  uint8_t m_crc;
304 };
305 
313 
320 IOStream& operator<<(IOStream& outs, OWI& owi);
321 
322 #endif
323 
uint8_t * rom()
Definition: OWI.hh:96
Definition: OWI.hh:36
int8_t alarm_search(int8_t last=FIRST)
Definition: OWI.cpp:264
uint8_t pin() const
Definition: Pin.hh:103
friend IOStream & operator<<(IOStream &outs, OWI &owi)
Definition: OWI.cpp:149
#define NULL
Definition: Types.h:101
Mode mode() const
Definition: IOPin.hh:71
void power_off()
Definition: OWI.hh:274
bool reset()
Definition: OWI.cpp:29
static const uint8_t ROMBITS
Definition: OWI.hh:53
void write(uint8_t value, uint8_t bits=CHARBITS, bool power=false)
Definition: OWI.cpp:90
Search(OWI *owi, uint8_t family=0)
Definition: OWI.hh:191
int8_t search(int8_t last=FIRST)
Definition: OWI.cpp:182
bool update_rom()
Definition: OWI.cpp:174
#define CHARBITS
Definition: Types.h:57
const class prog_str * str_P
Definition: Types.h:187
bool read_rom()
Definition: OWI.cpp:236
bool alarm_dispatch()
Definition: OWI.cpp:137
uint8_t m_rom[ROM_MAX]
Definition: OWI.hh:162
OWI(Board::DigitalPin pin)
Definition: OWI.hh:221
virtual void on_alarm()
Definition: OWI.hh:159
bool read() const
Definition: Pin.hh:172
Driver * m_next
Definition: OWI.hh:164
bool match_rom()
Definition: OWI.cpp:244
Definition: IOPin.hh:29
const uint8_t * ROM
Definition: OWI.hh:163
void reset()
Definition: OWI.hh:206
Driver * lookup(uint8_t *rom)
Definition: OWI.cpp:127
OWI * m_pin
Definition: OWI.hh:165
Driver(OWI *pin, const char *name=NULL)
Definition: OWI.hh:74
bool connect(uint8_t family, uint8_t index)
Definition: OWI.cpp:272
bool skip_rom()
Definition: OWI.cpp:256
void clear() const
Definition: OutputPin.hh:124
int8_t search_rom(int8_t last=FIRST)
Definition: OWI.cpp:228
static const uint8_t ROM_MAX
Definition: OWI.hh:50
uint8_t m_family
Definition: OWI.hh:212
int8_t m_last
Definition: OWI.hh:213
str_P NAME
Definition: OWI.hh:67