|
1 |
| -/* |
2 |
| -(C) 2015 dingus.dk J.Ø.N. |
3 |
| -
|
4 |
| -This file is part of ArduinoIHC. |
5 |
| -
|
6 |
| -ArduinoIHC is free software: you can redistribute it and/or modify |
7 |
| -it under the terms of the GNU General Public License as published by |
8 |
| -the Free Software Foundation, either version 3 of the License, or |
9 |
| -(at your option) any later version. |
10 |
| -
|
11 |
| -ArduinoIHC is distributed in the hope that it will be useful, |
12 |
| -but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
| -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
| -GNU General Public License for more details. |
15 |
| -
|
16 |
| -You should have received a copy of the GNU General Public License |
17 |
| -along with ArduinoIHC. If not, see <http://www.gnu.org/licenses/>. |
18 |
| -*/ |
19 |
| -#define IHC_USEPINCHANGEINT |
20 |
| -#include <Arduino.h> |
21 |
| -#ifndef ESP8266 |
22 |
| - #ifdef IHC_USEPINCHANGEINT |
23 |
| - #include <PinChangeInt.h> |
24 |
| - #else |
25 |
| - #include <pcint.h> |
26 |
| - #endif |
27 |
| -#endif |
28 |
| -#include "IHCInput.h" |
29 |
| - |
30 |
| -#define STARTPULSELENGTH 2500 |
31 |
| -#define MAXPULSELENGTH 400 |
32 |
| -#define THRESSHOLDPULSELENGTH 236 |
33 |
| - |
34 |
| -IHCinput* IHCinput::pTheFirst = NULL; |
35 |
| - |
36 |
| - |
37 |
| -IHCinput::IHCinput() { |
38 |
| - |
39 |
| - dataline = 0; |
40 |
| -#ifndef IHCINPUT_NO_MULTIPORTSUPPORT |
41 |
| - pNext = NULL; |
42 |
| -#endif |
43 |
| -} |
44 |
| - |
45 |
| - |
46 |
| -void IHCinput::Begin( int pin) { |
47 |
| - |
48 |
| - // We can only call begin one time, and we can only have one object |
49 |
| - if (dataline != 0) return; |
50 |
| - |
51 |
| -#ifndef IHCINPUT_NO_MULTIPORTSUPPORT |
52 |
| - pNext = pTheFirst; |
53 |
| -#endif |
54 |
| - pTheFirst = this; |
55 |
| - |
56 |
| - this->pin = pin; |
57 |
| - pinMode(pin, INPUT); |
58 |
| - // -1 when searching for start pulse |
59 |
| - dataline = -1; |
60 |
| - changemask = 0; |
61 |
| - input = 0; |
62 |
| - startpulse = micros(); |
63 |
| - // Any state change will trigger the interrupt. |
64 |
| -#ifdef ESP8266 |
65 |
| - attachInterrupt(digitalPinToInterrupt(pin), _PinChangeInterrupt, CHANGE); |
66 |
| -#else |
67 |
| - #ifdef IHC_USEPINCHANGEINT |
68 |
| - attachPinChangeInterrupt( pin, _PinChangeInterrupt, CHANGE); |
69 |
| - #else |
70 |
| - PCattachInterrupt(pin, _PinChangeInterrupt, CHANGE); |
71 |
| - #endif |
72 |
| -#endif |
73 |
| -} |
74 |
| - |
75 |
| - |
76 |
| -#ifdef ESP8266 |
77 |
| -void ICACHE_RAM_ATTR IHCinput::_PinChangeInterrupt() { |
78 |
| -#else |
79 |
| -void IHCinput::_PinChangeInterrupt() { |
80 |
| -#endif |
81 |
| - |
82 |
| - IHCinput* pInput = pTheFirst; |
83 |
| -#ifdef ESP8266 |
84 |
| - pInput->PinChangeInterrupt(digitalRead(pInput->pin)); |
85 |
| -#else |
86 |
| - #ifndef IHCINPUT_NO_MULTIPORTSUPPORT |
87 |
| - byte intpin = PCintPort::arduinoPin; |
88 |
| - while (pInput->pin != intpin) { |
89 |
| - pInput = pInput->pNext; |
90 |
| - if (pInput == NULL) return; |
91 |
| - } |
92 |
| - #endif |
93 |
| - #ifdef IHC_USEPINCHANGEINT |
94 |
| - pInput->PinChangeInterrupt(PCintPort::pinState); |
95 |
| - #else |
96 |
| - pInput->PinChangeInterrupt( digitalRead( pInput->pin)); |
97 |
| - #endif |
98 |
| -#endif |
99 |
| -} |
100 |
| - |
101 |
| -void IHCinput::PinChangeInterrupt(byte pinstate) { |
102 |
| - |
103 |
| - unsigned long time = micros(); |
104 |
| - if (pinstate) { |
105 |
| - startpulse = time; |
106 |
| - } |
107 |
| - else { |
108 |
| - // pulse length |
109 |
| - unsigned long l = time - startpulse; |
110 |
| - // Start pulse ? |
111 |
| - if (dataline < 0) { |
112 |
| - if (l > STARTPULSELENGTH) { |
113 |
| - // We have a start |
114 |
| - dataline = 0; |
115 |
| - newinput = 0; |
116 |
| - parity = 0; |
117 |
| - } |
118 |
| - } |
119 |
| - else { |
120 |
| - if (l > MAXPULSELENGTH) { |
121 |
| - // if the pulse is more than 300 it is an error - wait for new start pulse |
122 |
| - dataline = -1; |
123 |
| - return; |
124 |
| - } |
125 |
| - if (dataline >= 8) { |
126 |
| - // We ignore bit 8-15 |
127 |
| - if (dataline == 16) { |
128 |
| - // bit 16 is parity |
129 |
| - // done - now search for start pulse again |
130 |
| - dataline = -1; |
131 |
| - // parity error ? |
132 |
| - if ((l > THRESSHOLDPULSELENGTH) ^ (parity & 0x01)) { |
133 |
| - return; |
134 |
| - } |
135 |
| - changemask |= input ^ newinput; |
136 |
| - if (changemask) { |
137 |
| - input = newinput; |
138 |
| -#ifndef IHCINPUT_NO_CALLBACK |
139 |
| - OnChange(changemask, input); |
140 |
| -#endif |
141 |
| - } |
142 |
| - } |
143 |
| - } |
144 |
| - else if (l < THRESSHOLDPULSELENGTH) { |
145 |
| - newinput |= 1 << dataline; |
146 |
| - parity++; |
147 |
| - } |
148 |
| - dataline++; |
149 |
| - } |
150 |
| - } |
151 |
| -} |
152 |
| - |
153 |
| -bool IHCinput::GetData(int channel) { |
154 |
| - if (channel < 0 || channel > 7) return false; |
155 |
| - return (input & (0x01 << channel)) != 0; |
156 |
| -} |
157 |
| - |
158 |
| -byte IHCinput::GetInput() { |
159 |
| - return input; |
160 |
| -} |
161 |
| - |
162 |
| - |
163 |
| -#ifndef IHCINPUT_NO_CALLBACK |
164 |
| -void IHCinput::OnChange(byte changemask, byte data) { |
165 |
| - |
166 |
| -} |
167 |
| -#endif |
168 |
| - |
169 |
| -byte IHCinput::GetChangeMask() { |
170 |
| - |
171 |
| - byte mask = changemask; |
172 |
| - changemask = 0; |
173 |
| - return mask; |
174 |
| -} |
| 1 | +/* |
| 2 | +(C) 2015 dingus.dk J.Ø.N. |
| 3 | +
|
| 4 | +This file is part of ArduinoIHC. |
| 5 | +
|
| 6 | +ArduinoIHC is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +ArduinoIHC is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with ArduinoIHC. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | +#define IHC_USEPINCHANGEINT |
| 20 | +#include <Arduino.h> |
| 21 | +#ifndef ESP8266 |
| 22 | + #ifdef IHC_USEPINCHANGEINT |
| 23 | + #include <PinChangeInt.h> |
| 24 | + #else |
| 25 | + #include <pcint.h> |
| 26 | + #endif |
| 27 | +#endif |
| 28 | +#include "IHCInput.h" |
| 29 | + |
| 30 | +#define STARTPULSELENGTH 2500 |
| 31 | +#define MAXPULSELENGTH 400 |
| 32 | +#define THRESSHOLDPULSELENGTH 236 |
| 33 | + |
| 34 | +IHCinput* IHCinput::pTheFirst = NULL; |
| 35 | + |
| 36 | + |
| 37 | +IHCinput::IHCinput() { |
| 38 | + |
| 39 | + dataline = 0; |
| 40 | +#ifndef IHCINPUT_NO_MULTIPORTSUPPORT |
| 41 | + pNext = NULL; |
| 42 | +#endif |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +void IHCinput::Begin( int pin) { |
| 47 | + |
| 48 | + // We can only call begin one time, and we can only have one object |
| 49 | + if (dataline != 0) return; |
| 50 | + |
| 51 | +#ifndef IHCINPUT_NO_MULTIPORTSUPPORT |
| 52 | + pNext = pTheFirst; |
| 53 | +#endif |
| 54 | + pTheFirst = this; |
| 55 | + |
| 56 | + this->pin = pin; |
| 57 | + pinMode(pin, INPUT); |
| 58 | + // -1 when searching for start pulse |
| 59 | + dataline = -1; |
| 60 | + changemask = 0; |
| 61 | + input = 0; |
| 62 | + startpulse = micros(); |
| 63 | + // Any state change will trigger the interrupt. |
| 64 | +#ifdef ESP8266 |
| 65 | + attachInterrupt(digitalPinToInterrupt(pin), _PinChangeInterrupt, CHANGE); |
| 66 | +#else |
| 67 | + #ifdef IHC_USEPINCHANGEINT |
| 68 | + attachPinChangeInterrupt( pin, _PinChangeInterrupt, CHANGE); |
| 69 | + #else |
| 70 | + PCattachInterrupt(pin, _PinChangeInterrupt, CHANGE); |
| 71 | + #endif |
| 72 | +#endif |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +#ifdef ESP8266 |
| 77 | +void ICACHE_RAM_ATTR IHCinput::_PinChangeInterrupt() { |
| 78 | +#else |
| 79 | +void IHCinput::_PinChangeInterrupt() { |
| 80 | +#endif |
| 81 | + |
| 82 | + IHCinput* pInput = pTheFirst; |
| 83 | +#ifdef ESP8266 |
| 84 | + pInput->PinChangeInterrupt(digitalRead(pInput->pin)); |
| 85 | +#else |
| 86 | + #ifndef IHCINPUT_NO_MULTIPORTSUPPORT |
| 87 | + byte intpin = PCintPort::arduinoPin; |
| 88 | + while (pInput->pin != intpin) { |
| 89 | + pInput = pInput->pNext; |
| 90 | + if (pInput == NULL) return; |
| 91 | + } |
| 92 | + #endif |
| 93 | + #ifdef IHC_USEPINCHANGEINT |
| 94 | + pInput->PinChangeInterrupt(PCintPort::pinState); |
| 95 | + #else |
| 96 | + pInput->PinChangeInterrupt( digitalRead( pInput->pin)); |
| 97 | + #endif |
| 98 | +#endif |
| 99 | +} |
| 100 | + |
| 101 | +void IHCinput::PinChangeInterrupt(byte pinstate) { |
| 102 | + |
| 103 | + unsigned long time = micros(); |
| 104 | + if (pinstate) { |
| 105 | + startpulse = time; |
| 106 | + } |
| 107 | + else { |
| 108 | + // pulse length |
| 109 | + unsigned long l = time - startpulse; |
| 110 | + // Start pulse ? |
| 111 | + if (dataline < 0) { |
| 112 | + if (l > STARTPULSELENGTH) { |
| 113 | + // We have a start |
| 114 | + dataline = 0; |
| 115 | + newinput = 0; |
| 116 | + parity = 0; |
| 117 | + } |
| 118 | + } |
| 119 | + else { |
| 120 | + if (l > MAXPULSELENGTH) { |
| 121 | + // if the pulse is more than 300 it is an error - wait for new start pulse |
| 122 | + dataline = -1; |
| 123 | + return; |
| 124 | + } |
| 125 | + if (dataline >= 8) { |
| 126 | + // We ignore bit 8-15 |
| 127 | + if (dataline == 16) { |
| 128 | + // bit 16 is parity |
| 129 | + // done - now search for start pulse again |
| 130 | + dataline = -1; |
| 131 | + // parity error ? |
| 132 | + if ((l > THRESSHOLDPULSELENGTH) ^ (parity & 0x01)) { |
| 133 | + return; |
| 134 | + } |
| 135 | + changemask |= input ^ newinput; |
| 136 | + if (changemask) { |
| 137 | + input = newinput; |
| 138 | +#ifndef IHCINPUT_NO_CALLBACK |
| 139 | + OnChange(changemask, input); |
| 140 | +#endif |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + else if (l < THRESSHOLDPULSELENGTH) { |
| 145 | + newinput |= 1 << dataline; |
| 146 | + parity++; |
| 147 | + } |
| 148 | + dataline++; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +bool IHCinput::GetData(int channel) { |
| 154 | + if (channel < 0 || channel > 7) return false; |
| 155 | + return (input & (0x01 << channel)) != 0; |
| 156 | +} |
| 157 | + |
| 158 | +byte IHCinput::GetInput() { |
| 159 | + return input; |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +#ifndef IHCINPUT_NO_CALLBACK |
| 164 | +void IHCinput::OnChange(byte changemask, byte data) { |
| 165 | + |
| 166 | +} |
| 167 | +#endif |
| 168 | + |
| 169 | +byte IHCinput::GetChangeMask() { |
| 170 | + |
| 171 | + byte mask = changemask; |
| 172 | + changemask = 0; |
| 173 | + return mask; |
| 174 | +} |
0 commit comments