OLED_I2C_3D_Cube for ESP8266
Posted perseverance52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OLED_I2C_3D_Cube for ESP8266相关的知识,希望对你有一定的参考价值。
OLED_I2C_3D_Cube for ESP8266
- 视频演示效果
ESP8266 I2C 3DCube动态针显示
#include "SH1106Wire.h" //1.3寸用这个
//#include "SSD1306Wire.h" //0.96寸用这个
#include "OLEDDisplayUi.h"
#if defined(ESP8266) //OLED引脚定义
const int SDA_PIN = D2; //对应nodemcu接D5或者D1,,wemosD1mini的D2
const int SDC_PIN = D1; //对应nodemcu接D6或者D2,,wemosD1mini的D5
#else
const int SDA_PIN = D1; //对应nodemcu接D5或者D1
const int SCL = D2; //对应nodemcu接D6或者D2
#endif
const int I2C_DISPLAY_ADDRESS = 0x3c; //I2c地址默认
SH1106Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN); // 1.3寸用这个
//SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN); // 0.96寸用这个
double vectors[8][3] = {{20, 20, 20},{-20, 20, 20},{-20, -20, 20},{20, -20, 20},{20, 20, -20},{-20, 20, -20},{-20, -20, -20},{20, -20, -20}};
double perspective = 100.0f;
int deltaX, deltaY, deltaZ, iter = 0;
long stime, fps = 0, frames = 0;
OLEDDisplayUi ui ( &display );
void setup() {
Serial.begin(115200);
randomSeed(analogRead(0));
display.init();
display.clear();
display.display();
// display.flipScreenVertically(); //屏幕翻转
// display.mirrorScreen();//使用分光棱镜显示需要用到此函数
display.setContrast(240); //屏幕亮度
delay(1000);
ui.setTargetFPS(80); //刷新频率
ui.disableAllIndicators(); //不显示页码小点。
ui.enableAutoTransition();
ui.setFrameAnimation(SLIDE_LEFT); //切屏方向
// ui.disableAutoTransition();
ui.enableAutoTransition();
ui.setAutoTransitionForwards();//设置自动过渡方向,
// ui.setFrames(frames, numberOfFrames); // 设置框架和显示屏幕内容数
ui.setTimePerFrame(5000); //设置切换时间
ui.setTimePerTransition(500);//设置转场大约所需要时间
// ui.setOverlays(overlays, numberOfOverlays); //设置覆盖的画面数
ui.init();// UI负责初始化显示
stime = micros();
}
void loop()
{
// int remainingTimeBudget = ui.update(); //剩余时间预算
// if (remainingTimeBudget > 0)
// {
// //你可以在这里工作如果你低于你的时间预算。
// delay(remainingTimeBudget);
// }
display.clear();
// display.clrScr();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_10);
display.setLogBuffer(6, 45);
drawVectors();
if (iter == 0)
{
deltaX = random(7) - 3;
deltaY = random(7) - 3;
deltaZ = random(7) - 3;
iter = random(250) + 5;
}
rotateX(deltaX);
rotateY(deltaY);
rotateZ(deltaZ);
iter--;
fps += 1000000 / (micros() - stime);
stime = micros();
frames++;
display.println(fps / frames + String("fps"));
display.drawLogBuffer(0, 0);
// display.printNumI(fps / frames, 0, 0, 3); // Print average FPS on screen
display.display();
// display.update();
}
int translateX(double x, double z)
{
return (int)((x + 64) + (z * (x / perspective)));
}
int translateY(double y, double z)
{
return (int)((y + 32) + (z * (y / perspective)));
}
void rotateX(int angle)
{
double rad, cosa, sina, Yn, Zn;
rad = angle * PI / 180;
cosa = cos(rad);
sina = sin(rad);
for (int i = 0; i < 8; i++)
{
Yn = (vectors[i][1] * cosa) - (vectors[i][2] * sina);
Zn = (vectors[i][1] * sina) + (vectors[i][2] * cosa);
vectors[i][1] = Yn;
vectors[i][2] = Zn;
}
}
void rotateY(int angle)
{
double rad, cosa, sina, Xn, Zn;
rad = angle * PI / 180;
cosa = cos(rad);
sina = sin(rad);
for (int i = 0; i < 8; i++)
{
Xn = (vectors[i][0] * cosa) - (vectors[i][2] * sina);
Zn = (vectors[i][0] * sina) + (vectors[i][2] * cosa);
vectors[i][0] = Xn;
vectors[i][2] = Zn;
}
}
void rotateZ(int angle)
{
double rad, cosa, sina, Xn, Yn;
rad = angle * PI / 180;
cosa = cos(rad);
sina = sin(rad);
for (int i = 0; i < 8; i++)
{
Xn = (vectors[i][0] * cosa) - (vectors[i][1] * sina);
Yn = (vectors[i][0] * sina) + (vectors[i][1] * cosa);
vectors[i][0] = Xn;
vectors[i][1] = Yn;
}
}
void drawVectors()
{
display.drawLine(translateX(vectors[0][0], vectors[0][2]), translateY(vectors[0][1], vectors[0][2]), translateX(vectors[1][0], vectors[1][2]), translateY(vectors[1][1], vectors[1][2]));
display.drawLine(translateX(vectors[1][0], vectors[1][2]), translateY(vectors[1][1], vectors[1][2]), translateX(vectors[2][0], vectors[2][2]), translateY(vectors[2][1], vectors[2][2]));
display.drawLine(translateX(vectors[2][0], vectors[2][2]), translateY(vectors[2][1], vectors[2][2]), translateX(vectors[3][0], vectors[3][2]), translateY(vectors[3][1], vectors[3][2]));
display.drawLine(translateX(vectors[3][0], vectors[3][2]), translateY(vectors[3][1], vectors[3][2]), translateX(vectors[0][0], vectors[0][2]), translateY(vectors[0][1], vectors[0][2]));
display.drawLine(translateX(vectors[4][0], vectors[4][2]), translateY(vectors[4][1], vectors[4][2]), translateX(vectors[5][0], vectors[5][2]), translateY(vectors[5][1], vectors[5][2]));
display.drawLine(translateX(vectors[5][0], vectors[5][2]), translateY(vectors[5][1], vectors[5][2]), translateX(vectors[6][0], vectors[6][2]), translateY(vectors[6][1], vectors[6][2]));
display.drawLine(translateX(vectors[6][0], vectors[6][2]), translateY(vectors[6][1], vectors[6][2]), translateX(vectors[7][0], vectors[7][2]), translateY(vectors[7][1], vectors[7][2]));
display.drawLine(translateX(vectors[7][0], vectors[7][2]), translateY(vectors[7][1], vectors[7][2]), translateX(vectors[4][0], vectors[4][2]), translateY(vectors[4][1], vectors[4][2]));
display.drawLine(translateX(vectors[0][0], vectors[0][2]), translateY(vectors[0][1], vectors[0][2]), translateX(vectors[4][0], vectors[4][2]), translateY(vectors[4][1], vectors[4][2]));
display.drawLine(translateX(vectors[1][0], vectors[1][2]), translateY(vectors[1][1], vectors[1][2]), translateX(vectors[5][0], vectors[5][2]), translateY(vectors[5][1], vectors[5][2]));
display.drawLine(translateX(vectors[2][0], vectors[2][2]), translateY(vectors[2][1], vectors[2][2]), translateX(vectors[6][0], vectors[6][2]), translateY(vectors[6][1], vectors[6][2]));
display.drawLine(translateX(vectors[3][0], vectorsSTM32F103C8T6 + 0.96“ I2C OLED显示3D_Cube