Advertisement
microrobotics

OLED-13-I2C-BLU

May 17th, 2024
2,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "U8glib.h"
  2.  
  3. // Initialize the display with the correct driver
  4. U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);
  5.  
  6. void u8g_prepare(void)
  7. {
  8.   u8g.setFont(u8g_font_6x10);
  9.   u8g.setFontRefHeightExtendedText();
  10.   u8g.setDefaultForegroundColor();
  11.   u8g.setFontPosTop();
  12. }
  13.  
  14. const unsigned char mybotic[] PROGMEM =
  15. {
  16.    // (Your image data)
  17. };
  18.  
  19. void picture(uint8_t a)
  20. {
  21.   u8g.drawXBMP(26, 2, 70, 63, mybotic);
  22. }
  23.  
  24. void intro(uint8_t a)
  25. {
  26.   u8g.setFont(u8g_font_unifont);
  27.   u8g.drawStr(2, 19, "Welcome");
  28.   u8g.drawStr(2, 38, "to");
  29.   u8g.drawStr(2, 57, "Mybotic :)");
  30. }
  31.  
  32. void u8g_box_frame(uint8_t a)
  33. {
  34.   u8g.drawStr(2, 0, "drawBox");
  35.   u8g.drawBox(5, 10, 20, 10);
  36.   u8g.drawBox(10 + a, 15, 30, 7);
  37.   u8g.drawStr(2, 30, "drawFrame");
  38.   u8g.drawFrame(5, 40, 20, 10);
  39.   u8g.drawFrame(10 + a, 45, 30, 7);
  40. }
  41.  
  42. void u8g_disc_circle(uint8_t a)
  43. {
  44.   u8g.drawStr(2, 0, "drawDisc");
  45.   u8g.drawDisc(11, 18, 9);
  46.   u8g.drawDisc(24 + a, 16, 7);
  47.   u8g.drawStr(2, 30, "drawCircle");
  48.   u8g.drawCircle(11, 48, 9);
  49.   u8g.drawCircle(24 + a, 46, 7);
  50. }
  51.  
  52. void u8g_r_frame(uint8_t a)
  53. {
  54.   u8g.drawStr(2, 0, "drawRFrame/Box");
  55.   u8g.drawRFrame(5, 10, 40, 30, a + 1);
  56.   u8g.drawRBox(50, 10, 25, 40, a + 1);
  57. }
  58.  
  59. void u8g_string(uint8_t a)
  60. {
  61.   u8g.drawStr270(30, 31 - a, "0/360");
  62.   u8g.drawStr(30 + a, 31, "90");
  63.   u8g.drawStr90(30, 31 + a, "180");
  64.   u8g.drawStr180(30 - a, 31, "270");
  65. }
  66.  
  67. void u8g_line(uint8_t a)
  68. {
  69.   u8g.drawStr(2, 0, "drawLine");
  70.   u8g.drawLine(7 + a, 10, 40, 55);
  71.   u8g.drawLine(7 + a * 2, 10, 60, 55);
  72.   u8g.drawLine(7 + a * 3, 10, 80, 55);
  73.   u8g.drawLine(7 + a * 4, 10, 100, 55);
  74. }
  75.  
  76. void u8g_triangle(uint8_t a)
  77. {
  78.   uint16_t offset = a;
  79.   u8g.drawStr(2, 0, "drawTriangle");
  80.   u8g.drawTriangle(14, 7, 45, 30, 10, 40);
  81.   u8g.drawTriangle(10 + offset, 7 - offset, 45 + offset, 30 - offset, 57 + offset, 10 - offset);
  82.   u8g.drawTriangle(53 + offset * 2, 10, 45 + offset * 2, 30, 86 + offset * 2, 53);
  83.   u8g.drawTriangle(7 + offset, 40 + offset, 45 + offset, 30 + offset, 86 + offset, 53 + offset);
  84. }
  85.  
  86. void u8g_extra_page(uint8_t a)
  87. {
  88.   if (u8g.getMode() == U8G_MODE_HICOLOR || u8g.getMode() == U8G_MODE_R3G3B2) {
  89.     u8g_uint_t r, g, b;
  90.     b = a << 5;
  91.     for (g = 0; g < 64; g++) {
  92.       for (r = 0; r < 64; r++) {
  93.         u8g.setRGB(r << 2, g << 2, b);
  94.         u8g.drawPixel(g, r);
  95.       }
  96.     }
  97.     u8g.setRGB(255, 255, 255);
  98.     u8g.drawStr(66, 0, "Color Page");
  99.   } else if (u8g.getMode() == U8G_MODE_GRAY2BIT) {
  100.     u8g.drawStr(66, 0, "Gray Level");
  101.     u8g.setColorIndex(1);
  102.     u8g.drawBox(2, 4, 64, 32);
  103.     u8g.drawBox(70, 20, 4, 12);
  104.     u8g.setColorIndex(2);
  105.     u8g.drawBox(2 + 1 * a, 4 + 1 * a, 64 - 2 * a, 32 - 2 * a);
  106.     u8g.drawBox(74, 20, 4, 12);
  107.     u8g.setColorIndex(3);
  108.     u8g.drawBox(2 + 2 * a, 4 + 2 * a, 64 - 4 * a, 32 - 4 * a);
  109.     u8g.drawBox(78, 20, 4, 12);
  110.   } else {
  111.     u8g.drawStr(2, 13, "setScale2x2");
  112.     u8g.setScale2x2();
  113.     u8g.drawStr(1, 6 + a, "setScale2x2");
  114.     u8g.undoScale();
  115.   }
  116. }
  117.  
  118. uint8_t draw_state = 2;
  119.  
  120. void draw(void)
  121. {
  122.   u8g_prepare();
  123.   switch (draw_state >> 3) {
  124.     case 0: picture(draw_state & 7); break;
  125.     case 1: intro(draw_state & 7); break;
  126.     case 2: u8g_box_frame(draw_state & 7); break;
  127.     case 3: u8g_disc_circle(draw_state & 7); break;
  128.     case 4: u8g_r_frame(draw_state & 7); break;
  129.     case 5: u8g_string(draw_state & 7); break;
  130.     case 6: u8g_line(draw_state & 7); break;
  131.     case 7: u8g_triangle(draw_state & 7); break;
  132.     case 8: u8g_extra_page(draw_state & 7); break;
  133.   }
  134. }
  135.  
  136. void setup(void)
  137. {
  138. #if defined(ARDUINO)
  139.   pinMode(13, OUTPUT);          
  140.   digitalWrite(13, HIGH);  
  141. #endif
  142. }
  143.  
  144. void loop(void)
  145. {
  146.   u8g.firstPage();  
  147.   do
  148.   {
  149.     draw();
  150.   } while (u8g.nextPage());
  151.  
  152.   draw_state++;
  153.   if (draw_state >= 9 * 8)
  154.     draw_state = 0;
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement