gpt4 book ai didi

c++ - 在执行其他功能的同时播放动画(LED Matrix 和 Arduino/ESP8266)

转载 作者:行者123 更新时间:2023-11-30 16:26:06 25 4
gpt4 key购买 nike

我正在尝试用 C 语言编写草图,并且需要在初始化程序功能时在 LED 矩阵上播放动画。问题是,因为我需要循环播放动画,并且因为在延迟功能期间板不执行任何其他操作,所以我不知道如何在继续执行其他功能的同时循环它。

这是动画:

void Animation(){ 
max7219.clr();
if(InternetStatus){ //This is to draw a circle if internet has connected during the animation
max7219.setColumn(0, B11111111);
for(int i=1; i<7; i++){
max7219.setColumn(i, B10000001);
}
AnUp = B10011101;
AnCenter = B10111001;
AnDown = B11110001;
max7219.setColumn(10, B10000001);
max7219.setColumn(11, B10000001);
max7219.setColumn(15, B10000001);
max7219.setColumn(16, B10000001);
max7219.setColumn(20, B10000001);
max7219.setColumn(21, B10000001);
for(int i=25; i<31; i++){
max7219.setColumn(i, B10000001);
}
max7219.setColumn(31, B11111111);
} else {
AnUp = B00011100;
AnCenter = B00111000;
AnDown = B01110000;
}
max7219.setColumn(7, AnCenter);
max7219.setColumn(8, AnCenter);
max7219.setColumn(9, AnCenter);
max7219.setColumn(12, AnCenter);
max7219.setColumn(13, AnCenter);
max7219.setColumn(14, AnCenter);
max7219.setColumn(17, AnCenter);
max7219.setColumn(18, AnCenter);
max7219.setColumn(19, AnCenter);
max7219.setColumn(22, AnCenter);
max7219.setColumn(23, AnCenter);
max7219.setColumn(24, AnCenter);
max7219.refreshAll();
delay(AnDelay); //delays are set between frames
AnIndex = 0;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
delay(AnDelay);
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex = 1;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
delay(AnDelay);
AnIndex=0;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
AnIndex=1;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
delay(AnDelay);
AnIndex=0;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=1;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=3;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
delay(AnDelay);
AnIndex=1;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
AnIndex=3;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
max7219.refreshAll();
delay(AnDelay);
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=3;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
max7219.refreshAll();
delay(AnDelay);
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
max7219.refreshAll();
delay(AnDelay);
}

我需要在执行这些函数时运行该动画:

void Wait4Connection(){
Serial.println("Waiting for connection.");
InternetStatus = false;
WaitCon:
max7219.clr();
InternetStatus = false;
RetimeTry = 0;
Serial.println("Trying to connect.");
wifiManager.autoConnect("Clock Configuration");
ret = Ping.ping(ip);
if(ret){ //prova connessione internet
Serial.println("Device connected to the internet.");
timeClient.begin();
Retime:
InternetStatus = true;
Serial.println("Clock synchronization.");
timeClient.update();
Hours = timeClient.getHours() + HH + IsDst(day(timeClient.getEpochTime()), month(timeClient.getEpochTime()), weekday(timeClient.getEpochTime()));
Minutes = timeClient.getMinutes() + MM;
Seconds = timeClient.getSeconds();
if(timeClient.getEpochTime() > 1000){
RetimeTry=0;
Serial.println("Starting clock mode.");
tasker.setInterval(Clocking, 1000);
tasker.setInterval(Refresh, 300000);
} else {
if(RetimeTry<10){
Serial.print("No time received. Retrying. x");
Serial.println(RetimeTry+1);
RetimeTry++;
goto Retime;
} else {
Serial.println("Can't obtain time packets. Reconnecting.");
goto WaitCon;
}
RetimeTry++;
}
} else {
Serial.println("Connection failed.");
InternetStatus = false;
Serial.println("Slowing down 30s");
delay(30000);
tasker.cancel(Animation);
goto WaitCon;
}
}

谢谢。

最佳答案

好吧,最后我想出了这个:

short AnimationControl = 0;

void AnimationMain(){
switch (AnimationControl){
case 0:
Animation1();
break;

case 1:
Animation2();
break;

case 2:
Animation3();
break;

case 3:
Animation4();
break;

case 4:
Animation5();
break;

case 5:
Animation6();
break;

case 6:
Animation7();
break;

case 7:
Animation8();
break;
}
}

void Animation1()
max7219.clr();
if(InternetStatus){
max7219.setColumn(0, B11111111);
for(int i=1; i<7; i++){
max7219.setColumn(i, B10000001);
}
AnUp = B10011101;
AnCenter = B10111001;
AnDown = B11110001;
max7219.setColumn(10, B10000001);
max7219.setColumn(11, B10000001);
max7219.setColumn(15, B10000001);
max7219.setColumn(16, B10000001);
max7219.setColumn(20, B10000001);
max7219.setColumn(21, B10000001);
for(int i=25; i<31; i++){
max7219.setColumn(i, B10000001);
}
max7219.setColumn(31, B11111111);
} else {
AnUp = B00011100;
AnCenter = B00111000;
AnDown = B01110000;
}
max7219.setColumn(7, AnCenter);
max7219.setColumn(8, AnCenter);
max7219.setColumn(9, AnCenter);
max7219.setColumn(12, AnCenter);
max7219.setColumn(13, AnCenter);
max7219.setColumn(14, AnCenter);
max7219.setColumn(17, AnCenter);
max7219.setColumn(18, AnCenter);
max7219.setColumn(19, AnCenter);
max7219.setColumn(22, AnCenter);
max7219.setColumn(23, AnCenter);
max7219.setColumn(24, AnCenter);
max7219.refreshAll();
AnimationControl=1;
}


void Animation2(){
AnIndex = 0;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
AnimationControl=2;
}

void Animation3(){
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex = 1;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
AnimationControl=3;
}

void Animation4(){
AnIndex=0;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
AnIndex=1;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
AnimationControl = 4;
}

void Animation5(){
AnIndex=0;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=1;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=3;
max7219.setColumn(7+(AnIndex*5), AnUp);
max7219.setColumn(8+(AnIndex*5), AnUp);
max7219.setColumn(9+(AnIndex*5), AnUp);
max7219.refreshAll();
AnimationControl = 5;
}

void Animation6(){
AnIndex=1;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
AnIndex=3;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
max7219.refreshAll();
AnimationControl = 6;
}

void Animation7(){
AnIndex=2;
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
AnIndex=3;
max7219.setColumn(7+(AnIndex*5), AnDown);
max7219.setColumn(8+(AnIndex*5), AnDown);
max7219.setColumn(9+(AnIndex*5), AnDown);
max7219.refreshAll();
AnimationControl = 7;
}

void Animation8(){
max7219.setColumn(7+(AnIndex*5), AnCenter);
max7219.setColumn(8+(AnIndex*5), AnCenter);
max7219.setColumn(9+(AnIndex*5), AnCenter);
max7219.refreshAll();
AnimationControl = 0;
}

我每隔设定的时间间隔(在我的例子中为 125 毫秒)就使用 Ticker 库调用 AnimationMain。

希望对某人有帮助。

关于c++ - 在执行其他功能的同时播放动画(LED Matrix 和 Arduino/ESP8266),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53158896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com