Arduino-OWI
One-Wire Interface (OWI) library for Arduino
OWI.h
Go to the documentation of this file.
1 
19 #ifndef SOFTWARE_OWI_H
20 #define SOFTWARE_OWI_H
21 
22 #include "OWI.h"
23 #include "GPIO.h"
24 
29 namespace Software {
30 template<BOARD::pin_t PIN>
31 class OWI : public ::OWI {
32 public:
37  OWI()
38  {
39  m_pin.open_drain();
40  }
41 
48  virtual bool reset()
49  {
50  uint8_t retry = RESET_RETRY_MAX;
51  bool res;
52  do {
53  m_pin.output();
54  delayMicroseconds(490);
55  noInterrupts();
56  m_pin.input();
57  delayMicroseconds(70);
58  res = m_pin;
59  interrupts();
60  delayMicroseconds(410);
61  } while (retry-- && res);
62  return (res == 0);
63  }
64 
72  virtual uint8_t read(uint8_t bits = CHARBITS)
73  {
74  uint8_t adjust = CHARBITS - bits;
75  uint8_t res = 0;
76  while (bits--) {
77  noInterrupts();
78  m_pin.output();
79  delayMicroseconds(6);
80  m_pin.input();
81  delayMicroseconds(9);
82  res >>= 1;
83  res |= (m_pin ? 0x80 : 0x00);
84  interrupts();
85  delayMicroseconds(55);
86  }
87  res >>= adjust;
88  return (res);
89  }
90 
98  virtual void write(uint8_t value, uint8_t bits = CHARBITS)
99  {
100  while (bits--) {
101  noInterrupts();
102  m_pin.output();
103  if (value & 0x01) {
104  delayMicroseconds(6);
105  m_pin.input();
106  delayMicroseconds(64);
107  }
108  else {
109  delayMicroseconds(60);
110  m_pin.input();
111  delayMicroseconds(10);
112  }
113  interrupts();
114  value >>= 1;
115  }
116  }
117 
118  using ::OWI::read;
119  using ::OWI::write;
120 
121 protected:
123  GPIO<PIN> m_pin;
124 };
125 };
126 #endif
virtual uint8_t read(uint8_t bits=CHARBITS)
Definition: OWI.h:72
virtual void write(uint8_t value, uint8_t bits=CHARBITS)
Definition: OWI.h:98
static const uint8_t RESET_RETRY_MAX
Definition: OWI.h:348
virtual bool reset()
Definition: OWI.h:48
Definition: OWI.h:29
#define CHARBITS
Definition: OWI.h:23
GPIO< PIN > m_pin
Definition: OWI.h:123