Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit e3d77e3

Browse files
committed
added partial morse display
1 parent 1792de0 commit e3d77e3

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

examples/FirebaseMorse_ESP8266/FirebaseMorse_ESP8266.ino

+17-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#define WIFI_SSID "SSID"
2424
#define WIFI_PASSWORD "PASSWORD"
2525

26+
const int shortMillis = 500;
27+
const int longMillis = shortMillis * 3;
28+
2629
// We define morse . to be binary 0 and - to be binary 1. The longest letter
2730
// is 5 morse elements long so we would have a sparse array of 2^5=32. But
2831
// we need to add a leading 1 to ensure that .- and ..- are not the same value.
@@ -35,7 +38,7 @@ Adafruit_SSD1306 display(oledResetPin);
3538

3639
const int morseButtonPin = 2;
3740

38-
void updateDisplay(const String& message, const char& indicator);
41+
void updateDisplay(const String& message, const char& indicator, int code);
3942
void initializeMorseToChar();
4043

4144
void setup() {
@@ -59,21 +62,18 @@ void setup() {
5962
Serial.println(WiFi.localIP());
6063
}
6164

62-
const int shortMillis = 500;
63-
const int longMillis = shortMillis * 3;
64-
6565
String currentMessage;
6666
int currentLetter;
6767
void loop() {
6868
// Wait while button is up.
6969
int upStarted = millis();
7070
while (digitalRead(morseButtonPin) == HIGH) {
7171
if (millis() - upStarted > longMillis) {
72-
updateDisplay(currentMessage, 'w');
72+
updateDisplay(currentMessage, 'w', currentLetter);
7373
} else if (millis() - upStarted > shortMillis) {
74-
updateDisplay(currentMessage, 'l');
74+
updateDisplay(currentMessage, 'l', currentLetter);
7575
} else {
76-
updateDisplay(currentMessage, ' ');
76+
updateDisplay(currentMessage, ' ', currentLetter);
7777
}
7878
delay(1);
7979
}
@@ -99,11 +99,11 @@ void loop() {
9999
// Wait while button is down.
100100
while (digitalRead(morseButtonPin) == LOW) {
101101
if (millis() - pressStarted > longMillis) {
102-
updateDisplay(currentMessage, '-');
102+
updateDisplay(currentMessage, '-', currentLetter);
103103
} else if (millis() - pressStarted > shortMillis) {
104-
updateDisplay(currentMessage, '.');
104+
updateDisplay(currentMessage, '.', currentLetter);
105105
} else {
106-
updateDisplay(currentMessage, ' ');
106+
updateDisplay(currentMessage, ' ', currentLetter);
107107
}
108108
delay(1);
109109
}
@@ -114,20 +114,25 @@ void loop() {
114114
}
115115
}
116116

117-
void updateDisplay(const String& message, const char& indicator) {
117+
void updateDisplay(const String& message, const char& indicator, int code) {
118118
display.clearDisplay();
119119
display.setTextSize(2);
120120
display.setTextColor(WHITE);
121121
display.setCursor(0,0);
122122
display.println(message);
123123

124124
display.setTextSize(1);
125+
display.setCursor(0, display.height() - 10);
126+
const int mask = 1;
127+
while (code > 1) {
128+
display.print((code&mask) ? '-' : '.');
129+
code = code >> 1;
130+
}
125131
display.setCursor(display.width() - 10, display.height() - 10);
126132
display.print(indicator);
127133
display.display();
128134
}
129135

130-
131136
void initializeMorseToChar() {
132137
morseToChar[B101] = 'a';
133138
morseToChar[B11000] = 'b';

0 commit comments

Comments
 (0)