Skip to content

Commit

Permalink
1️⃣
Browse files Browse the repository at this point in the history
  • Loading branch information
bscheshirwork committed Oct 6, 2017
1 parent 75f9279 commit 8e4c3f0
Show file tree
Hide file tree
Showing 6 changed files with 491 additions and 0 deletions.
65 changes: 65 additions & 0 deletions README.md
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());
...
}
```
87 changes: 87 additions & 0 deletions VolumeControl.cpp
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;
}

113 changes: 113 additions & 0 deletions VolumeControl.h
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
26 changes: 26 additions & 0 deletions keywords.txt
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
87 changes: 87 additions & 0 deletions src/VolumeControl.cpp
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;
}

Loading

0 comments on commit 8e4c3f0

Please sign in to comment.