Advertisement
pleasedontcode

OLED Graphics rev_01

May 31st, 2024
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.08 KB | None | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: OLED Graphics
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-05-31 20:39:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create an interface to visualize real-time data on */
  21.     /* a Wemos OLED display using Adafruit_SSD1306 and */
  22.     /* U8g2_for_Adafruit_GFX libraries. Utilize I2C pins */
  23.     /* A4 (SDA) and A5 (SCL) with slave address 60. */
  24.     /* Define setup and loop functions. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <Adafruit_GFX.h>
  30. #include <Adafruit_SSD1306.h>
  31. #include <U8g2_for_Adafruit_GFX.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF I2C PINS *****/
  38. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  39. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  40. const uint8_t display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;  // I2C address for the display
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. Adafruit_SSD1306 display(-1);  // Initialize with the reset pin set to -1 for I2C
  44. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;  // Create an instance of U8G2_FOR_ADAFRUIT_GFX
  45.  
  46. void setup(void) {
  47.   // Initialize serial communication
  48.   Serial.begin(9600);
  49.  
  50.   // Initialize the display with the I2C address
  51.   display.begin(SSD1306_SWITCHCAPVCC, display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  52.   display.display();
  53.   delay(2000);
  54.   display.clearDisplay();
  55.  
  56.   // Initialize U8G2_FOR_ADAFRUIT_GFX
  57.   u8g2_for_adafruit_gfx.begin(display);
  58.  
  59.   // Draw a single pixel
  60.   display.drawPixel(10, 10, WHITE);
  61.   display.display();
  62.   delay(2000);
  63.   display.clearDisplay();
  64.  
  65.   // Draw many lines
  66.   testdrawline();
  67.   display.display();
  68.   delay(2000);
  69.   display.clearDisplay();
  70.  
  71.   // Draw rectangles
  72.   testdrawrect();
  73.   display.display();
  74.   delay(2000);
  75.   display.clearDisplay();
  76.  
  77.   // Draw multiple rectangles
  78.   testfillrect();
  79.   display.display();
  80.   delay(2000);
  81.   display.clearDisplay();
  82.  
  83.   // Draw multiple circles
  84.   testdrawcircle();
  85.   display.display();
  86.   delay(2000);
  87.   display.clearDisplay();
  88.  
  89.   // Draw a white circle, 10 pixel radius
  90.   display.fillCircle(display.width() / 2, display.height() / 2, 10, WHITE);
  91.   display.display();
  92.   delay(2000);
  93.   display.clearDisplay();
  94.  
  95.   testdrawroundrect();
  96.   delay(2000);
  97.   display.clearDisplay();
  98.  
  99.   testfillroundrect();
  100.   delay(2000);
  101.   display.clearDisplay();
  102.  
  103.   testdrawtriangle();
  104.   delay(2000);
  105.   display.clearDisplay();
  106.  
  107.   testfilltriangle();
  108.   delay(2000);
  109.   display.clearDisplay();
  110.  
  111.   // Draw the first ~12 characters in the font
  112.   testdrawchar();
  113.   display.display();
  114.   delay(2000);
  115.   display.clearDisplay();
  116.  
  117.   // Draw scrolling text
  118.   testscrolltext();
  119.   delay(2000);
  120.   display.clearDisplay();
  121.  
  122.   // Text display tests
  123.   display.setTextSize(1);
  124.   display.setTextColor(WHITE);
  125.   display.setCursor(0, 0);
  126.   display.println("Hello, world!");
  127.   display.setTextColor(BLACK, WHITE);  // 'inverted' text
  128.   display.println(3.141592);
  129.   display.setTextSize(2);
  130.   display.setTextColor(WHITE);
  131.   display.print("0x");
  132.   display.println(0xDEADBEEF, HEX);
  133.   display.display();
  134.   delay(2000);
  135.   display.clearDisplay();
  136.  
  137.   // Miniature bitmap display
  138.   display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1);
  139.   display.display();
  140.   delay(1);
  141.  
  142.   // Invert the display
  143.   display.invertDisplay(true);
  144.   delay(1000);
  145.   display.invertDisplay(false);
  146.   delay(1000);
  147.   display.clearDisplay();
  148.  
  149.   // Draw a bitmap icon and 'animate' movement
  150.   testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
  151. }
  152.  
  153. void loop(void) {
  154.   // put your main code here, to run repeatedly:
  155. }
  156.  
  157. // Function definitions for drawing shapes and text
  158. void testdrawline() {
  159.   for (int16_t i = 0; i < display.width(); i += 4) {
  160.     display.drawLine(0, 0, i, display.height() - 1, WHITE);
  161.     display.display();
  162.     delay(1);
  163.   }
  164.   for (int16_t i = 0; i < display.height(); i += 4) {
  165.     display.drawLine(0, 0, display.width() - 1, i, WHITE);
  166.     display.display();
  167.     delay(1);
  168.   }
  169.   delay(250);
  170.  
  171.   display.clearDisplay();
  172.   for (int16_t i = 0; i < display.width(); i += 4) {
  173.     display.drawLine(0, display.height() - 1, i, 0, WHITE);
  174.     display.display();
  175.     delay(1);
  176.   }
  177.   for (int16_t i = display.height() - 1; i >= 0; i -= 4) {
  178.     display.drawLine(0, display.height() - 1, display.width() - 1, i, WHITE);
  179.     display.display();
  180.     delay(1);
  181.   }
  182.   delay(250);
  183.  
  184.   display.clearDisplay();
  185.   for (int16_t i = display.width() - 1; i >= 0; i -= 4) {
  186.     display.drawLine(display.width() - 1, display.height() - 1, i, 0, WHITE);
  187.     display.display();
  188.     delay(1);
  189.   }
  190.   for (int16_t i = display.height() - 1; i >= 0; i -= 4) {
  191.     display.drawLine(display.width() - 1, display.height() - 1, 0, i, WHITE);
  192.     display.display();
  193.     delay(1);
  194.   }
  195.   delay(250);
  196.  
  197.   display.clearDisplay();
  198.   for (int16_t i = 0; i < display.height(); i += 4) {
  199.     display.drawLine(display.width() - 1, 0, 0, i, WHITE);
  200.     display.display();
  201.     delay(1);
  202.   }
  203.   for (int16_t i = 0; i < display.width(); i += 4) {
  204.     display.drawLine(display.width() - 1, 0, i, display.height() - 1, WHITE);
  205.     display.display();
  206.     delay(1);
  207.   }
  208.   delay(250);
  209. }
  210.  
  211. void testdrawrect(void) {
  212.   for (int16_t i = 0; i < display.height() / 2; i += 2) {
  213.     display.drawRect(i, i, display.width() - 2 * i, display.height() - 2 * i, WHITE);
  214.     display.display();
  215.     delay(1);
  216.   }
  217. }
  218.  
  219. void testfillrect(void) {
  220.   uint8_t color = 1;
  221.   for (int16_t i = 0; i < display.height() / 2; i += 3) {
  222.     // alternate colors
  223.     display.fillRect(i, i, display.width() - i * 2, display.height() - i * 2, color % 2);
  224.     display.display();
  225.     delay(1);
  226.     color++;
  227.   }
  228. }
  229.  
  230. void testdrawcircle(void) {
  231.   for (int16_t i = 0; i < display.height(); i += 2) {
  232.     display.drawCircle(display.width() / 2, display.height() / 2, i, WHITE);
  233.     display.display();
  234.     delay(1);
  235.   }
  236. }
  237.  
  238. void testdrawroundrect(void) {
  239.   for (int16_t i = 0; i < display.height() / 2 - 2; i += 2) {
  240.     display.drawRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i, display.height() / 4, WHITE);
  241.     display.display();
  242.     delay(1);
  243.   }
  244. }
  245.  
  246. void testfillroundrect(void) {
  247.   uint8_t color = WHITE;
  248.   for (int16_t i = 0; i < display.height() / 2 - 2; i += 2) {
  249.     display.fillRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i, display.height() / 4, color);
  250.     if (color == WHITE) color = BLACK;
  251.     else color = WHITE;
  252.     display.display();
  253.     delay(1);
  254.   }
  255. }
  256.  
  257. void testdrawtriangle(void) {
  258.   for (int16_t i = 0; i < min(display.width(), display.height()) / 2; i += 5) {
  259.     display.drawTriangle(display.width() / 2, display.height() / 2 - i,
  260.                          display.width() / 2 - i, display.height() / 2 + i,
  261.                          display.width() / 2 + i, display.height() / 2 + i, WHITE);
  262.     display.display();
  263.     delay(1);
  264.   }
  265. }
  266.  
  267. void testfilltriangle(void) {
  268.   uint8_t color = WHITE;
  269.   for (int16_t i = min(display.width(), display.height()) / 2; i > 0; i -= 5) {
  270.     display.fillTriangle(display.width() / 2, display.height() / 2 - i,
  271.                          display.width() / 2 - i, display.height() / 2 + i,
  272.                          display.width() / 2 + i, display.height() / 2 + i, WHITE);
  273.     if (color == WHITE) color = BLACK;
  274.     else color = WHITE;
  275.     display.display();
  276.     delay(1);
  277.   }
  278. }
  279.  
  280. void testdrawchar(void) {
  281.   display.setTextSize(1);
  282.   display.setTextColor(WHITE);
  283.   display.setCursor(0, 0);
  284.  
  285.   for (uint8_t i = 0; i < 168; i++) {
  286.     if (i == '\n') continue;
  287.     display.write(i);
  288.     if ((i > 0) && (i % 21 == 0))
  289.       display.println();
  290.   }
  291.   display.display();
  292.   delay(1);
  293. }
  294.  
  295. void testscrolltext(void) {
  296.   display.setTextSize(2);
  297.   display.setTextColor(WHITE);
  298.   display.setCursor(10, 0);
  299.   display.clearDisplay();
  300.   display.println("scroll");
  301.   display.display();
  302.   delay(1);
  303.  
  304.   display.startscrollright(0x00, 0x0F);
  305.   delay(2000);
  306.   display.stopscroll();
  307.   delay(1000);
  308.   display.startscrollleft(0x00, 0x0F);
  309.   delay(2000);
  310.   display.stopscroll();
  311.   delay(1000);
  312.   display.startscrolldiagright(0x00, 0x07);
  313.   delay(2000);
  314.   display.startscrolldiagleft(0x00, 0x07);
  315.   delay(2000);
  316.   display.stopscroll();
  317. }
  318.  
  319. void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  320.   uint8_t icons[NUMFLAKES][3];
  321.  
  322.   // initialize
  323.   for (uint8_t f = 0; f < NUMFLAKES; f++) {
  324.     icons[f][XPOS] = random(display.width());
  325.     icons[f][YPOS] = 0;
  326.     icons[f][DELTAY] = random(5) + 1;
  327.  
  328.     Serial.print("x: ");
  329.     Serial.print(icons[f][XPOS], DEC);
  330.     Serial.print(" y: ");
  331.     Serial.print(icons[f][YPOS], DEC);
  332.     Serial.print(" dy: ");
  333.     Serial.println(icons[f][DELTAY], DEC);
  334.   }
  335.  
  336.   while (1) {
  337.     // draw each icon
  338.     for (uint8_t f = 0; f < NUMFLAKES; f++) {
  339.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
  340.     }
  341.     display.display();
  342.     delay(200);
  343.  
  344.     // then erase it + move it
  345.     for (uint8_t f = 0; f < NUMFLAKES; f++) {
  346.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
  347.       // move it
  348.       icons[f][YPOS] += icons[f][DELTAY];
  349.       // if its gone, reinit
  350.       if (icons[f][YPOS] > display.height()) {
  351.         icons[f][XPOS] = random(display.width());
  352.         icons[f][YPOS] = 0;
  353.         icons[f][DELTAY] = random(5) + 1;
  354.       }
  355.     }
  356.   }
  357. }
  358.  
  359. #define NUMFLAKES 10
  360. #define XPOS 0
  361. #define YPOS 1
  362. #define DELTAY 2
  363.  
  364. #define LOGO16_GLCD_HEIGHT 16
  365. #define LOGO16_GLCD_WIDTH 16
  366. static const unsigned char PROGMEM logo16_glcd_bmp[] = {
  367.   B00000000, B11000000,
  368.   B00000001, B11000000,
  369.   B00000001, B11000000,
  370.   B00000011, B11100000,
  371.   B11110011, B11100000,
  372.   B11111110, B11111000,
  373.   B01111110, B11111111,
  374.   B00110011, B10011111,
  375.   B00011111, B11111100,
  376.   B00001101, B01110000,
  377.   B00011011, B10100000,
  378.   B00111111, B11100000,
  379.   B00111111, B11110000,
  380.   B01111100, B11110000,
  381.   B01110000, B01110000,
  382.   B00000000, B00110000
  383. };
  384.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement