#include <stdbool.h> // 引入stdbool.h頭文件以使用bool類型
// 定義狀態(tài)枚舉
typedef enum {
STATE_OFF,
STATE_ON
} State;
// 初始化狀態(tài)為關閉
State currentState = STATE_OFF;
void checkButton() {
if (KEY3 == 0) {
delay_ms(8);
if (KEY3 == 0) {
// 按鍵按下,切換狀態(tài)
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();
}
}
|