Arduino-OWI
One-Wire Interface (OWI) library for Arduino
Arduino.h
Go to the documentation of this file.
1 
19 #ifndef OWI_DRIVER_ARDUINO_H
20 #define OWI_DRIVER_ARDUINO_H
21 
22 #include "OWI.h"
23 
24 // Configure: Slave device addressing; skip_rom, match_rom, or match_label
25 // Use skip_rom for single device on bus otherwise match_rom or match_label
26 #define MATCH() m_owi.skip_rom()
27 // #define MATCH() m_owi.match_rom(m_rom)
28 // #define MATCH() m_owi.match_label(m_label)
29 
35 class Arduino : public OWI::Device {
36 public:
38  static const uint8_t FAMILY_CODE = 0x60;
39 
46  Arduino(OWI& owi, uint8_t* rom = NULL) :
47  OWI::Device(owi, rom),
48  m_label(0)
49  {
50  }
51 
56  uint8_t label()
57  {
58  return (m_label);
59  }
60 
65  void label(uint8_t nr)
66  {
67  m_label = nr;
68  }
69 
76  int pinMode(int pin, int mode)
77  {
78  if (!MATCH()) return (-1);
80  m_owi.write(pin, 6);
81  m_owi.write(mode, 2);
82  return (0);
83  }
84 
90  int digitalRead(int pin)
91  {
92  if (!MATCH()) return (-1);
94  m_owi.write(pin, 6);
95  return (m_owi.read(1));
96  }
97 
105  int digitalWrite(int pin, int value)
106  {
107  if (!MATCH()) return (-1);
109  m_owi.write(pin, 6);
110  m_owi.write(value != 0, 1);
111  return (0);
112  }
113 
120  int analogRead(int pin)
121  {
122  if (!MATCH()) return (-1);
123  analog_read_res_t res;
125  m_owi.write(pin, 6);
126  delayMicroseconds(200);
127  if (!m_owi.read(&res, sizeof(res))) return (-1);
128  return (res.value);
129  }
130 
138  int analogWrite(int pin, int duty)
139  {
140  if (!MATCH()) return (-1);
142  m_owi.write(pin, 6);
143  m_owi.write(duty);
144  return (0);
145  }
146 
153  {
154  if (!MATCH()) return (-1);
156  return (m_owi.read(6));
157  }
158 
165  {
166  if (!MATCH()) return (-1);
168  return (m_owi.read(6));
169  }
170 
176  int read_rom()
177  {
178  if (m_owi.read_rom(m_rom)) return (0);
179  return (-1);
180  }
181 
188  int label_rom(uint8_t nr)
189  {
191  m_owi.write(nr);
192  m_label = nr;
193  return (0);
194  }
195 
202  int print_rom(Print& out = Serial)
203  {
204  out.print(F("family="));
205  if (m_rom[0] < 0x10) out.print('0');
206  out.print(m_rom[0], HEX);
207  out.print(F(",rom="));
208  for (size_t i = 1; i < OWI::ROM_MAX - 1; i++) {
209  if (m_rom[i] < 0x10) out.print('0');
210  out.print(m_rom[i], HEX);
211  }
212  out.print(F(",crc="));
213  uint8_t crc = m_rom[sizeof(OWI::ROM_MAX) - 1];
214  if (crc < 0x10) out.print('0');
215  out.print(crc, HEX);
216  return (0);
217  }
218 
220  enum {
221  PIN_MODE = 0x11,
222  DIGITAL_READ = 0x22,
223  DIGITAL_WRITE = 0x33,
224  ANALOG_READ = 0x44,
225  ANALOG_WRITE = 0x55,
226  SRAM_READ = 0x66,
227  SRAM_WRITE = 0x77,
228  EEPROM_READ = 0x88,
229  EEPROM_WRITE = 0x99,
230  DIGITAL_PINS = 0xaa,
231  ANALOG_PINS = 0xbb
232  };
233 
234 protected:
236  uint8_t m_label;
237 
240  uint16_t value;
241  uint8_t crc;
242  } __attribute__((packed));
243 };
244 
245 #undef MATCH
246 
247 #endif
Definition: OWI.h:29
uint8_t crc
Cyclic Redundancy Check-sum.
Definition: Arduino.h:241
void label(uint8_t nr)
Definition: Arduino.h:65
Read digital pin: 6b pin, 1b return.
Definition: Arduino.h:222
int print_rom(Print &out=Serial)
Definition: Arduino.h:202
static const uint8_t FAMILY_CODE
Definition: Arduino.h:38
Arduino(OWI &owi, uint8_t *rom=NULL)
Definition: Arduino.h:46
Get number of analog inputs: 6b return.
Definition: Arduino.h:231
Write analog pin: 6b pin, 8b duty.
Definition: Arduino.h:225
Set pin mode: 6b pin, 2b mode.
Definition: Arduino.h:221
uint8_t * rom()
Definition: OWI.h:333
uint8_t label()
Definition: Arduino.h:56
uint8_t m_label
Definition: Arduino.h:236
OWI & m_owi
Definition: OWI.h:340
const int pin
Definition: DS18B20.ino:44
int num_digital_pins()
Definition: Arduino.h:152
int label_rom(uint8_t nr)
Definition: Arduino.h:188
int digitalWrite(int pin, int value)
Definition: Arduino.h:105
uint16_t value
Analog value read.
Definition: Arduino.h:240
virtual void write(uint8_t value, uint8_t bits=CHARBITS)=0
Software::Serial< BOARD::D0 > Serial
Definition: Alarm.ino:8
Software::OWI< BOARD::D7 > owi
Definition: Alarm.ino:7
Set short address (8-bit).
Definition: OWI.h:134
SRAM read.
Definition: Arduino.h:226
int num_analog_inputs()
Definition: Arduino.h:164
Write digital pin: 6b pin, 1b value.
Definition: Arduino.h:223
int analogWrite(int pin, int duty)
Definition: Arduino.h:138
Get number of digital pins: 6b return.
Definition: Arduino.h:230
uint8_t m_rom[ROM_MAX]
Definition: OWI.h:343
int digitalRead(int pin)
Definition: Arduino.h:90
int analogRead(int pin)
Definition: Arduino.h:120
SRAM write.
Definition: Arduino.h:227
int read_rom()
Definition: Arduino.h:176
Read analog pin: 6b pin, 16b+8b return.
Definition: Arduino.h:224
bool read_rom(uint8_t *code)
Definition: OWI.h:222
EEPROM read.
Definition: Arduino.h:228
int pinMode(int pin, int mode)
Definition: Arduino.h:76
static const size_t ROM_MAX
Definition: OWI.h:32
virtual uint8_t read(uint8_t bits=CHARBITS)=0
EEPROM write.
Definition: Arduino.h:229
#define MATCH()
Definition: Arduino.h:26
Device(OWI &owi, const uint8_t *rom=NULL)
Definition: OWI.h:293