-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWI.h
126 lines (118 loc) · 2.74 KB
/
OWI.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
/**
* @file Software/OWI.h
* @version 1.1
*
* @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 SOFTWARE_OWI_H
#define SOFTWARE_OWI_H
#include "OWI.h"
#include "GPIO.h"
/**
* One Wire Interface (OWI) Bus Manager template class using GPIO.
* @param[in] PIN board pin for 1-wire bus.
*/
namespace Software {
template<BOARD::pin_t PIN>
class OWI : public ::OWI {
public:
/**
* Construct one wire bus connected to the given template pin
* parameter.
*/
OWI()
{
m_pin.open_drain();
}
/**
* @override{OWI}
* Reset the one wire bus and check that at least one device is
* presence.
* @return true(1) if successful otherwise false(0).
*/
virtual bool reset()
{
uint8_t retry = RESET_RETRY_MAX;
bool res;
do {
m_pin.output();
delayMicroseconds(490);
noInterrupts();
m_pin.input();
delayMicroseconds(70);
res = m_pin;
interrupts();
delayMicroseconds(410);
} while (retry-- && res);
return (res == 0);
}
/**
* @override{OWI}
* Read the given number of bits from the one wire bus. Default
* number of bits is 8.
* @param[in] bits to be read.
* @return value read.
*/
virtual uint8_t read(uint8_t bits = CHARBITS)
{
uint8_t adjust = CHARBITS - bits;
uint8_t res = 0;
while (bits--) {
noInterrupts();
m_pin.output();
delayMicroseconds(6);
m_pin.input();
delayMicroseconds(9);
res >>= 1;
res |= (m_pin ? 0x80 : 0x00);
interrupts();
delayMicroseconds(55);
}
res >>= adjust;
return (res);
}
/**
* @override{OWI}
* Write the given value to the one wire bus. The bits are written
* from LSB to MSB.
* @param[in] value to write.
* @param[in] bits to be written.
*/
virtual void write(uint8_t value, uint8_t bits = CHARBITS)
{
while (bits--) {
noInterrupts();
m_pin.output();
if (value & 0x01) {
delayMicroseconds(6);
m_pin.input();
delayMicroseconds(64);
}
else {
delayMicroseconds(60);
m_pin.input();
delayMicroseconds(10);
}
interrupts();
value >>= 1;
}
}
using ::OWI::read;
using ::OWI::write;
protected:
/** 1-Wire bus pin. */
GPIO<PIN> m_pin;
};
};
#endif