#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
//cslg
// 如果使用用户界面控制(如电位器、开关等),则设置为1
#define USE_UI_CONTROL 0
#if USE_UI_CONTROL
#include <MD_UISwitch.h>
#endif
// 打开向串行输出调试语句的功能
#define DEBUG 0
#if DEBUG
#define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
#define PRINTS(x) Serial.print(F(x))
#define PRINTX(x) Serial.println(x, HEX)
#else
#define PRINT(s, x)
#define PRINTS(x)
#define PRINTX(x)
#endif
// 定义链中的设备数量以及硬件接口
// 注意:这些引脚编号可能不适用于您的硬件,可能需要进行调整
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 50
#define CLK_PIN 52
#define DATA_PIN 51
#define CS_PIN 10
// 使用硬件SPI
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// 使用软件SPI(如需)
//MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// 滚动参数
#if USE_UI_CONTROL
const uint8_t SPEED_IN = A5; // 速度控制电位器引脚
const uint8_t DIRECTION_SET = 8; // 方向控制开关引脚
const uint8_t INVERT_SET = 9; // 显示反转控制开关引脚
const uint8_t SPEED_DEADBAND = 5; // 速度调整死区,避免频繁调整
#endif // USE_UI_CONTROL
uint8_t scrollSpeed = 25; // 默认帧延迟值
textEffect_t scrollEffect = PA_SCROLL_LEFT; // 默认滚动效果
textPosition_t scrollAlign = PA_LEFT; // 默认文本对齐方式
uint16_t scrollPause = 2000; // 滚动暂停时间(毫秒)
// 串行和滚动函数共享的全局消息缓冲区
#define BUF_SIZE 75
char curMessage[BUF_SIZE] = { "" }; // 当前显示的消息
char newMessage[BUF_SIZE] = { "Hello! Changshu Institute of Technology" }; // 新消息缓冲区
bool newMessageAvailable = true; // 新消息可用标志
#if USE_UI_CONTROL
MD_UISwitch_Digital uiDirection(DIRECTION_SET); // 方向控制开关实例
MD_UISwitch_Digital uiInvert(INVERT_SET); // 显示反转控制开关实例
void doUI(void)
{
// 根据电位器调整速度
{
int16_t speed = map(analogRead(SPEED_IN), 0, 1023, 10, 150);
if ((speed >= ((int16_t)P.getSpeed() + SPEED_DEADBAND)) ||
(speed <= ((int16_t)P.getSpeed() - SPEED_DEADBAND)))
{
P.setSpeed(speed);
scrollSpeed = speed;
PRINT("\nChanged speed to ", P.getSpeed());
}
}
// 根据方向控制开关改变滚动方向
if (uiDirection.read() == MD_UISwitch::KEY_PRESS)
{
PRINTS("\nChanging scroll direction");
scrollEffect = (scrollEffect == PA_SCROLL_LEFT ? PA_SCROLL_RIGHT : PA_SCROLL_LEFT);
P.setTextEffect(scrollEffect, scrollEffect);
P.displayClear();
P.displayReset();
}
// 根据显示反转控制开关改变显示模式
if (uiInvert.read() == MD_UISwitch::KEY_PRESS)
{
PRINTS("\nChanging invert mode");
P.setInvert(!P.getInvert());
}
}
#endif // USE_UI_CONTROL
// 从串行接口读取输入消息
void readSerial(void)
{
static char *cp = newMessage;
while (Serial.available())
{
*cp = (char)Serial.read();
if ((*cp == '\n') || (cp - newMessage >= BUF_SIZE - 2)) // 检测到换行符或缓冲区满
{
*cp = '\0'; // 结束字符串
// 重置指针,准备接收下一条消息,并标记有新消息
cp = newMessage;
newMessageAvailable = true;
}
else // 移动指针到下一个位置
cp++;
}
}
void setup()
{
Serial.begin(57600);
Serial.print("\n[Changshu Institute of Technology]\nType a message for the scrolling display\nEnd message line with a newline");
#if USE_UI_CONTROL
uiDirection.begin();
uiInvert.begin();
pinMode(SPEED_IN, INPUT);
doUI();
#endif // USE_UI_CONTROL
P.begin();
P.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);
}
void loop()
{
#if USE_UI_CONTROL
doUI();
#endif // USE_UI_CONTROL
// 更新显示
if (P.displayAnimate())
{
if (newMessageAvailable)
{
strcpy(curMessage, newMessage); // 将新消息复制到当前消息缓冲区
newMessageAvailable = false;
}
P.displayReset();
}
readSerial();
}