Advertisement
pleasedontcode

"OLED Graphics" rev_02

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