So I’m posting a project for everyone to BETA TEST as it’s not really production code.
This project is a demo for a sound library I hacked together today that allows tones to be played both synchronously and asynchronously from EasyC on the cortex. The demo is tested using EasyC V 4.1.0.3 and plays mission impossible (thanks ROBOTC) while driver control is running.
User functions are as follows.
void PlayFrequency( int freq, int amplitude, int timems );
void PlayFrequencyWait( int freq, int amplitude, int timems );
void ClearSounds( void );
PlayFrequency does as it says, freq is in hertz, amplitude can vary between 0 (off) and 255 (full, may be too much). timems is the time this sound should play in milliseconds, it runs on a repeating timer for duration. The first time this is called the initialization of the DAC happens.
PlayFrequencyWait is the same but blocks until the sound is done.
ClearSounds just stop the currently playing sound.
There are also alternative functions to play tones in the background. Probably best not to mix these with the above, use one or the other.
void InitChipToneMusic( int len );
void SetNextTone( int freq, int amplitude, int timems );
void StartChipTone( int repeat );
InitChipToneMusic allocates storage for “len” samples each of which is a triplet of freq, amplitude and time.
See the included example on how to use but it goes basically like this.
// play mission impossible asynchronously
void
MissionImpossible()
{
InitChipToneMusic( 66 );
//
SetNextTone( 880, 128, 70); // Note(D, Duration(32th))
SetNextTone( 933, 128, 70); // Note(D#, Duration(32th))
SetNextTone( 880, 128, 70); // Note(D, Duration(32th))
// skipped code
SetNextTone( 932, 128, 140); // Note(A#5, Duration(16th))
SetNextTone( 784, 128, 140); // Note(C, Duration(16th))
SetNextTone( 0, 128, 500); //
StartChipTone(1); // 1 means repeat
return;
}
The sound library uses Timer7 and DMA2 channel 3, I don’t think these things clash with anything else in EasyC but have not tested all the sensors so use this code at you own risk for now. The code is based on examples in the STM32 standard peripheral library, I included some of the DAC code in vsl.c to simplify things but there is still a folder of header files.
Hopefully Intelitek will have some official code soon.
Enjoy