Skip to content

Commit

Permalink
Merge pull request #1 from ghostintranslation/potentiometer-callbacks
Browse files Browse the repository at this point in the history
Potentiometer callbacks
  • Loading branch information
ghostintranslation committed Aug 23, 2020
2 parents 37aaaa7 + e113b0e commit 0e6821a
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions Motherboard9/Motherboard9.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
* Motherboard9
* v1.1.0
* v1.1.1
*/
class Motherboard9{

Expand All @@ -26,6 +26,7 @@ class Motherboard9{
bool *buttons;
// Potentiometers
unsigned int *potentiometers;
unsigned int *potentiometersPrevious;
// For smoothing purposes
unsigned int *potentiometersTemp;
byte *potentiometersReadings;
Expand Down Expand Up @@ -91,6 +92,8 @@ class Motherboard9{
LongPressUpCallback *inputsLongPressUpCallback;
elapsedMillis *inputsPressTime;
bool *inputsLongPressDownFired;
using PotentiometerChangeCallback = void (*)(byte, unsigned int, int);
PotentiometerChangeCallback *inputsPotentiometerChangeCallback;
using RotaryChangeCallback = void (*)(bool);
RotaryChangeCallback *inputsRotaryChangeCallback;

Expand Down Expand Up @@ -118,7 +121,7 @@ class Motherboard9{
void update();
void setLED(byte ledIndex, byte ledStatus);
void setAllLED(unsigned int binary, byte ledStatus);
void toggleLED(byte index) ;
void toggleLED(byte index);
void resetAllLED();
int getInput(byte index);
bool getEncoderSwitch(byte index);
Expand All @@ -131,6 +134,7 @@ class Motherboard9{
void setHandleLongPressDown(byte inputIndex, LongPressDownCallback fptr);
void setHandlePressUp(byte inputIndex, PressUpCallback fptr);
void setHandleLongPressUp(byte inputIndex, LongPressUpCallback fptr);
void setHandlePotentiometerChange(byte inputIndex, PotentiometerChangeCallback fptr);
void setHandleRotaryChange(byte inputIndex, RotaryChangeCallback fptr);
};

Expand All @@ -148,6 +152,7 @@ inline Motherboard9::Motherboard9(){
this->ledsDuration = new unsigned int[this->ioNumber];
this->buttons = new bool[this->ioNumber];
this->potentiometers = new unsigned int[this->ioNumber];
this->potentiometersPrevious = new unsigned int[this->ioNumber];
this->potentiometersTemp = new unsigned int[this->ioNumber];
this->potentiometersReadings = new byte[this->ioNumber];
this->encoders = new int[this->ioNumber];
Expand All @@ -159,6 +164,7 @@ inline Motherboard9::Motherboard9(){
this->inputsLongPressUpCallback = new PressUpCallback[this->ioNumber];
this->inputsPressTime = new elapsedMillis[this->ioNumber];
this->inputsLongPressDownFired = new bool[this->ioNumber];
this->inputsPotentiometerChangeCallback = new PotentiometerChangeCallback[this->ioNumber];
this->inputsRotaryChangeCallback = new RotaryChangeCallback[this->ioNumber];

for(byte i = 0; i < this->ioNumber; i++){
Expand All @@ -167,6 +173,7 @@ inline Motherboard9::Motherboard9(){
this->ledsDuration[i] = 0;
this->buttons[i] = true;
this->potentiometers[i] = 0;
this->potentiometersPrevious[i] = 0;
this->potentiometersTemp[i] = 0;
this->potentiometersReadings[i] = 0;
this->encoders[i] = 0;
Expand All @@ -178,6 +185,7 @@ inline Motherboard9::Motherboard9(){
this->inputsLongPressUpCallback[i] = nullptr;
this->inputsPressTime[i] = 0;
this->inputsLongPressDownFired[i] = false;
this->inputsPotentiometerChangeCallback[i] = nullptr;
this->inputsRotaryChangeCallback[i] = nullptr;
}

Expand Down Expand Up @@ -566,13 +574,22 @@ inline void Motherboard9::readPotentiometer(byte inputIndex) {

this->potentiometersReadings[inputIndex] = this->potentiometersReadings[inputIndex] + 1;
this->potentiometersTemp[inputIndex] += analogRead(22);

if (this->potentiometersReadings[inputIndex] == 255) {
this->potentiometers[inputIndex] = this->potentiometersTemp[inputIndex] / 255;
if(this->potentiometersReadings[inputIndex] == 255){
this->potentiometers[inputIndex] = this->potentiometersTemp[inputIndex] / 255;
this->potentiometers[inputIndex] = map(this->potentiometers[inputIndex], this->getAnalogMinValue(), this->getAnalogMaxValue(), 0, 1023);
this->potentiometers[inputIndex] = constrain(this->potentiometers[inputIndex], (unsigned int)0, (unsigned int)1023);

if(this->potentiometers[inputIndex] != this->potentiometersPrevious[inputIndex]){
// Calling the potentiometer callback if there is one
if(this->inputsPotentiometerChangeCallback[inputIndex] != nullptr){
this->inputsPotentiometerChangeCallback[inputIndex](inputIndex, this->potentiometers[inputIndex], this->potentiometers[inputIndex] - this->potentiometersPrevious[inputIndex] );
}
}

this->potentiometersReadings[inputIndex] = 0;
this->potentiometersTemp[inputIndex] = 0;
this->potentiometersPrevious[inputIndex] = this->potentiometers[inputIndex];
}

}
Expand Down Expand Up @@ -893,6 +910,16 @@ inline void Motherboard9::setHandleLongPressUp(byte inputIndex, LongPressUpCallb
}
}

/**
* Handle potentiometer
*/
inline void Motherboard9::setHandlePotentiometerChange(byte inputIndex, PotentiometerChangeCallback fptr){
// Only for rotaries
if(this->inputs[inputIndex] == 2){
this->inputsPotentiometerChangeCallback[inputIndex] = fptr;
}
}

/**
* Handle rotary
*/
Expand Down

0 comments on commit 0e6821a

Please sign in to comment.