diff --git a/README.md b/README.md new file mode 100644 index 0000000..1e47ce5 --- /dev/null +++ b/README.md @@ -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 +``` +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 +``` +Объявите функцию регулировки громкости +```ino +void setVolume(int volume) +``` +и передайте её в конструктор по ссылке. +При изменении громкости эта функция будет вызвана. + +В цикле `loop()` вызывайте `void update(unsigned long currentMillis)` +для проверки изменений громкости (каждые `timeInterval`) и установки громкости + +## Example / Пример + +```ino +#include + +#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()); + ... +} +``` diff --git a/VolumeControl.cpp b/VolumeControl.cpp new file mode 100644 index 0000000..4431044 --- /dev/null +++ b/VolumeControl.cpp @@ -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 * + * . * + * * + ******************************************************************************/ + + /* + * name: VolumeControl + * version: 1.0 + * Author: BSCheshir + * 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; +} + diff --git a/VolumeControl.h b/VolumeControl.h new file mode 100644 index 0000000..a8160ef --- /dev/null +++ b/VolumeControl.h @@ -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 * + * . * + * * + ******************************************************************************/ + + /* + * name: VolumeControl + * version: 1.0 + * Author: BSCheshir + * 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 +* 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 +* Объявите функцию регулировки громкости +* void setVolume(int volume) +* и передайте её в конструктор по ссылке. +* При изменении громкости эта функция будет вызвана. +* В цикле loop() вызывайте +* void update(unsigned long currentMillis) +* для проверки изменений громкости (каждые timeInterval) и установки громкости +* +* Example / Пример + +#include + +#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 + +#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 \ No newline at end of file diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..82754d5 --- /dev/null +++ b/keywords.txt @@ -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 diff --git a/src/VolumeControl.cpp b/src/VolumeControl.cpp new file mode 100644 index 0000000..4431044 --- /dev/null +++ b/src/VolumeControl.cpp @@ -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 * + * . * + * * + ******************************************************************************/ + + /* + * name: VolumeControl + * version: 1.0 + * Author: BSCheshir + * 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; +} + diff --git a/src/VolumeControl.h b/src/VolumeControl.h new file mode 100644 index 0000000..a8160ef --- /dev/null +++ b/src/VolumeControl.h @@ -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 * + * . * + * * + ******************************************************************************/ + + /* + * name: VolumeControl + * version: 1.0 + * Author: BSCheshir + * 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 +* 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 +* Объявите функцию регулировки громкости +* void setVolume(int volume) +* и передайте её в конструктор по ссылке. +* При изменении громкости эта функция будет вызвана. +* В цикле loop() вызывайте +* void update(unsigned long currentMillis) +* для проверки изменений громкости (каждые timeInterval) и установки громкости +* +* Example / Пример + +#include + +#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 + +#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 \ No newline at end of file