Guest User

Untitled

a guest
Jun 11th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.73 KB | None | 0 0
  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.  // Use the corresponding display class:
  46.  
  47.  // Initialize the OLED display using SPI
  48.  // D5 -> CLK
  49.  // D7 -> MOSI (DOUT)
  50.  // D0 -> RES
  51.  // D2 -> DC
  52.  // D8 -> CS
  53.  // SSD1306Spi        display(D0, D2, D8);
  54.  // or
  55.  // SH1106Spi         display(D0, D2);
  56.  
  57.  // Initialize the OLED display using brzo_i2c
  58.  // D3 -> SDA
  59.  // D5 -> SCL
  60.  // SSD1306Brzo display(0x3c, D3, D5);
  61.  // or
  62.  // SH1106Brzo  display(0x3c, D3, D5);
  63.  
  64.  // Initialize the OLED display using Wire library
  65.  SSD1306Wire  display(0x3c, 5, 4);
  66.  // SH1106 display(0x3c, D3, D5);
  67.  
  68. // Adapted from Adafruit_SSD1306
  69. void drawLines() {
  70.   for (int16_t i=0; i<display.getWidth(); i+=4) {
  71.     display.drawLine(0, 0, i, display.getHeight()-1);
  72.     display.display();
  73.     delay(10);
  74.   }
  75.   for (int16_t i=0; i<display.getHeight(); i+=4) {
  76.     display.drawLine(0, 0, display.getWidth()-1, i);
  77.     display.display();
  78.     delay(10);
  79.   }
  80.   delay(250);
  81.  
  82.   display.clear();
  83.   for (int16_t i=0; i<display.getWidth(); i+=4) {
  84.     display.drawLine(0, display.getHeight()-1, i, 0);
  85.     display.display();
  86.     delay(10);
  87.   }
  88.   for (int16_t i=display.getHeight()-1; i>=0; i-=4) {
  89.     display.drawLine(0, display.getHeight()-1, display.getWidth()-1, i);
  90.     display.display();
  91.     delay(10);
  92.   }
  93.   delay(250);
  94.  
  95.   display.clear();
  96.   for (int16_t i=display.getWidth()-1; i>=0; i-=4) {
  97.     display.drawLine(display.getWidth()-1, display.getHeight()-1, i, 0);
  98.     display.display();
  99.     delay(10);
  100.   }
  101.   for (int16_t i=display.getHeight()-1; i>=0; i-=4) {
  102.     display.drawLine(display.getWidth()-1, display.getHeight()-1, 0, i);
  103.     display.display();
  104.     delay(10);
  105.   }
  106.   delay(250);
  107.   display.clear();
  108.   for (int16_t i=0; i<display.getHeight(); i+=4) {
  109.     display.drawLine(display.getWidth()-1, 0, 0, i);
  110.     display.display();
  111.     delay(10);
  112.   }
  113.   for (int16_t i=0; i<display.getWidth(); i+=4) {
  114.     display.drawLine(display.getWidth()-1, 0, i, display.getHeight()-1);
  115.     display.display();
  116.     delay(10);
  117.   }
  118.   delay(250);
  119. }
  120.  
  121. // Adapted from Adafruit_SSD1306
  122. void drawRect(void) {
  123.   for (int16_t i=0; i<display.getHeight()/2; i+=2) {
  124.     display.drawRect(i, i, display.getWidth()-2*i, display.getHeight()-2*i);
  125.     display.display();
  126.     delay(10);
  127.   }
  128. }
  129.  
  130. // Adapted from Adafruit_SSD1306
  131. void fillRect(void) {
  132.   uint8_t color = 1;
  133.   for (int16_t i=0; i<display.getHeight()/2; i+=3) {
  134.     display.setColor((color % 2 == 0) ? BLACK : WHITE); // alternate colors
  135.     display.fillRect(i, i, display.getWidth() - i*2, display.getHeight() - i*2);
  136.     display.display();
  137.     delay(10);
  138.     color++;
  139.   }
  140.   // Reset back to WHITE
  141.   display.setColor(WHITE);
  142. }
  143.  
  144. // Adapted from Adafruit_SSD1306
  145. void drawCircle(void) {
  146.   for (int16_t i=0; i<display.getHeight(); i+=2) {
  147.     display.drawCircle(display.getWidth()/2, display.getHeight()/2, i);
  148.     display.display();
  149.     delay(10);
  150.   }
  151.   delay(1000);
  152.   display.clear();
  153.  
  154.   // This will draw the part of the circel in quadrant 1
  155.   // Quadrants are numberd like this:
  156.   //   0010 | 0001
  157.   //  ------|-----
  158.   //   0100 | 1000
  159.   //
  160.   display.drawCircleQuads(display.getWidth()/2, display.getHeight()/2, display.getHeight()/4, 0b00000001);
  161.   display.display();
  162.   delay(200);
  163.   display.drawCircleQuads(display.getWidth()/2, display.getHeight()/2, display.getHeight()/4, 0b00000011);
  164.   display.display();
  165.   delay(200);
  166.   display.drawCircleQuads(display.getWidth()/2, display.getHeight()/2, display.getHeight()/4, 0b00000111);
  167.   display.display();
  168.   delay(200);
  169.   display.drawCircleQuads(display.getWidth()/2, display.getHeight()/2, display.getHeight()/4, 0b00001111);
  170.   display.display();
  171. }
  172.  
  173. void printBuffer(void) {
  174.   // Initialize the log buffer
  175.   // allocate memory to store 8 lines of text and 30 chars per line.
  176.   display.setLogBuffer(5, 30);
  177.  
  178.   // Some test data
  179.   const char* test[] = {
  180.       "Hello",
  181.       "World" ,
  182.       "----",
  183.       "Show off",
  184.       "how",
  185.       "the log buffer",
  186.       "is",
  187.       "working.",
  188.       "Even",
  189.       "scrolling is",
  190.       "working"
  191.   };
  192.  
  193.   for (uint8_t i = 0; i < 11; i++) {
  194.     display.clear();
  195.     // Print to the screen
  196.     display.println(test[i]);
  197.     // Draw it to the internal screen buffer
  198.     display.drawLogBuffer(0, 0);
  199.     // Display it on the screen
  200.     display.display();
  201.     delay(500);
  202.   }
  203. }
  204.  
  205. void setup() {
  206.   display.init();
  207.  
  208.   // display.flipScreenVertically();
  209.  
  210.   display.setContrast(255);
  211.  
  212.   drawLines();
  213.   delay(1000);
  214.   display.clear();
  215.  
  216.   drawRect();
  217.   delay(1000);
  218.   display.clear();
  219.  
  220.   fillRect();
  221.   delay(1000);
  222.   display.clear();
  223.  
  224.   drawCircle();
  225.   delay(1000);
  226.   display.clear();
  227.  
  228.   printBuffer();
  229.   delay(1000);
  230.   display.clear();
  231. }
  232.  
  233. void loop() { }
Advertisement
Add Comment
Please, Sign In to add comment