rtttl
Public Member Functions
Rtttl Class Reference

#include <rtttl.h>

List of all members.

Public Member Functions

 Rtttl ()
void begin (uint8_t tonePin)
void _tone (uint16_t freq)
void _noTone ()
void play_P (const prog_char *p, uint8_t octave_offset=0)
void play (const char *p, uint8_t octave_offset=0)
void _play (const prog_char *p, uint8_t octave_offset, bool pgm)

Detailed Description

Definition at line 108 of file rtttl.h.


Constructor & Destructor Documentation

Rtttl::Rtttl ( ) [inline]

Definition at line 116 of file rtttl.h.

    {
        //      this->begin(tonePin);
    }

Member Function Documentation

void Rtttl::_noTone ( ) [inline]

Definition at line 152 of file rtttl.h.

Referenced by _play().

    {
        //      Serial.println("2stop");
        noTone(this->_pinSpk);
    }
void Rtttl::_play ( const prog_char *  p,
uint8_t  octave_offset,
bool  pgm 
) [inline]

Definition at line 168 of file rtttl.h.

References _noTone(), _tone(), isdigit, read_byte(), and read_word().

Referenced by play(), and play_P().

    {
        // Absolutely no error checking in here

        byte default_dur = 4;
        byte default_oct = 6;
        int bpm = 63;
        int num;
        long wholenote;
        long duration;
        byte note;
        byte scale;

        // format: d=N,o=N,b=NNN:
        // find the start (skip name, etc)

        while (read_byte(p, pgm) != ':')
            p++; // ignore name
        p++; // skip ':'

        // get default duration
        if (read_byte(p, pgm) == 'd')
        {
            p++;
            p++; // skip "d="
            num = 0;
            while (isdigit(read_byte(p, pgm)))
            {
                num = (num * 10) + (read_byte(p, pgm) - '0');
                p++;
            }
            if (num > 0)
                default_dur = num;
            p++; // skip comma
        }

        // get default octave
        if (read_byte(p, pgm) == 'o')
        {
            p++;
            p++; // skip "o="
            num = read_byte(p, pgm) - '0';
            p++;
            if (num >= 3 && num <= 7)
                default_oct = num;
            p++; // skip comma
        }

        // get BPM
        if (read_byte(p, pgm) == 'b')
        {
            p++;
            p++; // skip "b="
            num = 0;
            while (isdigit(read_byte(p, pgm)))
            {
                num = (num * 10) + (read_byte(p, pgm) - '0');
                p++;
            }
            bpm = num;
            p++; // skip colon
        }

        // BPM usually expresses the number of quarter notes per minute
        wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in milliseconds)

        // now begin note loop
        while (read_byte(p, pgm))
        {
            // first, get note duration, if available
            num = 0;
            while (isdigit(read_byte(p, pgm)))
            {
                num = (num * 10) + (read_byte(p, pgm) - '0');
                p++;
            }

            if (num)
                duration = wholenote / num;
            else
                duration = wholenote / default_dur; // we will need to check if we are a dotted note after

            // now get the note
            note = 0;

            switch (read_byte(p, pgm))
            {
            case 'c':
                note = 1;
                break;
            case 'd':
                note = 3;
                break;
            case 'e':
                note = 5;
                break;
            case 'f':
                note = 6;
                break;
            case 'g':
                note = 8;
                break;
            case 'a':
                note = 10;
                break;
            case 'b':
                note = 12;
                break;
            case 'p':
            default:
                note = 0;
            }
            p++;

            // now, get optional '#' sharp
            if (read_byte(p, pgm) == '#')
            {
                note++;
                p++;
            }

            // now, get optional '.' dotted note
            if (read_byte(p, pgm) == '.')
            {
                duration += duration / 2;
                p++;
            }

            // now, get scale
            if (isdigit(read_byte(p, pgm)))
            {
                scale = read_byte(p, pgm) - '0';
                p++;
            }
            else
            {
                scale = default_oct;
            }

            scale += octave_offset;

            if (read_byte(p, pgm) == ',')
                p++; // skip comma for next note (or we may be at the end)

            // now play the note

            if (note)
            {
                _tone(read_word(&notes[(scale - 4) * 12 + note], pgm));
                delay(duration);
                _noTone();
            }
            else
            {
                delay(duration);
            }
        }
    }
void Rtttl::_tone ( uint16_t  freq) [inline]

Definition at line 146 of file rtttl.h.

Referenced by _play().

    {
        //      Serial.println(freq);
        tone(this->_pinSpk, freq);
    }
void Rtttl::begin ( uint8_t  tonePin) [inline]

Definition at line 121 of file rtttl.h.

    {
        this->_pinSpk = tonePin;
#ifdef _Tone_h
        //      Serial.println("begin");
        //      Serial.println(tonePin, DEC);
        this->m_tone.begin(tonePin);
        //      Serial.println("begin");
#endif

    }
void Rtttl::play ( const char *  p,
uint8_t  octave_offset = 0 
) [inline]

Definition at line 163 of file rtttl.h.

References _play().

    {
        _play(p, octave_offset, false);
    }
void Rtttl::play_P ( const prog_char *  p,
uint8_t  octave_offset = 0 
) [inline]

Definition at line 159 of file rtttl.h.

References _play().

    {
        _play(p, octave_offset, true);
    }

The documentation for this class was generated from the following file:
 All Classes Files Functions Variables Defines