Advertisement
hwthinker

simpleDemo

Feb 21st, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * The MIT License (MIT)
  3.  *
  4.  * Copyright (c) 2018 by ThingPulse, Daniel Eichhorn
  5.  * Copyright (c) 2018 by Fabrice Weinberg
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in all
  15.  * copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * ThingPulse invests considerable time and money to develop these open source libraries.
  26.  * Please support us by buying our products (and not the clones) from
  27.  * https://thingpulse.com
  28.  *
  29.  */
  30.  
  31. // Include the correct display library
  32. // For a connection via I2C using Wire include
  33. #include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
  34. #include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
  35. // or #include "SH1106Wire.h", legacy include: `#include "SH1106.h"`
  36. // For a connection via I2C using brzo_i2c (must be installed) include
  37. // #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
  38. // #include "SSD1306Brzo.h"
  39. // #include "SH1106Brzo.h"
  40. // For a connection via SPI include
  41. // #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
  42. // #include "SSD1306Spi.h"
  43. // #include "SH1106SPi.h"
  44.  
  45. // Include custom images
  46. #include "images.h"
  47.  
  48. // Initialize the OLED display using SPI
  49. // D5 -> CLK
  50. // D7 -> MOSI (DOUT)
  51. // D0 -> RES
  52. // D2 -> DC
  53. // D8 -> CS
  54. // SSD1306Spi        display(D0, D2, D8);
  55. // or
  56. // SH1106Spi         display(D0, D2);
  57.  
  58. // Initialize the OLED display using brzo_i2c
  59. // D1 -> SDA
  60. // D2 -> SCL
  61. // SSD1306Brzo display(0x3c, D3, D5);
  62. // or
  63. // SH1106Brzo  display(0x3c, D3, D5);
  64.  
  65. // Initialize the OLED display using Wire library
  66. SSD1306Wire  display(0x3c, D1, D2);
  67. // SH1106 display(0x3c, D3, D5);
  68.  
  69.  
  70. #define DEMO_DURATION 3000
  71. typedef void (*Demo)(void);
  72.  
  73. int demoMode = 0;
  74. int counter = 1;
  75.  
  76. void setup() {
  77.   Serial.begin(115200);
  78.   Serial.println();
  79.   Serial.println();
  80.  
  81.  
  82.   // Initialising the UI will init the display too.
  83.   display.init();
  84.  
  85.   display.flipScreenVertically();
  86.   display.setFont(ArialMT_Plain_10);
  87.  
  88. }
  89.  
  90. void drawFontFaceDemo() {
  91.     // Font Demo1
  92.     // create more fonts at http://oleddisplay.squix.ch/
  93.     display.setTextAlignment(TEXT_ALIGN_LEFT);
  94.     display.setFont(ArialMT_Plain_10);
  95.     display.drawString(0, 0, "Hello world");
  96.     display.setFont(ArialMT_Plain_16);
  97.     display.drawString(0, 10, "Hello world");
  98.     display.setFont(ArialMT_Plain_24);
  99.     display.drawString(0, 26, "Hello world");
  100. }
  101.  
  102. void drawTextFlowDemo() {
  103.     display.setFont(ArialMT_Plain_10);
  104.     display.setTextAlignment(TEXT_ALIGN_LEFT);
  105.     display.drawStringMaxWidth(0, 0, 128,
  106.       "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
  107. }
  108.  
  109. void drawTextAlignmentDemo() {
  110.     // Text alignment demo
  111.   display.setFont(ArialMT_Plain_10);
  112.  
  113.   // The coordinates define the left starting point of the text
  114.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  115.   display.drawString(0, 10, "Left aligned (0,10)");
  116.  
  117.   // The coordinates define the center of the text
  118.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  119.   display.drawString(64, 22, "Center aligned (64,22)");
  120.  
  121.   // The coordinates define the right end of the text
  122.   display.setTextAlignment(TEXT_ALIGN_RIGHT);
  123.   display.drawString(128, 33, "Right aligned (128,33)");
  124. }
  125.  
  126. void drawRectDemo() {
  127.       // Draw a pixel at given position
  128.     for (int i = 0; i < 10; i++) {
  129.       display.setPixel(i, i);
  130.       display.setPixel(10 - i, i);
  131.     }
  132.     display.drawRect(12, 12, 20, 20);
  133.  
  134.     // Fill the rectangle
  135.     display.fillRect(14, 14, 17, 17);
  136.  
  137.     // Draw a line horizontally
  138.     display.drawHorizontalLine(0, 40, 20);
  139.  
  140.     // Draw a line horizontally
  141.     display.drawVerticalLine(40, 0, 20);
  142. }
  143.  
  144. void drawCircleDemo() {
  145.   for (int i=1; i < 8; i++) {
  146.     display.setColor(WHITE);
  147.     display.drawCircle(32, 32, i*3);
  148.     if (i % 2 == 0) {
  149.       display.setColor(BLACK);
  150.     }
  151.     display.fillCircle(96, 32, 32 - i* 3);
  152.   }
  153. }
  154.  
  155. void drawProgressBarDemo() {
  156.   int progress = (counter / 5) % 100;
  157.   // draw the progress bar
  158.   display.drawProgressBar(0, 32, 120, 10, progress);
  159.  
  160.   // draw the percentage as String
  161.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  162.   display.drawString(64, 15, String(progress) + "%");
  163. }
  164.  
  165. void drawImageDemo() {
  166.     // see http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html
  167.     // on how to create xbm files
  168.     display.drawXbm(34, 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  169. }
  170.  
  171. Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};
  172. int demoLength = (sizeof(demos) / sizeof(Demo));
  173. long timeSinceLastModeSwitch = 0;
  174.  
  175. void loop() {
  176.   // clear the display
  177.   display.clear();
  178.   // draw the current demo method
  179.   demos[demoMode]();
  180.  
  181.   display.setTextAlignment(TEXT_ALIGN_RIGHT);
  182.   display.drawString(10, 128, String(millis()));
  183.   // write the buffer to the display
  184.   display.display();
  185.  
  186.   if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
  187.     demoMode = (demoMode + 1)  % demoLength;
  188.     timeSinceLastModeSwitch = millis();
  189.   }
  190.   counter++;
  191.   delay(10);
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement