-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75f9279
commit 8e4c3f0
Showing
6 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# VolumeControl | ||
|
||
This library provides a 2-source non-block volume control for DFPlayer mini mp3 module. | ||
|
||
## Usage | ||
Download ZIP; open `Sketch` menu and then `Include Library` -> `Manage Libraries` -> `Add .ZIP Library`; | ||
`Sketch` -> `Include Library` -> `Manage Libraries` -> `VolumeControl` | ||
> [Libraries guide](https://www.arduino.cc/en/Guide/Libraries) | ||
```ino | ||
#include <VolumeControl.h> | ||
``` | ||
You must declare function with your own setter | ||
```ino | ||
void setVolume(int volume) | ||
``` | ||
and pass it to constructor as link. | ||
This function will be called if volume is changed. | ||
In main `loop()` call | ||
`void update(unsigned long currentMillis)` | ||
to check changes and update volume (each `timeInterval` mills) | ||
See example below. | ||
## Использование | ||
Загрузите ZIP, после чего добавьте через меню `Скетч` -> `Подключить библиотеку` -> `Добавить ZIP библиотеку`; | ||
`Скетч` -> `Подключить библиотеку` -> `VolumeControl` | ||
```ino | ||
#include <VolumeControl.h> | ||
``` | ||
Объявите функцию регулировки громкости | ||
```ino | ||
void setVolume(int volume) | ||
``` | ||
и передайте её в конструктор по ссылке. | ||
При изменении громкости эта функция будет вызвана. | ||
В цикле `loop()` вызывайте `void update(unsigned long currentMillis)` | ||
для проверки изменений громкости (каждые `timeInterval`) и установки громкости | ||
## Example / Пример | ||
```ino | ||
#include <VolumeControl.h> | ||
#define VOL A0 | ||
void setVolume(int volume) { | ||
Serial.println((String)"Volume set to " + volume); | ||
mp3_set_volume(volume); | ||
} | ||
VolumeControl vc(VOL, 500, &setVolume); | ||
... | ||
void setup() { | ||
... | ||
} | ||
... | ||
void loop() { | ||
vc.update(millis()); | ||
... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2017 BSCheshir * | ||
* * | ||
* VolumeControl, This library provides a 2-source non-block volume control * | ||
* for DFPlayer mini mp3 module. * | ||
* www.github.com/bscheshir/VolumeControl (github as default source provider) * | ||
* * | ||
* This file is part of the VolumeControl library. * | ||
* * | ||
* VolumeControl is free software: you can redistribute it and/or * | ||
* modify it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation, either version 3 of * | ||
* the License, or any later version. * | ||
* * | ||
* VolumeControl is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU Lesser General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU Lesser General Public * | ||
* License along with VolumeControl. If not, see * | ||
* <http://www.gnu.org/licenses/>. * | ||
* * | ||
******************************************************************************/ | ||
|
||
/* | ||
* name: VolumeControl | ||
* version: 1.0 | ||
* Author: BSCheshir <[email protected]> | ||
* Date: 2017-10-05 | ||
* Description: 2-source non-block volume control library for DFPlayer mini board | ||
*/ | ||
|
||
|
||
#include "VolumeControl.h" //include the declaration for this class | ||
|
||
VolumeControl::VolumeControl(int analogPin, long timeInterval, callbackFunction f) { | ||
pin = analogPin; | ||
checkTime = timeInterval; | ||
func = f; | ||
hardwareRead(); | ||
currentVolumeHardware = targetVolumeHardware; | ||
currentVolumeSoftware = targetVolumeHardware; | ||
targetVolumeSoftware = targetVolumeHardware; | ||
currentVolume = targetVolumeHardware; | ||
prevMillis = 0; | ||
} | ||
|
||
void VolumeControl::update(unsigned long currentMillis) { | ||
if ((currentMillis - prevMillis >= checkTime)) | ||
{ | ||
prevMillis = currentMillis; // запоминаем момент времени | ||
|
||
bool changed = false; | ||
hardwareRead(); | ||
if (targetVolumeHardware != currentVolumeHardware) { | ||
currentVolumeHardware = targetVolumeHardware; | ||
currentVolume = currentVolumeHardware; | ||
changed = true; | ||
} | ||
else if (targetVolumeSoftware != currentVolumeSoftware) { | ||
currentVolumeSoftware = targetVolumeSoftware; | ||
currentVolume = currentVolumeSoftware; | ||
changed = true; | ||
} | ||
if (changed) { | ||
// вызов внешней функции | ||
func(currentVolume); | ||
} | ||
} | ||
} | ||
|
||
|
||
void VolumeControl::hardwareRead() { | ||
// http://arduino.ru/Reference/Map | ||
int val = analogRead(pin); | ||
targetVolumeHardware = constrain(map(val, 0, VOLUME_CONTROL_HARDWARE_MAX, 0, VOLUME_CONTROL_COMMON_MAX), 0, VOLUME_CONTROL_COMMON_MAX); | ||
} | ||
|
||
void VolumeControl::softwareSet(int val) { | ||
targetVolumeSoftware = constrain(map(val, 0, VOLUME_CONTROL_SOFTWARE_MAX, 0, VOLUME_CONTROL_COMMON_MAX), 0, VOLUME_CONTROL_COMMON_MAX); | ||
} | ||
|
||
int VolumeControl::current() { | ||
return currentVolume; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2017 BSCheshir * | ||
* * | ||
* VolumeControl, This library provides a 2-source non-block volume control * | ||
* for DFPlayer mini mp3 module. * | ||
* www.github.com/bscheshir/VolumeControl (github as default source provider) * | ||
* * | ||
* This file is part of the VolumeControl library. * | ||
* * | ||
* VolumeControl is free software: you can redistribute it and/or * | ||
* modify it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation, either version 3 of * | ||
* the License, or any later version. * | ||
* * | ||
* VolumeControl is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU Lesser General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU Lesser General Public * | ||
* License along with VolumeControl. If not, see * | ||
* <http://www.gnu.org/licenses/>. * | ||
* * | ||
******************************************************************************/ | ||
|
||
/* | ||
* name: VolumeControl | ||
* version: 1.0 | ||
* Author: BSCheshir <[email protected]> | ||
* Date: 2017-10-05 | ||
* Description: 2-source non-block volume control library for DFPlayer mini board | ||
*/ | ||
|
||
|
||
/* | ||
* Download ZIP; open `Sketch` menu and then `Include Library` -> `Manage Libraries` -> `Add .ZIP Library`; | ||
* `Sketch` -> `Include Library` -> `Manage Libraries` -> `VolumeControl` | ||
* #include <VolumeControl.h> | ||
* You must declare function with your own setter | ||
* void setVolume(int volume) | ||
* and pass it to constructor as link. | ||
* This function will be called if volume is changed | ||
* In main loop() call | ||
* void update(unsigned long currentMillis) | ||
* to check changes and update volume (each timeInterval mills) | ||
* See example below. | ||
* | ||
* Загрузите ZIP, после чего добавьте через меню `Скетч` -> `Подключить библиотеку` -> `Добавить ZIP библиотеку`; | ||
* `Скетч` -> `Подключить библиотеку` -> `VolumeControl` | ||
* #include <VolumeControl.h> | ||
* Объявите функцию регулировки громкости | ||
* void setVolume(int volume) | ||
* и передайте её в конструктор по ссылке. | ||
* При изменении громкости эта функция будет вызвана. | ||
* В цикле loop() вызывайте | ||
* void update(unsigned long currentMillis) | ||
* для проверки изменений громкости (каждые timeInterval) и установки громкости | ||
* | ||
* Example / Пример | ||
#include <VolumeControl.h> | ||
#define VOL A0 | ||
void setVolume(int volume) { | ||
Serial.println((String)"Volume set to " + volume); | ||
mp3_set_volume(volume); | ||
} | ||
VolumeControl vc(VOL, 500, &setVolume); | ||
... | ||
void setup() { | ||
... | ||
} | ||
... | ||
void loop() { | ||
vc.update(millis()); | ||
... | ||
} | ||
*/ | ||
|
||
#ifndef VolumeControl_h | ||
#define VolumeControl_h | ||
|
||
#include <Arduino.h> | ||
|
||
#define VOLUME_CONTROL_SOFTWARE_MAX 30 // Максимальное значение громкости при установке програмным путём | ||
#define VOLUME_CONTROL_HARDWARE_MAX 1023 // Максимальное значение громкости при установке резистором (AnalogRead) | ||
#define VOLUME_CONTROL_COMMON_MAX 30 // Максимальное значение громкости регулируемого устройства (максимально возможное для пердачи в функцию обратного вызова) | ||
|
||
class VolumeControl { | ||
public: | ||
typedef void (*callbackFunction)(int); | ||
VolumeControl(int analogPin, long timeInterval, callbackFunction f); | ||
void update(unsigned long currentMillis); // Проверить необходимость обновления громкости ("неблокирующая" функция), обновить, установить в случае изменений | ||
void hardwareRead(); // Прочитать значение громкости из аппаратной части | ||
void softwareSet(int val); // Установить значение громкости как "програмное" | ||
int current(); // Получить текущее значение приведённой громкости | ||
private: | ||
callbackFunction func; | ||
int pin;//потенциометр | ||
int currentVolume;//текущее значение громкости после всех преобразований | ||
int currentVolumeHardware;// текущее значение громкости для резистора | ||
int currentVolumeSoftware;// текущее значение громкости для програмной регулировки | ||
int targetVolumeHardware;// целевое значение громкости резистора | ||
int targetVolumeSoftware;// целевое значение громкости програмной регулировки | ||
long checkTime; | ||
unsigned long prevMillis; // последний момент смены состояния | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
####################################### | ||
# Syntax Coloring Map For VolumeControl | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
callbackFunction KEYWORD1 callbackFunction | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
update KEYWORD2 | ||
hardwareRead KEYWORD2 | ||
softwareSet KEYWORD2 | ||
current KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|
||
VOLUME_CONTROL_SOFTWARE_MAX LITERAL1 | ||
VOLUME_CONTROL_HARDWARE_MAX LITERAL1 | ||
VOLUME_CONTROL_COMMON_MAX LITERAL1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2017 BSCheshir * | ||
* * | ||
* VolumeControl, This library provides a 2-source non-block volume control * | ||
* for DFPlayer mini mp3 module. * | ||
* www.github.com/bscheshir/VolumeControl (github as default source provider) * | ||
* * | ||
* This file is part of the VolumeControl library. * | ||
* * | ||
* VolumeControl is free software: you can redistribute it and/or * | ||
* modify it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation, either version 3 of * | ||
* the License, or any later version. * | ||
* * | ||
* VolumeControl is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU Lesser General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU Lesser General Public * | ||
* License along with VolumeControl. If not, see * | ||
* <http://www.gnu.org/licenses/>. * | ||
* * | ||
******************************************************************************/ | ||
|
||
/* | ||
* name: VolumeControl | ||
* version: 1.0 | ||
* Author: BSCheshir <[email protected]> | ||
* Date: 2017-10-05 | ||
* Description: 2-source non-block volume control library for DFPlayer mini board | ||
*/ | ||
|
||
|
||
#include "VolumeControl.h" //include the declaration for this class | ||
|
||
VolumeControl::VolumeControl(int analogPin, long timeInterval, callbackFunction f) { | ||
pin = analogPin; | ||
checkTime = timeInterval; | ||
func = f; | ||
hardwareRead(); | ||
currentVolumeHardware = targetVolumeHardware; | ||
currentVolumeSoftware = targetVolumeHardware; | ||
targetVolumeSoftware = targetVolumeHardware; | ||
currentVolume = targetVolumeHardware; | ||
prevMillis = 0; | ||
} | ||
|
||
void VolumeControl::update(unsigned long currentMillis) { | ||
if ((currentMillis - prevMillis >= checkTime)) | ||
{ | ||
prevMillis = currentMillis; // запоминаем момент времени | ||
|
||
bool changed = false; | ||
hardwareRead(); | ||
if (targetVolumeHardware != currentVolumeHardware) { | ||
currentVolumeHardware = targetVolumeHardware; | ||
currentVolume = currentVolumeHardware; | ||
changed = true; | ||
} | ||
else if (targetVolumeSoftware != currentVolumeSoftware) { | ||
currentVolumeSoftware = targetVolumeSoftware; | ||
currentVolume = currentVolumeSoftware; | ||
changed = true; | ||
} | ||
if (changed) { | ||
// вызов внешней функции | ||
func(currentVolume); | ||
} | ||
} | ||
} | ||
|
||
|
||
void VolumeControl::hardwareRead() { | ||
// http://arduino.ru/Reference/Map | ||
int val = analogRead(pin); | ||
targetVolumeHardware = constrain(map(val, 0, VOLUME_CONTROL_HARDWARE_MAX, 0, VOLUME_CONTROL_COMMON_MAX), 0, VOLUME_CONTROL_COMMON_MAX); | ||
} | ||
|
||
void VolumeControl::softwareSet(int val) { | ||
targetVolumeSoftware = constrain(map(val, 0, VOLUME_CONTROL_SOFTWARE_MAX, 0, VOLUME_CONTROL_COMMON_MAX), 0, VOLUME_CONTROL_COMMON_MAX); | ||
} | ||
|
||
int VolumeControl::current() { | ||
return currentVolume; | ||
} | ||
|
Oops, something went wrong.