Hi bersaelor,
ich kann leider nicht mehr genau sagen woher ich es habe.
Zunächst dachte ich aus den Beispielen von Arduino. Es kann aber auch aus dem Netz gewesen sein.
Hier das funktionsfähige Beispiel, falls Interesse besteht:
// For a connection via I2C using the Arduino Wire include:
#include <Wire.h> //display initialization
#include "HT_SSD1306Wire.h" //display initialization
#include "images.h"
static SSD1306Wire display(0x3c, 500000, SDA_OLED, SCL_OLED, GEOMETRY_128_64, RST_OLED);
// 0x3c: I²C address of the display , 500000: I²C clock frequency in Hz , SDA_OLED: SDA pin for the I²C bus , SCL_OLED: SCL pin for the I²C bus,
// resolution GEOMETRY_128_64 x=128px y=64px , RST_OLED: Reset pin for the display
#define DEMO_DURATION 3000
typedef void (*Demo)(void);
int demoMode = 0;
int counter = 1; //Zähler vordefiniert
// enable external voltage (Vext) to power the display (if needed):
void setup() {
Serial.begin(115200); // baudrate
Serial.println();
Serial.println();
VextON(); // Turn on external voltage
delay(100); // Wait for display power stabilization 100 sec
// Initialising the UI will init the display too.
display.init(); // Initialisierung des Displays
// display.clear(); Display löschen
// display.display(); Display updaten
// display.setContrast(255); Kontrastlevel einstellen (0-255)
display.setFont(ArialMT_Plain_10); // allg. Textschriftart und Größe festgelegt, z.B. ArialMT_Plain_10: Small 10px font, ArialMT_Plain_16: Medium 16px font, ArialMT_Plain_24: Large 24px font
// max. 64px font hier möglich
}
// TEXT AUF BILDSCHIRM ANZEIGEN LASSEN
void drawFontFaceDemo() {
// Font Demo1
// create more fonts at http://oleddisplay.squix.ch/
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Hello world");
display.setFont(ArialMT_Plain_16);
display.drawString(0, 10, "Hello world");
display.setFont(ArialMT_Plain_24);
display.drawString(0, 26, "Hello world");
}
// TEXT AUF BILDSCHIRM FLIESSEN LASSEN
void drawTextFlowDemo() {
display.setFont(ArialMT_Plain_10); //Schriftgröße
display.setTextAlignment(TEXT_ALIGN_LEFT); //Anordnung Schrift (linksbündig: TEXT_ALIGN_LEFT, zentriert: TEXT_ALIGN_CENTER, rechtsbündig: TEXT_ALIGN_RIGHT)
display.drawStringMaxWidth(0, 0, 128, //drawStringMaxWidth: multi-line text with automatic wrapping (text at spaces and hyphens to fit within the specified width);
// Anordnung Schrift: x-Startpunkt, y-Startpunkt, max. Breite, Ausgabe des Textes innerhalb " "
"Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
}
// TEXT AUF VERSCH. ARTEN ANORDNEN
void drawTextAlignmentDemo() {
// Text alignment demo
char str[30];
int x = 0;
int y = 0;
display.setFont(ArialMT_Plain_10);
// The coordinates define the left starting point of the text
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(x, y, "Left aligned (0,0)");
// The coordinates define the center of the text
display.setTextAlignment(TEXT_ALIGN_CENTER);
x = display.width()/2; //Berechnung desr Mitte im Display
y = display.height()/2-5;
sprintf(str,"Center aligned (%d,%d)",x,y);
display.drawString(x, y, str);
// The coordinates define the right end of the text
display.setTextAlignment(TEXT_ALIGN_RIGHT);
x = display.width();
y = display.height()-12;
sprintf(str,"Right aligned (%d,%d)",x,y);
display.drawString(x, y, str);
}
// Draw a pixel at given position
void drawRectDemo() {
for (int i = 0; i < 10; i++) {
display.setPixel(i, i);
display.setPixel(10 - i, i);
}
display.drawRect(12, 12, 20, 20);
// Fill the rectangle
display.fillRect(14, 14, 17, 17);
// Draw a line horizontally
display.drawHorizontalLine(0, 40, 20);
// Draw a line horizontally
display.drawVerticalLine(40, 0, 20);
}
// Draw a circels at given position
void drawCircleDemo() {
for (int i=1; i < 8; i++) {
display.setColor(WHITE);
display.drawCircle(32, 32, i*3); //display.drawCircle(x Start, y Start, Radius); draw circle outline
if (i % 2 == 0) {
display.setColor(BLACK); //Spirale erzeugen indem best. Pixel dunkel geschaltet werden
}
display.fillCircle(96, 32, 32 - i* 3); //display.fillCircle(x Start, y Start, Radius); draw filled circle
}
}
// Fortschrittsbalken anzeigen mit prozentualer Anzeige
void drawProgressBarDemo() {
int progress = (counter / 5) % 100; // Zähler zuvor oben initialisiert: int counter = 1;
// draw the progress bar // Fortschrittsbalken zeichnen
display.drawProgressBar(0, 32, 120, 10, progress); // drawProgressBar(x Start, y Start, width, height, percentage)
// draw the percentage as String
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 15, String(progress) + "%"); // %-Fortschritt anzeigen
}
// ANZEIGE VON statischen BILDERN MIT xbm-files
void drawImageDemo() {
// see http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html on how to create xbm files
display.drawXbm(34, 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits); //display.drawXbm(x Start, y Start, width, height, bitmap_data); Xbm (X BitMap) = Karte / 2D-Tabelle mit Bit-Zahlen
}
void VextON(void)
{
pinMode(Vext,OUTPUT);
digitalWrite(Vext, LOW);
}
void VextOFF(void) //Vext default OFF
{
pinMode(Vext,OUTPUT);
digitalWrite(Vext, HIGH);
}
Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};
int demoLength = (sizeof(demos) / sizeof(Demo));
long timeSinceLastModeSwitch = 0;
// ERZEUGUNG ANZEIGENSCHLEIFE
void loop() {
// clear the display
display.clear(); //.clear(): Clears the display buffer
// draw the current demo method
demos[demoMode]();
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(10, 128, String(millis()));
// write the buffer to the display
display.display(); //.display(): Sends the buffer content to the physical display
if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
demoMode = (demoMode + 1) % demoLength;
timeSinceLastModeSwitch = millis();
}
counter++;
delay(10); // Wait for display power stabilization 10sec
}