Created
March 25, 2021 16:46
-
-
Save mvklingeren/acb99e7d279bb9decf4e8bbb5e4c77a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2016 by Daniel Eichhorn | |
* Copyright (c) 2016 by Fabrice Weinberg | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
* | |
*/ | |
// This is a basic code example, based on a Adafruit SSD1306 example: | |
// LILYGO® TTGO ESP32 WiFi + bluetooth 18650 Battery Protection Board 0.96 Inch OLED Development Tool | |
// LILYGO® TTGO WiFi & Bluetooth Battery ESP32 Module ESP32 0.96 inch OLED Development Board | |
// 0.96 Inch OLED Display 18650 Lithium Battery WiFi Bluetooth Shield ESP32 ESP-32 ESP8266 CP2102 Module Development Board Module | |
// diymore.cc 18650 oled. | |
// In Arduino IDE make sure to select the right board: | |
// 'ESP32 Dev Module' | |
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"` | |
// Include the UI lib | |
#include "OLEDDisplayUi.h" | |
// Initialize the OLED display | |
SSD1306 display(0x3c, 5, 4); | |
OLEDDisplayUi ui(&display); | |
// custom images | |
#define WiFi_Logo_width 60 | |
#define WiFi_Logo_height 36 | |
const char WiFi_Logo_bits[] PROGMEM = { | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, | |
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, | |
0xFF, 0x03, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, | |
0x00, 0xFF, 0xFF, 0xFF, 0x07, 0xC0, 0x83, 0x01, 0x80, 0xFF, 0xFF, 0xFF, | |
0x01, 0x00, 0x07, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0x00, | |
0xC0, 0xFF, 0xFF, 0x7C, 0x00, 0x60, 0x0C, 0x00, 0xC0, 0x31, 0x46, 0x7C, | |
0xFC, 0x77, 0x08, 0x00, 0xE0, 0x23, 0xC6, 0x3C, 0xFC, 0x67, 0x18, 0x00, | |
0xE0, 0x23, 0xE4, 0x3F, 0x1C, 0x00, 0x18, 0x00, 0xE0, 0x23, 0x60, 0x3C, | |
0x1C, 0x70, 0x18, 0x00, 0xE0, 0x03, 0x60, 0x3C, 0x1C, 0x70, 0x18, 0x00, | |
0xE0, 0x07, 0x60, 0x3C, 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C, | |
0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00, | |
0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x8F, 0x71, 0x3C, | |
0x1C, 0x70, 0x18, 0x00, 0xC0, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x08, 0x00, | |
0xC0, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x0C, 0x00, 0x80, 0xFF, 0xFF, 0x1F, | |
0x00, 0x00, 0x06, 0x00, 0x80, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x07, 0x00, | |
0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0xF8, 0xFF, 0xFF, | |
0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00, | |
0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, | |
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const char activeSymbol[] PROGMEM = { | |
B00000000, | |
B00000000, | |
B00011000, | |
B00100100, | |
B01000010, | |
B01000010, | |
B00100100, | |
B00011000 | |
}; | |
const char inactiveSymbol[] PROGMEM = { | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00011000, | |
B00011000, | |
B00000000, | |
B00000000 | |
}; | |
void msOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) { | |
display->setTextAlignment(TEXT_ALIGN_RIGHT); | |
display->setFont(ArialMT_Plain_10); | |
display->drawString(128, 0, String(millis())); | |
} | |
void drawFrame1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { | |
// draw an xbm image. | |
// Please note that everything that should be transitioned | |
// needs to be drawn relative to x and y | |
display->drawXbm(x + 34, y + 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits); | |
} | |
void drawFrame2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { | |
// Demonstrates the 3 included default sizes. The fonts come from SSD1306Fonts.h file | |
// Besides the default fonts there will be a program to convert TrueType fonts into this format | |
display->setTextAlignment(TEXT_ALIGN_LEFT); | |
display->setFont(ArialMT_Plain_10); | |
display->drawString(0 + x, 10 + y, "Arial 10"); | |
display->setFont(ArialMT_Plain_16); | |
display->drawString(0 + x, 20 + y, "Arial 16"); | |
display->setFont(ArialMT_Plain_24); | |
display->drawString(0 + x, 34 + y, "Arial 24"); | |
} | |
void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { | |
// Text alignment demo | |
display->setFont(ArialMT_Plain_10); | |
// The coordinates define the left starting point of the text | |
display->setTextAlignment(TEXT_ALIGN_LEFT); | |
display->drawString(0 + x, 11 + y, "Left aligned (0,10)"); | |
// The coordinates define the center of the text | |
display->setTextAlignment(TEXT_ALIGN_CENTER); | |
display->drawString(64 + x, 22 + y, "Center aligned (64,22)"); | |
// The coordinates define the right end of the text | |
display->setTextAlignment(TEXT_ALIGN_RIGHT); | |
display->drawString(128 + x, 33 + y, "Right aligned (128,33)"); | |
} | |
void drawFrame4(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { | |
// Demo for drawStringMaxWidth: | |
// with the third parameter you can define the width after which words will be wrapped. | |
// Currently only spaces and "-" are allowed for wrapping | |
display->setTextAlignment(TEXT_ALIGN_LEFT); | |
display->setFont(ArialMT_Plain_10); | |
display->drawStringMaxWidth(0 + x, 10 + y, 128, "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore."); | |
} | |
void drawFrame5(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { | |
} | |
// This array keeps function pointers to all frames | |
// frames are the single views that slide in | |
FrameCallback frames[] = { drawFrame1, drawFrame2, drawFrame3, drawFrame4, drawFrame5 }; | |
// how many frames are there? | |
int frameCount = 5; | |
// Overlays are statically drawn on top of a frame eg. a clock | |
OverlayCallback overlays[] = { msOverlay }; | |
int overlaysCount = 1; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println(); | |
// The ESP is capable of rendering 60fps in 80Mhz mode | |
// but that won't give you much time for anything else | |
// run it in 160Mhz mode or just set it to 30 fps | |
ui.setTargetFPS(60); | |
// Customize the active and inactive symbol | |
ui.setActiveSymbol(activeSymbol); | |
ui.setInactiveSymbol(inactiveSymbol); | |
// You can change this to | |
// TOP, LEFT, BOTTOM, RIGHT | |
ui.setIndicatorPosition(BOTTOM); | |
// Defines where the first frame is located in the bar. | |
ui.setIndicatorDirection(LEFT_RIGHT); | |
// You can change the transition that is used | |
// SLIDE_LEFT, SLIDE_RIGHT, SLIDE_UP, SLIDE_DOWN | |
ui.setFrameAnimation(SLIDE_LEFT); | |
// Add frames | |
ui.setFrames(frames, frameCount); | |
// Add overlays | |
ui.setOverlays(overlays, overlaysCount); | |
// Initialising the UI will init the display too. | |
ui.init(); | |
display.flipScreenVertically(); | |
} | |
void loop() { | |
int remainingTimeBudget = ui.update(); | |
if (remainingTimeBudget > 0) { | |
// You can do some work here | |
// Don't do stuff if you are below your | |
// time budget. | |
delay(remainingTimeBudget); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment