📄 music.c
字号:
/******************************************************************************
Copyright (c) 2006 by RockOS.
All rights reserved.
This software is supported by the Rock Software Workroom only.
Any bugs please contact the author with e-mail or QQ:
E-mail : baobaoba520@yahoo.com.cn
QQ : 59681888
*******************************************************************************
File name : music.c
Description : play music with ITSN44b0x develop board.
:
:
Auther : sunxinqiu
History :
2006-03-15 first release.
******************************************************************************/
#include "os.h"
#include "music.h"
HSEMA mutex; /* a mutex semaphore for accessing PWM Timer2. */
HTASK g_musicTask; /* the music task. */
MUSIC_NOTE defaultMusic[] =
{
/* note duration */
/* ======== =========*/
{NOTE_D1, 50},
{NOTE_D2, 50},
{NOTE_D3, 50},
{NOTE_D4, 50},
{NOTE_D5, 50},
{NOTE_D6, 50},
{NOTE_D7, 50},
{NOTE_Z1, 50},
{NOTE_Z2, 50},
{NOTE_Z3, 50},
{NOTE_Z4, 50},
{NOTE_Z5, 50},
{NOTE_Z6, 50},
{NOTE_Z7, 50},
{NOTE_G1, 50},
{NOTE_G2, 50},
{NOTE_G3, 50},
{NOTE_G4, 50},
{NOTE_G5, 50},
{NOTE_G6, 50},
{NOTE_G7, 50},
{NOTE_G6, 50},
{NOTE_G5, 50},
{NOTE_G4, 50},
{NOTE_G3, 50},
{NOTE_G2, 50},
{NOTE_G1, 50},
{NOTE_Z7, 50},
{NOTE_Z6, 50},
{NOTE_Z5, 50},
{NOTE_Z4, 50},
{NOTE_Z3, 50},
{NOTE_Z2, 50},
{NOTE_Z1, 50},
{NOTE_D7, 50},
{NOTE_D6, 50},
{NOTE_D5, 50},
{NOTE_D4, 50},
{NOTE_D3, 50},
{NOTE_D2, 50},
{NOTE_D1, 50},
{0,0}
};
STATUS app_entry()
{
TASK_INFO taskInfo;
mutex = semCreate(OS_SEMA_MUTEX, 1, "Beep mutex");
taskInfo.priority = 10;
sprintf(&taskInfo.name[0], "tMusic");
taskInfo.taskEntry = tMusicEntry;
taskInfo.pData = (void *)defaultMusic;
taskInfo.msgQSize = 0;
taskInfo.semaQSize = DEFAULT_MUTEX_Q_SIZE;
taskInfo.stackSize = TICK_TASK_STACK_SIZE;
taskInfo.option = OPT_TASK_DEFAULT;
g_musicTask = taskCreate(&taskInfo);
/* type command 'play' in shell prompt will cause the music task play. */
regShellCmd(play, "play", "play a short test music.");
return OS_SUCCESS;
}
/******************************************************************************
Function : void tMusicEntry (void * p)
Params : p - the music note sequence.
:
:
:
Return : N/A
Description : the main entry for music task.
:
******************************************************************************/
void tMusicEntry (void * p)
{
MUSIC_NOTE * music;
music = &defaultMusic[0];
while(1)
{
taskSuspend(g_musicTask);
semTake(mutex, OS_WAIT_FOR_EVER);
play_music(music);
semGive(mutex);
}
}
/******************************************************************************
Function : int play(int argc, char * argv[])
Params : N/A
:
:
:
Return : always 0.
Description : Play music by wakeing up music task.
:
******************************************************************************/
int play(int argc, char * argv[])
{
taskWakeup(g_musicTask);
return 0;
}
/******************************************************************************
Function : void play_note(int note)
Params : note - the music note, since NOTE_0 to NOTE_G7
:
:
:
Return : N/A
Description : plat a single music note.
: To run this function normally, the Tick frequance must be 100Hz,
: i.e. 100Ticks per second.
******************************************************************************/
void play_note(int note)
{
unsigned int tout;
switch(note)
{
case NOTE_0:
tout = FREQ_MUTE;
break;
case NOTE_D1:
tout = 100000/FREQ_D1;
break;
case NOTE_D2:
tout = 100000/FREQ_D2;
break;
case NOTE_D3:
tout = 100000/FREQ_D3;
break;
case NOTE_D4:
tout = 100000/FREQ_D4;
break;
case NOTE_D5:
tout = 100000/FREQ_D5;
break;
case NOTE_D6:
tout = 100000/FREQ_D6;
break;
case NOTE_D7:
tout = 100000/FREQ_D7;
break;
case NOTE_Z1:
tout = 100000/FREQ_Z1;
break;
case NOTE_Z2:
tout = 100000/FREQ_Z2;
break;
case NOTE_Z3:
tout = 100000/FREQ_Z3;
break;
case NOTE_Z4:
tout = 100000/FREQ_Z4;
break;
case NOTE_Z5:
tout = 100000/FREQ_Z5;
break;
case NOTE_Z6:
tout = 100000/FREQ_Z6;
break;
case NOTE_Z7:
tout = 100000/FREQ_Z7;
break;
case NOTE_G1:
tout = 100000/FREQ_G1;
break;
case NOTE_G2:
tout = 100000/FREQ_G2;
break;
case NOTE_G3:
tout = 100000/FREQ_G3;
break;
case NOTE_G4:
tout = 100000/FREQ_G4;
break;
case NOTE_G5:
tout = 100000/FREQ_G5;
break;
case NOTE_G6:
tout = 100000/FREQ_G6;
break;
case NOTE_G7:
tout = 100000/FREQ_G7;
break;
default:
tout = FREQ_MUTE;
break;
}
if (tout != FREQ_MUTE)
{
pwm_start(PWM_TIMER2, tout, 1);
}
else
{
pwm_stop(PWM_TIMER2);
}
}
/******************************************************************************
Function : void play_music (MUSIC_NOTE * pMusic)
Params : pMusic - the music notes sequance which combineing a music.
:
:
:
Return : N/A
Description : To run this function normally, the Tick frequance must be 100Hz,
: i.e. 100 Ticks per second.
******************************************************************************/
void play_music (MUSIC_NOTE * pMusic)
{
MUSIC_NOTE * p;
if (pMusic == NULL)
{
OS_printf("play_music(): no valid music notes sequance...\n");
return;
}
p = pMusic;
while(p->duration != 0)
{
play_note(p->note);
taskDelay(p->duration);
p++;
}
play_note(NOTE_0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -