/** * PWM tone generator library * * Author: Jan Dvořák z Vozerovic * E-mail: dvorkaman@gmail.com * Web: dvorkaman.asp2.cz * Created: 2014 */ /* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __dvorkaman_PWMTone_base_h__ #define __dvorkaman_PWMTone_base_h__ /* INCLUDES */ #include "libSystem_base.h" #include "libSystemTime_base.h" #include "libPWM_base.h" /* TEMPO */ #define PWM_TONE_DEFAULT_TEMPO 120 // beats per minute #define PWM_TONE_DEFAULT_BEATS 4 // beats per minute #define PWM_TONE_DEFAULT_TEMPO_MS (PWM_TONE_DEFAULT_TEMPO*1000) // beats per minute /* DURATION */ #define PWM_TONE_DEFAULT_DURATION_1_1 (PWM_TONE_DEFAULT_TEMPO_MS) #define PWM_TONE_DEFAULT_DURATION_1_2 (PWM_TONE_DEFAULT_TEMPO_MS >> 1) #define PWM_TONE_DEFAULT_DURATION_1_4 (PWM_TONE_DEFAULT_TEMPO_MS >> 2) #define PWM_TONE_DEFAULT_DURATION_1_8 (PWM_TONE_DEFAULT_TEMPO_MS >> 3) #define PWM_TONE_DEFAULT_DURATION_1_16 (PWM_TONE_DEFAULT_TEMPO_MS >> 4) #define PWM_TONE_DEFAULT_DURATION_1_32 (PWM_TONE_DEFAULT_TEMPO_MS >> 5) #define PWM_TONE_DEFAULT_DURATION (PWM_TONE_DEFAULT_DURATION_1_4) // beats per s /* DEFINES NOTES IN HZ */ #define PWM_NOTE_NONE 0 #define PWM_NOTE_H0 31 // B in some countries #define PWM_NOTE_B0 31 // #define PWM_NOTE_C1 33 #define PWM_NOTE_CS1 35 #define PWM_NOTE_D1 37 #define PWM_NOTE_DS1 39 #define PWM_NOTE_E1 41 #define PWM_NOTE_F1 44 #define PWM_NOTE_FS1 46 #define PWM_NOTE_G1 49 #define PWM_NOTE_GS1 52 #define PWM_NOTE_A1 55 #define PWM_NOTE_AS1 58 #define PWM_NOTE_H1 62 // B in some countries #define PWM_NOTE_B1 62 // #define PWM_NOTE_C2 65 #define PWM_NOTE_CS2 69 #define PWM_NOTE_D2 73 #define PWM_NOTE_DS2 78 #define PWM_NOTE_E2 82 #define PWM_NOTE_F2 87 #define PWM_NOTE_FS2 93 #define PWM_NOTE_G2 98 #define PWM_NOTE_GS2 104 #define PWM_NOTE_A2 110 #define PWM_NOTE_AS2 117 #define PWM_NOTE_H2 123 // B in some countries #define PWM_NOTE_B2 123 // #define PWM_NOTE_C3 131 #define PWM_NOTE_CS3 139 #define PWM_NOTE_D3 147 #define PWM_NOTE_DS3 156 #define PWM_NOTE_E3 165 #define PWM_NOTE_F3 175 #define PWM_NOTE_FS3 185 #define PWM_NOTE_G3 196 #define PWM_NOTE_GS3 208 #define PWM_NOTE_A3 220 #define PWM_NOTE_AS3 233 #define PWM_NOTE_H3 247 // B in some countries #define PWM_NOTE_B3 247 // #define PWM_NOTE_C4 262 #define PWM_NOTE_CS4 277 #define PWM_NOTE_D4 294 #define PWM_NOTE_DS4 311 #define PWM_NOTE_E4 330 #define PWM_NOTE_F4 349 #define PWM_NOTE_FS4 370 #define PWM_NOTE_G4 392 #define PWM_NOTE_GS4 415 #define PWM_NOTE_A4 440 #define PWM_NOTE_AS4 466 #define PWM_NOTE_H4 494 // B in some countries #define PWM_NOTE_B4 494 // #define PWM_NOTE_C5 523 #define PWM_NOTE_CS5 554 #define PWM_NOTE_D5 587 #define PWM_NOTE_DS5 622 #define PWM_NOTE_E5 659 #define PWM_NOTE_F5 698 #define PWM_NOTE_FS5 740 #define PWM_NOTE_G5 784 #define PWM_NOTE_GS5 831 #define PWM_NOTE_A5 880 #define PWM_NOTE_AS5 932 #define PWM_NOTE_H5 988 // B in some countries #define PWM_NOTE_B5 988 // #define PWM_NOTE_C6 1047 #define PWM_NOTE_CS6 1109 #define PWM_NOTE_D6 1175 #define PWM_NOTE_DS6 1245 #define PWM_NOTE_E6 1319 #define PWM_NOTE_F6 1397 #define PWM_NOTE_FS6 1480 #define PWM_NOTE_G6 1568 #define PWM_NOTE_GS6 1661 #define PWM_NOTE_A6 1760 #define PWM_NOTE_AS6 1865 #define PWM_NOTE_H6 1976 // B in some countries #define PWM_NOTE_B6 1976 // #define PWM_NOTE_C7 2093 #define PWM_NOTE_CS7 2217 #define PWM_NOTE_D7 2349 #define PWM_NOTE_DS7 2489 #define PWM_NOTE_E7 2637 #define PWM_NOTE_F7 2794 #define PWM_NOTE_FS7 2960 #define PWM_NOTE_G7 3136 #define PWM_NOTE_GS7 3322 #define PWM_NOTE_A7 3520 #define PWM_NOTE_AS7 3729 #define PWM_NOTE_H7 3951 // B in some countries #define PWM_NOTE_B7 3951 // #define PWM_NOTE_C8 4186 #define PWM_NOTE_CS8 4435 #define PWM_NOTE_D8 4699 #define PWM_NOTE_DS8 4978 // Horn, Beep, Siren, PDC #define PWM_TONE_HORN PWM_NOTE_A4 #define PWM_TONE_BEEP_TONE PWM_NOTE_A5 #define PWM_TONE_BEEP_TONE_DURATION 750 #define PWM_TONE_BEEP_SILENCE_DURATION 750 #define PWM_TONE_SIREN_LOW_TONE PWM_NOTE_G3 #define PWM_TONE_SIREN_HIGH_TONE PWM_NOTE_C4 #define PWM_TONE_SIREN_TONE_INCREMENT 1 // tones are in LOGARITHMIC SCALE! #define PWM_TONE_SIREN_TONE_INCREMENT_EACH 25 // ms #define PWM_TONE_SIREN_TONE_DURATION 250 // ms - on high and low tone #define PWM_TONE_PDC PWM_NOTE_C6 #define PWM_TONE_PDC_TONE_DURATION 500 #define PWM_TONE_PDC_MIN_PAUSE 125 #define PWM_TONE_PDC_MAX_PAUSE 1500 /* STRUCTURES */ typedef struct { uint8_t notesCount; uint16_t* notes; // array pointer uint8_t* volumes; // array pointer; 1 .. 100 uint16_t* durations; // array pointer; in ms libPWMpin* pwmPin; systemTime nextNoteTime; // internal timming of notes uint8_t nextNoteIndex; // internal note selection } libPWMmelody; typedef struct { libPWMpin* pwmPin; uint16_t tone; uint16_t toneDuration; // ms uint16_t silenceDuration; // ms systemTime lastNoteTime; uint8_t mode; // 0 - stopped, 1 - note, 2 - silence } libPWMbeep; typedef struct { libPWMpin* pwmPin; uint16_t tone; uint16_t lowTone; uint16_t highTone; uint16_t toneIncrement; uint16_t incrementEach; // ms uint16_t toneDuration; // ms - low and hight tone systemTime nextNoteTime; uint8_t mode; // 0 - stopped, 1 - increment, 2 - decrement } libPWMsiren; typedef struct { libPWMpin* pwmPin; systemTime nextChange; uint8_t mode; // 0 - no tone, 1 beep tone - play; 2 beep silent, 3 = full tone uint16_t firstToneDistance; uint16_t fullToneDistance; uint16_t silenceDuration; } libPWMPDC; /* HEADERS */ void pwmTone_playTone(libPWMpin* pwmPin, uint16_t tone); void pwmTone_playToneVolume(libPWMpin* pwmPin, uint16_t tone, uint8_t volume); // Melody void pwmTone_melodyInitialize(libPWMmelody* pwmTone, libPWMpin* pwmPin, uint8_t notesCount, uint16_t* notes); void pwmTone_melodyInitializeDurations(libPWMmelody* pwmTone, libPWMpin* pwmPin, uint8_t notesCount, uint16_t* notes, uint16_t* durations); void pwmTone_melodyInitializeVolumes(libPWMmelody* pwmTone, libPWMpin* pwmPin, uint8_t notesCount, uint16_t* notes, uint16_t* durations, uint8_t* volumes); void pwmTone_melodyStop(libPWMmelody* pwmTone); void pwmTone_melodyPlay(libPWMmelody* pwmTone); bool pwmTone_melodyFinished(libPWMmelody* pwmTone); uint16_t pwmTone_getDuration(uint8_t tempo, uint8_t count, uint8_t length); uint16_t pwmTone_getTone(char noteName, bool sharp, uint8_t octave); void pwmTone_loadMelodyFromNokiaString(libPWMmelody* pwmTone, char* string, uint8_t maximalNotesCount, uint8_t tempo); bool pwmTone_nokiaStringParseNextNote(char* string, uint16_t* startIndex, uint8_t tempo, uint16_t* note, uint16_t* duration); // Horn void pwmTone_hornStart(libPWMpin* pwmPin); void pwmTone_hornStop(libPWMpin* pwmPin); // Beeping void pwmTone_beepInitialize(libPWMbeep* pwmBeep, libPWMpin* pwmPin); void pwmTone_beepStart(libPWMbeep* pwmBeep); void pwmTone_beepPlay(libPWMbeep* pwmBeep); void pwmTone_beepStop(libPWMbeep* pwmBeep); // Siren void pwmTone_sirenInitialize(libPWMsiren* pwmSiren, libPWMpin* pwmPin); void pwmTone_sirenStart(libPWMsiren* pwmSiren); void pwmTone_sirenPlay(libPWMsiren* pwmSiren); void pwmTone_sirenStop(libPWMsiren* pwmSiren); // PDC void pwmTone_pdcInitialize(libPWMPDC* pwmPDC, libPWMpin* pwmPin, uint16_t firstToneDistance, uint16_t fullToneDistance); void pwmTone_pdcStop(libPWMPDC* pwmPDC); void pwmTone_pdcSetDistance(libPWMPDC* pwmPDC, uint16_t distance); void pwmTone_pdcLoop(libPWMPDC* pwmPDC); #endif