Arduino Pin Library v4.2.2
An easy to use Arduino library for fast and simultaneous operations on Arduino I/O pins.
Loading...
Searching...
No Matches
PinGroup.h
Go to the documentation of this file.
1
9#include "Pin.h"
10
11#pragma once
12
18class PinGroup {
19 public:
25 template<size_t N>
26 PinGroup(Pin (&pins)[N]) {
27 _offset = pins[0].getOffset();
28 _PIN = pins[0].getPIN();
29 _PORT = pins[0].getPORT();
30 _DDR = pins[0].getDDR();
31 _numbers[0] = pins[0].getNumber();
32 _valid = true;
33
34 for (int i = 1; i < N; i++) {
35 if (_DDR != pins[i].getDDR()) {
36 _valid = false;
37 }
38 _offset |= pins[i].getOffset();
39 _numbers[i] = pins[i].getNumber();
40 }
41 _ioffset = ~_offset;
42 }
43
51 bool operator ==(uint8_t value) {
52 uint8_t status = *_PIN;
53 if ((status & _offset) == _offset) {
54 return (value == HIGH);
55 } else if ((status | _ioffset) == _ioffset) {
56 return (value == LOW);
57 } else {
58 return false;
59 }
60 }
61
69 bool operator !=(uint8_t value) {
70 uint8_t status = *_PIN;
71 if ((status & _offset) == _offset) {
72 return (value == LOW);
73 } else if ((status | _ioffset) == _ioffset) {
74 return (value == HIGH);
75 } else {
76 return false;
77 }
78 }
79
85 PinGroup& operator =(uint8_t state) {
86 uint8_t oldSREG = SREG;
87 cli();
88 if (state == LOW) {
90 } else {
92 }
93 SREG = oldSREG;
94
95 return *this;
96 }
97
103 uint8_t* getNumbers() {
104 return _numbers;
105 }
106
112 uint8_t getOffset() {
113 return _offset;
114 }
115
122 return _ioffset;
123 }
124
130 volatile uint8_t* getPIN() {
131 return _PIN;
132 }
133
139 volatile uint8_t* getPORT() {
140 return _PORT;
141 }
142
148 volatile uint8_t* getDDR() {
149 return _DDR;
150 }
151
157 uint8_t getMode() {
158 uint8_t status = *_DDR;
159 if ((status & _offset) == _offset) {
160 return OUTPUT;
161 } else if ((status | _ioffset) == _ioffset) {
162 return INPUT;
163 } else {
164 return -1;
165 }
166 }
167
173 uint8_t getState() {
174 uint8_t status = *_PORT;
175 if ((status & _offset) == _offset) {
176 return HIGH;
177 } else if ((status | _ioffset) == _ioffset) {
178 return LOW;
179 } else {
180 return -1;
181 }
182 }
183
189 uint8_t getValue() {
190 uint8_t status = *_PIN;
191 if ((status & _offset) == _offset) {
192 return HIGH;
193 } else if ((status | _ioffset) == _ioffset) {
194 return LOW;
195 } else {
196 return -1;
197 }
198 }
199
205 bool isValid() {
206 return _valid;
207 }
208
215 void set(uint8_t mode, uint8_t state) {
216 uint8_t oldSREG = SREG;
217 cli();
218 if (mode == INPUT) {
219 DDR_LOW;
220 } else {
221 DDR_HIGH;
222 }
223 if (state == LOW) {
224 PORT_LOW;
225 } else {
226 PORT_HIGH;
227 }
228 SREG = oldSREG;
229 }
230
236 void setMode(uint8_t mode) {
237 uint8_t oldSREG = SREG;
238 cli();
239 if (mode == INPUT) {
240 DDR_LOW;
241 } else {
242 DDR_HIGH;
243 }
244 SREG = oldSREG;
245 }
246
252 void setState(uint8_t state) {
253 uint8_t oldSREG = SREG;
254 cli();
255 if (state == LOW) {
256 PORT_LOW;
257 } else {
258 PORT_HIGH;
259 }
260 SREG = oldSREG;
261 }
262
266 void setInput() {
267 uint8_t oldSREG = SREG;
268 cli();
269 DDR_LOW;
270 SREG = oldSREG;
271 }
272
276 void setPullupOn() {
277 uint8_t oldSREG = SREG;
278 cli();
279 PORT_HIGH;
280 SREG = oldSREG;
281 }
282
287 uint8_t oldSREG = SREG;
288 cli();
289 PORT_LOW;
290 SREG = oldSREG;
291 }
292
297 uint8_t oldSREG = SREG;
298 cli();
299 DDR_LOW;
300 PORT_HIGH;
301 SREG = oldSREG;
302 }
303
308 uint8_t oldSREG = SREG;
309 cli();
310 DDR_LOW;
311 PORT_LOW;
312 SREG = oldSREG;
313 }
314
318 void setOutput() {
319 uint8_t oldSREG = SREG;
320 cli();
321 DDR_HIGH;
322 SREG = oldSREG;
323 }
324
328 void setHigh() {
329 uint8_t oldSREG = SREG;
330 cli();
331 PORT_HIGH;
332 SREG = oldSREG;
333 }
334
338 void setLow() {
339 uint8_t oldSREG = SREG;
340 cli();
341 PORT_LOW;
342 SREG = oldSREG;
343 }
344
349 uint8_t oldSREG = SREG;
350 cli();
351 DDR_HIGH;
352 PORT_HIGH;
353 SREG = oldSREG;
354 }
355
360 uint8_t oldSREG = SREG;
361 cli();
362 DDR_HIGH;
363 PORT_LOW;
364 SREG = oldSREG;
365 }
366
370 void toggleMode() {
371 uint8_t oldSREG = SREG;
372 cli();
374 SREG = oldSREG;
375 }
376
380 void toggleState() {
381 uint8_t oldSREG = SREG;
382 cli();
384 SREG = oldSREG;
385 }
386
387 private:
388 uint8_t _numbers[8];
389 uint8_t _offset;
390 uint8_t _ioffset;
391 bool _valid;
392 volatile uint8_t* _PIN;
393 volatile uint8_t* _PORT;
394 volatile uint8_t* _DDR;
395};
Fast operations on Arduino I/O pins.
#define DDR_HIGH
Set the DDR register to HIGH for the pin.
Definition Pin.h:18
#define PORT_LOW
Set the PORT register to LOW for the pin.
Definition Pin.h:24
#define PORT_HIGH
Set the PORT register to HIGH for the pin.
Definition Pin.h:22
#define DDR_TOGGLE
Set the DDR register to the inverse for the pin.
Definition Pin.h:19
#define PORT_TOGGLE
Set the PORT register to the inverse for the pin.
Definition Pin.h:23
#define DDR_LOW
Set the DDR register to LOW for the pin.
Definition Pin.h:20
Class for simultaneous operations on Arduino I/O pins.
Definition PinGroup.h:18
void setOutputLow()
Set the pin mode to output and the pin output to LOW.
Definition PinGroup.h:359
PinGroup & operator=(uint8_t state)
Set the pin state.
Definition PinGroup.h:85
void toggleState()
Toggle the pin state (HIGH -> LOW, LOW -> HIGH)
Definition PinGroup.h:380
void setOutput()
Set the pin mode to output.
Definition PinGroup.h:318
void setInput()
Set the pin mode to input.
Definition PinGroup.h:266
void set(uint8_t mode, uint8_t state)
Set the pin mode and pin state.
Definition PinGroup.h:215
void setLow()
Set the pin output to LOW.
Definition PinGroup.h:338
uint8_t getOffset()
Get the pin offset.
Definition PinGroup.h:112
uint8_t getMode()
Get the mode of the pin from the DDR register.
Definition PinGroup.h:157
void setInputPullupOff()
Set the pin mode to input and the pin pullup resistor to off.
Definition PinGroup.h:307
void setPullupOff()
Set the pin pullup resistor to off.
Definition PinGroup.h:286
volatile uint8_t * getPORT()
Get a pointer to the PORT register.
Definition PinGroup.h:139
void setState(uint8_t state)
Set the pin state.
Definition PinGroup.h:252
void setHigh()
Set the pin output to HIGH.
Definition PinGroup.h:328
volatile uint8_t * getPIN()
Get a pointer to the PIN register.
Definition PinGroup.h:130
uint8_t * getNumbers()
Get the pin numbers.
Definition PinGroup.h:103
uint8_t getInverseOffset()
Get the inverse pin offset.
Definition PinGroup.h:121
PinGroup(Pin(&pins)[N])
Default constructor.
Definition PinGroup.h:26
volatile uint8_t * getDDR()
Get a pointer to the DDR register.
Definition PinGroup.h:148
bool operator!=(uint8_t value)
Compare the value of the pin.
Definition PinGroup.h:69
void toggleMode()
Toggle the pin mode (OUTPUT -> INPUT, INPUT -> OUTPUT)
Definition PinGroup.h:370
void setOutputHigh()
Set the pin mode to output and the pin output to HIGH.
Definition PinGroup.h:348
void setInputPullupOn()
Set the pin mode to input and the pin pullup resistor to on.
Definition PinGroup.h:296
uint8_t getState()
Get the state of the pin from the PORT register.
Definition PinGroup.h:173
bool operator==(uint8_t value)
Compare the value of the pin.
Definition PinGroup.h:51
void setPullupOn()
Set the pin pullup resistor to on.
Definition PinGroup.h:276
uint8_t getValue()
Get the value of the pin from the PIN register.
Definition PinGroup.h:189
void setMode(uint8_t mode)
Set the pin mode.
Definition PinGroup.h:236
bool isValid()
Check the group to ensure all pins use the same registers.
Definition PinGroup.h:205
Class for fast operations on Arduino I/O pins.
Definition Pin.h:40