[IDE] ESP32-S3(N4) IoT 开发板以I2C方式驱动OLED屏

[复制链接]
74|0
Lily李 发表于 2025-10-4 20:08 | 显示全部楼层 |阅读模式
Arduino IDE的环境下要以I2C方式实现OLED屏的显示驱动还是较为方便的,它需要相应的库进行支持。
其相应的工作条件为:
#include <Wire.h>
#include "SSD1306Wire.h"
#include "OLEDDisplayUi.h"
#include "images.h"
SSD1306Wire display(0x3c, 1, 2);
OLEDDisplayUi ui     ( &display );

相应的辅助函数为:
  1. void msOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
  2.   display->setTextAlignment(TEXT_ALIGN_RIGHT);
  3.   display->setFont(ArialMT_Plain_10);
  4.   display->drawString(128, 0, String(millis()));
  5. }

  6. void drawFrame1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  7.   display->drawXbm(x + 34, y + 14, WiFi_**_width, WiFi_**_height, WiFi_**_bits);
  8. }

  9. void drawFrame2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  10.   display->setTextAlignment(TEXT_ALIGN_LEFT);
  11.   display->setFont(ArialMT_Plain_10);
  12.   display->drawString(0 + x, 10 + y, "Arial 10");
  13.   display->setFont(ArialMT_Plain_16);
  14.   display->drawString(0 + x, 20 + y, "Arial 16");
  15.   display->setFont(ArialMT_Plain_24);
  16.   display->drawString(0 + x, 34 + y, "Arial 24");
  17. }

  18. void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  19.   // Text alignment demo
  20.   display->setFont(ArialMT_Plain_10);

  21.   // The coordinates define the left starting point of the text
  22.   display->setTextAlignment(TEXT_ALIGN_LEFT);
  23.   display->drawString(0 + x, 11 + y, "Left aligned (0,10)");

  24.   // The coordinates define the center of the text
  25.   display->setTextAlignment(TEXT_ALIGN_CENTER);
  26.   display->drawString(64 + x, 22 + y, "Center aligned (64,22)");

  27.   // The coordinates define the right end of the text
  28.   display->setTextAlignment(TEXT_ALIGN_RIGHT);
  29.   display->drawString(128 + x, 33 + y, "Right aligned (128,33)");
  30. }

  31. void drawFrame4(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  32. display->setTextAlignment(TEXT_ALIGN_LEFT);
  33.   display->setFont(ArialMT_Plain_10);
  34.   display->drawStringMaxWidth(0 + x, 10 + y, 128, "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore.");
  35. }

  36. void drawFrame5(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
  37. }

  38. FrameCallback frames[] = { drawFrame1, drawFrame2, drawFrame3, drawFrame4, drawFrame5 };

  39. int frameCount = 5;

  40. OverlayCallback overlays[] = { msOverlay };
  41. int overlaysCount = 1;

实现驱动效果测试的主程序为:
  1. void setup() {
  2.   Serial.begin(115200);
  3.   Serial.println();
  4.   Serial.println();
  5.   ui.setTargetFPS(60);
  6.   ui.setActiveSymbol(activeSymbol);
  7.   ui.setInactiveSymbol(inactiveSymbol);
  8. ui.setIndicatorPosition(BOTTOM);
  9.   ui.setIndicatorDirection(LEFT_RIGHT);
  10.   ui.setFrameAnimation(SLIDE_LEFT);
  11.   ui.setFrames(frames, frameCount);
  12.   ui.setOverlays(overlays, overlaysCount);
  13.   ui.init();
  14.   display.flipScreenVertically();
  15. }

  16. void loop() {
  17.   int remainingTimeBudget = ui.update();
  18.   if (remainingTimeBudget > 0) {
  19.     delay(remainingTimeBudget);
  20.   }
  21. }

经程序的上传,其测试效果如图1至图3所示。
5795868e10d955cc8b.png
1 显示图案

5504968e10dfc68960.png
2 变换字体大小

5564768e10e1390c2c.png
3 对齐效果


您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

15

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部