diff --git a/README.md b/README.md index d956cd9ee9c31b00a844e2ab5af8759c48ebc15e..afe8344f70c33ca22aa7633ee1dac7a3904d1ddf 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ The SSD1306Demo is a very comprehensive example demonstrating the most important ## Features * Draw pixels at given coordinates +* Draw lines from given coordinates to given coordinates * Draw or fill a rectangle with given dimensions * Draw Text at given coordinates: * Define Alignment: Left, Right and Center @@ -91,6 +92,9 @@ void setColor(SSD1306_COLOR color); // Draw a pixel at given position void setPixel(int16_t x, int16_t y); +// Draw a line from position 0 to position 1 +void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1); + // Draw the border of a rectangle at the given location void drawRect(int16_t x, int16_t y, int16_t width, int16_t height); diff --git a/SSD1306.cpp b/SSD1306.cpp index 29f845987e3ed50e37ea250b27edb319496d346e..6f290540f008d705b40d05c12a5e176b87d0c2c9 100644 --- a/SSD1306.cpp +++ b/SSD1306.cpp @@ -96,6 +96,46 @@ void SSD1306::setPixel(int16_t x, int16_t y) { } } +// Bresenham's algorithm - thx wikipedia and Adafruit_GFX +void SSD1306::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1) { + int16_t steep = abs(y1 - y0) > abs(x1 - x0); + if (steep) { + _swap_int16_t(x0, y0); + _swap_int16_t(x1, y1); + } + + if (x0 > x1) { + _swap_int16_t(x0, x1); + _swap_int16_t(y0, y1); + } + + int16_t dx, dy; + dx = x1 - x0; + dy = abs(y1 - y0); + + int16_t err = dx / 2; + int16_t ystep; + + if (y0 < y1) { + ystep = 1; + } else { + ystep = -1; + } + + for (; x0<=x1; x0++) { + if (steep) { + setPixel(y0, x0); + } else { + setPixel(x0, y0); + } + err -= dy; + if (err < 0) { + y0 += ystep; + err += dx; + } + } +} + void SSD1306::drawRect(int16_t x, int16_t y, int16_t width, int16_t height) { drawHorizontalLine(x, y, width); drawVerticalLine(x, y, height); diff --git a/SSD1306.h b/SSD1306.h index 65f0828d3e0cf50a782608cb927eb764d6a0093d..3b878e65991cf309a3fa1f173aa9b1ab9d62c5db 100644 --- a/SSD1306.h +++ b/SSD1306.h @@ -91,6 +91,10 @@ #define SETVCOMDETECT 0xDB #define SWITCHCAPVCC 0x2 +#ifndef _swap_int16_t +#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; } +#endif + enum SSD1306_COLOR { BLACK = 0, WHITE = 1, @@ -159,6 +163,9 @@ class SSD1306 { // Draw a pixel at given position void setPixel(int16_t x, int16_t y); + + // Draw a line from position 0 to position 1 + void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1); // Draw the border of a rectangle at the given location void drawRect(int16_t x, int16_t y, int16_t width, int16_t height); diff --git a/SSD1306Ui.cpp b/SSD1306Ui.cpp index 36b46aa129e836f96bedec98e2956bff829bcb5b..d6d7bdace48b22fb445dffa3ab04d7d32dc0e336 100644 --- a/SSD1306Ui.cpp +++ b/SSD1306Ui.cpp @@ -96,6 +96,7 @@ void SSD1306Ui::setFrameAnimation(AnimationDirection dir) { void SSD1306Ui::setFrames(FrameCallback* frameFunctions, uint8_t frameCount) { this->frameCount = frameCount; this->frameFunctions = frameFunctions; + this->state.currentFrame = 0; } // -/----- Overlays ------\- diff --git a/examples/SSD1306ClockDemo/SSD1306ClockDemo.ino b/examples/SSD1306ClockDemo/SSD1306ClockDemo.ino new file mode 100644 index 0000000000000000000000000000000000000000..6a46bd32219d6cecb4ebb77747ed488a93f38aa5 --- /dev/null +++ b/examples/SSD1306ClockDemo/SSD1306ClockDemo.ino @@ -0,0 +1,175 @@ +/** + * 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. + * + */ + +#include <Wire.h> +#include <TimeLib.h> +#include "SSD1306.h" +#include "SSD1306Ui.h" +#include "images.h" + +// Initialize the OLED display on address 0x3c +SSD1306 display(0x3c, 5, 4); +SSD1306Ui ui ( &display ); + +int screenW = 128; +int screenH = 64; +int clockCenterX = screenW/2; +int clockCenterY = ((screenH-16)/2)+16; // top yellow part is 16 px height +int clockRadius = 23; + +// utility function for digital clock display: prints leading 0 +String twoDigits(int digits){ + if(digits < 10) { + String i = '0'+String(digits); + return i; + } + else { + return String(digits); + } +} + +void clockOverlay(SSD1306 *display, SSD1306UiState* state) { + +} + +void analogClockFrame(SSD1306 *display, SSD1306UiState* state, int16_t x, int16_t y) { +// ui.disableIndicator(); + + // Draw the clock face +// display->drawCircle(clockCenterX + x, clockCenterY + y, clockRadius); + display->drawCircle(clockCenterX + x, clockCenterY + y, 2); + // + //hour ticks + for( int z=0; z < 360;z= z + 30 ){ + //Begin at 0° and stop at 360° + float angle = z ; + angle = ( angle / 57.29577951 ) ; //Convert degrees to radians + int x2 = ( clockCenterX + ( sin(angle) * clockRadius ) ); + int y2 = ( clockCenterY - ( cos(angle) * clockRadius ) ); + int x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 8 ) ) ) ); + int y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 8 ) ) ) ); + display->drawLine( x2 + x , y2 + y , x3 + x , y3 + y); + } + + // display second hand + float angle = second() * 6 ; + angle = ( angle / 57.29577951 ) ; //Convert degrees to radians + int x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 5 ) ) ) ); + int y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 5 ) ) ) ); + display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y); + // + // display minute hand + angle = minute() * 6 ; + angle = ( angle / 57.29577951 ) ; //Convert degrees to radians + x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 4 ) ) ) ); + y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 4 ) ) ) ); + display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y); + // + // display hour hand + angle = hour() * 30 + int( ( minute() / 12 ) * 6 ) ; + angle = ( angle / 57.29577951 ) ; //Convert degrees to radians + x3 = ( clockCenterX + ( sin(angle) * ( clockRadius - ( clockRadius / 2 ) ) ) ); + y3 = ( clockCenterY - ( cos(angle) * ( clockRadius - ( clockRadius / 2 ) ) ) ); + display->drawLine( clockCenterX + x , clockCenterY + y , x3 + x , y3 + y); +} + +void digitalClockFrame(SSD1306 *display, SSD1306UiState* state, int16_t x, int16_t y) { + String timenow = String(hour())+":"+twoDigits(minute())+":"+twoDigits(second()); + display->setTextAlignment(TEXT_ALIGN_CENTER); + display->setFont(ArialMT_Plain_24); + display->drawString(clockCenterX + x , clockCenterY + y, timenow ); +} + +// This array keeps function pointers to all frames +// frames are the single views that slide in +FrameCallback frames[] = { analogClockFrame, digitalClockFrame }; + +// how many frames are there? +int frameCount = 2; + +// Overlays are statically drawn on top of a frame eg. a clock +OverlayCallback overlays[] = { clockOverlay }; +int overlaysCount = 1; + +void setup() { + Serial.begin(9600); + 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(TOP); + + // 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(); + + unsigned long secsSinceStart = millis(); + // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: + const unsigned long seventyYears = 2208988800UL; + // subtract seventy years: + unsigned long epoch = secsSinceStart - seventyYears * SECS_PER_HOUR; + setTime(epoch); + +} + + +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); + + } + + +} + diff --git a/examples/SSD1306ClockDemo/images.h b/examples/SSD1306ClockDemo/images.h new file mode 100644 index 0000000000000000000000000000000000000000..a220a27c2b6637946f5df26f500f0eb88998a2c8 --- /dev/null +++ b/examples/SSD1306ClockDemo/images.h @@ -0,0 +1,21 @@ +const char activeSymbol[] PROGMEM = { + B00000000, + B00000000, + B00011000, + B00100100, + B01000010, + B01000010, + B00100100, + B00011000 +}; + +const char inactiveSymbol[] PROGMEM = { + B00000000, + B00000000, + B00000000, + B00000000, + B00011000, + B00011000, + B00000000, + B00000000 +};