Home | History | Annotate | Download | only in buzzer
      1 /*
      2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha (at) intel.com>
      3  * Copyright (c) 2014 Intel Corporation.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 #pragma once
     25 
     26 #include <string>
     27 #include <mraa/pwm.h>
     28 
     29 #define  DO     3300    // 261 Hz 3830
     30 #define  RE     2930    // 294 Hz
     31 #define  MI     2600    // 329 Hz
     32 #define  FA     2460    // 349 Hz
     33 #define  SOL    2190    // 392 Hz
     34 #define  LA     1960    // 440 Hz
     35 #define  SI     1750    // 493 Hz
     36 
     37 namespace upm {
     38 
     39 /**
     40  * @brief Buzzer library
     41  * @defgroup buzzer libupm-buzzer
     42  * @ingroup seeed pwm sound gsk
     43  */
     44 
     45 /**
     46  * @library buzzer
     47  * @sensor buzzer
     48  * @comname Grove Buzzer
     49  * @type sound
     50  * @man seeed
     51  * @con pwm
     52  * @kit gsk
     53  *
     54  * @brief API for the Buzzer component
     55  *
     56  * This module defines the Buzzer interface for libbuzzer.
     57  * This sensor can make different tones when connected to
     58  * a pin capable of analog pulse-width modulation. It emits
     59  * sound using a piezoelectric material that vibrates at different
     60  * frequencies based on the input voltage.
     61  *
     62  * @image html buzzer.jpg
     63  * @snippet buzzer-sound.cxx Interesting
     64  */
     65 class Buzzer {
     66     public:
     67         /**
     68          * Instantiates a Buzzer object.
     69          *
     70          * @param pinNumber Buzzer pin number
     71          */
     72         Buzzer (int pinNumber);
     73 
     74         /**
     75          * Buzzer object destructor.
     76          */
     77         ~Buzzer ();
     78 
     79         /**
     80          * Plays a tone for a certain amount of time or indefinitely. When delay
     81          * is not used, the sound can be stopped by calling stopSound().
     82          *
     83          * @param note Note to play (C, D, E, etc.) or frequency
     84          * @param delay Time in microseconds for which to play the sound; if the value is
     85          * 0, the sound is played indefinitely
     86          *
     87          * @return Note played
     88          */
     89         int playSound (int note, int delay);
     90 
     91         /**
     92          * Stops the sound currently playing. Should be called when playSound()
     93          * does not have a delay value.
     94          */
     95         void stopSound();
     96 
     97         /**
     98          * Sets the volume for the buzzer, but may affect the sound timbre.
     99          * Works best with halved values; e.g., 1.0, 0.5, 0.25, etc.
    100          *
    101          * @param vol Value to set the volume to, from 0.0 to 1.0
    102          */
    103         void setVolume(float vol);
    104 
    105         /**
    106          * Gets the buzzer volume.
    107          *
    108          * @return Value the volume was set to
    109          */
    110         float getVolume();
    111 
    112         /**
    113          * Returns the name of the sensor.
    114          *
    115          * @return Name of the sensor
    116          */
    117         std::string name()
    118         {
    119             return m_name;
    120         }
    121     protected:
    122         std::string m_name;
    123     private:
    124         mraa_pwm_context m_pwm_context;
    125         float m_volume;
    126 };
    127 }
    128