cgnuino
CgnDI.cpp
1 
8 #include "Arduino.h"
9 #include "cgnuino.h"
10 
18 CgnDI::CgnDI(byte p, byte s, byte o, byte d) {
19  first = p;
20  n = s;
21  relay = o;
22  relaid = (o != NULL);
23  r = d;
24  last = millis();
25  for (int i = 0; i < N_CGNDI; i++) {
26  rest[i] = 0;
27  if (i < n) {
28  pinMode(first + i, INPUT_PULLUP);
29  cur[i] = digitalRead(first + i) ? false : true;
30  pre[i] = cur[i];
31 
32  if (relaid) {
33  pinMode(relay + i, OUTPUT);
34  digitalWrite(relay + i, cur[i] ? HIGH : LOW);
35  }
36  } else {
37  cur[i] = false;
38  pre[i] = false;
39  }
40  }
41 }
42 
48 void CgnDI::update() {
49  int past;
50  past = millis() - last;
51  last = millis();
52 
53  for (int i = 0; i < n; i++) {
54  if (rest[i] > past) {
55  rest[i] -= past;
56  continue;
57 
58  } else {
59  rest[i] = 0;
60  pre[i] = cur[i];
61  cur[i] = digitalRead(first + i) ? false : true;
62 
63  if (cur[i] != pre[i]) {
64  rest[i] = r;
65  if (relaid) {
66  digitalWrite(relay + i, cur[i] ? HIGH : LOW);
67  }
68  }
69  }
70  }
71 }
72 
77 bool CgnDI::on(byte i) {
78  return cur[i];
79 }
80 
85 bool CgnDI::off(byte i) {
86  return !cur[i];
87 }
88 
93 bool CgnDI::turnon(byte i) {
94  return cur[i] && !pre[i];
95 }
96 
101 bool CgnDI::turnoff(byte i) {
102  return !cur[i] && pre[i];
103 }
104 
109 bool CgnDI::change(byte i) {
110  return cur[i] != pre[i];
111 }
112 
117 bool CgnDI::keep(byte i) {
118  return cur[i] == pre[i];
119 }
120 
void update()
Updates DI buffer by current pin voltages.
Definition: CgnDI.cpp:48
bool turnoff(byte=0)
Checks whether i-th DI pin was turned off in current loop.
Definition: CgnDI.cpp:101
bool on(byte=0)
Checks whether i-th DI pin is on (active).
Definition: CgnDI.cpp:77
bool change(byte=0)
Checks whether i-th DI pin was changed from previous loop.
Definition: CgnDI.cpp:109
bool turnon(byte=0)
Checks whether i-th DI pin was turned on in current loop.
Definition: CgnDI.cpp:93
bool off(byte=0)
Checks whether i-th DI pin is off (inactive).
Definition: CgnDI.cpp:85
bool keep(byte=0)
Checks whether i-th DI pin kept unchanged from previous loop.
Definition: CgnDI.cpp:117
CgnDI(byte, byte=1, byte=NULL, byte=2)
Constructor.
Definition: CgnDI.cpp:18