Skip to content

Commit

Permalink
analog: add configurable lower and upper adc offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
paradajz committed Feb 13, 2022
1 parent 7295285 commit 07c3003
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/application/database/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class Database : public LESSDB
lowerLimit,
upperLimit,
midiChannel,
lowerOffset,
upperOffset,
AMOUNT
};

Expand Down
26 changes: 23 additions & 3 deletions src/application/database/Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ namespace
.address = 0,
},

// lower cc limit
// lower value limit
{
.numberOfParameters = IO::Analog::Collection::size(),
.parameterType = LESSDB::sectionParameterType_t::word,
Expand All @@ -282,7 +282,7 @@ namespace
.address = 0,
},

// upper cc limit
// upper value limit
{
.numberOfParameters = IO::Analog::Collection::size(),
.parameterType = LESSDB::sectionParameterType_t::word,
Expand All @@ -300,7 +300,27 @@ namespace
.defaultValue = 0,
.autoIncrement = false,
.address = 0,
}
},

// lower adc percentage offset
{
.numberOfParameters = IO::Analog::Collection::size(),
.parameterType = LESSDB::sectionParameterType_t::byte,
.preserveOnPartialReset = false,
.defaultValue = 0,
.autoIncrement = false,
.address = 0,
},

// upper adc percentage offset
{
.numberOfParameters = IO::Analog::Collection::size(),
.parameterType = LESSDB::sectionParameterType_t::byte,
.preserveOnPartialReset = false,
.defaultValue = 0,
.autoIncrement = false,
.address = 0,
},
};

