14
14
// limitations under the License.
15
15
//
16
16
17
- // FirebaseStream_ESP8266 is a sample that stream bitcoin price from a
18
- // public Firebase and optionally display them on a OLED i2c screen.
19
-
20
17
#include < FirebaseArduino.h>
21
18
#include < ESP8266WiFi.h>
22
19
#include < Adafruit_GFX.h>
@@ -38,7 +35,7 @@ Adafruit_SSD1306 display(oledResetPin);
38
35
39
36
const int morseButtonPin = 2 ;
40
37
41
- void updateDisplay (const String& message);
38
+ void updateDisplay (const String& message, const char & indicator );
42
39
void initializeMorseToChar ();
43
40
44
41
void setup () {
@@ -63,13 +60,21 @@ void setup() {
63
60
}
64
61
65
62
const int shortMillis = 500 ;
63
+ const int longMillis = shortMillis * 3 ;
66
64
67
65
String currentMessage;
68
66
int currentLetter;
69
67
void loop () {
70
68
// Wait while button is up.
71
69
int upStarted = millis ();
72
70
while (digitalRead (morseButtonPin) == HIGH) {
71
+ if (millis () - upStarted > longMillis) {
72
+ updateDisplay (currentMessage, ' w' );
73
+ } else if (millis () - upStarted > shortMillis) {
74
+ updateDisplay (currentMessage, ' l' );
75
+ } else {
76
+ updateDisplay (currentMessage, ' ' );
77
+ }
73
78
delay (1 );
74
79
}
75
80
int upTime = millis () - upStarted;
@@ -83,35 +88,46 @@ void loop() {
83
88
Serial.println (" Listening for new letter." );
84
89
currentLetter = B1;
85
90
91
+ // 7 * short timing is a new word.
86
92
if (upTime > shortMillis * 7 ) {
87
93
currentMessage += " " ;
88
94
}
89
95
} // else the upTime < shortMillis we attribute to button bounce.
90
96
Serial.println (currentMessage);
91
- updateDisplay (currentMessage);
92
97
93
98
int pressStarted = millis ();
94
99
// Wait while button is down.
95
100
while (digitalRead (morseButtonPin) == LOW) {
101
+ if (millis () - pressStarted > longMillis) {
102
+ updateDisplay (currentMessage, ' -' );
103
+ } else if (millis () - pressStarted > shortMillis) {
104
+ updateDisplay (currentMessage, ' .' );
105
+ } else {
106
+ updateDisplay (currentMessage, ' ' );
107
+ }
96
108
delay (1 );
97
109
}
98
110
int pressTime = millis () - pressStarted;
99
111
if (pressTime > shortMillis) {
100
112
// Shift the binary representation left once then set last bit to 0 or 1.
101
- // A long is 3 times a short
102
- currentLetter = (currentLetter << 1 ) | (pressTime > shortMillis*3 );
113
+ currentLetter = (currentLetter << 1 ) | (pressTime > longMillis);
103
114
}
104
115
}
105
116
106
- void updateDisplay (const String& message) {
117
+ void updateDisplay (const String& message, const char & indicator ) {
107
118
display.clearDisplay ();
108
119
display.setTextSize (2 );
109
120
display.setTextColor (WHITE);
110
121
display.setCursor (0 ,0 );
111
122
display.println (message);
123
+
124
+ display.setTextSize (1 );
125
+ display.setCursor (display.width () - 10 , display.height () - 10 );
126
+ display.print (indicator);
112
127
display.display ();
113
128
}
114
129
130
+
115
131
void initializeMorseToChar () {
116
132
morseToChar[B101] = ' a' ;
117
133
morseToChar[B11000] = ' b' ;
0 commit comments