-
Notifications
You must be signed in to change notification settings - Fork 14
/
Arduino.h
247 lines (225 loc) · 6.43 KB
/
Arduino.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* @file Driver/Arduino.h
* @version 1.0
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef OWI_DRIVER_ARDUINO_H
#define OWI_DRIVER_ARDUINO_H
#include "OWI.h"
// Configure: Slave device addressing; skip_rom, match_rom, or match_label
// Use skip_rom for single device on bus otherwise match_rom or match_label
#define MATCH() m_owi.skip_rom()
// #define MATCH() m_owi.match_rom(m_rom)
// #define MATCH() m_owi.match_label(m_label)
/**
* One-Wire Interface (OWI) Remote Arduino Device Driver. Core
* functions are implemented as one-wire communication. See
* OWI/examples/Slave, Slave and Master sketches for examples.
*/
class Arduino : public OWI::Device {
public:
/** Family code. */
static const uint8_t FAMILY_CODE = 0x60;
/**
* Construct One-Wire Interface (OWI) Remote Arduino with given bus
* and device address.
* @param[in] owi bus manager.
* @param[in] rom code (default NULL).
*/
Arduino(OWI& owi, uint8_t* rom = NULL) :
OWI::Device(owi, rom),
m_label(0)
{
}
/**
* Get device label.
* @return short address.
*/
uint8_t label()
{
return (m_label);
}
/**
* Set device label.
* @param[in] label short address.
*/
void label(uint8_t nr)
{
m_label = nr;
}
/**
* Set given pin to given mode (OUTPUT, INPUT, INPUT_PULLUP).
* @param[in] pin digital pin number.
* @param[in] mode pin mode.
* @return zero(0) or negative error code.
*/
int pinMode(int pin, int mode)
{
if (!MATCH()) return (-1);
m_owi.write(PIN_MODE);
m_owi.write(pin, 6);
m_owi.write(mode, 2);
return (0);
}
/**
* Read given pin and return current state or negative error code.
* @param[in] pin digital pin number.
* @return pin state (LOW, HIGH) or negative error code.
*/
int digitalRead(int pin)
{
if (!MATCH()) return (-1);
m_owi.write(DIGITAL_READ);
m_owi.write(pin, 6);
return (m_owi.read(1));
}
/**
* Write given value to given pin. Return zero(0) if successful,
* otherwise negative error code.
* @param[in] pin digital pin number.
* @param[in] value to write pin.
* @return zero(0) or negative error code.
*/
int digitalWrite(int pin, int value)
{
if (!MATCH()) return (-1);
m_owi.write(DIGITAL_WRITE);
m_owi.write(pin, 6);
m_owi.write(value != 0, 1);
return (0);
}
/**
* Read analog value from given pin. Return read value if successful,
* otherwise negative error code.
* @param[in] pin analog pin number.
* @return value read or negative error code.
*/
int analogRead(int pin)
{
if (!MATCH()) return (-1);
analog_read_res_t res;
m_owi.write(ANALOG_READ);
m_owi.write(pin, 6);
delayMicroseconds(200);
if (!m_owi.read(&res, sizeof(res))) return (-1);
return (res.value);
}
/**
* Set given duty to given pulse width modulated (PWM) pin. Return
* zero(0) if successful, otherwise negative error code.
* @param[in] pin digital pin number.
* @param[in] duty of pulse width.
* @return zero(0) or negative error code.
*/
int analogWrite(int pin, int duty)
{
if (!MATCH()) return (-1);
m_owi.write(ANALOG_WRITE);
m_owi.write(pin, 6);
m_owi.write(duty);
return (0);
}
/**
* Return number of digital pins, if successful otherwise negative
* error code.
* @return pins or negative error code.
*/
int num_digital_pins()
{
if (!MATCH()) return (-1);
m_owi.write(DIGITAL_PINS);
return (m_owi.read(6));
}
/**
* Return number of analog inputs, if successful otherwise negative
* error code.
* @return pins or negative error code.
*/
int num_analog_inputs()
{
if (!MATCH()) return (-1);
m_owi.write(ANALOG_PINS);
return (m_owi.read(6));
}
/**
* Read rom identity code for device. Return zero(0) if successful,
* otherwise negative error code.
* @return zero(0) or negative error code.
*/
int read_rom()
{
if (m_owi.read_rom(m_rom)) return (0);
return (-1);
}
/**
* Set rom label for current addressed device. Return zero(0) if
* successful, otherwise negative error code.
* @param[in] nr label number.
* @return zero(0) or negative error code.
*/
int label_rom(uint8_t nr)
{
m_owi.write(OWI::LABEL_ROM);
m_owi.write(nr);
m_label = nr;
return (0);
}
/**
* Print device rom idenity code to given output stream. Return
* zero(0) if successful, otherwise negative error code.
* @param[in] out output stream (default Serial).
* @return zero(0) or negative error code.
*/
int print_rom(Print& out = Serial)
{
out.print(F("family="));
if (m_rom[0] < 0x10) out.print('0');
out.print(m_rom[0], HEX);
out.print(F(",rom="));
for (size_t i = 1; i < OWI::ROM_MAX - 1; i++) {
if (m_rom[i] < 0x10) out.print('0');
out.print(m_rom[i], HEX);
}
out.print(F(",crc="));
uint8_t crc = m_rom[sizeof(OWI::ROM_MAX) - 1];
if (crc < 0x10) out.print('0');
out.print(crc, HEX);
return (0);
}
/** One-Wire Interface (OWI) Remote Arduino Device function codes. */
enum {
PIN_MODE = 0x11, //!< Set pin mode: 6b pin, 2b mode
DIGITAL_READ = 0x22, //!< Read digital pin: 6b pin, 1b return
DIGITAL_WRITE = 0x33, //!< Write digital pin: 6b pin, 1b value
ANALOG_READ = 0x44, //!< Read analog pin: 6b pin, 16b+8b return
ANALOG_WRITE = 0x55, //!< Write analog pin: 6b pin, 8b duty
SRAM_READ = 0x66, //!< SRAM read
SRAM_WRITE = 0x77, //!< SRAM write
EEPROM_READ = 0x88, //!< EEPROM read
EEPROM_WRITE = 0x99, //!< EEPROM write
DIGITAL_PINS = 0xaa, //!< Get number of digital pins: 6b return
ANALOG_PINS = 0xbb //!< Get number of analog inputs: 6b return
};
protected:
/** Short address. */
uint8_t m_label;
/** Return value for ANALOG_READ. */
struct analog_read_res_t {
uint16_t value; //!< Analog value read.
uint8_t crc; //!< Cyclic Redundancy Check-sum.
} __attribute__((packed));
};
#undef MATCH
#endif