官方提供了一个demo但是不够完善,只有一个按键的demo,没有做6个按键,为了测试全部功能,我把那一个按键的demo复制出来了5份,实现了6个按键按下都能够发出不同的声音,接下来就给大家讲一下需要修改的地方及其注意事项
1.首先在中断时间这里添加六个按键的事件,我这里是每个按键对应位置的两个灯进行点亮,不做熄灭处理,两个灯的三个颜色都使用rand函数随机生成。
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == GPIO_PIN_1) { HAL_TIM_Base_Stop(&htim2); i = 0; curr_play = (int16_t*)Drum_Wave_1; stop = Drum_Wave_1_Size; WS2812_Buff[0][0] = rand(); WS2812_Buff[0][1] = rand(); WS2812_Buff[0][2] = rand(); WS2812_Buff[1][0] = rand(); WS2812_Buff[1][1] = rand(); WS2812_Buff[1][2] = rand(); WS2812_Refresh(); HAL_TIM_Base_Start_IT(&htim2); } if(GPIO_Pin == GPIO_PIN_3) { HAL_TIM_Base_Stop(&htim2); i = 0; curr_play = (int16_t*)Drum_Wave_2; stop = Drum_Wave_2_Size; WS2812_Buff[2][0] = rand(); WS2812_Buff[2][1] = rand(); WS2812_Buff[2][2] = rand(); WS2812_Buff[3][0] = rand(); WS2812_Buff[3][1] = rand(); WS2812_Buff[3][2] = rand(); WS2812_Refresh(); HAL_TIM_Base_Start_IT(&htim2); } if(GPIO_Pin == GPIO_PIN_6) { HAL_TIM_Base_Stop(&htim2); i = 0; curr_play = (int16_t*)Drum_Wave_3; stop = Drum_Wave_3_Size; WS2812_Buff[4][0] = rand(); WS2812_Buff[4][1] = rand(); WS2812_Buff[4][2] = rand(); WS2812_Buff[5][0] = rand(); WS2812_Buff[5][1] = rand(); WS2812_Buff[5][2] = rand(); WS2812_Refresh(); HAL_TIM_Base_Start_IT(&htim2); } if(GPIO_Pin == GPIO_PIN_12) { HAL_TIM_Base_Stop(&htim2); i = 0; curr_play = (int16_t*)Drum_Wave_4; stop = Drum_Wave_4_Size; WS2812_Buff[6][0] = rand(); WS2812_Buff[6][1] = rand(); WS2812_Buff[6][2] = rand(); WS2812_Buff[7][0] = rand(); WS2812_Buff[7][1] = rand(); WS2812_Buff[7][2] = rand(); WS2812_Refresh(); HAL_TIM_Base_Start_IT(&htim2); } if(GPIO_Pin == GPIO_PIN_2) { HAL_TIM_Base_Stop(&htim2); i = 0; curr_play = (int16_t*)Drum_Wave_5; stop = Drum_Wave_5_Size; WS2812_Buff[8][0] = rand(); WS2812_Buff[8][1] = rand(); WS2812_Buff[8][2] = rand(); WS2812_Buff[9][0] = rand(); WS2812_Buff[9][1] = rand(); WS2812_Buff[9][2] = rand(); WS2812_Refresh(); HAL_TIM_Base_Start_IT(&htim2); } if(GPIO_Pin == GPIO_PIN_0) { HAL_TIM_Base_Stop(&htim2); i = 0; curr_play = (int16_t*)Drum_Wave_6; stop = Drum_Wave_6_Size; WS2812_Buff[10][0] = rand(); WS2812_Buff[10][1] = rand(); WS2812_Buff[10][2] = rand(); WS2812_Buff[11][0] = rand(); WS2812_Buff[11][1] = rand(); WS2812_Buff[11][2] = rand(); WS2812_Refresh(); HAL_TIM_Base_Start_IT(&htim2); } }
废话少说,直接上视频效果,演示视频EEPW电子鼓DIY活动,demo跑一下_哔哩哔哩_bilibili
音乐这方面真的不太行,但是每一个按键都能发出不同的声音,且对应位置的灯光都能发生变化了,