-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRalph.ino
336 lines (288 loc) · 8.4 KB
/
Ralph.ino
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
Encoder setup check sketch, Author Mark Naylor, 2020
Arduino sketch to help check the inputs are being seen correctly before using the encoder quality check sketch
First identifies which of first 21 digital pins will support interrupts then looks (for 15 seconds) for signals with pins in INPUT_PULLUP mode
and then checks any remaining pins (for another 15 seconds) using INPUT if any signals subsequently seen. Stops after 3 or more pins seen genertaing interrupts
It uses interrupts to process in the incoming signals and hence requires an Arduino with 3 pins that can generate interrupts. All testing has been done on an Arduino Mega2560.
Copyright - free to be used at own risk, no warranty given or implied.
Ver 1 - Initial release
*/
const static char sInput[] = "Input mode";
const static char sInputPullup[] = "Input with pullup mode";
const static char sUnknown[] = "Unexpected mode";
// forward declarations
void ISR0 ();
void ISR1 ();
void ISR2 ();
void ISR3 ();
void ISR4 ();
void ISR5 ();
void ISR6 ();
void ISR7 ();
void ISR8 ();
void ISR9 ();
void ISR10 ();
void ISR11 ();
void ISR12 ();
void ISR13 ();
void ISR14 ();
void ISR15 ();
void ISR16 ();
void ISR17 ();
void ISR18 ();
void ISR19 ();
void ISR20 ();
void ISR21 ();
void (*ListISR [] )(void) =
{
ISR0,
ISR1,
ISR2,
ISR3,
ISR4,
ISR5,
ISR6,
ISR7,
ISR8,
ISR9,
ISR10,
ISR11,
ISR12,
ISR13,
ISR14,
ISR15,
ISR16,
ISR17,
ISR18,
ISR19,
ISR20,
ISR21
};
#define MAX_PINS_TO_CHECK min ( NUM_DIGITAL_PINS, sizeof ( ListISR ) / sizeof ( ListISR[0] ) )
// Configuration items
#define BAUD_RATE 115200
#define VER 1
// Create struct to monitor pin results
typedef struct
{
int iPinNum;
bool bHasBeenSignalled; // true if signals seen on this pin
int iModeWhenSignalled;
void (*pISR)();
} PINDATA;
volatile typedef struct
{
int iNumPins; // num of entries in sPinList that are valid
PINDATA sPinList [ MAX_PINS_TO_CHECK ];
} PININFO;
PININFO sPinInfo;
volatile int iPinMode; // current mode of pins being tested
void setup ()
{
Serial.begin (BAUD_RATE);
// Print config state for this run
Serial.print ( F ("Encoder Setup Check Ver. " ) );
Serial.println ( VER );
Serial.print ( F ( "Serial output running at " ) );
Serial.println ( BAUD_RATE );
if ( NUM_DIGITAL_PINS > MAX_PINS_TO_CHECK )
{
Serial.print ( F ( "Compatibility warning: This program will only check the first " ) );
Serial.print ( MAX_PINS_TO_CHECK );
Serial.print ( F ( " digital pins, your machine has " ) );
Serial.println ( NUM_DIGITAL_PINS );
}
Serial.println();
// Initialise pin data structure
InitPinInfo ( &sPinInfo );
Serial.print ( sPinInfo.iNumPins );
Serial.println ( F ( " pin(s) can be used for interrupts as follows:\nPin #\tInt Vector" ) );
for ( int i = 0 ; i < sPinInfo.iNumPins ; i++ )
{
Serial.print ( sPinInfo.sPinList [ i ].iPinNum );
Serial.print ( F ( "\t" ) );
Serial.println ( digitalPinToInterrupt ( sPinInfo.sPinList [ i ].iPinNum ) );
}
Serial.println ( F ( "\nStarting to look for signals on pins" ) );
Serial.println();
}
void loop ()
{
static int iCountSec = 0;
switch ( iCountSec )
{
case 0:
// put in PULLUP mode
Serial.println ();
Serial.println ( F ( "Checking INPUT_PULLUP mode" ) ) ;
StopISRs( &sPinInfo );
iPinMode = INPUT_PULLUP;
SetPinsMode ( &sPinInfo, INPUT_PULLUP );
StartISRs ( &sPinInfo );
Serial.println ( F ( "Please turn encoder" ) );
delay ( 1000 );
break;
case 15:
// put in input mode
Serial.println ( F ( "Checking INPUT mode" ) ) ;
StopISRs( &sPinInfo );
iPinMode = INPUT;
SetPinsMode ( &sPinInfo, INPUT );
StartISRs ( &sPinInfo );
Serial.println ( F ( "Please continmue to turn encoder" ) );
delay ( 1000 );
break;
case 30:
// end of program
Serial.println ();
Serial.println ( F ( "Finished testing" ) );
DisplayResults ( &sPinInfo );
TerminateProgram ( F ( "Program ending" ) );
break;
default:
// check if 3 pins have signalled
Serial.print ( F ( "." ) );
if ( NumPinsSignalled ( &sPinInfo ) >= 3 )
{
DisplayResults ( &sPinInfo );
TerminateProgram ( F ( "3 pins found, terminating program" ) );
}
delay ( 1000 );
break;
}
iCountSec++;
}
void InitPinInfo ( PININFO * psPinInfo )
{
for ( int i = 0 ; i < MAX_PINS_TO_CHECK ; i++ )
{
psPinInfo->sPinList [ i ].iPinNum = -1;
psPinInfo->sPinList [ i ].bHasBeenSignalled = false;
psPinInfo->sPinList [ i ].iModeWhenSignalled = -1;
psPinInfo->sPinList [ i ].pISR = 0;
}
psPinInfo->iNumPins = 0;
// determine which pins can support interrupts
for ( int i = 0 ; i < MAX_PINS_TO_CHECK ; i++ )
{
if ( digitalPinToInterrupt ( i ) != -1 )
{
psPinInfo->sPinList [ sPinInfo.iNumPins ].iPinNum = i;
psPinInfo->sPinList [ sPinInfo.iNumPins++ ].pISR = ListISR [ i ] ;
}
}
}
int NumPinsSignalled ( PININFO * psPinInfo )
{
int iResult = 0;
for ( int i = 0 ; i < psPinInfo->iNumPins ; i++ )
{
if ( psPinInfo->sPinList [ i ].bHasBeenSignalled == true )
{
iResult++;
}
}
return iResult;
}
void DisplayResults ( PININFO * psPinInfo )
{
for ( int i = 0 ; i < psPinInfo->iNumPins ; i++ )
{
Serial.print ( F ( "\n Pin " ) );
Serial.print ( psPinInfo->sPinList [ i ].iPinNum );
Serial.print ( F ( " attached to interrupt " ) );
Serial.print ( digitalPinToInterrupt ( psPinInfo->sPinList [ i ].iPinNum ) );
if ( psPinInfo->sPinList [ i ].bHasBeenSignalled == true )
{
Serial.print ( F ( " signalled when pin was set to mode : " ) );
Serial.println ( ModetoString ( psPinInfo->sPinList [ i ].iModeWhenSignalled ) );
}
else
{
Serial.print ( F ( " did not signal" ) );
}
Serial.println();
}
}
void StartISRs ( PININFO * psPinInfo )
{
for ( int i = 0 ; i < psPinInfo->iNumPins ; i++ )
{
attachInterrupt ( digitalPinToInterrupt ( psPinInfo->sPinList [ i ].iPinNum ), psPinInfo->sPinList [ i ].pISR, FALLING);
}
}
void StopISRs ( PININFO * psPinInfo )
{
for ( int i = 0 ; i < psPinInfo->iNumPins ; i++ )
{
detachInterrupt ( digitalPinToInterrupt ( psPinInfo->sPinList [ i ].iPinNum ) );
}
}
void SetPinsMode ( PININFO * psPinInfo, int iMode )
{
for ( int i = 0 ; i < psPinInfo->iNumPins ; i++ )
{
if ( psPinInfo->sPinList [ i ].bHasBeenSignalled == false )
{
psPinInfo->sPinList [ i ].iModeWhenSignalled = iMode;
pinMode ( psPinInfo->sPinList [ i ].iPinNum , iMode);
}
}
}
const char* ModetoString (int iMode)
{
const static char* pResult;
switch (iMode)
{
case INPUT:
pResult = sInput;
break;
case INPUT_PULLUP:
pResult = sInputPullup;
break;
default:
pResult = sUnknown;
break;
}
return pResult;
}
void TerminateProgram (const __FlashStringHelper* pErrMsg)
{
Serial.println (pErrMsg);
Serial.flush ();
exit (0);
}
void BaseISRCode ( int iIndex )
{
for ( int i = 0 ; i < sPinInfo.iNumPins ; i++ )
{
if ( sPinInfo.sPinList [ i ].iPinNum == iIndex )
{
// Set has signalled to true
sPinInfo.sPinList [ i ].bHasBeenSignalled = true;
sPinInfo.sPinList [ i ].iModeWhenSignalled = iPinMode;
}
}
}
void ISR0 () { BaseISRCode ( 0 ); }
void ISR1 () { BaseISRCode ( 1 ); }
void ISR2 () { BaseISRCode ( 2 ); }
void ISR3 () { BaseISRCode ( 3 ); }
void ISR4 () { BaseISRCode ( 4 ); }
void ISR5 () { BaseISRCode ( 5 ); }
void ISR6 () { BaseISRCode ( 6 ); }
void ISR7 () { BaseISRCode ( 7 ); }
void ISR8 () { BaseISRCode ( 8 ); }
void ISR9 () { BaseISRCode ( 9 ); }
void ISR10 () { BaseISRCode ( 10 ); }
void ISR11 () { BaseISRCode ( 11 ); }
void ISR12 () { BaseISRCode ( 12 ); }
void ISR13 () { BaseISRCode ( 13 ); }
void ISR14 () { BaseISRCode ( 14 ); }
void ISR15 () { BaseISRCode ( 15 ); }
void ISR16 () { BaseISRCode ( 16 ); }
void ISR17 () { BaseISRCode ( 17 ); }
void ISR18 () { BaseISRCode ( 18 ); }
void ISR19 () { BaseISRCode ( 19 ); }
void ISR20 () { BaseISRCode ( 20 ); }
void ISR21 () { BaseISRCode ( 21 ); }