#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
#include <Fonts/Org_01.h>
//#include <Fonts/Org_01.h> // 使用其他字体
// 定义屏幕宽度和高度
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// 创建一个SSD1306显示对象
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int linePos = 0; // 线条位置,初始为0
bool direction = true; // 线条运动方向,true表示向右,false表示向左
unsigned long startTime = 0; // 程序开始运行时间
void setup() {
// 初始化串口通讯
Serial.begin(115200);
// 初始化I2C0
Wire.setSDA(0); // 设置 SDA 引脚为 GPIO 0
Wire.setSCL(1); // 设置 SCL 引脚为 GPIO 1
Wire.begin();
// 初始化显示器,I2C地址为0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
startTime = millis(); // 记录程序开始运行的时间
// 更新显示器
display.display();
}
void loop() {
// 更新显示屏上的运行时间
updateDisplay();
// 更新线条位置
updateLine();
// int sensorValue = analogRead(potentiometerPin); // 读取电位器的模拟值
// float voltage = sensorValue * (12 / 1023.0); // 将模拟值转换为电压值
// Serial.print("Sensor Value: ");
// Serial.print(sensorValue);
// Serial.print(" - Voltage: ");
// Serial.print(voltage);
// Serial.println(" V");
delay(50); // 延迟,稳定输出
}
void updateDisplay() {
display.clearDisplay(); // 清除屏幕内容
// 设置文本大小、颜色和光标位置
//display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// 显示当前PWM值
// display.setCursor(0, 0);
// display.print("PWM Value: ");
// display.println(lastPwmValue);
//读取电压 测试中
//int adcValue = analogRead(ADC_Pin); // 读取ADC值
//float voltage = adcValue * (3.7 / 4095.0); // 将ADC值转换为电压(3.3V为RP2040的参考电压)
//display.setCursor(0, 0);
//display.print("Voltage: ");
//display.println(voltage);
//Serial.print("ADC Value: ");
//Serial.print(adcValue);
//Serial.print("\t Voltage: ");
//Serial.println(voltage);
// 显示当前PWM百分比
// display.setCursor(0, 10);
// display.print("Oxygen Speed: ");
// display.print(pwmPercentage, 0); // 百分比显示,不显示小数部分
// display.println("%");
display.setTextSize(5);
display.setCursor(65, 45);
display.setFont(&Org_01); // 使用Org_01字体
//display.print("Oxygen Speed: ");
display.print("99");; // 百分比显示,不显示小数部分
//display.println("%");
// 显示程序运行时间
unsigned long elapsedTime = millis() - startTime;
int seconds = elapsedTime / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
display.setTextSize(1);
display.setCursor(60, 5); // 设置光标位置
//display.setFont(&FreeMono9pt7b);
display.print("Time: ");
// 打印小时(两位数)
display.print((hours < 10 ? "0" : ""));
display.print(hours);
// 打印分钟(两位数)
display.print(":");
display.print((minutes % 60 < 10 ? "0" : ""));
display.print(minutes % 60, DEC);
// 打印秒钟(两位数)
display.print(":");
display.print((seconds % 60 < 10 ? "0" : ""));
display.print(seconds % 60, DEC);
// 绘制Logo
//display.drawBitmap(0, 20, TrapboT_Logo, SCREEN_WIDTH, SCREEN_HEIGHT - 20, WHITE); //显示TrapboT_Logo
// 绘制线条
updateLine();
display.display(); // 更新显示
}
void updateLine() {
// 清除上次的线条
display.drawLine(0, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, BLACK);
// 更新线条位置
if (direction) {
linePos++; // 向右移动
if (linePos >= SCREEN_WIDTH - 5) {
direction = false; // 到达右边界,改为向左移动
}
} else {
linePos--; // 向左移动
if (linePos <= 0) {
direction = true; // 到达左边界,改为向右移动
}
}
// 绘制新的线条
display.drawLine(linePos, SCREEN_HEIGHT - 1, linePos + 5, SCREEN_HEIGHT - 1, WHITE);
}