#include <stdbool.h> // 引入stdbool.h头文件以使用bool类型
// 定义状态枚举
typedef enum {
STATE_OFF,
STATE_ON
} State;
// 初始化状态为关闭
State currentState = STATE_OFF;
void checkButton() {
if (KEY3 == 0) {
delay_ms(8);
if (KEY3 == 0) {
// 按键按下,切换状态
currentState = (currentState == STATE_OFF) ? STATE_ON : STATE_OFF;
}
}
}
void controlRGB() {
if (currentState == STATE_ON) {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
for (z = 0; z < 24; z++) {
ws2812_write_byte(tableRGB[z]);
}
} else {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
for (i = 24; i < 48; i++) {
ws2812_write_byte(tableRGB[i]);
}
}
}
void Main(void) {
while (1) {
checkButton();
controlRGB();
}
}
|