|
1 | 1 | /*
|
2 |
| - Simple logic analyzer. Triggers when the data pin goes low. |
3 |
| - Records bits until 30 1's in a row are received. |
| 2 | + * Simple logic analyzer. Triggers when the data pin goes low. |
| 3 | + * Records bits until 30 1's in a row are received. |
| 4 | + * Attemps to debounce the button (poorly). Resets the board |
| 5 | + * (reset pin low) when the button is pressed, and starts listening. |
4 | 6 | */
|
5 | 7 |
|
| 8 | +const int buttonPin = 7; // the number of the pushbutton pin |
6 | 9 | const int dataPin = 2; // corresponds to pin 6 on the KB connector (and pin 6 of the RJ12)
|
7 | 10 | const int resetPin = 5; // corresponds to pin 2 on the KB connector (and pin 4 on the RJ12)
|
8 | 11 |
|
| 12 | +int buttonState = 0; // variable for reading the pushbutton status |
| 13 | + |
9 | 14 | void setup() {
|
10 | 15 | pinMode(LED_BUILTIN, OUTPUT);
|
| 16 | + pinMode(buttonPin, INPUT); |
11 | 17 | pinMode(resetPin, OUTPUT);
|
12 | 18 | pinMode(dataPin, INPUT);
|
13 | 19 |
|
14 | 20 | // Set the reset pin low for 10 ms.
|
15 |
| - digitalWrite(resetPin , LOW); |
| 21 | + digitalWrite(resetPin, LOW); |
16 | 22 | delay(10);
|
17 |
| - digitalWrite(resetPin , HIGH); |
| 23 | + digitalWrite(resetPin, HIGH); |
18 | 24 |
|
19 |
| - Serial.begin(1000000); |
| 25 | + Serial.begin(9600); |
20 | 26 | Serial.println("Hello logic_analyzer");
|
21 | 27 | }
|
22 | 28 |
|
23 |
| -const int baudDelay=3333; |
| 29 | +int changed = 0; |
| 30 | +int shouldRead = 0; |
24 | 31 | int data = 0;
|
| 32 | +int inKey = 0; // are we reading a key? |
| 33 | +int numOnes = 0; // the # of 1s in a row |
25 | 34 |
|
26 | 35 | void loop() {
|
27 |
| - data = digitalRead(dataPin); |
28 |
| - if (data == LOW) { |
29 |
| - int numOnes = 0; |
| 36 | + // read the state of the pushbutton value: |
| 37 | + buttonState = digitalRead(buttonPin); |
| 38 | + |
| 39 | + // check if the pushbutton is pressed. If it is, the buttonState is HIGH: |
| 40 | + if (buttonState == HIGH && !changed) { |
| 41 | + // turn LED on, and toggle recording. |
30 | 42 | digitalWrite(LED_BUILTIN, HIGH);
|
31 |
| -// Serial.print("START: "); |
32 |
| - // unsigned long started = micros(); |
33 |
| - // Serial.print(0); Serial.print(", 0, "); |
34 |
| - int index = 0; |
35 |
| - int allData[9]; |
36 |
| - int theKey=0; |
37 |
| - delayMicroseconds(baudDelay/2); |
38 |
| - while (index <= 8) { |
39 |
| - // unsigned long now = micros(); |
40 |
| - data = digitalRead(dataPin); |
41 |
| - // Serial.print(now - started); |
42 |
| - allData[index++] = data; |
| 43 | + Serial.println("on"); |
| 44 | + shouldRead = 1 - shouldRead; |
| 45 | + Serial.print("ShouldRead is "); Serial.println(shouldRead); |
| 46 | + changed = 1; |
| 47 | + } else if (buttonState == LOW && changed) { |
| 48 | + // turn LED off: |
| 49 | + digitalWrite(LED_BUILTIN, LOW); |
| 50 | + Serial.println("off"); |
| 51 | + changed = 0; |
| 52 | + |
| 53 | + // beep/reset, and keep reading. |
| 54 | + digitalWrite(resetPin , LOW); |
| 55 | + delay(10); |
| 56 | + digitalWrite(resetPin , HIGH); |
| 57 | + } |
| 58 | + |
| 59 | + if (shouldRead == 1) { |
| 60 | + data = digitalRead(dataPin); |
| 61 | + if (data == LOW && !inKey) { |
| 62 | + inKey=1; // we're recording until numones==30 |
| 63 | + numOnes=0; |
| 64 | + Serial.println("START"); |
| 65 | + Serial.println("0"); |
| 66 | + } else if (inKey) { |
43 | 67 | if (data == HIGH) {
|
44 |
| - theKey = (theKey>>1)|0x80; |
45 |
| - //Serial.print(", 1, "); |
46 |
| - //Serial.print(1); |
| 68 | + Serial.println("1"); |
47 | 69 | numOnes++;
|
| 70 | + if (numOnes == 30) { |
| 71 | + inKey=0; |
| 72 | + numOnes=0; |
| 73 | + Serial.println("END"); |
| 74 | + delay(500); |
| 75 | + } |
48 | 76 | } else {
|
49 |
| - theKey = (theKey>>1); |
50 |
| - numOnes = 0; |
51 |
| - // Serial.print(", 0, "); |
52 |
| - //Serial.print(0); |
| 77 | + numOnes=0; |
| 78 | + Serial.println("0"); |
53 | 79 | }
|
54 |
| - delayMicroseconds(baudDelay); |
55 |
| - } |
56 |
| - Serial.print("TYPED: "); |
57 |
| - Serial.print((char)theKey); |
58 |
| - Serial.print(" ("); |
59 |
| - for (int i = 0; i < index; ++i) { |
60 |
| - Serial.print(allData[i]); |
61 | 80 | }
|
62 |
| - Serial.println(")"); |
63 |
| - digitalWrite(LED_BUILTIN, LOW); |
64 |
| -// Serial.println("END"); |
65 | 81 | }
|
66 | 82 | }
|
0 commit comments