エレキジャック Web版 mbedで初めてのマイコン開発 メモリカードを使ったデータの読み書き<2/3> 音符データをSDから読み込んで音楽を鳴らすプログラムです。 音楽データはエレキジャックのサイトに置いてあります。 music.txt http://www.eleki-jack.com/arm/2010/12/mbed-6.html
Dependencies: TextLCD mbed SDFileSystem
Revision 0:f0cd2b9de695, committed 2011-02-04
- Comitter:
- sunifu
- Date:
- Fri Feb 04 15:09:01 2011 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r f0cd2b9de695 FATFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FATFileSystem.lib Fri Feb 04 15:09:01 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_unsupported/code/fatfilesystem/ \ No newline at end of file
diff -r 000000000000 -r f0cd2b9de695 MySound.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MySound.cpp Fri Feb 04 15:09:01 2011 +0000 @@ -0,0 +1,118 @@ +#include "MySound.h" + +MySound::MySound(PinName out) : _out(out){ + _out = 0; +} + +void MySound::play(char pn,int s, char l) +{ + double freq,f; + double scale ; + float length ; + + switch(l){ + case 'W': + length = 4 ; + break ; + case 't': + length = 3; + break ; + case 'D': + length = 2 ; + break; + case 'Q': + length = 1; + break; + case 'q': + length = 1.5; + break; + case 'E': + length = 0.5; + break; + case 'S': + length = 0.25; + break; + case 'T': + length = 0.125; + break; + default: + length = 1; + } + + switch(pn){ + case 'a': + freq = 415.30469 ; + break; + case 'A': + freq = 440.0; + break ; + case 'b': + freq = 466.16876; + break; + case 'B': + freq = 493.88330; + break ; + case 'C': + freq = 261.62556; + break ; + case 'd': + freq = 277.18263; + break ; + case 'D': + freq = 293.66476; + break; + case 'e': + freq = 311.12698; + break ; + case 'E': + freq = 329.62755; + break ; + case 'F': + freq = 349.22823; + break; + case 'g': + freq = 369.99442; + break ; + case 'G': + freq = 391.99543; + break ; + case 'R': + freq = 0.0; + break ; + default: + freq = 440; + } + + switch ( s ){ + case 6: + f = freq * 4.0 ; + break; + case 5: + f = freq * 2.0 ; + break; + case 4: + f = freq; + break; + case 3: + f = freq / 2.0 ; + break; + case 2: + f = freq / 4.0; + break; + default: + f = freq ; + break ; + } + + scale = 1.0/(2.0*f); + +// printf("scale=%lf\tf=%f\tfreq=%f\tlength=%lf\n",scale,f,freq,length); +// printf("f*length=%lf\tfreq=%lf\t%lf \n",f*length,freq,scale); + + for(int i = 0 ; i <= (int)( f * length ) ; i++ ){ + _out = 1; + wait(scale) ; + _out = 0 ; + wait(scale) ; + } +} \ No newline at end of file
diff -r 000000000000 -r f0cd2b9de695 MySound.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MySound.h Fri Feb 04 15:09:01 2011 +0000 @@ -0,0 +1,16 @@ +#ifndef MBED_MYSOUND_H +#define MBED_MYSOUND_H + +#include "mbed.h" + +class MySound { + +public : + MySound(PinName out); + void play(char,int,char); + +protected: + DigitalOut _out; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r f0cd2b9de695 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Fri Feb 04 15:09:01 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/SDFileSystem/#b1ddfc9a9b25
diff -r 000000000000 -r f0cd2b9de695 TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Fri Feb 04 15:09:01 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 000000000000 -r f0cd2b9de695 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Feb 04 15:09:01 2011 +0000 @@ -0,0 +1,30 @@ +#include "mbed.h" +#include "TextLCD.h" +#include "SDFileSystem.h" +#include "MySound.h" + +TextLCD lcd(p24, p26, p27, p28, p29, p30); +SDFileSystem sd(p5, p6, p7, p8, "sd") ; +MySound music(p21) ; + + +int main() { + char pitch ; + int scale; + char ln; + FILE *fp; + int n; + + if ( (fp = fopen("/sd/music.txt","r")) == NULL ) { + printf("Open Failed. \n") ; + return -1; + } + lcd.printf("Silent Night"); + + while( (n = fscanf(fp,"%c %d %c ",&pitch,&scale,&ln)) != EOF ){ + printf("%d %c %d %c\n",n,pitch,scale,ln); + music.play(pitch, scale, ln) ; + } + + fclose(fp) ; +}
diff -r 000000000000 -r f0cd2b9de695 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Feb 04 15:09:01 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e