Advertisement
ccarman602

Adafruit OLED Demo

Jun 3rd, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************************************************
  2. This is an example for our Monochrome OLEDs based on SSD1306 drivers
  3.  
  4.   Pick one up today in the adafruit shop!
  5.   ------> http://www.adafruit.com/category/63_98
  6.  
  7. This example is for a 128x64 size display using SPI to communicate
  8. 4 or 5 pins are required to interface
  9.  
  10. Adafruit invests time and resources providing this open source code,
  11. please support Adafruit and open-source hardware by purchasing
  12. products from Adafruit!
  13.  
  14. Written by Limor Fried/Ladyada  for Adafruit Industries.  
  15. BSD license, check license.txt for more information
  16. All text above, and the splash screen must be included in any redistribution
  17. *********************************************************************/
  18.  
  19. #include <SPI.h>
  20. #include <Wire.h>
  21. #include <Adafruit_GFX.h>
  22. #include <Adafruit_SSD1306.h>
  23.  
  24. // If using software SPI (the default case):
  25. #define OLED_MOSI   9
  26. #define OLED_CLK   10
  27. #define OLED_DC    11
  28. #define OLED_CS    12
  29. #define OLED_RESET 13
  30. Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  31.  
  32. /* Uncomment this block to use hardware SPI
  33. #define OLED_DC     6
  34. #define OLED_CS     7
  35. #define OLED_RESET  8
  36. Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
  37. */
  38.  
  39. #define NUMFLAKES 10
  40. #define XPOS 0
  41. #define YPOS 1
  42. #define DELTAY 2
  43.  
  44. #define LOGO16_GLCD_HEIGHT 16
  45. #define LOGO16_GLCD_WIDTH  16
  46. static const unsigned char PROGMEM logo16_glcd_bmp[] =
  47. { B00000000, B11000000,
  48.   B00000001, B11000000,
  49.   B00000001, B11000000,
  50.   B00000011, B11100000,
  51.   B11110011, B11100000,
  52.   B11111110, B11111000,
  53.   B01111110, B11111111,
  54.   B00110011, B10011111,
  55.   B00011111, B11111100,
  56.   B00001101, B01110000,
  57.   B00011011, B10100000,
  58.   B00111111, B11100000,
  59.   B00111111, B11110000,
  60.   B01111100, B11110000,
  61.   B01110000, B01110000,
  62.   B00000000, B00110000 };
  63.  
  64. #define SSD1306_LCDHEIGHT 64
  65. #if (SSD1306_LCDHEIGHT != 64)
  66. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  67. #endif
  68.  
  69. void setup()   {                
  70.   Serial.begin(9600);
  71.  
  72.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  73.   display.begin(SSD1306_SWITCHCAPVCC);
  74.   // init done
  75.  
  76.   // Show image buffer on the display hardware.
  77.   // Since the buffer is intialized with an Adafruit splashscreen
  78.   // internally, this will display the splashscreen.
  79.   display.display();
  80.   delay(2000);
  81.  
  82.   // Clear the buffer.
  83.   display.clearDisplay();
  84.  
  85.   // draw a single pixel
  86.   display.drawPixel(10, 10, WHITE);
  87.   // Show the display buffer on the hardware.
  88.   // NOTE: You _must_ call display after making any drawing commands
  89.   // to make them visible on the display hardware!
  90.   display.display();
  91.   delay(2000);
  92.   display.clearDisplay();
  93.  
  94.   // draw many lines
  95.   testdrawline();
  96.   display.display();
  97.   delay(2000);
  98.   display.clearDisplay();
  99.  
  100.   // draw rectangles
  101.   testdrawrect();
  102.   display.display();
  103.   delay(2000);
  104.   display.clearDisplay();
  105.  
  106.   // draw multiple rectangles
  107.   testfillrect();
  108.   display.display();
  109.   delay(2000);
  110.   display.clearDisplay();
  111.  
  112.   // draw mulitple circles
  113.   testdrawcircle();
  114.   display.display();
  115.   delay(2000);
  116.   display.clearDisplay();
  117.  
  118.   // draw a white circle, 10 pixel radius
  119.   display.fillCircle(display.width()/2, display.height()/2, 10, WHITE);
  120.   display.display();
  121.   delay(2000);
  122.   display.clearDisplay();
  123.  
  124.   testdrawroundrect();
  125.   delay(2000);
  126.   display.clearDisplay();
  127.  
  128.   testfillroundrect();
  129.   delay(2000);
  130.   display.clearDisplay();
  131.  
  132.   testdrawtriangle();
  133.   delay(2000);
  134.   display.clearDisplay();
  135.    
  136.   testfilltriangle();
  137.   delay(2000);
  138.   display.clearDisplay();
  139.  
  140.   // draw the first ~12 characters in the font
  141.   testdrawchar();
  142.   display.display();
  143.   delay(2000);
  144.   display.clearDisplay();
  145.  
  146.   // draw scrolling text
  147.   testscrolltext();
  148.   delay(2000);
  149.   display.clearDisplay();
  150.  
  151.   // text display tests
  152.   display.setTextSize(1);
  153.   display.setTextColor(WHITE);
  154.   display.setCursor(0,0);
  155.   display.println("Hello, world!");
  156.   display.setTextColor(BLACK, WHITE); // 'inverted' text
  157.   display.println(3.141592);
  158.   display.setTextSize(2);
  159.   display.setTextColor(WHITE);
  160.   display.print("0x"); display.println(0xDEADBEEF, HEX);
  161.   display.display();
  162.   delay(2000);
  163.   display.clearDisplay();
  164.  
  165.   // miniature bitmap display
  166.   display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);
  167.   display.display();
  168.  
  169.   // invert the display
  170.   display.invertDisplay(true);
  171.   delay(1000);
  172.   display.invertDisplay(false);
  173.   delay(1000);
  174.   display.clearDisplay();
  175.  
  176.   // draw a bitmap icon and 'animate' movement
  177.   testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
  178. }
  179.  
  180. void loop() {
  181. }
  182.  
  183. void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  184.   uint8_t icons[NUMFLAKES][3];
  185.  
  186.   // initialize
  187.   for (uint8_t f=0; f< NUMFLAKES; f++) {
  188.     icons[f][XPOS] = random(display.width());
  189.     icons[f][YPOS] = 0;
  190.     icons[f][DELTAY] = random(5) + 1;
  191.    
  192.     Serial.print("x: ");
  193.     Serial.print(icons[f][XPOS], DEC);
  194.     Serial.print(" y: ");
  195.     Serial.print(icons[f][YPOS], DEC);
  196.     Serial.print(" dy: ");
  197.     Serial.println(icons[f][DELTAY], DEC);
  198.   }
  199.  
  200.   while (1) {
  201.     // draw each icon
  202.     for (uint8_t f=0; f< NUMFLAKES; f++) {
  203.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
  204.     }
  205.     display.display();
  206.     delay(200);
  207.    
  208.     // then erase it + move it
  209.     for (uint8_t f=0; f< NUMFLAKES; f++) {
  210.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
  211.       // move it
  212.       icons[f][YPOS] += icons[f][DELTAY];
  213.       // if its gone, reinit
  214.       if (icons[f][YPOS] > display.height()) {
  215.         icons[f][XPOS] = random(display.width());
  216.         icons[f][YPOS] = 0;
  217.         icons[f][DELTAY] = random(5) + 1;
  218.       }
  219.     }
  220.    }
  221. }
  222.  
  223. void testdrawchar(void) {
  224.   display.setTextSize(1);
  225.   display.setTextColor(WHITE);
  226.   display.setCursor(0,0);
  227.  
  228.   for (uint8_t i=0; i < 168; i++) {
  229.     if (i == '\n') continue;
  230.     display.write(i);
  231.     if ((i > 0) && (i % 21 == 0))
  232.       display.println();
  233.   }    
  234.   display.display();
  235. }
  236.  
  237. void testdrawcircle(void) {
  238.   for (int16_t i=0; i<display.height(); i+=2) {
  239.     display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
  240.     display.display();
  241.   }
  242. }
  243.  
  244. void testfillrect(void) {
  245.   uint8_t color = 1;
  246.   for (int16_t i=0; i<display.height()/2; i+=3) {
  247.     // alternate colors
  248.     display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
  249.     display.display();
  250.     color++;
  251.   }
  252. }
  253.  
  254. void testdrawtriangle(void) {
  255.   for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
  256.     display.drawTriangle(display.width()/2, display.height()/2-i,
  257.                      display.width()/2-i, display.height()/2+i,
  258.                      display.width()/2+i, display.height()/2+i, WHITE);
  259.     display.display();
  260.   }
  261. }
  262.  
  263. void testfilltriangle(void) {
  264.   uint8_t color = WHITE;
  265.   for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
  266.     display.fillTriangle(display.width()/2, display.height()/2-i,
  267.                      display.width()/2-i, display.height()/2+i,
  268.                      display.width()/2+i, display.height()/2+i, WHITE);
  269.     if (color == WHITE) color = BLACK;
  270.     else color = WHITE;
  271.     display.display();
  272.   }
  273. }
  274.  
  275. void testdrawroundrect(void) {
  276.   for (int16_t i=0; i<display.height()/2-2; i+=2) {
  277.     display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
  278.     display.display();
  279.   }
  280. }
  281.  
  282. void testfillroundrect(void) {
  283.   uint8_t color = WHITE;
  284.   for (int16_t i=0; i<display.height()/2-2; i+=2) {
  285.     display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
  286.     if (color == WHITE) color = BLACK;
  287.     else color = WHITE;
  288.     display.display();
  289.   }
  290. }
  291.    
  292. void testdrawrect(void) {
  293.   for (int16_t i=0; i<display.height()/2; i+=2) {
  294.     display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
  295.     display.display();
  296.   }
  297. }
  298.  
  299. void testdrawline() {  
  300.   for (int16_t i=0; i<display.width(); i+=4) {
  301.     display.drawLine(0, 0, i, display.height()-1, WHITE);
  302.     display.display();
  303.   }
  304.   for (int16_t i=0; i<display.height(); i+=4) {
  305.     display.drawLine(0, 0, display.width()-1, i, WHITE);
  306.     display.display();
  307.   }
  308.   delay(250);
  309.  
  310.   display.clearDisplay();
  311.   for (int16_t i=0; i<display.width(); i+=4) {
  312.     display.drawLine(0, display.height()-1, i, 0, WHITE);
  313.     display.display();
  314.   }
  315.   for (int16_t i=display.height()-1; i>=0; i-=4) {
  316.     display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
  317.     display.display();
  318.   }
  319.   delay(250);
  320.  
  321.   display.clearDisplay();
  322.   for (int16_t i=display.width()-1; i>=0; i-=4) {
  323.     display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
  324.     display.display();
  325.   }
  326.   for (int16_t i=display.height()-1; i>=0; i-=4) {
  327.     display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
  328.     display.display();
  329.   }
  330.   delay(250);
  331.  
  332.   display.clearDisplay();
  333.   for (int16_t i=0; i<display.height(); i+=4) {
  334.     display.drawLine(display.width()-1, 0, 0, i, WHITE);
  335.     display.display();
  336.   }
  337.   for (int16_t i=0; i<display.width(); i+=4) {
  338.     display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE);
  339.     display.display();
  340.   }
  341.   delay(250);
  342. }
  343.  
  344. void testscrolltext(void) {
  345.   display.setTextSize(2);
  346.   display.setTextColor(WHITE);
  347.   display.setCursor(10,0);
  348.   display.clearDisplay();
  349.   display.println("scroll");
  350.   display.display();
  351.  
  352.   display.startscrollright(0x00, 0x0F);
  353.   delay(2000);
  354.   display.stopscroll();
  355.   delay(1000);
  356.   display.startscrollleft(0x00, 0x0F);
  357.   delay(2000);
  358.   display.stopscroll();
  359.   delay(1000);    
  360.   display.startscrolldiagright(0x00, 0x07);
  361.   delay(2000);
  362.   display.startscrolldiagleft(0x00, 0x07);
  363.   delay(2000);
  364.   display.stopscroll();
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement