SSD1306SimpleDemo.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // This example just provide basic function test;
  2. // For more informations, please vist www.heltec.cn or mail to support@heltec.cn
  3. #include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
  4. #include "heltec.h" // alias for `#include "SSD1306Wire.h"`
  5. #include "images.h"
  6. #define DEMO_DURATION 3000
  7. typedef void (*Demo)(void);
  8. int demoMode = 0;
  9. int counter = 1;
  10. void setup() {
  11. Heltec.begin(true /*DisplayEnable Enable*/, true /*Serial Enable*/);
  12. Heltec.display->flipScreenVertically();
  13. Heltec.display->setFont(ArialMT_Plain_10);
  14. }
  15. void drawFontFaceDemo() {
  16. // Font Demo1
  17. // create more fonts at http://oledHeltec.display->squix.ch/
  18. Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  19. Heltec.display->setFont(ArialMT_Plain_10);
  20. Heltec.display->drawString(0, 0, "Hello world");
  21. Heltec.display->setFont(ArialMT_Plain_16);
  22. Heltec.display->drawString(0, 10, "Hello world");
  23. }
  24. void drawTextFlowDemo() {
  25. Heltec.display->setFont(ArialMT_Plain_10);
  26. Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  27. Heltec.display->drawStringMaxWidth(0, 0, 128,
  28. "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr" );
  29. }
  30. void drawTextAlignmentDemo() {
  31. // Text alignment demo
  32. Heltec.display->setFont(ArialMT_Plain_10);
  33. // The coordinates define the left starting point of the text
  34. Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  35. Heltec.display->drawString(0, 0, "Left aligned (0,10)");
  36. // The coordinates define the center of the text
  37. Heltec.display->setTextAlignment(TEXT_ALIGN_CENTER);
  38. Heltec.display->drawString(64, 10, "Center aligned (64,10)");
  39. // The coordinates define the right end of the text
  40. Heltec.display->setTextAlignment(TEXT_ALIGN_RIGHT);
  41. Heltec.display->drawString(128, 20, "Right aligned (128,20)");
  42. }
  43. void drawRectDemo() {
  44. // Draw a pixel at given position
  45. for (int i = 0; i < 10; i++) {
  46. Heltec.display->setPixel(i, i);
  47. Heltec.display->setPixel(10 - i, i);
  48. }
  49. Heltec.display->drawRect(12, 12, 20, 20);
  50. // Fill the rectangle
  51. Heltec.display->fillRect(14, 14, 17, 17);
  52. // Draw a line horizontally
  53. Heltec.display->drawHorizontalLine(0, 40, 20);
  54. // Draw a line horizontally
  55. Heltec.display->drawVerticalLine(40, 0, 20);
  56. }
  57. void drawCircleDemo() {
  58. for (int i=1; i < 8; i++) {
  59. Heltec.display->setColor(WHITE);
  60. Heltec.display->drawCircle(32, 16, i*2);
  61. if (i % 2 == 0) {
  62. Heltec.display->setColor(BLACK);
  63. }
  64. Heltec.display->fillCircle(96, 16, 32 - i* 2);
  65. }
  66. }
  67. void drawProgressBarDemo() {
  68. int progress = (counter / 5) % 100;
  69. // draw the progress bar
  70. Heltec.display->drawProgressBar(0, 10, 120, 10, progress);
  71. // draw the percentage as String
  72. Heltec.display->setTextAlignment(TEXT_ALIGN_CENTER);
  73. Heltec.display->drawString(64, 0, String(progress) + "%");
  74. }
  75. void drawImageDemo() {
  76. // see http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html
  77. // on how to create xbm files
  78. Heltec.display->drawXbm(34, 0, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
  79. }
  80. Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};
  81. int demoLength = (sizeof(demos) / sizeof(Demo));
  82. long timeSinceLastModeSwitch = 0;
  83. void loop() {
  84. // clear the display
  85. Heltec.display->clear();
  86. // draw the current demo method
  87. demos[demoMode]();
  88. Heltec.display->setTextAlignment(TEXT_ALIGN_RIGHT);
  89. Heltec.display->drawString(10, 128, String(millis()));
  90. // write the buffer to the display
  91. Heltec.display->display();
  92. if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
  93. demoMode = (demoMode + 1) % demoLength;
  94. timeSinceLastModeSwitch = millis();
  95. }
  96. counter++;
  97. delay(10);
  98. }