本文來源電子發(fā)燒友社區(qū),作者:華仔stm32, 帖子地址:https://bbs.elecfans.com/jishu_2299368_1_1.html
|
前面因為北海的疫情,科室抽搞了人員去支援,所單位的事一直在超負荷工作,這一個月都沒有時間寫文章,現(xiàn)在恢復(fù)正常,接上面的繼續(xù)學(xué)習(xí)套件的學(xué)習(xí),并分享給大家。
前面自己用pwm寫過一個可以播放音樂的帖子,是用liteos來寫的,這次學(xué)習(xí)了HarmonyOS來實現(xiàn)這一功能。
1、新建工程,按下圖
然后點擊創(chuàng)建:
等待下載結(jié)束,在彈出的對話框中選擇當(dāng)前窗口:
2、主要代碼分析:
#define IOT_TEST_PWM_GPIO9 9 // 指定蜂鳴器的pwm輸出IO
#define IOT_PWM_PORT0 0
#define IOT_IO_NAME_GPIO_8 8 // 按鍵檢測IO
#define PWM_DUTY 50 //占空比
#define M_INTERVAL_TIME_TICK 60
#define TICKS_DELAY 125*1000
3、頻率與音符的對應(yīng):
static const uint16 g_tuneFreqs[] = {
0, // 40M Hz 對應(yīng)的分頻系數(shù):
1046 * 4, // 1046.50 1
1174 * 4, // 1174.66 2
1318 * 4, // 1318.51 3
1396 * 4, // 1396.91 4
1567 * 4, // 1567.99 5
1760 * 4, // 1760.00 6
1975 * 4, // 1975.53 7
523 * 4, // 523.25 1- 低一個8度的1
587 * 4, // 587.33 2-
659 * 4, // 659.26 3-
698 * 4, // 698.46 4-
783 * 4, // 783.99 5-
880 * 4, // 880.00 6-
987 * 4, // 987.77 7-
};
4、定義音符與節(jié)拍:
typedef enum
{
D_DO = 1, // 1
D_RE, // 2
D_MI, // 3
D_FA, // 4
D_SO, // 5
D_LA, // 6
D_SI, // 7
C_DO, // 1-
C_RE, // 2-
C_MI, // 3-
C_FA, // 4-
C_SO, // 5-
C_LA, // 6-
C_SI // 7-
} MusicTuneNotes;
typedef enum
{
BEAT_1X4B = 1, //4分之1拍為基礎(chǔ)值1
BEAT_1X2B,
BEAT_3X4B,
BEAT_1B,
BEAT_3X2B = 6,
BEAT_2B = 8,
BEAT_3B = 12,
BEAT_4B = 16
} MusicTuneInterval;
/*音符與時間間隔結(jié)構(gòu)體*/
typedef struct
{
MusicTuneNotes tuneNotes; //音符
MusicTuneInterval interval; //時間間隔
} MusicNotesInterval;
5、曲譜,數(shù)組里面定義了音符與節(jié)拍:
*曲譜*/
static const MusicNotesInterval g_interval[] = {
{D_MI,BEAT_1X2B},{D_SO,BEAT_1X2B},{D_SO,BEAT_3X4B},{D_LA,BEAT_1X4B},{D_SO,BEAT_1X2B},{D_MI,BEAT_1X2B},{D_DO,BEAT_1X2B},
{D_DO,BEAT_1X4B},{D_RE,BEAT_1X4B},{D_MI,BEAT_1X2B},{D_MI,BEAT_1X2B},{D_RE,BEAT_1X2B},{D_DO,BEAT_1X2B},{D_RE,BEAT_2B},
{D_MI,BEAT_1X2B},{D_SO,BEAT_1X2B},{D_SO,BEAT_3X4B},{D_LA,BEAT_1X4B},{D_SO,BEAT_1X2B},{D_MI,BEAT_1X2B},{D_DO,BEAT_1X2B},
{D_DO,BEAT_1X4B},{D_RE,BEAT_1X4B},{D_MI,BEAT_1X2B},{D_MI,BEAT_1X2B},{D_RE,BEAT_1X2B},{D_RE,BEAT_1X2B},{D_DO,BEAT_2B},
{D_FA,BEAT_1B},{D_FA,BEAT_1B},{D_FA,BEAT_1X2B},{D_LA,BEAT_3X2B},{D_SO,BEAT_1B},{D_SO,BEAT_3X4B},{D_MI,BEAT_1X4B},
{D_RE,BEAT_2B},{D_MI,BEAT_1X2B},{D_SO,BEAT_1X2B},{D_SO,BEAT_3X4B},{D_LA,BEAT_1X4B},{D_SO,BEAT_1X2B},{D_MI,BEAT_1X2B},
{D_DO,BEAT_1X2B},{D_DO,BEAT_1X4B},{D_RE,BEAT_1X4B},{D_MI,BEAT_1X2B},{D_MI,BEAT_1X2B},{D_RE,BEAT_1X2B},{D_RE,BEAT_1X2B},
{D_DO,BEAT_2B}
};
6、按鍵響應(yīng)函數(shù),當(dāng)按鍵按下后更新標(biāo)志位music:
/*按鍵響應(yīng)函數(shù)*/
static void OnButtonPressed(char *arg)
{
(void)arg;
IoTGpioUnregisterIsrFunc(IOT_IO_NAME_GPIO_8);
music = 1;
printf("
>>>>>>>>>>button is enter >>>.[%d]
", music);
IoTGpioRegisterIsrFunc(IOT_IO_NAME_GPIO_8, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW,
OnButtonPressed, NULL);
}
7、音樂處理函數(shù):
static void *BeeperMusicTask(const char *arg)
{
(void)arg;
printf("BeeperMusicTask start!
");
hi_pwm_set_clock(PWM_CLK_XTAL); // 設(shè)置時鐘源為晶體時鐘(40MHz,默認時鐘源160MHz)
while (1)
{
osDelay(M_INTERVAL_TIME_TICK);
/*第一次點擊按鍵播放,播放完以后按鍵才能繼續(xù)生效*/
if (music == 1)
{
for (size_t i = 0; i < sizeof(g_interval) / sizeof(g_interval[0]); i++)
{
uint32 tune = g_interval[i].tuneNotes; // 音符
uint16 freqDivisor = g_tuneFreqs[tune];
uint32 tuneInterval = g_interval[i].interval * (TICKS_DELAY); // 音符時間
IoTPwmStart(IOT_PWM_PORT0, PWM_DUTY, freqDivisor);
usleep(tuneInterval);
IoTPwmStop(IOT_PWM_PORT0);
music = 0;
}
}
}
return NULL;
}
8、創(chuàng)建音樂播放任務(wù):
static void BeepMusicEntry(void)
{
osThreadAttr_t attr;
/* 初始化按鍵 */
IoTGpioInit(IOT_IO_NAME_GPIO_8); //button
IoTGpioSetDir(IOT_IO_NAME_GPIO_8, IOT_GPIO_DIR_IN);
/* 注冊按鍵中斷回調(diào) */
IoTGpioRegisterIsrFunc(IOT_IO_NAME_GPIO_8, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, OnButtonPressed, NULL);
/* 初始化蜂鳴器輸出IO */
IoTGpioInit(IOT_TEST_PWM_GPIO9); //BWM
IoTGpioSetDir(IOT_TEST_PWM_GPIO9, IOT_GPIO_DIR_OUT);
IoTPwmInit(IOT_PWM_PORT0);
IoTWatchDogDisable();
attr.name = "BeeperMusicTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)BeeperMusicTask, NULL, &attr) == NULL)
{
printf("[BeeperMusicTask] Falied to create BeepTask!
");
}
}
9、開機創(chuàng)建啟動:APP_FEATURE_INIT(BeepMusicEntry);
【設(shè)置編譯選項】
1、在applications/sample/wifi-iot/app/demo_beep下的BUILD.gn創(chuàng)建jison:
static_library("demo_beep") {
sources = [
"beep_music_demo.c"
]
include_dirs = [
"http://utils/native/lite/include",
"http://kernel/liteos_m/components/cmsis/2.0",
"http://base/iot_hardware/peripheral/interfaces/kits",
]
}
2、在applications/sample/wifi-iot/app下面創(chuàng)建BUILD.gn:
import("http://build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"demo_beep",
]
deps = []
}
【編譯】:
到這一步就顯示編譯成功了:
【拷貝文件到HiBurn目錄下面】輸出的文件在/home/hihope/Documents/DevEco/Projects/lyevk_hm_beep/out/hispark_pegasus/wifiiot_hispark_pegasus目錄下面:
將Hi3861_wifiiot_app_allinone.bin復(fù)制到win下面。然后燒錄到開發(fā)板就OK了。
重啟開發(fā)板,按下按鍵,就可以播放音樂了。
-
wi-fi
+關(guān)注
關(guān)注
14文章
2169瀏覽量
124823 -
HarmonyOS
+關(guān)注
關(guān)注
79文章
1982瀏覽量
30411 -
HiSpark
+關(guān)注
關(guān)注
1文章
156瀏覽量
6945
發(fā)布評論請先 登錄
相關(guān)推薦
評論