Guest User

Untitled

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