#include "SSD1306.h"
SSD1306 display(0x3c, 21, 22); // SDA=21, SCL=22
const int MAX_CHARS_PER_LINE = 16; // 1行の最大文字数
const int LINE_HEIGHT = 18; // フォント行高さ
const int DISPLAY_LINES = 4; // 画面に表示できる行数(64px ÷ 18px ≒ 3.5 → 4行まで)
String text = "Long ago, in an age when many court ladies served the emperor well, there was among them one lady of no high rank, yet he loved her more than any other—his favor shone on her.";
String lines[30]; // 折り返した行を格納(最大30行まで対応)
int totalLines = 0;
int currentLine = 0; // 先頭に表示される行番号
void setup() {
display.init();
display.setFont(ArialMT_Plain_10);
// 単語単位で折り返して行に分割
String currentLineStr = "";
int pos = 0;
while (pos < text.length()) {
int spaceIndex = text.indexOf(' ', pos);
if (spaceIndex == -1) spaceIndex = text.length();
String word = text.substring(pos, spaceIndex);
pos = spaceIndex + 1;
if (currentLineStr.length() + word.length() + 1 <= MAX_CHARS_PER_LINE) {
if (currentLineStr.length() > 0) currentLineStr += " ";
currentLineStr += word;
} else {
lines[totalLines++] = currentLineStr;
currentLineStr = word;
}
}
if (currentLineStr.length() > 0) {
lines[totalLines++] = currentLineStr;
}
display.clear();
}
void loop() {
display.clear();
// 表示範囲内の行を描画(Y位置をピクセル単位でずらす)
for (int i = 0; i <= DISPLAY_LINES; i++) {
int idx = (currentLine + i) % totalLines;
int y = i * LINE_HEIGHT - scrollOffset;
if (y >= -LINE_HEIGHT && y < 64) {
display.drawString(0, y, lines[idx]);
}
}
display.display();
scrollOffset++;
if (scrollOffset >= LINE_HEIGHT) {
scrollOffset = 0;
currentLine = (currentLine + 1) % totalLines;
}
delay(30); // スクロール速度(小さいほど速い)
}