Advertisement
hwthinker

Demo2

Feb 13th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************
  2.   This is a example sketch demonstrating the graphics
  3.   capabilities of the SSD1331 library  for the 0.96"
  4.   16-bit Color OLED with SSD1331 driver chip
  5.  
  6.   Pick one up today in the adafruit shop!
  7.   ------> http://www.adafruit.com/products/684
  8.  
  9.   These displays use SPI to communicate, 4 or 5 pins are required to  
  10.   interface
  11.   Adafruit invests time and resources providing this open source code,
  12.   please support Adafruit and open-source hardware by purchasing
  13.   products from Adafruit!
  14.  
  15.   Written by Limor Fried/Ladyada for Adafruit Industries.  
  16.   BSD license, all text above must be included in any redistribution
  17.  ****************************************************/
  18.  
  19. #include <Adafruit_GFX.h>
  20. #include <Adafruit_SSD1331.h>
  21. #include <SPI.h>
  22.  
  23.  
  24. // You can use any (4 or) 5 pins
  25. const uint8_t SCLK_OLED =  14; //SCLK
  26. const uint8_t MOSI_OLED =  13; //MOSI (Master Output Slave Input)
  27. const uint8_t MISO_OLED =  12; // (Master Input Slave Output)  belum ******
  28. const uint8_t CS_OLED = 15;
  29. const uint8_t DC_OLED =  16; //OLED DC(Data/Command)
  30. const uint8_t RST_OLED =  4; //OLED Reset
  31.  
  32.  
  33. // Color definitions
  34. #define BLACK           0x0000
  35. #define BLUE            0x001F
  36. #define RED             0xF800
  37. #define GREEN           0x07E0
  38. #define CYAN            0x07FF
  39. #define MAGENTA         0xF81F
  40. #define YELLOW          0xFFE0  
  41. #define WHITE           0xFFFF
  42.  
  43. // Option 1: use any pins but a little slower
  44. //Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);  
  45. Adafruit_SSD1331 display = Adafruit_SSD1331(CS_OLED, DC_OLED, MOSI_OLED, SCLK_OLED, RST_OLED);
  46.  
  47. // Option 2: must use the hardware SPI pins
  48. // (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
  49. // an output. This is much faster - also required if you want
  50. // to use the microSD card (see the image drawing example)
  51. //Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, rst);
  52.  
  53.  
  54. float p = 3.1415926;
  55.  
  56. void setup(void) {
  57.   Serial.begin(9600);
  58.   Serial.print("hello!");
  59.   display.begin();
  60.  
  61.   Serial.println("init");
  62.   uint16_t time = millis();
  63.   display.fillScreen(BLACK);
  64.   time = millis() - time;
  65.  
  66.   Serial.println(time, DEC);
  67.   delay(500);
  68.    
  69.   lcdTestPattern();
  70.   delay(1000);
  71.  
  72.   display.fillScreen(BLACK);
  73.   display.setCursor(0,0);
  74.   display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
  75.   delay(1000);
  76.  
  77.   // tft print function!
  78.   tftPrintTest();
  79.   delay(2000);
  80.  
  81.   //a single pixel
  82.   display.drawPixel(display.width()/2, display.height()/2, GREEN);
  83.   delay(500);
  84.  
  85.   // line draw test
  86.   testlines(YELLOW);
  87.   delay(500);    
  88.  
  89.   // optimized lines
  90.   testfastlines(RED, BLUE);
  91.   delay(500);    
  92.  
  93.   testdrawrects(GREEN);
  94.   delay(1000);
  95.  
  96.   testfillrects(YELLOW, MAGENTA);
  97.   delay(1000);
  98.  
  99.   display.fillScreen(BLACK);
  100.   testfillcircles(10, BLUE);
  101.   testdrawcircles(10, WHITE);
  102.   delay(1000);
  103.  
  104.   testroundrects();
  105.   delay(500);
  106.  
  107.   testtriangles();
  108.   delay(500);
  109.  
  110.   Serial.println("done");
  111.   delay(1000);
  112. }
  113.  
  114. void loop() {
  115. }
  116.  
  117. void testlines(uint16_t color) {
  118.    display.fillScreen(BLACK);
  119.    for (int16_t x=0; x < display.width()-1; x+=6) {
  120.      display.drawLine(0, 0, x, display.height()-1, color);
  121.    }
  122.    for (int16_t y=0; y < display.height()-1; y+=6) {
  123.      display.drawLine(0, 0, display.width()-1, y, color);
  124.    }
  125.    
  126.    display.fillScreen(BLACK);
  127.    for (int16_t x=0; x < display.width()-1; x+=6) {
  128.      display.drawLine(display.width()-1, 0, x, display.height()-1, color);
  129.    }
  130.    for (int16_t y=0; y < display.height()-1; y+=6) {
  131.      display.drawLine(display.width()-1, 0, 0, y, color);
  132.    }
  133.  
  134.    // To avoid ESP8266 watchdog timer resets when not using the hardware SPI pins
  135.    delay(0);
  136.  
  137.    display.fillScreen(BLACK);
  138.    for (int16_t x=0; x < display.width()-1; x+=6) {
  139.      display.drawLine(0, display.height()-1, x, 0, color);
  140.    }
  141.    for (int16_t y=0; y < display.height()-1; y+=6) {
  142.      display.drawLine(0, display.height()-1, display.width()-1, y, color);
  143.    }
  144.  
  145.    display.fillScreen(BLACK);
  146.    for (int16_t x=0; x < display.width()-1; x+=6) {
  147.      display.drawLine(display.width()-1, display.height()-1, x, 0, color);
  148.    }
  149.    for (int16_t y=0; y < display.height()-1; y+=6) {
  150.      display.drawLine(display.width()-1, display.height()-1, 0, y, color);
  151.    }
  152.    
  153. }
  154.  
  155. void testdrawtext(char *text, uint16_t color) {
  156.   display.setTextSize(1);
  157.   display.setTextColor(WHITE);
  158.   display.setCursor(0,0);
  159.  
  160.   for (uint8_t i=0; i < 168; i++) {
  161.     if (i == '\n') continue;
  162.     display.write(i);
  163.     if ((i > 0) && (i % 21 == 0))
  164.       display.println();
  165.   }    
  166. }
  167.  
  168. void testfastlines(uint16_t color1, uint16_t color2) {
  169.    display.fillScreen(BLACK);
  170.    for (int16_t y=0; y < display.height()-1; y+=5) {
  171.      display.drawFastHLine(0, y, display.width()-1, color1);
  172.    }
  173.    for (int16_t x=0; x < display.width()-1; x+=5) {
  174.      display.drawFastVLine(x, 0, display.height()-1, color2);
  175.    }
  176. }
  177.  
  178. void testdrawrects(uint16_t color) {
  179.  display.fillScreen(BLACK);
  180.  for (int16_t x=0; x < display.height()-1; x+=6) {
  181.    display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color);
  182.  }
  183. }
  184.  
  185. void testfillrects(uint16_t color1, uint16_t color2) {
  186.  display.fillScreen(BLACK);
  187.  for (int16_t x=display.height()-1; x > 6; x-=6) {
  188.    display.fillRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color1);
  189.    display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color2);
  190.  }
  191. }
  192.  
  193. void testfillcircles(uint8_t radius, uint16_t color) {
  194.   for (uint8_t x=radius; x < display.width()-1; x+=radius*2) {
  195.     for (uint8_t y=radius; y < display.height()-1; y+=radius*2) {
  196.       display.fillCircle(x, y, radius, color);
  197.     }
  198.   }  
  199. }
  200.  
  201. void testdrawcircles(uint8_t radius, uint16_t color) {
  202.   for (int16_t x=0; x < display.width()-1+radius; x+=radius*2) {
  203.     for (int16_t y=0; y < display.height()-1+radius; y+=radius*2) {
  204.       display.drawCircle(x, y, radius, color);
  205.     }
  206.   }  
  207. }
  208.  
  209. void testtriangles() {
  210.   display.fillScreen(BLACK);
  211.   int color = 0xF800;
  212.   int t;
  213.   int w = display.width()/2;
  214.   int x = display.height();
  215.   int y = 0;
  216.   int z = display.width();
  217.   for(t = 0 ; t <= 15; t+=1) {
  218.     display.drawTriangle(w, y, y, x, z, x, color);
  219.     x-=4;
  220.     y+=4;
  221.     z-=4;
  222.     color+=100;
  223.   }
  224. }
  225.  
  226. void testroundrects() {
  227.   display.fillScreen(BLACK);
  228.   int color = 100;
  229.   int i;
  230.   int t;
  231.   for(t = 0 ; t <= 4; t+=1) {
  232.     int x = 0;
  233.     int y = 0;
  234.     int w = display.width();
  235.     int h = display.height();
  236.     for(i = 0 ; i <= 8; i+=1) {
  237.       display.drawRoundRect(x, y, w, h, 5, color);
  238.       x+=2;
  239.       y+=3;
  240.       w-=4;
  241.       h-=6;
  242.       color+=1100;
  243.     }
  244.     color+=100;
  245.   }
  246. }
  247.  
  248. void tftPrintTest() {
  249.   display.fillScreen(BLACK);
  250.   display.setCursor(0, 5);
  251.   display.setTextColor(RED);  
  252.   display.setTextSize(1);
  253.   display.println("Hello World!");
  254.   display.setTextColor(YELLOW, GREEN);
  255.   display.setTextSize(2);
  256.   display.print("Hello Wo");
  257.   display.setTextColor(BLUE);
  258.   display.setTextSize(3);
  259.   display.print(1234.567);
  260.   delay(1500);
  261.   display.setCursor(0, 5);
  262.   display.fillScreen(BLACK);
  263.   display.setTextColor(WHITE);
  264.   display.setTextSize(0);
  265.   display.println("Hello World!");
  266.   display.setTextSize(1);
  267.   display.setTextColor(GREEN);
  268.   display.print(p, 5);
  269.   display.println(" Want pi?");
  270.   display.print(8675309, HEX); // print 8,675,309 out in HEX!
  271.   display.print(" Print HEX");
  272.   display.setTextColor(WHITE);
  273.   display.println("Sketch has been");
  274.   display.println("running for: ");
  275.   display.setTextColor(MAGENTA);
  276.   display.print(millis() / 1000);
  277.   display.setTextColor(WHITE);
  278.   display.print(" seconds.");
  279. }
  280.  
  281. void mediabuttons() {
  282.  // play
  283.   display.fillScreen(BLACK);
  284.   display.fillRoundRect(25, 10, 78, 60, 8, WHITE);
  285.   display.fillTriangle(42, 20, 42, 60, 90, 40, RED);
  286.   delay(500);
  287.   // pause
  288.   display.fillRoundRect(25, 90, 78, 60, 8, WHITE);
  289.   display.fillRoundRect(39, 98, 20, 45, 5, GREEN);
  290.   display.fillRoundRect(69, 98, 20, 45, 5, GREEN);
  291.   delay(500);
  292.   // play color
  293.   display.fillTriangle(42, 20, 42, 60, 90, 40, BLUE);
  294.   delay(50);
  295.   // pause color
  296.   display.fillRoundRect(39, 98, 20, 45, 5, RED);
  297.   display.fillRoundRect(69, 98, 20, 45, 5, RED);
  298.   // play color
  299.   display.fillTriangle(42, 20, 42, 60, 90, 40, GREEN);
  300. }
  301.  
  302. /**************************************************************************/
  303. /*!
  304.     @brief  Renders a simple test pattern on the LCD
  305. */
  306. /**************************************************************************/
  307. void lcdTestPattern(void)
  308. {
  309.   uint8_t w,h;
  310.   display.setAddrWindow(0, 0, 96, 64);
  311.  
  312.   for(h=0; h<64; h++)
  313.   {
  314.     for(w=0; w<96; w++)
  315.     {
  316.       if(w>83){display.writePixel(WHITE);}
  317.       else if(w>71){display.writePixel(BLUE);}
  318.       else if(w>59){display.writePixel(GREEN);}
  319.       else if(w>47){display.writePixel(CYAN);}
  320.       else if(w>35){display.writePixel(RED);}
  321.       else if(w>23){display.writePixel(MAGENTA);}
  322.       else if(w>11){display.writePixel(YELLOW);}
  323.       else {display.writePixel(BLACK);}
  324.     }
  325.   }
  326.   display.endWrite();
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement