PWMポートを使ってビープを鳴らすライブラリです。周波数指定、単純なON/OFF、ワンショット、指定回数の繰り返しに対応しています。(It is a library that sounds a beep using the PWM port. It supports frequency specification, simple ON / OFF, one shot, and specified number of repetitions.)

Dependents:   PwmBeep_hello DLC_STARTER

Committer:
hmizuno
Date:
Fri May 22 08:33:18 2020 +0000
Revision:
0:c6220b0517a5
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hmizuno 0:c6220b0517a5 1 #ifndef PWMBEEP_H
hmizuno 0:c6220b0517a5 2 #define PWMBEEP_H
hmizuno 0:c6220b0517a5 3
hmizuno 0:c6220b0517a5 4 #include "mbed.h"
hmizuno 0:c6220b0517a5 5
hmizuno 0:c6220b0517a5 6 /** PwmBeep class.
hmizuno 0:c6220b0517a5 7 * PwmOutを使用してビープを鳴らすライブラリです。(A library that generate a beep using PwmOut.)
hmizuno 0:c6220b0517a5 8 */
hmizuno 0:c6220b0517a5 9 class PwmBeep{
hmizuno 0:c6220b0517a5 10 public:
hmizuno 0:c6220b0517a5 11 /**
hmizuno 0:c6220b0517a5 12 * @param pin 使用するPwmOutポート (PwmOut port to use)
hmizuno 0:c6220b0517a5 13 * @param initialfreq 周波数(Hz) (Frequency(Hz))
hmizuno 0:c6220b0517a5 14 *
hmizuno 0:c6220b0517a5 15 * 周波数はsetFreq()により後で変更できます。(The frequency can be changed later with setFreq().)
hmizuno 0:c6220b0517a5 16 */
hmizuno 0:c6220b0517a5 17 PwmBeep(PinName pin, int initialfreq = 1000);
hmizuno 0:c6220b0517a5 18
hmizuno 0:c6220b0517a5 19 /**
hmizuno 0:c6220b0517a5 20 * @brief ビープをオフします。(Turn off a beep.)
hmizuno 0:c6220b0517a5 21 */
hmizuno 0:c6220b0517a5 22 void turnOff();
hmizuno 0:c6220b0517a5 23
hmizuno 0:c6220b0517a5 24 /**
hmizuno 0:c6220b0517a5 25 * @brief ビープをオンします。(Turn on a beep.)
hmizuno 0:c6220b0517a5 26 */
hmizuno 0:c6220b0517a5 27 void turnOn();
hmizuno 0:c6220b0517a5 28
hmizuno 0:c6220b0517a5 29 /**
hmizuno 0:c6220b0517a5 30 * @brief 一度だけ指定時間鳴らします。内部でタイムアウト割り込みを使用しており、鳴らしながら他の処理ができます。ボタン操作音用です。
hmizuno 0:c6220b0517a5 31 * (Sounds once for a specified time. The timeout interrupt is used internally, and other processing can be done while sounding.For button touch sounds.)
hmizuno 0:c6220b0517a5 32 * @param time 鳴らす時間(秒) (Duration time(sec))
hmizuno 0:c6220b0517a5 33 */
hmizuno 0:c6220b0517a5 34 void oneshotOn(float time);
hmizuno 0:c6220b0517a5 35
hmizuno 0:c6220b0517a5 36 /**
hmizuno 0:c6220b0517a5 37 * @brief 指定回数繰り返し鳴らします。内部でタイマー割り込みを使用しており、鳴らしながら他の処理ができます。
hmizuno 0:c6220b0517a5 38 * (Repeat the specified number of times. The timer interrupt is used internally, and other processing can be done while sounding.)
hmizuno 0:c6220b0517a5 39 * @param num 繰り返し回数 (Number of repetitions)
hmizuno 0:c6220b0517a5 40 * @param ontime 一周期中のON時間(秒) (On time in one cycle(sec))
hmizuno 0:c6220b0517a5 41 * @param offtime 一周期中のoff時間(秒) (Off time in one cycle(sec))
hmizuno 0:c6220b0517a5 42 *
hmizuno 0:c6220b0517a5 43 * ※鳴動回数でエラーコードを示すなど確実に指定回数鳴らしたい場面では、NshotOnwithWait の使用を検討ください。
hmizuno 0:c6220b0517a5 44 * (Consider using NshotOnwithWait in situations where you want to be sure to sound the specified number of times,
hmizuno 0:c6220b0517a5 45 * such as showing an error code by the number of times you ring.)
hmizuno 0:c6220b0517a5 46 */
hmizuno 0:c6220b0517a5 47 void NshotOn(int num,float ontime,float offtime);
hmizuno 0:c6220b0517a5 48
hmizuno 0:c6220b0517a5 49 /**
hmizuno 0:c6220b0517a5 50 * @brief 指定回数繰り返し鳴らします。内部でwait()関数を使用しており、指定数鳴らし終わるまで他の処理を止めます。
hmizuno 0:c6220b0517a5 51 * (Repeat the specified number of times. The wait() function is used internally, and other processing is stopped until the specified number of sounds have been played.)
hmizuno 0:c6220b0517a5 52 * @param num 繰り返し回数 (Number of repetitions)
hmizuno 0:c6220b0517a5 53 * @param ontime 一周期中のON時間(秒) (On time in one cycle(秒))
hmizuno 0:c6220b0517a5 54 * @param offtime 一周期中のoff時間(秒) (Off time in one cycle(秒))
hmizuno 0:c6220b0517a5 55 *
hmizuno 0:c6220b0517a5 56 * 途中でビープを操作する割り込み処理がかからないよう、必要に応じて割り込みの禁止・許可を外部で設定してください。
hmizuno 0:c6220b0517a5 57 * (If necessary, disable or enable interrupts externally so that interrupt processing that operates the beep does not occur during the process.)
hmizuno 0:c6220b0517a5 58 */
hmizuno 0:c6220b0517a5 59 void NshotOnwithWait(int num,float ontime,float offtime);
hmizuno 0:c6220b0517a5 60
hmizuno 0:c6220b0517a5 61 /**
hmizuno 0:c6220b0517a5 62 * @brief ビープの周波数を設定します。(Set frequency of beep.)
hmizuno 0:c6220b0517a5 63 * @param freq 周波数(Hz) (Frequency(Hz))
hmizuno 0:c6220b0517a5 64 */
hmizuno 0:c6220b0517a5 65 void setFreq(int freq);
hmizuno 0:c6220b0517a5 66
hmizuno 0:c6220b0517a5 67 private:
hmizuno 0:c6220b0517a5 68 PwmOut _pwmbeep;
hmizuno 0:c6220b0517a5 69 Timeout timeout;
hmizuno 0:c6220b0517a5 70 int counter;
hmizuno 0:c6220b0517a5 71 int repeat_count;
hmizuno 0:c6220b0517a5 72 float repeat_ontime;
hmizuno 0:c6220b0517a5 73 float repeat_offtime;
hmizuno 0:c6220b0517a5 74
hmizuno 0:c6220b0517a5 75 void turnOn_and_setOffTimer();
hmizuno 0:c6220b0517a5 76 void turnOff_and_setOnTimer();
hmizuno 0:c6220b0517a5 77 };
hmizuno 0:c6220b0517a5 78
hmizuno 0:c6220b0517a5 79 #endif