-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicBox.cpp
More file actions
100 lines (83 loc) · 2.26 KB
/
MusicBox.cpp
File metadata and controls
100 lines (83 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Methods and statics for the MusicBox singleton object
* Copyright(c) Charles Shapiro November 2017
This file is part of MusicBox.
MusicBox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MusicBox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MusicBox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Arduino.h"
#include <MusicBox.h>
#include <pitches.h>
#define CHS_DEBUG
static note _OopsSong[] =
{
{NOTE_C4,4}, {NOTE_G3,8}, {NOTE_G3,8}, {NOTE_A3,4},
{NOTE_G3,4}, {0,4}, {NOTE_B3,4}, {NOTE_C4,4},
{-1,-1}
};
#define MUSICLIBSIZE ( (sizeof(musicLibrary)/sizeof(note *)) - 1 )
MusicBox::MusicBox()
{
begin(-1);
}
/*
* Pass output pin to creator
*/
MusicBox::MusicBox(int thepin)
{
begin(thepin);
}
/*
* Load a tune into the music box for future reference
*/
int MusicBox::loadATune(note *thesong, int thePlace)
{
boolean retval=true; //retval false means a trouble happened.
if((thePlace < 0) || (thePlace > MUSICLIBSIZE))
retval=false;
if(true == retval) {
musicLibrary[thePlace+1]=thesong;
}
}
/*
* Public interface play
*/
void MusicBox::playATune(int tuneIdx)
{
if((tuneIdx < MUSICLIBSIZE) && (tuneIdx >= 0) && (musicLibrary[tuneIdx]) )
playTune(musicLibrary[tuneIdx+1]);
else
playTune(musicLibrary[0]);
}
/*
* Actually play a tune (e.g. a series of notes)
*/
void MusicBox::playTune(note *theTune)
{
int thePause;
while(theTune->duration != -1) {
tone(MusicOutPin,theTune->pitch,(unsigned long)(1000/(theTune->duration)));
delay(( (1000/(theTune->duration)) * 1.30));
++theTune;
}
}
/*
* Public interface init
*/
void MusicBox::begin(int thePin)
{
if(thePin > 0) {
pinMode(thePin,OUTPUT);
MusicOutPin=thePin;
}
memset(musicLibrary,0,sizeof(musicLibrary));
musicLibrary[0]=_OopsSong;
}