-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathplcLib.cpp
More file actions
1118 lines (1008 loc) · 36.8 KB
/
plcLib.cpp
File metadata and controls
1118 lines (1008 loc) · 36.8 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
plcLib Version 1.4.0, last updated 1 October, 2017.
A simple Programmable Logic Controller (PLC) library for the
Arduino and compatibles.
Author: W. Ditch
https://github.com/wditch/plcLib
This program 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.
This program 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, available from:
<http://www.gnu.org/licenses/>
*/
#include "Arduino.h"
#include "plcLib.h"
extern unsigned int scanValue = 0;
// Define array size for pin monitoring
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
#define maxPins 70 // Set maxPins to 70 if Arduino Mega or Due are detected
#else
#define maxPins 20 // otherwise set maxPins to 20
#endif // maxPins value is returned by serial monitor P command
// Define serial monitor status bit positions for pinStatusUpdate function (0=RHS, 7=LHS)
#define pinUsedFlag 7 // Value = 128 if active
#define reportingEnabledFlag 6 // Value = 64 if active
#define digitalInputFlag 5 // Value = 32 if active
#define analogInputFlag 4 // Value = 16 if active
#define digitalOutputFlag 3 // Value = 8 if active
#define analogOutputFlag 2 // Value = 4 if active
#define servoOutputFlag 1 // Value = 2 if active
#define pinUpdatedFlag 0 // Value = 1 if active
// Define variables used by serial command monitor
String serialCommand = ""; // Create text string for serial commands
String serialCommandArgument = ""; // Create text string for serial command argument
boolean serialCommandFlag = false; // Flag to indicate a command is available
boolean serialFirstRun = true; // Flag to trigger serial Monitor initialisation
String appName = "plcLib"; // Appname returned by serial monitor A command
char appMajorVersion = '1'; // Major version returned by serial monitor M command
char appMinorVersion = '4'; // Minor version returned by serial monitor m command
// Create a 'struct' data type to hold serial monitor pin values
typedef struct
{
byte statusValue = 0;
unsigned int previousValue = 0;
} pinValueType;
// Initialise pin value struct
pinValueType pinValue[maxPins];
// Values can be accessed using pinValue[pin].statusValue
// or pinValue[pin].previousValue
// Define default pin directions and initial output levels.
// (or leave these unconfigured if noPinDefs is set in the user sketch)
void setupPLC() {
#ifndef noPinDefs
// Basic input pins
pinMode(X0, INPUT);
pinMode(X1, INPUT);
pinMode(X2, INPUT);
pinMode(X3, INPUT);
// Basic output pins
pinMode(Y0, OUTPUT);
pinMode(Y1, OUTPUT);
pinMode(Y2, OUTPUT);
pinMode(Y3, OUTPUT);
// Additional pins for Mega, Mega 2560 and Due boards
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
pinMode(X4, INPUT);
pinMode(X5, INPUT);
pinMode(X6, INPUT);
pinMode(X7, INPUT);
pinMode(Y4, OUTPUT);
pinMode(Y5, OUTPUT);
pinMode(Y6, OUTPUT);
pinMode(Y7, OUTPUT);
#endif /* Additional pins for Mega, Mega 2560 and Due boards */
//Motor Shield pins
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BRAKEA, OUTPUT);
pinMode(BRAKEB, OUTPUT);
pinMode(CURRENTA, INPUT);
pinMode(CURRENTB, INPUT);
// (The Motor Shield also has four Tinkerkit compatible pins.
// These are inputs X2-X3/A2-A3/I2-I3 and outputs Y1-Y2/D5-D6/O4-O3.)
// Default output port values
digitalWrite(Y0, LOW);
digitalWrite(Y1, LOW);
digitalWrite(Y2, LOW);
digitalWrite(Y3, LOW);
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
digitalWrite(Y4, LOW);
digitalWrite(Y5, LOW);
digitalWrite(Y6, LOW);
digitalWrite(Y7, LOW);
#endif /* if defined Mega, Mega 2560 and Due */
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, LOW);
digitalWrite(PWMA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(BRAKEA, LOW); // Channel A Brake off
digitalWrite(BRAKEB, LOW); // Channel B Brake off
#endif /* noPinDefs */
}
// Read an input pin (pin number supplied as integer)
unsigned int in(int input) {
scanValue = digitalRead(input);
#ifdef monitorEnable
pinStatusUpdate(input, digitalInputFlag, scanValue); // pin number = input, type = digital input, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Read an auxiliary input (variable supplied as unsigned integer)
unsigned int in(unsigned int input) {
scanValue = input;
return(scanValue);
}
// Read an auxiliary input (variable supplied as unsigned long)
unsigned int in(unsigned long input) {
scanValue = input;
return(scanValue);
}
// Read an inverted input (pin number supplied as integer)
unsigned int inNot(int input) {
if (digitalRead(input) == 1) {
scanValue = 0;
}
else {
scanValue = 1;
}
#ifdef monitorEnable
pinStatusUpdate(input, digitalInputFlag, scanValue); // pin number = input, type = digital input, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Read an inverted auxiliary input (variable supplied as unsigned integer)
unsigned int inNot(unsigned int input) {
if (input == 1) {
scanValue = 0;
}
else {
scanValue = 1;
}
return(scanValue);
}
// Read an inverted auxiliary input (variable supplied as unsigned long)
unsigned int inNot(unsigned long input) {
if (input == 1) {
scanValue = 0;
}
else {
scanValue = 1;
}
return(scanValue);
}
// Read an analogue input (input pin supplied as an integer)
unsigned int inAnalog(int input) {
scanValue = analogRead(input);
#ifdef monitorEnable
pinStatusUpdate(input, analogInputFlag, scanValue); // pin number = input, type = analogue input, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Read an analogue auxiliary value (variable supplied as an unsigned integer)
unsigned int inAnalog(unsigned int input) {
scanValue = input;
return(scanValue);
}
// Read an analogue auxiliary value (variable supplied as an unsigned long)
unsigned int inAnalog(unsigned long input) {
scanValue = input;
return(scanValue);
}
// Output to an output pin
unsigned int out(int output) {
if (scanValue == 1) {
digitalWrite(output, HIGH);
}
else {
digitalWrite(output, LOW);
}
#ifdef monitorEnable
pinStatusUpdate(output, digitalOutputFlag, scanValue); // pin number = output, type = digital output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Output to an auxiliary variable (variable type = unsigned integer)
unsigned int out(unsigned int &output) {
if (scanValue == 1) {
output = 1;
}
else {
output = 0;
}
return(scanValue);
}
// Output to an auxiliary variable (variable type = unsigned long)
unsigned int out(unsigned long &output) {
if (scanValue == 1) {
output = 1;
}
else {
output = 0;
}
return(scanValue);
}
// Output to an output pin (inverted)
unsigned int outNot(int output) {
if (scanValue == 1) {
digitalWrite(output, LOW);
}
else {
digitalWrite(output, HIGH);
}
#ifdef monitorEnable
pinStatusUpdate(output, digitalOutputFlag, scanValue); // pin number = output, type = digital output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Output to an auxiliary variable (inverted) (variable type = unsigned integer)
unsigned int outNot(unsigned int &output) {
if (scanValue == 1) {
output = 0;
}
else {
output = 1;
}
return(scanValue);
}
// Output to an auxiliary variable (inverted) (variable type = unsigned long)
unsigned int outNot(unsigned long &output) {
if (scanValue == 1) {
output = 0;
}
else {
output = 1;
}
return(scanValue);
}
// Output a PWM value to an output pin (scanValue in range 0-1023)
unsigned int outPWM(int output) {
analogWrite(output, scanValue / 4);
#ifdef monitorEnable
pinStatusUpdate(output, analogOutputFlag, scanValue); // pin number = output, type = analogue output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// AND scanValue with input (pin number supplied as integer)
unsigned int andBit(int input) {
scanValue = scanValue & digitalRead(input);
return(scanValue);
}
// AND scanValue with auxiliary variable (variable supplied as unsigned integer)
unsigned int andBit(unsigned int input) {
scanValue = scanValue & input;
return(scanValue);
}
// AND scanValue with auxiliary variable (variable supplied as unsigned long)
unsigned int andBit(unsigned long input) {
scanValue = scanValue & input;
return(scanValue);
}
// AND scanValue with inverted input (pin number supplied as integer)
unsigned int andNotBit(int input) {
scanValue = scanValue & ~digitalRead(input);
return(scanValue);
}
// AND scanValue with inverted auxiliary variable (variable supplied as unsigned integer)
unsigned int andNotBit(unsigned int input) {
scanValue = scanValue & ~input;
return(scanValue);
}
// AND scanValue with inverted auxiliary variable (variable supplied as unsigned long)
unsigned int andNotBit(unsigned long input) {
scanValue = scanValue & ~input;
return(scanValue);
}
// OR scanValue with input (pin number supplied as integer)
unsigned int orBit(int input) {
scanValue = scanValue | digitalRead(input);
return(scanValue);
}
// OR scanValue with auxiliary variable (variable supplied as unsigned integer)
unsigned int orBit(unsigned int input) {
scanValue = scanValue | input;
return(scanValue);
}
// OR scanValue with auxiliary variable (variable supplied as unsigned long)
unsigned int orBit(unsigned long input) {
scanValue = scanValue | input;
return(scanValue);
}
// OR scanValue with inverted input (pin number supplied as integer)
unsigned int orNotBit(int input) {
if (scanValue == 1) {
}
else {
if (digitalRead(input) == 0) {
scanValue = 1;
}
else {
scanValue = 0;
}
}
return(scanValue);
}
// OR scanValue with inverted auxiliary variable (variable supplied as unsigned integer)
unsigned int orNotBit(unsigned int input) {
if (scanValue == 1) {
}
else {
if (input == 0) {
scanValue = 1;
}
else {
scanValue = 0;
}
}
return(scanValue);
}
// OR scanValue with inverted auxiliary variable (variable supplied as unsigned long)
unsigned int orNotBit(unsigned long input) {
if (scanValue == 1) {
}
else {
if (input == 0) {
scanValue = 1;
}
else {
scanValue = 0;
}
}
return(scanValue);
}
// XOR scanValue with input (pin number supplied as integer)
unsigned int xorBit(int input) {
scanValue = scanValue ^ digitalRead(input);
return(scanValue);
}
// XOR scanValue with auxiliary variable (variable supplied as unsigned integer)
unsigned int xorBit(unsigned int input) {
scanValue = scanValue ^ input;
return(scanValue);
}
// XOR scanValue with auxiliary variable (variable supplied as unsigned long)
unsigned int xorBit(unsigned long input) {
scanValue = scanValue ^ input;
return(scanValue);
}
// Set - Reset latch (output and reset pin numbers supplied as integers)
unsigned int latch(int output, int reset) {
scanValue = scanValue | digitalRead(output); // Self latch by ORing with Output pin (Q)
scanValue = scanValue & ~digitalRead(reset); // AND-Not with Reset Pin
if (scanValue == 1) {
digitalWrite(output, HIGH);
}
else {
digitalWrite(output, LOW);
}
#ifdef monitorEnable
pinStatusUpdate(output, digitalOutputFlag, scanValue); // pin number = output, type = digital output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Set - Reset latch (output pin number supplied as integer, reset as unsigned integer variable)
unsigned int latch(int output, unsigned int reset) {
scanValue = scanValue | digitalRead(output); // Self latch by ORing with Output pin (Q)
scanValue = scanValue & ~reset; // AND-Not with Reset variable
if (scanValue == 1) {
digitalWrite(output, HIGH);
}
else {
digitalWrite(output, LOW);
}
#ifdef monitorEnable
pinStatusUpdate(output, digitalOutputFlag, scanValue); // pin number = output, type = digital output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Set - Reset latch (output pin number supplied as integer, reset as unsigned long variable)
unsigned int latch(int output, unsigned long reset) {
scanValue = scanValue | digitalRead(output); // Self latch by ORing with Output pin (Q)
scanValue = scanValue & ~reset; // AND-Not with Reset variable
if (scanValue == 1) {
digitalWrite(output, HIGH);
}
else {
digitalWrite(output, LOW);
}
#ifdef monitorEnable
pinStatusUpdate(output, digitalOutputFlag, scanValue); // pin number = output, type = digital output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Set - Reset latch (output as unsigned integer variable and reset pin as integer)
unsigned int latch(unsigned int &output, int reset) {
scanValue = scanValue | output; // Self latch by ORing with Output pin (Q)
scanValue = scanValue & ~digitalRead(reset); // AND-Not with Reset Pin
if (scanValue == 1) {
output = 1;
}
else {
output = 0;
}
return(scanValue);
}
// Set - Reset latch (output as unsigned long variable and reset pin as integer)
unsigned int latch(unsigned long &output, int reset) {
scanValue = scanValue | output; // Self latch by ORing with Output pin (Q)
scanValue = scanValue & ~digitalRead(reset); // AND-Not with Reset Pin
if (scanValue == 1) {
output = 1;
}
else {
output = 0;
}
return(scanValue);
}
// Set - Reset latch (output and reset values are unsigned integer auxiliary variables)
unsigned int latch(unsigned int &output, unsigned int reset) {
scanValue = scanValue | output; // Self latch by ORing with Output variable (Q)
scanValue = scanValue & ~reset; // AND-Not with Reset variable
if (scanValue == 1) {
output = 1;
}
else {
output = 0;
}
return(scanValue);
}
// Set - Reset latch (output and reset values are unsigned long auxiliary variables)
unsigned int latch(unsigned long &output, unsigned long reset) {
scanValue = scanValue | output; // Self latch by ORing with Output variable (Q)
scanValue = scanValue & ~reset; // AND-Not with Reset variable
if (scanValue == 1) {
output = 1;
}
else {
output = 0;
}
return(scanValue);
}
// On delay timer - requires continuously enabled input from previous scanValue
// (timerState and timerPeriod are unsigned long values - 32 bit)
unsigned int timerOn(unsigned long &timerState, unsigned long timerPeriod) {
if (scanValue == 0) { // timer is disabled
timerState = 0; // Clear timerState (0 = 'not started')
}
else { // Timer is enabled
if (timerState == 0) { // Timer hasn't started counting yet
timerState = millis(); // Set timerState to current time in milliseconds
scanValue = 0; // Result = 'not finished' (0)
}
else { // Timer is active and counting
if (millis() - timerState >= timerPeriod) { // Timer has finished
scanValue = 1; // Result = 'finished' (1)
}
else { // Timer has not finished
scanValue = 0; // Result = 'not finished' (0)
}
}
}
return(scanValue); // Return result (1 = 'finished',
// 0 = 'not started' / 'not finished')
}
// Fixed width pulse - enabled by momentary input from previous scanValue
// (timerState and timerPeriod are unsigned long values - 32 bit)
unsigned int timerPulse(unsigned long &timerState, unsigned long timerPeriod) {
if (scanValue == 1 || timerState != 0){ // Timer is enabled
if (timerState == 0) { // Timer hasn't started counting yet
timerState = millis(); // Set timerState to current time in milliseconds
scanValue = 1; // Pulse = 'Active' (1)
}
else { // Timer is active and counting
if (millis() - timerState >= timerPeriod) { // Timer has finished
if (scanValue == 0) { // Finished AND trigger is low
timerState = 0; // Re-enabled timer
scanValue = 0; // Pulse = 'finished' (0)
}
else { // Finished but trigger is still high
scanValue = 0; // Wait for trigger to go low before re-enabling
}
}
else { // Timer has not finished
scanValue = 1; // Pulse = 'Active' (1)
}
}
}
return(scanValue); // Return result (1 = 'active',
// 0 = 'not started' / 'not yet re-enabled')
}
// Off delay timer - turns on immediately when enabled, then delays turning off when previous scanValue goes low
// (timerState and timerPeriod are unsigned long values - 32 bit)
unsigned int timerOff(unsigned long &timerState, unsigned long timerPeriod) {
if (scanValue == 0) { // Timer input is off (scanValue = 0)
if (timerState == 0) { // Timer is not started so do nothing
}
else { // Timer is active and counting
if (millis() - timerState >= timerPeriod) { // Timer has finished
scanValue = 0; // Result = 'turn-off delay finished' (0)
}
else { // Timer has not finished
scanValue = 1; // Result = 'turn-off delay not finished' (1)
}
}
}
else { // Timer input is high (scanValue = 1)
timerState = millis(); // Set timerState to current time in milliseconds
}
return(scanValue); // Return result (1 = 'pulse On' / 'turn-off delay in progress',
// 0 = 'not started' / 'finished')
}
// Cycle timer - creates a repeating pulse waveform when enabled by previous scanValue
// (timer1State, timer1Period, timer2State, timer2Period are unsigned long values - 32 bit)
unsigned int timerCycle(unsigned long &timer1State, unsigned long timer1Period, unsigned long &timer2State, unsigned long timer2Period) {
if (scanValue == 0) { // Enable input is off (scanValue = 0)
timer2State = 0; // Ready to start LOW pulse period when enabled
timer1State = 1;
}
else { // Enabled
if (timer2State == 0) { // Low pulse Active
if (timer1State == 1) { // LOW pulse period starting
timer1State = millis(); // Set timerState to current time in milliseconds
}
else if (millis() - timer1State >= timer1Period) { // Low pulse period has finished
timer1State = 0;
timer2State = 1; // Ready to start HIGH pulse period
}
scanValue = 0; // Result = 'Pulse LOW' (0)
}
if (timer1State == 0) { // High pulse Active
if (timer2State == 1) { // HIGH pulse period starting
timer2State = millis(); // Set timerState to current time in milliseconds
}
else if (millis() - timer2State >= timer2Period) { // High pulse has finished
timer2State = 0;
timer1State = 1; // Ready to start LOW pulse period
}
scanValue = 1; // Result = 'Pulse HIGH' (1)
}
}
return(scanValue);
}
// Test whether an analogue input is greater than a second analogue input
unsigned int compareGT(int input) {
if (scanValue > analogRead(input)) {
scanValue = 1;
}
else {
scanValue = 0;
}
return(scanValue);
}
// Test whether an analogue input is greater than a fixed unsigned int value
unsigned int compareGT(unsigned int input) {
if (scanValue > input) {
scanValue = 1;
}
else {
scanValue = 0;
}
return(scanValue);
}
// Test whether an analogue input is greater than a fixed unsigned long value
unsigned int compareGT(unsigned long input) {
if (scanValue > input) {
scanValue = 1;
}
else {
scanValue = 0;
}
return(scanValue);
}
// Test whether an analogue input is less than a second analogue input
unsigned int compareLT(int input) {
if (scanValue < analogRead(input)) {
scanValue = 1;
}
else {
scanValue = 0;
}
return(scanValue);
}
// Test whether an analogue input is less than a fixed unsigned int value
unsigned int compareLT(unsigned int input) {
if (scanValue < input) {
scanValue = 1;
}
else {
scanValue = 0;
}
return(scanValue);
}
// Test whether an analogue input is less than a fixed unsigned long value
unsigned int compareLT(unsigned long input) {
if (scanValue < input) {
scanValue = 1;
}
else {
scanValue = 0;
}
return(scanValue);
}
// Set a latched output (output pin number supplied as integer)
unsigned int set(int output) {
scanValue = scanValue | digitalRead(output); // Self latch by ORing with Output pin
if (scanValue == 1) {
digitalWrite(output, HIGH);
}
#ifdef monitorEnable
pinStatusUpdate(output, digitalOutputFlag, scanValue); // pin number = output, type = digital output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// Set a latched output (variable supplied as unsigned integer)
unsigned int set(unsigned int &output) {
scanValue = scanValue | output; // Self latch by ORing with Output pin
if (scanValue == 1) {
output = 1;
}
return(scanValue);
}
// Set a latched output (variable supplied as unsigned long)
unsigned int set(unsigned long &output) {
scanValue = scanValue | output; // Self latch by ORing with Output pin
if (scanValue == 1) {
output = 1;
}
return(scanValue);
}
// reset (or clear) a latched output (output pin number supplied as integer)
unsigned int reset(int output) {
if (scanValue == 1) {
digitalWrite(output, LOW);
}
#ifdef monitorEnable
pinStatusUpdate(output, digitalOutputFlag, scanValue); // pin number = output, type = digital output, command result = scanValue
#endif /* serial monitor is enabled */
return(scanValue);
}
// reset (or clear) a latched output (variable supplied as unsigned integer)
unsigned int reset(unsigned int &output) {
if (scanValue == 1) {
output = 0;
}
return(scanValue);
}
// reset (or clear) a latched output (variable supplied as unsigned long)
unsigned int reset(unsigned long &output) {
if (scanValue == 1) {
output = 0;
}
return(scanValue);
}
// Up, down, or up-down counter
Counter::Counter(unsigned int pv) // Counter constructor method
{ // (Default values are for an up counter)
_pv = pv; // Set preset value using supplied parameter
_ct = 0; // Running count = zero
_uQ = 0; // Up counter upper Q output = 0
_lQ = 1; // Down counter lower Q output = 1
_ctUpEdge = 0; // Prepare rising edge detect for up counter
_ctDownEdge = 0; // Prepare rising edge detect for down counter
}
Counter::Counter(unsigned int pv, unsigned int direction) // Counter constructor method
{ // (Second parameter sets default direction)
_pv = pv; // Set preset value using supplied parameter
if(direction == 0){ // Set start values for an up counter
_ct = 0; // Running count = zero
_uQ = 0; // Up counter upper Q output = 0
_lQ = 1; // Down counter lower Q output = 1
}
else { // Set start values for a down counter
_ct = _pv; // Running count = preset value
_uQ = 1; // Up counter upper Q output = 1
_lQ = 0; // Down counter lower Q output = 0
}
_ctUpEdge = 0; // Prepare rising edge detect for up counter
_ctDownEdge = 0; // Prepare rising edge detect for down counter
}
unsigned int Counter::presetValue() // Return preset value method
{
return(_pv); // Return preset value
}
void Counter::clear() // Clear counter method
{
if(scanValue == 1) { // Enabled if scanValue = 1
_ct = 0; // Running count = 0
_uQ = 0; // Up counter upper Q output = 0
_lQ = 1; // Down counter lower Q output = 1
_ctUpEdge = 0; // Prepare rising edge detect for up counter
_ctDownEdge = 0; // Prepare rising edge detect for down counter
}
}
void Counter::preset() // Preset counter method
{
if(scanValue == 1) { // Enabled if scanValue = 1
_ct = _pv; // Running count = preset value
_uQ = 1; // Up counter upper Q output = 1
_lQ = 0; // Down counter lower Q output = 0
_ctUpEdge = 0; // Prepare rising edge detect for up counter
_ctDownEdge = 0; // Prepare rising edge detect for down counter
}
}
unsigned int Counter::upperQ() // Read up counter upper Q output method
{
if (_uQ == 1){ // Set scanValue = 1 if upper Q = 1
scanValue = 1;
}
else {
scanValue = 0; // Otherwise set scanValue = 0
}
return(_uQ); // Return upper Q value
}
unsigned int Counter::lowerQ() // Read down counter lower Q output method
{
if (_lQ == 1){ // Set scanValue = 1 if lower Q = 1
scanValue = 1;
}
else {
scanValue = 0; // Otherwise set scanValue = 0
}
return(_lQ); // Return lower Q value
}
unsigned int Counter::count() // Return current count value method
{
return(_ct); // Return count value
}
void Counter::countUp() // Count up method
{
if (_ct != _pv) { // Not yet finished counting so continue
if (scanValue == 0) { // clock = 0 so clear counter edge detect
_ctUpEdge = 0;
}
else { // Clock = 1
if (_ctUpEdge == 0) { // Rising edge detected so increment count
_ctUpEdge = 1; // Set counter edge
_ct++; // Increment count
if(_ct == _pv) { // Counter has reached final value
_uQ = 1; // Counter upper Q output = 1
_lQ = 0; // Counter lower Q output = 0
}
if(_ct != _pv){ // Counter is not yet finished
_uQ = 0; // Counter upper Q output = 0
_lQ = 0; // Counter lower Q output = 0
}
}
}
}
}
void Counter::countDown() // Count down method
{
if (_ct != 0) { // Not yet finished so continue
if (scanValue == 0) { // clock = 0 so clear counter edge detect
_ctDownEdge = 0;
}
else { // Clock = 1
if (_ctDownEdge == 0) { // Rising edge detected so decrement count
_ctDownEdge = 1; // Set counter edge
_ct--; // Decrement count
if(_ct == 0) { // Counter has reached final value
_uQ = 0; // Counter QUp output = 0
_lQ = 1; // Counter QDown output = 1
}
if(_ct != 0) { // Counter is not yet finished
_uQ = 0; // Counter upper Q output = 0
_lQ = 0; // Counter lower Q output = 0
}
}
}
}
}
// Shift register
Shift::Shift() // Shift register constructor method
{ // (Register width = 32 bits)
_sreg = 0; // Set the shift register to zero
_srLeftEdge = 0; // Prepare rising edge detect for left shift
_srRightEdge = 0; // Prepare rising edge detect for right shift
}
Shift::Shift(unsigned int sreg) // Shift register constructor method
{ // (Register width = 32 bits)
_sreg = sreg; // Set initial value
_srLeftEdge = 0; // Prepare rising edge detect for left shift
_srRightEdge = 0; // Prepare rising edge detect for right shift
}
unsigned int Shift::bitValue(unsigned int bitno) // Read a bit at a specified position
{
if(bitRead(_sreg, bitno) == 1) {
scanValue = 1; // Tested bit = 1
}
else {
scanValue = 0; // Tested bit = 0
}
return(scanValue); // Return tested bit value
}
unsigned int Shift::value() // Return the current shift register value
{
return(_sreg);
}
void Shift::reset() // Reset the shift register if scanValue = 0
{
if(scanValue == 1){
_sreg = 0; // Set the shift register to zero
_srLeftEdge = 0; // Prepare rising edge detect for left shift
_srRightEdge = 0; // Prepare rising edge detect for right shift
}
}
void Shift::inputBit() // Set the input bit of the shift register
{
if (scanValue == 0) { // If scanValue = 0, clear input bit
_inbit = 0;
}
else { // Otherwise set the input bit
_inbit = 1;
}
}
void Shift::shiftRight() // Shift right method
{
if (scanValue == 0) { // clock = 0 so clear shift right edge detect
_srRightEdge = 0;
}
else { // Clock = 1
if (_srRightEdge == 0) { // Rising edge detected so shift right
_srRightEdge = 1; // Set shift right edge detect
_sreg = _sreg >> 1; // Shift to the right
if (_inbit == 1) { // Shift-in new input bit at LHS
bitSet(_sreg, 15);
}
}
}
}
void Shift::shiftLeft() // Shift left method
{
if (scanValue == 0) { // clock = 0 so clear shift left edge detect
_srLeftEdge = 0;
}
else { // Clock = 1
if (_srLeftEdge == 0) { // Rising edge detected so shift left
_srLeftEdge = 1; // Set shift left edge detect
_sreg = _sreg << 1; // Shift to the left
if (_inbit == 1) { // Shift-in new input bit at RHS
bitSet(_sreg, 0);
}
}
}
}
// Single-bit Software Stack
Stack::Stack() // Stack constructor method
{ // (Register width = 32 bits)
_sreg = 0; // Set the stack to zero
}
void Stack::push() // Push scanValue bit onto the stack method
{
_sreg = _sreg >> 1; // Shift stack 1-bit to the right
if (scanValue == 1) { // Shift-in scanValue bit at LHS
bitSet(_sreg, 31);
}
else {
bitClear(_sreg, 31);
}
}
void Stack::pop() // Pop scanValue bit from the stack method
{
scanValue = bitRead(_sreg, 31); // Set scanValue to leftmost bit of stack
_sreg = _sreg << 1; // Shift stack 1-bit to the left
}
void Stack::orBlock() // OR scanValue with value Popped from stack method
{
scanValue = scanValue | bitRead(_sreg, 31); // OR scanValue with top of stack
_sreg = _sreg << 1; // Shift stack 1-bit to the left
}
void Stack::andBlock() // AND scanValue with value Popped from stack method
{
scanValue = scanValue & bitRead(_sreg, 31); // AND scanValue with top of stack
_sreg = _sreg << 1; // Shift stack 1-bit to the left
}
// Single scan cycle Pulse with rising or falling edge detection
Pulse::Pulse() // Pulse constructor method
{
_pulseInput = 0; // Set pulse input tracker to zero
_pulseUpEdge = 0; // Prepare rising edge detect
_pulseDownEdge = 0; // Prepare falling edge detect
}
void Pulse::inClock() // Read the clock input method
{
if (scanValue != _pulseInput) { // Rising or falling edge detected
if (scanValue == 1) { // Rising edge detected
_pulseUpEdge = 1; // Set rising edge detect value
_pulseDownEdge = 0; // Clear falling edge detect value
_pulseInput = 1; // Pulse input tracker = 1
}
else { // Falling edge detected
_pulseUpEdge = 0; // Clear rising edge detect value
_pulseDownEdge = 1; // Set falling edge detect value
_pulseInput = 0; // Pulse input tracker = 0
}
}
else { // No change detected
_pulseUpEdge = 0; // Set both edge detect values to zero
_pulseDownEdge = 0; // (and leave pulse tracker unchanged)
}
}