OLED液晶屏幕(4)串口读取文字并分割,液晶屏幕显示

时间:2021-05-14 08:47:12

 

OLED液晶屏幕(4)串口读取文字并分割,液晶屏幕显示

 

ESP8266-07

 0.93存 液晶屏 128*64  驱动芯片 ssd1306

接线

VCC-5v

GND-GND 

SCL-D1(SCL)

SDA-D2(SDA)

 

安装两个库

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

 

OLED液晶屏幕(4)串口读取文字并分割,液晶屏幕显示

 串口输入一句话

液晶屏幕显示出来

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET    -1 //4  Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

String comdata = "";

void oledint(){
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println("SSD1306 allocation failed");
    for(;;); // Don't proceed, loop forever
  }
  Serial.println("success");
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);


  // Invert and restore display, pausing in-between
  display.invertDisplay(true);
  delay(1000);
  display.invertDisplay(false);
  delay(1000);
  
  }
void setup() {
  Serial.begin(9600);
  while(Serial.read()>= 0){} //clear serialbuffer
  oledint();

}

void loop() {

   if(Serial.available()>0){
      delay(100);
      comdata = Serial.readString();
    //  Serial.print("Serial.readString:");
    //  Serial.println(comdata);
      testdrawstyles(comdata); 
    }
    comdata = "";


}


String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

// x;31.112;y;31.123;z;23.3213;AP1;-23;AP2;-26;AP3;-21
void testdrawstyles(String msg) {
  display.clearDisplay();

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  
 //display.println(msg);
 display.println(  "*-------------------*");

   
      String part01 = getValue(msg,';',1);
   display.print(F(" X : "));   display.println(part01); 

       String part02 = getValue(msg,';',3);
   display.print(F(" Y : "));   display.println(part02); 

       String part03 = getValue(msg,';',5);
    display.print(F(" Z : "));   display.println(part03); 
      
 display.println(  " ------------------- ");
      String part04 = getValue(msg,';',7);
   display.print(F("AP1: "));   display.println(part04); 

       String part05 = getValue(msg,';',9);
   display.print(F("AP2: "));   display.println(part05); 

       String part06 = getValue(msg,';',11);
    display.print(F("AP3: "));   display.println(part06); 


  display.display();
  delay(2000);
}