Go Back   VEX Forum > Community > Technical Discussion

Technical Discussion Open Technical Discussion of the VEX Robotics System.

Reply
 
Thread Tools
  #1  
Old 04-25-2012, 10:17 PM
jpearman's Avatar
jpearman jpearman is offline
Senior Member
VEX # 8888
 
Join Date: Apr 2011
Location: Los Angeles
Posts: 3,251
Images: 2
EasyC 4 sound library for cortex

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.

Code:
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.

Code:
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.

Code:
// 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

Last edited by jpearman; 04-29-2012 at 07:04 PM.
Reply With Quote
  #2  
Old 04-26-2012, 05:47 PM
devinc devinc is offline
Senior Member
VEX # 569C
 
Join Date: May 2010
Location: Murrieta,CA
Posts: 537
Images: 1
Re: EasyC 4 sound library for cortex

I just downloaded your zip file. Im going to try this out tomorrow.
__________________
569A, B, & C Mentor
Reply With Quote
  #3  
Old 04-26-2012, 06:03 PM
jpearman's Avatar
jpearman jpearman is offline
Senior Member
VEX # 8888
 
Join Date: Apr 2011
Location: Los Angeles
Posts: 3,251
Images: 2
Re: EasyC 4 sound library for cortex

Actually, here is an update, now you can play ring tones directly in RTTTL format such as.

Code:
char tune4[] = "Beethoven:d=4,o=6,b=125:8e,8d,8e,8d,8e,8b5,8d,8c,a5,8p,8c5,8e5,8a5,b5,8p,8e5,8g5,8b5,c,8p,8e5,8e,8d,8e,8d,8e,8b5,8d,8c,a5,8p,8c5,8e5,8a5,b5,8p,8e5,8c,8b5,a5";
See the demo for details, one new function added. (an example)

Code:
PlayRtttl( tune5, 128, 1 );
This plays the ring tone stored in the array "tune5" at amplitude 128 and repeats in a loop (the final 1).

The demo has 5 tunes included, change using joystick button 8R and 8L.

There are dozens of RTTTL tunes on the internet, anything you want.

Enjoy

Last edited by jpearman; 04-29-2012 at 07:04 PM.
Reply With Quote
  #4  
Old 04-26-2012, 07:03 PM
devinc devinc is offline
Senior Member
VEX # 569C
 
Join Date: May 2010
Location: Murrieta,CA
Posts: 537
Images: 1
Re: EasyC 4 sound library for cortex

Do I just download the code straight into my cortex and have the speaker in the speaker port?
Edit: is there any way I can use a speaker on a microcontroller? I am working on a project currently and im using an old brain that we have and I would like to use my new speaker.
__________________
569A, B, & C Mentor

Last edited by devinc; 04-26-2012 at 07:43 PM.
Reply With Quote
  #5  
Old 04-26-2012, 07:54 PM
jpearman's Avatar
jpearman jpearman is offline
Senior Member
VEX # 8888
 
Join Date: Apr 2011
Location: Los Angeles
Posts: 3,251
Images: 2
Re: EasyC 4 sound library for cortex

Quote:
Originally Posted by devinc View Post
Do I just download the code straight into my cortex and have the speaker in the speaker port?
It's a complete EasyC project so just open and download. It will write some information on the LCD if that is connected, the project I posted today can use the joystick 8R and 8L to change tunes.

If you want to add this to a new project the following files should be moved into the project directory.

vsl.h
vsl.c
vslrtttl.c
the vsl_include folder with everything in it.

vsl.c and vslrtttl.c need to be added as user source files and the following added to the UserInclude.h file along with any functions you use to call them.

Code:
void PlayFrequency( int freq, int amplitude, int timems );
void ClearSounds( void );
void InitChipToneMusic( int len );
void SetNextTone( int freq, int amplitude, int timems );
void StartChipTone( int repeat  );
char *PlayRtttl( char *song , int amplitude, int repeat );
Quote:
Edit: is there any way I can use a speaker on a microcontroller? I am working on a project currently and im using an old brain that we have and I would like to use my new speaker.
I seem to remember that Quazar posted something for sound on the PIC last year, I was looking for it yesterday but could not find it, I will look again.
Reply With Quote
  #6  
