|
| 1 | +// |
| 2 | +// Copyright 2015 Google Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 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 | +#include <FirebaseArduino.h> |
| 21 | +#include <ESP8266WiFi.h> |
| 22 | +#include <Adafruit_GFX.h> |
| 23 | +#include <Adafruit_SSD1306.h> |
| 24 | + |
| 25 | +// Set these to run example. |
| 26 | +#define WIFI_SSID "SSID" |
| 27 | +#define WIFI_PASSWORD "PASSWORD" |
| 28 | + |
| 29 | +// We define morse . to be binary 0 and - to be binary 1. The longest letter |
| 30 | +// is 5 morse elements long so we would have a sparse array of 2^5=32. But |
| 31 | +// we need to add a leading 1 to ensure that .- and ..- are not the same value. |
| 32 | +// 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[B1 |
| 47 | + |
| 48 | +const int oledResetPin = 3; |
| 49 | +Adafruit_SSD1306 display(oledResetPin); |
| 50 | + |
| 51 | +const int morseButtonPin = 2; |
| 52 | + |
| 53 | +void setup() { |
| 54 | + Serial.begin(9600); |
| 55 | + |
| 56 | + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| 57 | + display.display(); |
| 58 | + |
| 59 | + // connect to wifi. |
| 60 | + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 61 | + Serial.print("connecting"); |
| 62 | + while (WiFi.status() != WL_CONNECTED) { |
| 63 | + Serial.print("."); |
| 64 | + delay(500); |
| 65 | + } |
| 66 | + Serial.println(); |
| 67 | + Serial.print("connected: "); |
| 68 | + Serial.println(WiFi.localIP()); |
| 69 | + |
| 70 | + Firebase.begin("publicdata-cryptocurrency.firebaseio.com"); |
| 71 | + Firebase.stream("/bitcoin/last"); |
| 72 | +} |
| 73 | + |
| 74 | +const int longPressThresholdMillis = 1000; |
| 75 | + |
| 76 | +bool morseButtonDownTime = 0; |
| 77 | +String currentMessage; |
| 78 | +String currentMorseLetter; |
| 79 | +void loop() { |
| 80 | + // ButtonDown. |
| 81 | + if (morseButtonDownTime == 0 && digitalRead(morseButtonPin) == LOW) { |
| 82 | + morseButtonDownTime = millis(); |
| 83 | + |
| 84 | + // ButtonUp. |
| 85 | + } else if (morseButtonDownTime != 0 && digitalRead(morseButtonPin) == HIGH) { |
| 86 | + const int millisDown = millis() - moreseButtonDownTime; |
| 87 | + morseButtonDownTime = 0; |
| 88 | + |
| 89 | + const bool longPress = millisDown >= longPressThreshold; |
| 90 | + currentMorseLetter += longPress ? "." : "_"; |
| 91 | + } |
| 92 | +} |
0 commit comments