LESSDB::section_t ledSections[static_cast<uint8_t>(Database::Section::leds_t::AMOUNT)] = {
Expand Down
8 changes: 6 additions & 2 deletions src/application/io/analog/Analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ void Analog::processReading(size_t index, uint16_t value)

fillAnalogDescriptor(index, analogDescriptor);

filterDescriptor.type = analogDescriptor.type;
filterDescriptor.value = value;
filterDescriptor.type = analogDescriptor.type;
filterDescriptor.value = value;
filterDescriptor.lowerOffset = analogDescriptor.lowerOffset;
filterDescriptor.upperOffset = analogDescriptor.upperOffset;

if (!_filter.isFiltered(index, filterDescriptor))
{
Expand Down Expand Up @@ -372,6 +374,8 @@ void Analog::fillAnalogDescriptor(size_t index, analogDescriptor_t& descriptor)
descriptor.inverted = _database.read(Database::Section::analog_t::invert, index);
descriptor.lowerLimit = _database.read(Database::Section::analog_t::lowerLimit, index);
descriptor.upperLimit = _database.read(Database::Section::analog_t::upperLimit, index);
descriptor.lowerOffset = _database.read(Database::Section::analog_t::lowerOffset, index);
descriptor.upperOffset = _database.read(Database::Section::analog_t::upperOffset, index);
descriptor.dispatchMessage.componentIndex = index;
descriptor.dispatchMessage.midiChannel = _database.read(Database::Section::analog_t::midiChannel, index);
descriptor.dispatchMessage.midiIndex = _database.read(Database::Section::analog_t::midiID, index);
Expand Down
16 changes: 10 additions & 6 deletions src/application/io/analog/Analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ namespace IO

struct descriptor_t
{
Analog::type_t type = Analog::type_t::potentiometerControlChange;
uint16_t value = 0;
Analog::type_t type = Analog::type_t::potentiometerControlChange;
uint16_t value = 0;
uint16_t lowerOffset = 0;
uint16_t upperOffset = 0;
};

virtual bool isFiltered(size_t index, descriptor_t& descriptor) = 0;
Expand All @@ -104,10 +106,12 @@ namespace IO
private:
struct analogDescriptor_t
{
type_t type = type_t::potentiometerControlChange;
bool inverted = false;
uint16_t lowerLimit = 0;
uint16_t upperLimit = 0;
type_t type = type_t::potentiometerControlChange;
bool inverted = false;
uint16_t lowerLimit = 0;
uint16_t upperLimit = 0;
uint8_t lowerOffset = 0;
uint8_t upperOffset = 0;
Util::MessageDispatcher::message_t dispatchMessage;

analogDescriptor_t() = default;
Expand Down
37 changes: 34 additions & 3 deletions src/application/io/analog/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ namespace IO

bool isFiltered(size_t index, descriptor_t& descriptor) override
{
descriptor.value = CONSTRAIN(descriptor.value, _adcConfig.adcMinValue, _adcConfig.adcMaxValue);
const uint32_t adcMinValue = lowerOffsetRaw(descriptor.lowerOffset);
const uint32_t adcMaxValue = upperOffsetRaw(descriptor.upperOffset);

descriptor.value = CONSTRAIN(descriptor.value, adcMinValue, adcMaxValue);

// avoid filtering in this case for faster response
if (descriptor.type == Analog::type_t::button)
Expand Down Expand Up @@ -109,7 +112,7 @@ namespace IO
const bool use14bit = (descriptor.type == Analog::type_t::nrpn14bit) || (descriptor.type == Analog::type_t::pitchBend) || (descriptor.type == Analog::type_t::controlChange14bit);
const uint16_t maxLimit = use14bit ? MIDI::MIDI_14_BIT_VALUE_MAX : MIDI::MIDI_7_BIT_VALUE_MAX;
const bool direction = descriptor.value >= _lastStableValue[index];
const auto oldMIDIvalue = core::misc::mapRange(static_cast<uint32_t>(_lastStableValue[index]), static_cast<uint32_t>(_adcConfig.adcMinValue), static_cast<uint32_t>(_adcConfig.adcMaxValue), static_cast<uint32_t>(0), static_cast<uint32_t>(maxLimit));
const auto oldMIDIvalue = core::misc::mapRange(static_cast<uint32_t>(_lastStableValue[index]), adcMinValue, adcMaxValue, static_cast<uint32_t>(0), static_cast<uint32_t>(maxLimit));
uint16_t stepDiff = 1;

if (((direction != lastStableDirection(index)) || !fastFilter) && ((oldMIDIvalue != 0) && (oldMIDIvalue != maxLimit)))
Expand Down Expand Up @@ -160,7 +163,7 @@ namespace IO
}
#endif

const auto midiValue = core::misc::mapRange(static_cast<uint32_t>(descriptor.value), static_cast<uint32_t>(_adcConfig.adcMinValue), static_cast<uint32_t>(_adcConfig.adcMaxValue), static_cast<uint32_t>(0), static_cast<uint32_t>(maxLimit));
const auto midiValue = core::misc::mapRange(static_cast<uint32_t>(descriptor.value), adcMinValue, adcMaxValue, static_cast<uint32_t>(0), static_cast<uint32_t>(maxLimit));

if (midiValue == oldMIDIvalue)
return false;
Expand Down Expand Up @@ -242,6 +245,34 @@ namespace IO
return BIT_READ(_lastStableDirection[arrayIndex], analogIndex);
}

uint32_t lowerOffsetRaw(uint8_t percentage)
{
// calculate raw adc value based on percentage

if (percentage != 0)
{
return static_cast<double>(_adcConfig.adcMaxValue) * static_cast<double>(percentage / 100.0);
}
else
{
return _adcConfig.adcMinValue;
}
}

uint32_t upperOffsetRaw(uint8_t percentage)
{
// calculate raw adc value based on percentage

if (percentage != 0)
{
return static_cast<double>(_adcConfig.adcMaxValue) - static_cast<double>(_adcConfig.adcMaxValue * static_cast<double>(percentage / 100.0));
}
else
{
return _adcConfig.adcMaxValue;
}
}

adcConfig_t adc10bit = {
.adcMinValue = 10,
.adcMaxValue = 1000,
Expand Down
6 changes: 4 additions & 2 deletions src/application/io/analog/stub/Analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ namespace IO

struct descriptor_t
{
Analog::type_t type = Analog::type_t::potentiometerControlChange;
uint16_t value = 0;
Analog::type_t type = Analog::type_t::potentiometerControlChange;
uint16_t value = 0;
uint16_t lowerOffset = 0;
uint16_t upperOffset = 0;
};

virtual bool isFiltered(size_t index, descriptor_t& descriptor) = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/application/system/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace System
upperLimit,
upperLimit_MSB,
midiChannel,
lowerOffset,
upperOffset,
AMOUNT
};

Expand Down
24 changes: 19 additions & 5 deletions src/application/system/Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,28 +199,28 @@ namespace
127,
},

// lower cc limit, lsb
// lower value limit, lsb
{
IO::Analog::Collection::size(),
0,
16383,
},

// lower cc limit, msb
// lower value limit, msb
{
IO::Analog::Collection::size(),
0,
127,
},

// upper cc limit, lsb
// upper value limit, lsb
{
IO::Analog::Collection::size(),
0,
16383,
},

// upper cc limit, msb
// upper value limit, msb
{
IO::Analog::Collection::size(),
0,
Expand All @@ -232,7 +232,21 @@ namespace
IO::Analog::Collection::size(),
1,
16,
}
},

