Arduino Pin Library  v4.2.0
An easy to use Arduino library for fast and simultaneous operations on Arduino I/O pins.
PinGroup.h
Go to the documentation of this file.
1 
8 #include "Pin.h"
9 
10 #pragma once
11 
12 
18 class PinGroup {
19  public:
20  // ################################# Constructors #################################
21  template<size_t N>
22  PinGroup(Pin (&pins)[N]) {
23  _offset = pins[0].getOffset();
24  _PIN = pins[0].getPIN();
25  _PORT = pins[0].getPORT();
26  _DDR = pins[0].getDDR();
27  _numbers[0] = pins[0].getNumber();
28  _valid = true;
29 
30  for (int i = 1; i < N; i++) {
31  if (_DDR != pins[i].getDDR()) {
32  _valid = false;
33  }
34  _offset |= pins[i].getOffset();
35  _numbers[i] = pins[i].getNumber();
36  }
37  _ioffset = ~_offset;
38  }
39 
40  // ################################# Operators #################################
41 
49  bool operator ==(uint8_t value) {
50  uint8_t status = *_PIN;
51  if ((status & _offset) == _offset) {
52  return (value == HIGH);
53  } else if ((status | _ioffset) == _ioffset) {
54  return (value == LOW);
55  } else {
56  return false;
57  }
58  }
59 
67  bool operator !=(uint8_t value) {
68  uint8_t status = *_PIN;
69  if ((status & _offset) == _offset) {
70  return (value == LOW);
71  } else if ((status | _ioffset) == _ioffset) {
72  return (value == HIGH);
73  } else {
74  return false;
75  }
76  }
77 
83  PinGroup& operator =(uint8_t state) {
84  uint8_t oldSREG = SREG;
85  cli();
86  if (state == LOW) {
87  PORT_LOW;
88  } else {
89  PORT_HIGH;
90  }
91  SREG = oldSREG;
92 
93  return *this;
94  }
95 
96  // ################################# Getters #################################
97 
103  uint8_t* getNumbers() {
104  return _numbers;
105  }
106 
112  uint8_t getOffset() {
113  return _offset;
114  }
115 
121  uint8_t getInverseOffset() {
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 
209 
210  // ################################# Setters #################################
211 
212  // #################### Generic ####################
213 
220  void set(uint8_t mode, uint8_t state) {
221  uint8_t oldSREG = SREG;
222  cli();
223  if (mode == INPUT) {
224  DDR_LOW;
225  } else {
226  DDR_HIGH;
227  }
228  if (state == LOW) {
229  PORT_LOW;
230  } else {
231  PORT_HIGH;
232  }
233  SREG = oldSREG;
234  }
235 
241  void setMode(uint8_t mode) {
242  uint8_t oldSREG = SREG;
243  cli();
244  if (mode == INPUT) {
245  DDR_LOW;
246  } else {
247  DDR_HIGH;
248  }
249  SREG = oldSREG;
250  }
251 
257  void setState(uint8_t state) {
258  uint8_t oldSREG = SREG;
259  cli();
260  if (state == LOW) {
261  PORT_LOW;
262  } else {
263  PORT_HIGH;
264  }
265  SREG = oldSREG;
266  }
267 
268  // #################### Input ####################
269 
273  void setInput() {
274  uint8_t oldSREG = SREG;
275  cli();
276  DDR_LOW;
277  SREG = oldSREG;
278  }
279 
283  void setPullupOn() {
284  uint8_t oldSREG = SREG;
285  cli();
286  PORT_HIGH;
287  SREG = oldSREG;
288  }
289 
293  void setPullupOff() {
294  uint8_t oldSREG = SREG;
295  cli();
296  PORT_LOW;
297  SREG = oldSREG;
298  }
299 
304  uint8_t oldSREG = SREG;
305  cli();
306  DDR_LOW;
307  PORT_HIGH;
308  SREG = oldSREG;
309  }
310 
315  uint8_t oldSREG = SREG;
316  cli();
317  DDR_LOW;
318  PORT_LOW;
319  SREG = oldSREG;
320  }
321 
322  // #################### Output ####################
323 
327  void setOutput() {
328  uint8_t oldSREG = SREG;
329  cli();
330  DDR_HIGH;
331  SREG = oldSREG;
332  }
333 
337  void setHigh() {
338  uint8_t oldSREG = SREG;
339  cli();
340  PORT_HIGH;
341  SREG = oldSREG;
342  }
343 
347  void setLow() {
348  uint8_t oldSREG = SREG;
349  cli();
350  PORT_LOW;
351  SREG = oldSREG;
352  }
353 
357  void setOutputHigh() {
358  uint8_t oldSREG = SREG;
359  cli();
360  DDR_HIGH;
361  PORT_HIGH;
362  SREG = oldSREG;
363  }
364 
368  void setOutputLow() {
369  uint8_t oldSREG = SREG;
370  cli();
371  DDR_HIGH;
372  PORT_LOW;
373  SREG = oldSREG;
374  }
375 
376 
377  // ################################# Utilities #################################
378 
379  // #################### Toggle ####################
380 
384  void toggleMode() {
385  uint8_t oldSREG = SREG;
386  cli();
387  DDR_TOGGLE;
388  SREG = oldSREG;
389  }
390 
394  void toggleState() {
395  uint8_t oldSREG = SREG;
396  cli();
397  PORT_TOGGLE;
398  SREG = oldSREG;
399  }
400 
401  private:
402  // Variables
403  uint8_t _numbers[8];
404  uint8_t _offset;
405  uint8_t _ioffset;
406  bool _valid;
407  volatile uint8_t* _PIN;
408  volatile uint8_t* _PORT;
409  volatile uint8_t* _DDR;
410 };
void setInput()
Set the pin mode to input.
Definition: PinGroup.h:273
Class for fast operations on Arduino I/O pins.
Definition: Pin.h:39
uint8_t getOffset()
Get the pin offset.
Definition: PinGroup.h:112
void setState(uint8_t state)
Set the pin state.
Definition: PinGroup.h:257
uint8_t getState()
Get the state of the pin from the PORT register.
Definition: PinGroup.h:173
#define DDR_LOW
Set the DDR register to LOW for the pin.
Definition: Pin.h:18
void setInputPullupOff()
Set the pin mode to input and the pin pullup resistor to off.
Definition: PinGroup.h:314
uint8_t getInverseOffset()
Get the inverse pin offset.
Definition: PinGroup.h:121
bool isValid()
Check the group to ensure all pins use the same registers.
Definition: PinGroup.h:205
volatile uint8_t * getPORT()
Get a pointer to the PORT register.
Definition: PinGroup.h:139
void setOutput()
Set the pin mode to output.
Definition: PinGroup.h:327
#define PORT_TOGGLE
Set the PORT register to the inverse for the pin.
Definition: Pin.h:21
void setPullupOff()
Set the pin pullup resistor to off.
Definition: PinGroup.h:293
bool operator!=(uint8_t value)
Compare the value of the pin.
Definition: PinGroup.h:67
uint8_t getValue()
Get the value of the pin from the PIN register.
Definition: PinGroup.h:189
Fast operations on Arduino I/O pins.
void setLow()
Set the pin output to LOW.
Definition: PinGroup.h:347
#define DDR_HIGH
Set the DDR register to HIGH for the pin.
Definition: Pin.h:16
void setOutputHigh()
Set the pin mode to output and the pin output to HIGH.
Definition: PinGroup.h:357
void setMode(uint8_t mode)
Set the pin mode.
Definition: PinGroup.h:241
#define PORT_HIGH
Set the PORT register to HIGH for the pin.
Definition: Pin.h:20
void toggleMode()
Toggle the pin mode (OUTPUT -> INPUT, INPUT -> OUTPUT)
Definition: PinGroup.h:384
void setOutputLow()
Set the pin mode to output and the pin output to LOW.
Definition: PinGroup.h:368
PinGroup & operator=(uint8_t state)
Set the pin state.
Definition: PinGroup.h:83
#define PORT_LOW
Set the PORT register to LOW for the pin.
Definition: Pin.h:22
void setPullupOn()
Set the pin pullup resistor to on.
Definition: PinGroup.h:283
volatile uint8_t * getDDR()
Get a pointer to the DDR register.
Definition: PinGroup.h:148
Class for simultaneous operations on Arduino I/O pins.
Definition: PinGroup.h:18
PinGroup(Pin(&pins)[N])
Definition: PinGroup.h:22
void setHigh()
Set the pin output to HIGH.
Definition: PinGroup.h:337
uint8_t getMode()
Get the mode of the pin from the DDR register.
Definition: PinGroup.h:157
bool operator==(uint8_t value)
Compare the value of the pin.
Definition: PinGroup.h:49
uint8_t * getNumbers()
Get the pin numbers.
Definition: PinGroup.h:103
volatile uint8_t * getPIN()
Get a pointer to the PIN register.
Definition: PinGroup.h:130
void toggleState()
Toggle the pin state (HIGH -> LOW, LOW -> HIGH)
Definition: PinGroup.h:394
#define DDR_TOGGLE
Set the DDR register to the inverse for the pin.
Definition: Pin.h:17
void setInputPullupOn()
Set the pin mode to input and the pin pullup resistor to on.
Definition: PinGroup.h:303