-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIHCInput.cpp
199 lines (173 loc) · 4.31 KB
/
IHCInput.cpp
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
/*
(C) 2015 dingus.dk J.Ø.N.
This file is part of ArduinoIHC.
ArduinoIHC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ArduinoIHC 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with ArduinoIHC. If not, see <http://www.gnu.org/licenses/>.
*/
#define IHC_USEPINCHANGEINT
#include <Arduino.h>
#ifndef ESP8266
#ifdef IHC_USEPINCHANGEINT
#include <PinChangeInt.h>
#else
#include <pcint.h>
#endif
#endif
#include "IHCInput.h"
#define STARTPULSELENGTH 2500
#define MAXPULSELENGTH 330
#define MINPULSELENGTH 149
#define THRESSHOLDPULSELENGTHLOW 166
#define THRESSHOLDPULSELENGTHHIGH 294
#define MINPULSESPACE 300
IHCinput* IHCinput::pTheFirst = NULL;
IHCinput::IHCinput() {
dataline = 0;
#ifndef IHCINPUT_NO_MULTIPORTSUPPORT
pNext = NULL;
#endif
}
void IHCinput::Begin( int pin) {
// We can only call begin one time, and we can only have one object
if (dataline != 0) return;
#ifndef IHCINPUT_NO_MULTIPORTSUPPORT
pNext = pTheFirst;
#endif
pTheFirst = this;
this->pin = pin;
pinMode(pin, INPUT);
// -1 when searching for start pulse
dataline = -1;
changemask = 0;
input = 0;
startpulse = micros();
// Any state change will trigger the interrupt.
#ifdef ESP8266
attachInterrupt(digitalPinToInterrupt(pin), _PinChangeInterrupt, CHANGE);
#else
#ifdef IHC_USEPINCHANGEINT
attachPinChangeInterrupt( pin, _PinChangeInterrupt, CHANGE);
#else
PCattachInterrupt(pin, _PinChangeInterrupt, CHANGE);
#endif
#endif
}
#ifdef ESP8266
void ICACHE_RAM_ATTR IHCinput::_PinChangeInterrupt() {
#else
void IHCinput::_PinChangeInterrupt() {
#endif
IHCinput* pInput = pTheFirst;
#ifdef ESP8266
pInput->PinChangeInterrupt(digitalRead(pInput->pin));
#else
#ifndef IHCINPUT_NO_MULTIPORTSUPPORT
byte intpin = PCintPort::arduinoPin;
while (pInput->pin != intpin) {
pInput = pInput->pNext;
if (pInput == NULL) return;
}
#endif
#ifdef IHC_USEPINCHANGEINT
pInput->PinChangeInterrupt(PCintPort::pinState);
#else
pInput->PinChangeInterrupt( digitalRead( pInput->pin));
#endif
#endif
}
void IHCinput::PinChangeInterrupt(byte pinstate) {
unsigned long time = micros();
if (pinstate) {
startpulse = time;
if (dataline >= 0) {
unsigned long l = time - endpulse;
if (l < MINPULSESPACE) {
dataline = -1;
return;
}
}
}
else {
endpulse = time;
// pulse length
unsigned long l = time - startpulse;
// Looking for start pulse ?
if (dataline < 0) {
if (l > STARTPULSELENGTH) {
// We have a start
dataline = 0;
newinput = 0;
parity = 0;
#ifdef _DEBUG
pulsecount++;
#endif
}
}
else {
if (l > MAXPULSELENGTH || l < MINPULSELENGTH ||
(l > THRESSHOLDPULSELENGTHLOW && l < THRESSHOLDPULSELENGTHHIGH)) {
// if the pulse is more than 300 it is an error - wait for new start pulse
dataline = -1;
return;
}
#ifdef _DEBUG
if (l < (THRESSHOLDPULSELENGTHHIGH + THRESSHOLDPULSELENGTHLOW) / 2) {
if (l < lowmin) lowmin = l;
if (l > lowmax) lowmax = l;
}
else {
if (l < highmin) highmin = l;
if (l > highmax) highmax = l;
}
#endif
if (dataline >= 8) {
// We ignore bit 8-15
if (dataline == 16) {
// bit 16 is parity
// done - now search for start pulse again
dataline = -1;
// parity error ?
if ((l > THRESSHOLDPULSELENGTHHIGH) ^ (parity & 0x01)) {
return;
}
changemask |= input ^ newinput;
if (changemask) {
input = newinput;
#ifndef IHCINPUT_NO_CALLBACK
OnChange(changemask, input);
#endif
}
}
}
else if (l < THRESSHOLDPULSELENGTHLOW) {
newinput |= 1 << dataline;
parity++;
}
dataline++;
}
}
}
bool IHCinput::GetData(int channel) {
if (channel < 0 || channel > 7) return false;
return (input & (0x01 << channel)) != 0;
}
byte IHCinput::GetInput() {
return input;
}
#ifndef IHCINPUT_NO_CALLBACK
void IHCinput::OnChange(byte changemask, byte data) {
}
#endif
byte IHCinput::GetChangeMask() {
byte mask = changemask;
changemask = 0;
return mask;
}