source: rtos_arduino/trunk/arduino_lib/libraries/Robot_Control/src/Melody.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 2.8 KB
Line 
1#include "ArduinoRobot.h"
2#include "SquawkSD.h"
3#include "Fat16.h"
4
5
6
7SQUAWK_CONSTRUCT_ISR(SQUAWK_PWM_PIN5);
8
9
10void RobotControl::beginSpeaker(uint16_t frequency){
11 SquawkSynth::begin(frequency);
12 SquawkSynth::play();
13 osc[2].vol = 0x7F;
14}
15
16void RobotControl::playNote(byte period, word length, char modifier) {
17 // Modifier . makes note length 2/3
18 if(modifier == '.') length = (length * 2) / 3;
19 // Set up the play frequency, 352800 is [sample_rate]=44100 * [tuning]=8.0
20 osc[2].freq = 352800 / period;
21 // Delay, silence, delay
22 delay(length);
23 osc[2].freq = 0;
24 delay(length);
25}
26
27void RobotControl::playMelody(char* script){
28 // Find length of play string
29 word length = strlen(script);
30 // Set the default note time
31 word time = 500;
32 // Loop through each character in the play string
33 for(int n = 0; n < length; n++) {
34 // Fetch the character AFTER the current one - it may contain a modifier
35 char modifier = script[n + 1];
36 // Fetch the current character and branch accordingly
37 switch(script[n]) {
38 // Notes
39 case 'c': playNote(214, time, modifier); break; // Play a C
40 case 'C': playNote(202, time, modifier); break; // Play a C#
41 case 'd': playNote(190, time, modifier); break; // Play a D
42 case 'D': playNote(180, time, modifier); break; // Play a D#
43 case 'e': playNote(170, time, modifier); break; // Play an E
44 case 'f': playNote(160, time, modifier); break; // Play an F
45 case 'F': playNote(151, time, modifier); break; // Play an F#
46 case 'g': playNote(143, time, modifier); break; // Play a G
47 case 'G': playNote(135, time, modifier); break; // Play a G#
48 case 'a': playNote(127, time, modifier); break; // Play an A
49 case 'A': playNote(120, time, modifier); break; // Play an A#
50 case 'b': playNote(113, time, modifier); break; // Play a B
51 // Delay
52 case '-': playNote(0, time, modifier); break; // Play a quiet note
53 // Note lengths
54 case '1': time = 1000; break; // Full note
55 case '2': time = 500; break; // Half note
56 case '4': time = 250; break; // Quarter note
57 case '8': time = 50; break; // Eigth note
58 // Modifier '.' makes note length 2/3
59
60 }
61 }
62}
63
64void RobotControl::beep(int beep_length){
65 char scr1[]="8F";
66 char scr2[]="8Fe";
67 char scr3[]="1F";
68
69 switch (beep_length)
70 {
71 case BEEP_SIMPLE:
72 default:
73 playMelody(scr1);
74 break;
75
76 case BEEP_DOUBLE:
77 playMelody(scr2);
78 break;
79
80 case BEEP_LONG:
81 playMelody(scr3);
82 }
83
84}
85
86void RobotControl::tempoWrite(int tempo){
87 SquawkSynthSD::tempo(tempo);
88}
89void RobotControl::tuneWrite(float tune){
90 SquawkSynthSD::tune(tune);
91}
92
93void RobotControl::playFile(char* filename){
94 melody.open(filename,O_READ);
95 SquawkSynthSD::play(melody);
96}
97
98void RobotControl::stopPlayFile(){
99 melody.close();
100}
Note: See TracBrowser for help on using the repository browser.