// Minimalis kode OLED utk centering text
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
String formatAngka(String angka) {
String output = "";
int ctr=angka.length();
int ribu = 0;
while (ctr >= 0) {
output=angka[ctr]+output;
if (ribu==3 && ctr>1)
{
output=","+output;
ribu=0;
}
ctr=ctr-1;
ribu=ribu+1;
}
return output;
}
void drawCenterString(String text,int Yaxis) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
display.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
if (Yaxis<0)
{
// display on horizontal and vertical center
display.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
} else
{
// display on horizontal center
display.setCursor((SCREEN_WIDTH - width) / 2, Yaxis);
}
display.println(text); // text to display
display.display();
}
int countLetters(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (isAlpha(str[i])) {
count++;
}
}
return count;
}
void separateWords(String sentence) {
char charArray[sentence.length() + 1];
int baris = 5;
sentence.toCharArray(charArray, sentence.length() + 1);
char* token = strtok(charArray, " ");
while (token != NULL) {
// Serial.println(token);
drawCenterString(token,baris);
token = strtok(NULL, " ");
baris = baris + 30;
}
}
void ShowBigText(String kalimat){
display.clearDisplay();
Serial.print(kalimat+"= ");
Serial.println(String(kalimat.length()));
if (kalimat.length()>10) separateWords (kalimat);
else drawCenterString(kalimat,-1);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
drawCenterString("Connecting",5);
drawCenterString("Tello",35);
delay(2000);
ShowBigText("cw 180");
delay(2000);
ShowBigText("forward 300");
}
void loop() {
// put your main code here, to run repeatedly:
}
Loading
ssd1306
ssd1306