forked from rcalme/puzzle-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPegBoard.cpp
92 lines (83 loc) · 2.07 KB
/
PegBoard.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
#include "PegBoard.h"
// Constructor
PegBoard::PegBoard(const byte numPegs, const byte pins[]) {
pegCount = numPegs;
pegPins = new byte[pegCount];
for(int i=0; i<pegCount; i++){
pegPins[i]=pins[i];
}
lastConn = Connection();
}
// Destructor
PegBoard::~PegBoard() {
delete [] pegPins;
}
// Sets up pin modes for peg board
void PegBoard::begin() {
for(int i=0; i<pegCount; i++){
pinMode(pegPins[i],INPUT_PULLUP);
}
}
/*
* Scans hardware, returns a connection
* object representing the pins (if any)
* that are connected
*/
Connection PegBoard::scanPins() {
// One by one, pick a starting peg
for (byte in=0; in<pegCount; in++) {
startProbe(in);
// One by one, pick every OTHER peg
for (byte out=0; out<pegCount; out++) {
// Don't consider the same peg simultaneously as both input and output
if (in == out) {
continue;
}
if (!digitalRead(pegPins[out])) {
// We've seen a connected pair of pegs
stopProbe(in);
return Connection(in, out);
}
}
// No connections seen using this peg
stopProbe(in);
}
// No pins are connected
return Connection();
}
void PegBoard::startProbe(byte in) {
// Set it as output
pinMode(pegPins[in],OUTPUT);
// Tie that peg to ground
digitalWrite(pegPins[in], LOW);
}
void PegBoard::stopProbe(byte in) {
// Send a 5v signal on this pin
digitalWrite(pegPins[in],HIGH);
// Set it as an input, with pullup resistor
pinMode(pegPins[in],INPUT_PULLUP);
}
// Returns true, if there's a connection (different than the last seen)
// Otherwise, returns false.
boolean PegBoard::hasNewConnection() {
// Scan the hardware for connections
Connection currentConn = scanPins();
if (currentConn.isConnected()) {
// Same as the last one
if( currentConn == lastConn ) {
return false;
}
// A new connection has been established
else {
lastConn = currentConn;
return true;
}
}
// No connection present
else {
return false;
}
}
Connection PegBoard::getConnection() {
return lastConn;
}