// lower adc percentage offset
{
IO::Analog::Collection::size(),
0,
100,
},

// upper adc percentage offset
{
IO::Analog::Collection::size(),
0,
100,
},
};

std::vector<SysExConf::Section> ledSections = {
Expand Down
4 changes: 3 additions & 1 deletion src/application/util/conversion/Conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ namespace
Database::Section::analog_t::lowerLimit,
Database::Section::analog_t::upperLimit,
Database::Section::analog_t::upperLimit,
Database::Section::analog_t::midiChannel
Database::Section::analog_t::midiChannel,
Database::Section::analog_t::lowerOffset,
Database::Section::analog_t::upperOffset,
};

const Database::Section::leds_t _sysEx2DB_leds[static_cast<uint8_t>(System::Config::Section::leds_t::AMOUNT)] = {
Expand Down
10 changes: 10 additions & 0 deletions tests/src/database/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ TEST_CASE(ReadInitialValues)
for (int i = 0; i < IO::Analog::Collection::size(); i++)
TEST_ASSERT_EQUAL_UINT32(0, database.read(Database::Section::analog_t::midiChannel, i));

// lower offset section
// all values should be set to 0
for (int i = 0; i < IO::Analog::Collection::size(); i++)
TEST_ASSERT_EQUAL_UINT32(0, database.read(Database::Section::analog_t::lowerOffset, i));

// upper offset section
// all values should be set to 0
for (int i = 0; i < IO::Analog::Collection::size(); i++)
TEST_ASSERT_EQUAL_UINT32(0, database.read(Database::Section::analog_t::upperOffset, i));

// LED block
//----------------------------------
// global section
Expand Down
10 changes: 10 additions & 0 deletions tests/src/hw/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@ TEST_CASE(DatabaseInitialValues)
for (size_t i = 0; i < IO::Analog::Collection::size(); i += PARAM_SKIP)
TEST_ASSERT_EQUAL_UINT32(1, MIDIHelper::readFromDevice(System::Config::Section::analog_t::midiChannel, i));

// lower offset section
// all values should be set to 0
for (int i = 0; i < IO::Analog::Collection::size(); i += PARAM_SKIP)
TEST_ASSERT_EQUAL_UINT32(0, MIDIHelper::readFromDevice(System::Config::Section::analog_t::lowerOffset, i));

// upper offset section
// all values should be set to 0
for (int i = 0; i < IO::Analog::Collection::size(); i += PARAM_SKIP)
TEST_ASSERT_EQUAL_UINT32(0, MIDIHelper::readFromDevice(System::Config::Section::analog_t::upperOffset, i));

// LED block
//----------------------------------
// global section
Expand Down

0 comments on commit 07c3003

Please sign in to comment.