3030// is 5 morse elements long so we would have a sparse array of 2^5=32. But
3131// we need to add a leading 1 to ensure that .- and ..- are not the same value.
3232// This gives us a size of 2^6=64.
33- char morseToChar[64 ] = {};
34- morseToChar[B101] = ' a' ;
35- morseToChar[B11000] = ' b' ;
36- morseToChar[B11010] = ' c' ;
37- morseToChar[B1100] = ' d' ;
38- morseToChar[B10] = ' e' ;
39- morseToChar[B10010] = ' f' ;
40- morseToChar[B1110] = ' g' ;
41- morseToChar[B10000] = ' h' ;
42- morseToChar[B100] = ' i' ;
43- morseToChar[B1101] = ' k' ;
44- morseToChar[B10100] = ' l' ;
45- morseToChar[B111] = ' m' ;
46- morseToChar[B110] = ' n' ;
47- morseToChar[B1111] = ' o' ;
48- morseToChar[B10110] = ' p' ;
49- morseToChar[B11101] = ' q' ;
50- morseToChar[B1010] = ' r' ;
51- morseToChar[B1000] = ' s' ;
52- morseToChar[B11] = ' t' ;
53- morseToChar[B1001] = ' u' ;
54- morseToChar[B10001] = ' v' ;
55- morseToChar[B1011] = ' w' ;
56- morseToChar[B11001] = ' x' ;
57- morseToChar[B11011] = ' y' ;
58- morseToChar[B11100] = ' z' ;
59- morseToChar[B101111] = ' 1' ;
60- morseToChar[B100111] = ' 2' ;
61- morseToChar[B100011] = ' 3' ;
62- morseToChar[B100001] = ' 4' ;
63- morseToChar[B100000] = ' 5' ;
64- morseToChar[B110000] = ' 6' ;
65- morseToChar[B111000] = ' 7' ;
66- morseToChar[B111100] = ' 8' ;
67- morseToChar[B111110] = ' 9' ;
68- morseToChar[B111111] = ' 0' ;
33+ const int morseToCharSize = 64 ;
34+ char morseToChar[morseToCharSize] = {};
6935
7036const int oledResetPin = 3 ;
7137Adafruit_SSD1306 display (oledResetPin);
7238
7339const int morseButtonPin = 2 ;
7440
7541void setup () {
42+ initializeMorseToChar ();
43+
7644 Serial.begin (9600 );
7745
7846 display.begin (SSD1306_SWITCHCAPVCC, 0x3C ); // initialize with the I2C addr 0x3C (for the 128x32)
@@ -86,29 +54,98 @@ void setup() {
8654 delay (500 );
8755 }
8856 Serial.println ();
57+
8958 Serial.print (" connected: " );
9059 Serial.println (WiFi.localIP ());
91-
92- Firebase.begin (" publicdata-cryptocurrency.firebaseio.com" );
93- Firebase.stream (" /bitcoin/last" );
9460}
9561
96- const int longPressThresholdMillis = 1000 ;
62+ const int shortMillis = 500 ;
9763
98- bool morseButtonDownTime = 0 ;
9964String currentMessage;
100- String currentMorseLetter ;
65+ int currentLetter ;
10166void loop () {
102- // ButtonDown.
103- if (morseButtonDownTime == 0 && digitalRead (morseButtonPin) == LOW) {
104- morseButtonDownTime = millis ();
105-
106- // ButtonUp.
107- } else if (morseButtonDownTime != 0 && digitalRead (morseButtonPin) == HIGH) {
108- const int millisDown = millis () - moreseButtonDownTime;
109- morseButtonDownTime = 0 ;
67+ // Wait while button is up.
68+ int upStarted = millis ();
69+ while (digitalRead (morseButtonPin) == HIGH) {
70+ delay (1 );
71+ }
72+ int upTime = millis () - upStarted;
73+ if (upTime > shortMillis*3 ) {
74+ // A letter break is 3 * the length of a short (.). We give a lot of
75+ // lee way for poor morse-coders.
76+ if (currentLetter > morseToCharSize || morseToChar[currentLetter] == ' \0 ' ) {
77+ Serial.println (" Invalid morse char, ignoring" );
78+ } else {
79+ currentMessage += morseToChar[currentLetter];
80+ }
81+ Serial.println (" Listening for new letter." );
82+ currentLetter = B1;
11083
111- const bool longPress = millisDown >= longPressThreshold;
112- currentMorseLetter += longPress ? " ." : " _" ;
84+ if (upTime > shortMillis * 5 ) {
85+ // A word break is 7 * the length of a short(.). We are being generous and
86+ // accepting anything over 5.
87+ currentMessage += " " ;
88+ }
89+ } // else the upTime < shortMillis we attribute to button bounce.
90+ Serial.println (currentMessage);
91+ updateDisplay (currentMessage);
92+
93+ int pressStarted = millis ();
94+ // Wait while button is down.
95+ while (digitalRead (morseButtonPin) == LOW) {
96+ delay (1 );
11397 }
98+ int pressTime = millis () - pressStarted;
99+ if (pressTime > shortMillis) {
100+ // 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 );
103+ }
104+ }
105+
106+ void updateDisplay (const String& message) {
107+ display.clearDisplay ();
108+ display.setTextSize (2 );
109+ display.setTextColor (WHITE);
110+ display.setCursor (0 ,0 );
111+ display.println (message);
112+ display.display ();
113+ }
114+
115+ void initializeMorseToChar () {
116+ morseToChar[B101] = ' a' ;
117+ morseToChar[B11000] = ' b' ;
118+ morseToChar[B11010] = ' c' ;
119+ morseToChar[B1100] = ' d' ;
120+ morseToChar[B10] = ' e' ;
121+ morseToChar[B10010] = ' f' ;
122+ morseToChar[B1110] = ' g' ;
123+ morseToChar[B10000] = ' h' ;
124+ morseToChar[B100] = ' i' ;
125+ morseToChar[B1101] = ' k' ;
126+ morseToChar[B10100] = ' l' ;
127+ morseToChar[B111] = ' m' ;
128+ morseToChar[B110] = ' n' ;
129+ morseToChar[B1111] = ' o' ;
130+ morseToChar[B10110] = ' p' ;
131+ morseToChar[B11101] = ' q' ;
132+ morseToChar[B1010] = ' r' ;
133+ morseToChar[B1000] = ' s' ;
134+ morseToChar[B11] = ' t' ;
135+ morseToChar[B1001] = ' u' ;
136+ morseToChar[B10001] = ' v' ;
137+ morseToChar[B1011] = ' w' ;
138+ morseToChar[B11001] = ' x' ;
139+ morseToChar[B11011] = ' y' ;
140+ morseToChar[B11100] = ' z' ;
141+ morseToChar[B101111] = ' 1' ;
142+ morseToChar[B100111] = ' 2' ;
143+ morseToChar[B100011] = ' 3' ;
144+ morseToChar[B100001] = ' 4' ;
145+ morseToChar[B100000] = ' 5' ;
146+ morseToChar[B110000] = ' 6' ;
147+ morseToChar[B111000] = ' 7' ;
148+ morseToChar[B111100] = ' 8' ;
149+ morseToChar[B111110] = ' 9' ;
150+ morseToChar[B111111] = ' 0' ;
114151}
0 commit comments