-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswitches.cpp
243 lines (188 loc) · 5.83 KB
/
switches.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
@file
@see template.h for details
@version 1.0
@author Arco van Geest <arco@appeltaart.mine.nu>
@copyright 2013 Arco van Geest <arco@appeltaart.mine.nu> All right reserved.
This file is part of PinDA.
PinDA 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.
PinDA 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 PinDA. If not, see <http://www.gnu.org/licenses/>.
@date 20130520 Initial documented version
@brief The code part of template
*/
//#include "lamps.h"
#include "switches.h"
#ifdef ARDUINO
#include "Arduino.h"
#define println(line) Serial.println(line)
#define print(line) Serial.print(line)
#else
#include "arduino_compat.h"
#endif
SWITCHES::SWITCHES(void) {
}
void SWITCHES::clear(void) {
currentRow=0;
for (uint8_t i=0;i<8;i++) {
switchdata[i]=0;
switchdatalast[i]=0;
flankupdata[i]=0;
flankdowndata[i]=0;
changed=false;
}
} //SWITCHES::switches
void SWITCHES::interrupt( void ) {
currentRow++;
if ( currentRow>7) currentRow=0;
} //SWITCHES::interrupt
void SWITCHES::serviceLoop(void){
println("SwitchServiceLoop");
} //SWITCHES::serviceLoop
void SWITCHES::show(void){
for (uint8_t i=0;i<8;i++) {
printf("%02X ",switchdata[i]);
}
println();
} //SWITCHES::show
String SWITCHES::getString(void){
String s;
for (uint8_t i=0;i<8;i++) {
s+= String( switchdata[i], HEX )+" ";
}
return s;
} //SWITCHES::getString
String SWITCHES::getFlank(void){
String s,t;
for (uint8_t i=0;i<8;i++) {
t="0"+String( flankupdata[i], HEX );
s+= t.substring(t.length()-2);
t="0"+String( flankdowndata[i], HEX );
s+= t.substring(t.length()-2);
flankupdata[i]=0;
flankdowndata[i]=0;
}
return s;
} //SWITCHES::getFlank
bool SWITCHES::hasFlank(void) {
uint8_t c=0;
for (uint8_t i=0;i<8;i++) {
c |= flankupdata[i] | flankdowndata[i];
// flankupdata[i]=0;
// flankdowndata[i]=0;
}
return ( c )?true:false;
}
uint8_t SWITCHES::getRow(uint8_t row){
return switchdata[ row & 0x7 ];
} //SWITCHES::getRow
bool SWITCHES::isOn( uint8_t number ){
return ( switchdata[ number >>3 ] & ( 1<< (number & 0x7) ) );
} //SWITCHES::isOn
bool SWITCHES::flankup( uint8_t number) {
if ( flankupdata[ number >>3 ] & ( 1<< (number & 0x7) ) ) {
flankupdata[ number >>3 ] &= ~( 1<< (number & 0x7)) ;
return true;
}
return false;
} // SWITCHES::flankup
bool SWITCHES::flankdown( uint8_t number) {
if ( flankdowndata[ number >>3 ] & ( 1<< (number & 0x7) ) ) {
flankdowndata[ number >>3 ] &= ~( 1<< (number & 0x7)) ;
return true;
}
return false;
} // SWITCHES::flankdown
bool SWITCHES::isChanged( void ) {
if ( changed == false ) return false;
changed=false;
return true;
}
SWITCHES_demo::SWITCHES_demo(String _name) {
objName=_name;
//demo switches with pullup
clear();
pinMode( sw1_pin, INPUT);
digitalWrite( sw1_pin,HIGH);
pinMode( sw2_pin, INPUT);
digitalWrite( sw2_pin,HIGH);
pinMode( sw3_pin, INPUT);
digitalWrite( sw3_pin,HIGH);
pinMode( sw4_pin, INPUT);
digitalWrite( sw4_pin,HIGH);
pinMode( sw5_pin, INPUT);
digitalWrite( sw5_pin,HIGH);
pinMode( sw6_pin, INPUT);
digitalWrite( sw6_pin,HIGH);
pinMode( sw7_pin, INPUT);
digitalWrite( sw7_pin,HIGH);
pinMode( sw8_pin, INPUT);
digitalWrite( sw8_pin,HIGH);
pinda.AddInterrupt ( this , 1);
} // switches_demo::init
/* a bit silly matrix subset
cols87654321
row1_______0
row2______1_
row3_____2__
row4____3___
row5___4____
row6__5_____
row7_6______
row87_______
*/
void SWITCHES_demo::interrupt(void) {
uint8_t number=currentRow * 9;
uint8_t index[8] = {
sw1_pin,
sw2_pin,
sw3_pin,
sw4_pin,
sw5_pin,
sw6_pin,
sw7_pin,
sw8_pin
};
switchdatalast[ currentRow ] = switchdata[ currentRow ] ;
if ( digitalRead( index[currentRow]) == 0 ) {
switchdata[ currentRow ] |= ( 1<< currentRow);
} else {
switchdata[ currentRow ] &= ~( 1<< currentRow);
}
if ( switchdatalast[ currentRow ] != switchdata[ currentRow ] ) changed=true;
// XOR the data to get the changes and only for the flank direction
flankupdata[ currentRow ] |= ( switchdatalast[ currentRow ] ^ switchdata[ currentRow ] ) & switchdata[ currentRow ] ;
flankdowndata[ currentRow ] |= ( switchdatalast[ currentRow ] ^ switchdata[ currentRow ] ) & switchdatalast[ currentRow ] ;
currentRow++;
if ( currentRow>7) currentRow=0;
} // switches_demo::interrupt
SWITCHES_williams11a::SWITCHES_williams11a( MC6821 * piaptr, String _name ) {
objName=_name;
pia=piaptr;
clear();
pia->write_ddra(0); // A ingang
pia->write_ddrb(255); //B uitgang
pia->write_pdra(0x00);
pia->write_pdrb(0x00);
pinda.AddInterrupt ( this , 1);
}
void SWITCHES_williams11a::interrupt( void ) {
// print("L");
// print(row);
switchdatalast[ currentRow ] = switchdata[ currentRow ] ;
switchdata[ currentRow ] = pia->read_pdra() ;
pia->write_pdrb( ( 1 << currentRow ) ); // set row
if ( switchdatalast[ currentRow ] != switchdata[ currentRow ] ) changed=true;
// XOR the data to get the changes and only for the flank direction
flankupdata[ currentRow ] |= ( switchdatalast[ currentRow ] ^ switchdata[ currentRow ] ) & switchdata[ currentRow ] ;
flankdowndata[ currentRow ] |= ( switchdatalast[ currentRow ] ^ switchdata[ currentRow ] ) & switchdatalast[ currentRow ] ;
currentRow++;
if ( currentRow>7) currentRow=0;
}