mbed/ARM 活用事例 第3章 SDカードを使ってファイルを操作するプログラムを作る

Dependencies:   TextLCD mbed SDFileSystem

Revision:
0:c743b6678d88
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MySound.cpp	Tue Oct 04 12:59:42 2011 +0000
@@ -0,0 +1,125 @@
+// --- MySound.cpp ---
+#include "MySound.h"
+
+MySound::MySound(PinName out) : _out(out){
+
+}
+
+void MySound::play(char pn,int s, char l)
+{
+    double freq,f;
+    float length ;
+    int scale ;
+    int begin;
+      
+    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 ;
+            break;
+        case 5:
+            f = freq * 2 ;
+            break;
+        case 4:
+            f = freq;
+            break;
+        case 3: 
+            f = freq / 2 ;
+            break;
+        case 2:
+            f = freq / 4;
+            break; 
+        default:
+            f = freq ;
+        break ;
+    }
+
+    scale = (int)((1.0/f)*1e6) ;
+    _out.period_us(scale);
+
+    t.start();
+    begin = t.read_ms() ;
+    if ( pn != 'R' ){
+        while(t.read_ms()-begin < (int)(500 * length) ){
+            _out.write(0.5f);    
+        }
+    }else{
+        while(t.read_ms()-begin < (int)(500 * length) ){
+            _out.write(0.0f);    
+        }    
+    }
+    t.stop();
+    t.reset();
+}