Daiki Kato / PwmOutSpeaker
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PwmOutSpeaker.h Source File

PwmOutSpeaker.h

00001 /* mbed PwmOutSpeaker Library
00002  * Copyright (C) 2016 dkato
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef PWMOUT_SPEAKER_H
00018 #define PWMOUT_SPEAKER_H
00019 
00020 #include "mbed.h"
00021 #include "AUDIO_RBSP.h"
00022 
00023 /** PwmOutSpeaker class
00024 *
00025 */
00026 class PwmOutSpeaker : public AUDIO_RBSP {
00027 public:
00028     /** Create a PwmOutSpeaker
00029      * 
00030      * @param pwm_l  
00031      * @param pwm_r  
00032      */
00033     PwmOutSpeaker(PinName pwm_l, PinName pwm_r);
00034 
00035     virtual void power(bool type = true) {
00036         return;
00037     }
00038 
00039     /** Set I2S interface bit length and mode
00040      *
00041      * @param length Set bit length to 8 or 16 bits
00042      * @return true = success, false = failure
00043      */
00044     virtual bool format(char length);
00045 
00046     /** Set sample frequency
00047      *
00048      * @param frequency Sample frequency of data in Hz
00049      * @return true = success, false = failure
00050      * 
00051      * Supports the following frequencies: 8kHz, 8.021kHz, 32kHz, 44.1kHz, 48kHz
00052      * Default is 44.1kHz
00053      */
00054     virtual bool frequency(int hz);
00055 
00056     /** Enqueue asynchronous write request
00057      *
00058      * @param p_data Location of the data
00059      * @param data_size Number of bytes to write
00060      * @return Number of bytes written on success. negative number on error.
00061      */
00062     virtual int write(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf = NULL);
00063 
00064     virtual int read(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf = NULL) {
00065         return -1;
00066     }
00067 
00068     /** Volume control
00069      *
00070      * @param volume Speaker volume
00071      * @return Returns "true" for success, "false" if parameters are out of range
00072      * Parameters accept a value, where 0.0 <= parameter <= 1.0 (1.0 = default)
00073      */
00074     virtual bool outputVolume(float leftVolumeOut, float rightVolumeOut);
00075 
00076     virtual bool micVolume(float VolumeIn) {
00077         return false;
00078     }
00079 
00080 private:
00081     #define WRITE_BUFF_SIZE    (1024 * 4)
00082     #define MSK_RING_BUFF      (WRITE_BUFF_SIZE - 1)
00083 
00084     PwmOut   _speaker_l;
00085     PwmOut   _speaker_r;
00086     Ticker   _timer;
00087     int      _length;
00088     int      _hz_multi;
00089     int      _data_cnt;
00090     bool     _playing;
00091     volatile uint32_t _bottom;
00092     volatile uint32_t _top;
00093     float    _speaker_vol_l;
00094     float    _speaker_vol_r;
00095     float    _pwm_duty_buf[WRITE_BUFF_SIZE];
00096 
00097     void sound_out(void);
00098 };
00099 
00100 #endif // PWMOUT_SPEAKER_H