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

2ba500ada26d52af03fbd1b5a07301e3.png

ESP8266-07

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

接线

VCC-5v

GND-GND

SCL-D1(SCL)

SDA-D2(SDA)

安装两个库

#include

#include

55ee73b530c0216830321ce63ca202bc.png

串口输入一句话

液晶屏幕显示出来

#include

#include

#include

#include

#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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值