Bzzer_HelloWorld Example for WIZwiki-W7500

Dependencies:   mbed

Committer:
IOP
Date:
Wed Aug 05 00:41:00 2015 +0000
Revision:
2:bdbe512fe5b2
Parent:
1:067ddbdbac9c
Child:
4:2821beb9bfc8
add auto song(tones_school)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:c4205a3ad6fe 1 /**
justinkim 0:c4205a3ad6fe 2 ******************************************************************************
justinkim 0:c4205a3ad6fe 3 * @project Web Piano
justinkim 0:c4205a3ad6fe 4 * @author Justin Kim
justinkim 0:c4205a3ad6fe 5 * @version V1.0.0
justinkim 0:c4205a3ad6fe 6 * @date 03-JUL-2015
justinkim 0:c4205a3ad6fe 7 * @brief Main program body
justinkim 0:c4205a3ad6fe 8 *******************************************************************************
justinkim 0:c4205a3ad6fe 9 **/
justinkim 0:c4205a3ad6fe 10 /* Includes ------------------------------------------------------------------*/
justinkim 0:c4205a3ad6fe 11 #include "mbed.h"
justinkim 0:c4205a3ad6fe 12
justinkim 0:c4205a3ad6fe 13 /* Private typedef -----------------------------------------------------------*/
justinkim 0:c4205a3ad6fe 14 /* Private define ------------------------------------------------------------*/
justinkim 0:c4205a3ad6fe 15 #define Do3 131 //C octave3
justinkim 0:c4205a3ad6fe 16 #define Do3s 139 //C#
justinkim 0:c4205a3ad6fe 17 #define Re3 147 //D
justinkim 0:c4205a3ad6fe 18 #define Re3s 156//D#
justinkim 0:c4205a3ad6fe 19 #define Mi3 165 //E
justinkim 0:c4205a3ad6fe 20 #define Fa3 175 //F
justinkim 0:c4205a3ad6fe 21 #define Fa3s 185 //F#
justinkim 0:c4205a3ad6fe 22 #define So3 196 //G
justinkim 0:c4205a3ad6fe 23 #define So3s 208 //G#
justinkim 0:c4205a3ad6fe 24 #define La3 220 //A
justinkim 0:c4205a3ad6fe 25 #define La3s 233 //A#
justinkim 0:c4205a3ad6fe 26 #define Ti3 247 //B
justinkim 0:c4205a3ad6fe 27 #define Do4 262 //C octave4
justinkim 0:c4205a3ad6fe 28 #define Do4s 277 //C#
justinkim 0:c4205a3ad6fe 29 #define Re4 294 //D
justinkim 0:c4205a3ad6fe 30 #define Re4s 311//D#
justinkim 0:c4205a3ad6fe 31 #define Mi4 330 //E
justinkim 0:c4205a3ad6fe 32 #define Fa4 349 //F
justinkim 0:c4205a3ad6fe 33 #define Fa4s 370 //F#
justinkim 0:c4205a3ad6fe 34 #define So4 392 //G
justinkim 0:c4205a3ad6fe 35 #define So4s 415 //G#
justinkim 0:c4205a3ad6fe 36 #define La4 440 //A
justinkim 0:c4205a3ad6fe 37 #define La4s 466 //A#
justinkim 0:c4205a3ad6fe 38 #define Ti4 494 //B
justinkim 0:c4205a3ad6fe 39 #define Do5 523 //C octave5
justinkim 0:c4205a3ad6fe 40 #define Do5s 554 //C#
justinkim 0:c4205a3ad6fe 41 #define Re5 587 //D
justinkim 0:c4205a3ad6fe 42 #define Re5s 622//D#
justinkim 0:c4205a3ad6fe 43 #define Mi5 659 //E
justinkim 0:c4205a3ad6fe 44 #define Fa5 699 //F
justinkim 0:c4205a3ad6fe 45 #define Fa5s 740 //F#
justinkim 0:c4205a3ad6fe 46 #define So5 784 //G
justinkim 0:c4205a3ad6fe 47 #define So5s 831 //G#
justinkim 0:c4205a3ad6fe 48 #define La5 880 //A
justinkim 0:c4205a3ad6fe 49 #define La5s 932 //A#
justinkim 0:c4205a3ad6fe 50 #define Ti5 988 //B
justinkim 0:c4205a3ad6fe 51
justinkim 0:c4205a3ad6fe 52 /* Private variables ---------------------------------------------------------*/
justinkim 1:067ddbdbac9c 53 PwmOut Buzzer(D5);
justinkim 0:c4205a3ad6fe 54
IOP 2:bdbe512fe5b2 55 float C_3 = 1000000/Do3,
justinkim 0:c4205a3ad6fe 56 Cs_3 = 1000000/Do3s,
justinkim 0:c4205a3ad6fe 57 D_3 = 1000000/Re3,
justinkim 0:c4205a3ad6fe 58 Ds_3 = 1000000/Re3s,
justinkim 0:c4205a3ad6fe 59 E_3 = 1000000/Mi3,
justinkim 0:c4205a3ad6fe 60 F_3 = 1000000/Fa3,
justinkim 0:c4205a3ad6fe 61 Fs_3 = 1000000/Fa3s,
justinkim 0:c4205a3ad6fe 62 G_3 = 1000000/So3,
justinkim 0:c4205a3ad6fe 63 Gs_3 = 1000000/So3s,
justinkim 0:c4205a3ad6fe 64 A_3 = 1000000/La3,
justinkim 0:c4205a3ad6fe 65 As_3 = 1000000/La3s,
justinkim 0:c4205a3ad6fe 66 B_3 = 1000000/Ti3,
justinkim 0:c4205a3ad6fe 67 C_4 = 1000000/Do4,
justinkim 0:c4205a3ad6fe 68 Cs_4 = 1000000/Do4s,
justinkim 0:c4205a3ad6fe 69 D_4 = 1000000/Re4,
justinkim 0:c4205a3ad6fe 70 Ds_4 = 1000000/Re4s,
justinkim 0:c4205a3ad6fe 71 E_4 = 1000000/Mi4,
justinkim 0:c4205a3ad6fe 72 F_4 = 1000000/Fa4,
justinkim 0:c4205a3ad6fe 73 Fs_4 = 1000000/Fa4s,
justinkim 0:c4205a3ad6fe 74 G_4 = 1000000/So4,
justinkim 0:c4205a3ad6fe 75 Gs_4 = 1000000/So4s,
justinkim 0:c4205a3ad6fe 76 A_4 = 1000000/La4,
justinkim 0:c4205a3ad6fe 77 As_4 = 1000000/La4s,
justinkim 0:c4205a3ad6fe 78 B_4 = 1000000/Ti4,
justinkim 0:c4205a3ad6fe 79 C_5 = 1000000/Do5,
justinkim 0:c4205a3ad6fe 80 Cs_5 = 1000000/Do5s,
justinkim 0:c4205a3ad6fe 81 D_5 = 1000000/Re5,
justinkim 0:c4205a3ad6fe 82 Ds_5 = 1000000/Re5s,
justinkim 0:c4205a3ad6fe 83 E_5 = 1000000/Mi5,
justinkim 0:c4205a3ad6fe 84 F_5 = 1000000/Fa5,
justinkim 0:c4205a3ad6fe 85 Fs_5 = 1000000/Fa5s,
justinkim 0:c4205a3ad6fe 86 G_5 = 1000000/So5,
justinkim 0:c4205a3ad6fe 87 Gs_5 = 1000000/So5s,
justinkim 0:c4205a3ad6fe 88 A_5 = 1000000/La5,
justinkim 0:c4205a3ad6fe 89 As_5 = 1000000/La5s,
justinkim 0:c4205a3ad6fe 90 B_5 = 1000000/Ti5;
justinkim 0:c4205a3ad6fe 91
IOP 2:bdbe512fe5b2 92 int tones_airplane[] = {E_4, D_4, C_4, D_4, E_4, E_4, E_4, 0, D_4, D_4, D_4, 0, E_4, G_4, G_4, 0,
IOP 2:bdbe512fe5b2 93 E_4, D_4, C_4, D_4, E_4, E_4, E_4, 0, D_4, D_4, E_4, D_4, C_4, 0, 0};
IOP 2:bdbe512fe5b2 94
IOP 2:bdbe512fe5b2 95 int tones_school[] = {D_4, D_4, E_4, E_4, D_4, D_4, B_3, 0, D_4, D_4, B_3, B_3, A_3, 0,
IOP 2:bdbe512fe5b2 96 D_4, D_4, E_4, E_4, D_4, D_4, B_3, 0, D_4, B_3, A_3, B_3, G_3, 0, 0, 0, 0};
IOP 2:bdbe512fe5b2 97
IOP 2:bdbe512fe5b2 98 int tones_num = 31;
justinkim 0:c4205a3ad6fe 99
justinkim 0:c4205a3ad6fe 100 /* Private function prototypes -----------------------------------------------*/
justinkim 0:c4205a3ad6fe 101 void Tune(PwmOut name, int period);
justinkim 0:c4205a3ad6fe 102 void Auto_tunes(PwmOut name, int period);
justinkim 0:c4205a3ad6fe 103 void Stop_tunes(PwmOut name);
IOP 2:bdbe512fe5b2 104
justinkim 0:c4205a3ad6fe 105
justinkim 0:c4205a3ad6fe 106 int main(void)
justinkim 0:c4205a3ad6fe 107 {
justinkim 0:c4205a3ad6fe 108 int i;
justinkim 0:c4205a3ad6fe 109
justinkim 0:c4205a3ad6fe 110 for(i=0; i<tones_num; i++)
justinkim 0:c4205a3ad6fe 111 {
IOP 2:bdbe512fe5b2 112 Auto_tunes(Buzzer, tones_airplane[i]); // can change tones_airplane[i] -> tones_achool[i]
justinkim 0:c4205a3ad6fe 113 Stop_tunes(Buzzer);
justinkim 0:c4205a3ad6fe 114 }
justinkim 0:c4205a3ad6fe 115 }
justinkim 0:c4205a3ad6fe 116
justinkim 0:c4205a3ad6fe 117 /**
IOP 2:bdbe512fe5b2 118 * @brief Tune Function
justinkim 0:c4205a3ad6fe 119 * @param name : Choose the PwmOut
IOP 2:bdbe512fe5b2 120 period : this param is tune value. (C_3...B_5)
justinkim 0:c4205a3ad6fe 121 */
justinkim 0:c4205a3ad6fe 122 void Tune(PwmOut name, int period)
justinkim 0:c4205a3ad6fe 123 {
justinkim 0:c4205a3ad6fe 124 name.period_us(period);
justinkim 0:c4205a3ad6fe 125 name.write(0.50f); // 50% duty cycle
justinkim 0:c4205a3ad6fe 126 wait(1); // 1 beat
justinkim 0:c4205a3ad6fe 127 name.period_us(0); // Sound off
justinkim 0:c4205a3ad6fe 128 }
justinkim 0:c4205a3ad6fe 129
justinkim 0:c4205a3ad6fe 130 /**
IOP 2:bdbe512fe5b2 131 * @brief Auto tunes Function
justinkim 0:c4205a3ad6fe 132 * @param name : Choose the PwmOut
IOP 2:bdbe512fe5b2 133 period : this param is tune value. (C_3...B_5)
justinkim 0:c4205a3ad6fe 134 */
justinkim 0:c4205a3ad6fe 135 void Auto_tunes(PwmOut name, int period)
justinkim 0:c4205a3ad6fe 136 {
justinkim 0:c4205a3ad6fe 137 name.period_us(period);
justinkim 0:c4205a3ad6fe 138 name.write(0.50f); // 50% duty cycle
justinkim 0:c4205a3ad6fe 139 wait_ms(250); // 1/4 beat
justinkim 0:c4205a3ad6fe 140 }
justinkim 0:c4205a3ad6fe 141
IOP 2:bdbe512fe5b2 142 /**
IOP 2:bdbe512fe5b2 143 * @brief Stop tunes Function
IOP 2:bdbe512fe5b2 144 * @param name : Choose the PwmOut
IOP 2:bdbe512fe5b2 145 */
justinkim 0:c4205a3ad6fe 146 void Stop_tunes(PwmOut name)
justinkim 0:c4205a3ad6fe 147 {
justinkim 0:c4205a3ad6fe 148 name.period_us(0);
justinkim 0:c4205a3ad6fe 149 }