Old 04-26-2012, 08:02 PM
devinc devinc is offline
Senior Member
VEX # 569C
 
Join Date: May 2010
Location: Murrieta,CA
Posts: 537
Images: 1
Re: EasyC 4 sound library for cortex

I will definately give the code a try tomorrow. I will look also to see if I can use it on PIC.
__________________
569A, B, & C Mentor
Reply With Quote
  #7  
Old 04-26-2012, 08:15 PM
jpearman's Avatar
jpearman jpearman is offline
Senior Member
VEX # 8888
 
Join Date: Apr 2011
Location: Los Angeles
Posts: 3,251
Images: 2
Re: EasyC 4 sound library for cortex

Quote:
Originally Posted by devinc View Post
I will definately give the code a try tomorrow. I will look also to see if I can use it on PIC.
The code I posted will only work on a cortex, it accesses the cortex hardware directly, it will not work on a PIC. Let me look again for Quazar's code.
Reply With Quote
  #8  
Old 04-27-2012, 10:55 AM
Quazar's Avatar
Quazar Quazar is offline
Senior Member
 
Join Date: Aug 2007
Location: SF Bay Area
Posts: 1,234
Images: 16
Re: EasyC 4 sound library for cortex

Quote:
Originally Posted by jpearman View Post
The code I posted will only work on a cortex, it accesses the cortex hardware directly, it will not work on a PIC. Let me look again for Quazar's code.
I don't recall doing audio support for the PIC, though it shouldn't be too challenging if you don't mind dedicating a motor port (not sure if the new Vex speaker would survive the voltage of a motor port!). This whitepaper describes the timer gymnastics, and if you switch the motor port to user processor control, the timer can drive it directly.

If you can chain into an interrupt handler under EasyC, then you could get it to work with one of the I/O ports.

Instead of PIC support, I can offer a utility function for ROBOTC+Cortex that lets you pass in a music string and play it as a series of notes/rests. Here is the wiki page.

Cheers,
- Dean
Reply With Quote
  #9  
Old 04-27-2012, 11:27 AM
jpearman's Avatar
jpearman jpearman is offline
Senior Member
VEX # 8888
 
Join Date: Apr 2011
Location: Los Angeles
Posts: 3,251
Images: 2
Re: EasyC 4 sound library for cortex

Quote:
Originally Posted by Quazar View Post
I don't recall doing audio support for the PIC, though it shouldn't be too challenging if you don't mind dedicating a motor port (not sure if the new Vex speaker would survive the voltage of a motor port!). This whitepaper describes the timer gymnastics, and if you switch the motor port to user processor control, the timer can drive it directly.

If you can chain into an interrupt handler under EasyC, then you could get it to work with one of the I/O ports.

Instead of PIC support, I can offer a utility function for ROBOTC+Cortex that lets you pass in a music string and play it as a series of notes/rests. Here is the wiki page.

Cheers,
- Dean
That's what I remember you posted before, guess I was wrong about the PIC sound support. I was going to port the RTTTL code I posted yesterday to ROBOTC but ran into the 20 char limit on strings so gave up for now. It was going to be hard without pointers anyway and ROBOTC has a utility to convert RTTTL to a function with suitable PlayTone commands.
Reply With Quote
  #10  
Old 04-27-2012, 09:56 PM
devinc devinc is offline
Senior Member
VEX # 569C
 
Join Date: May 2010
Location: Murrieta,CA
Posts: 537
Images: 1
Re: EasyC 4 sound library for cortex

I tried your code out today. it worked perfectly. it was really weird hearing super mario and pac man coming out of my robot. I really hope that the speaker is not legal in competition because it got annoying just after a few seconds.
__________________
569A, B, & C Mentor
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 08:09 AM.


VEX and VEX Robotics are trademarks or service marks of Innovation First International, Inc.
Copyright © 2002-2013. All Rights Reserved. VEX Robotics, Inc. is a subsidiary of Innovation First International, Inc.
All other product names/marks of others are the property of their respective owners.