226 lines
6.1 KiB
C++
226 lines
6.1 KiB
C++
/**
|
|
* @file menuIntInput.cpp
|
|
* @author Alexander Klein (alex@kleiax.de)
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2022-09-12
|
|
*
|
|
* @copyright Copyright (c) 2022
|
|
*
|
|
*/
|
|
|
|
#include "menuIntInput.h"
|
|
|
|
MenuIntInput::MenuIntInput(int16_t values[], char *names, uint8_t length, MenuIntInputWrapper* wrapper) {
|
|
this->values = values;
|
|
this->names = names;
|
|
this->length = length;
|
|
this->wrapper = wrapper;
|
|
|
|
this->min = new int16_t[this->length];
|
|
this->max = new int16_t[this->length];
|
|
this->originalValues = new int16_t[this->length];
|
|
this->stepsPerInput = new uint8_t[this->length];
|
|
|
|
for (uint8_t i = 0; i < this->length; i++) {
|
|
this->min[i] = INT16_MIN;
|
|
this->max[i] = INT16_MAX;
|
|
this->originalValues[i] = this->values[i];
|
|
this->stepsPerInput[i] = 1;
|
|
}
|
|
}
|
|
|
|
MenuIntInput::MenuIntInput(uint8_t length, MenuIntInputWrapper *wrapper) {
|
|
this->length = length;
|
|
this->wrapper = wrapper;
|
|
|
|
this->values = new int16_t[this->length];
|
|
this->names = (char*) new char[this->length][MAX_NAME_LENGTH];
|
|
this->min = new int16_t[this->length];
|
|
this->max = new int16_t[this->length];
|
|
this->originalValues = new int16_t[this->length];
|
|
this->stepsPerInput = new uint8_t[this->length];
|
|
|
|
for (uint8_t i = 0; i < this->length; i++) {
|
|
this->min[i] = INT16_MIN;
|
|
this->max[i] = INT16_MAX;
|
|
this->values[i] = 0;
|
|
this->originalValues[i] = 0;
|
|
this->stepsPerInput[i] = 1;
|
|
strcpy(this->names + MAX_NAME_LENGTH * i, "Default");
|
|
}
|
|
}
|
|
|
|
MenuIntInput::~MenuIntInput() {
|
|
delete this->min;
|
|
delete this->max;
|
|
delete this->originalValues;
|
|
delete this->stepsPerInput;
|
|
delete this->values;
|
|
delete this->names;
|
|
delete this->wrapper;
|
|
}
|
|
|
|
void MenuIntInput::setEntry(uint8_t valueNumber, const char* name, int16_t startValue) {
|
|
if (this->length < valueNumber)
|
|
return;
|
|
|
|
if (strlen(name) > MAX_NAME_LENGTH)
|
|
return;
|
|
|
|
|
|
strcpy(this->names + MAX_NAME_LENGTH * valueNumber, name);
|
|
this->values[valueNumber] = startValue;
|
|
this->originalValues[valueNumber] = startValue;
|
|
}
|
|
|
|
void MenuIntInput::setMin(uint8_t valueNumber, int16_t min) {
|
|
if (this->length < valueNumber)
|
|
return;
|
|
|
|
this->min[valueNumber] = min;
|
|
}
|
|
|
|
void MenuIntInput::setMax(uint8_t valueNumber, int16_t max) {
|
|
if (this->length < valueNumber)
|
|
return;
|
|
|
|
this->max[valueNumber] = max;
|
|
}
|
|
|
|
void MenuIntInput::setMinMax(uint8_t valueNumber, int16_t min, int16_t max) {
|
|
this->setMin(valueNumber, min);
|
|
this->setMax(valueNumber, max);
|
|
}
|
|
|
|
void MenuIntInput::setMin(int16_t min) {
|
|
for (uint8_t i = 0; i < this->length; i++)
|
|
this->min[i] = min;
|
|
}
|
|
|
|
void MenuIntInput::setMax(int16_t max) {
|
|
for (uint8_t i = 0; i < this->length; i++)
|
|
this->max[i] = max;
|
|
}
|
|
|
|
void MenuIntInput::setMinMax(int16_t min, int16_t max) {
|
|
this->setMin(min);
|
|
this->setMax(max);
|
|
}
|
|
|
|
void MenuIntInput::setMinMaxSteps(uint8_t valueNumber, int16_t min, int16_t max, uint8_t steps) {
|
|
this->setMin(valueNumber, min);
|
|
this->setMax(valueNumber, max);
|
|
this->setStepsPerInput(valueNumber, steps);
|
|
}
|
|
|
|
void MenuIntInput::setMinMaxSteps(int16_t min, int16_t max, uint8_t steps) {
|
|
this->setMin(min);
|
|
this->setMax(max);
|
|
this->setStepsPerInput(steps);
|
|
}
|
|
|
|
void MenuIntInput::setStepsPerInput(uint8_t valueNumber, uint8_t steps) {
|
|
if (this->length < valueNumber)
|
|
return;
|
|
|
|
this->stepsPerInput[valueNumber] = steps;
|
|
}
|
|
|
|
void MenuIntInput::setStepsPerInput(uint8_t steps) {
|
|
for (uint8_t i = 0; i < this->length; i++)
|
|
this->stepsPerInput[i] = steps;
|
|
}
|
|
|
|
void MenuIntInput::down() {
|
|
if (this->values[this->currentPosition]
|
|
- this->stepsPerInput[this->currentPosition]
|
|
>= this->min[currentPosition])
|
|
{
|
|
this->values[this->currentPosition] -= this->stepsPerInput[this->currentPosition];
|
|
} else {
|
|
this->values[this->currentPosition] = this->max[this->currentPosition];
|
|
}
|
|
|
|
this->printMenu();
|
|
}
|
|
|
|
void MenuIntInput::up() {
|
|
if (this->values[this->currentPosition]
|
|
+ this->stepsPerInput[this->currentPosition]
|
|
<= this->max[this->currentPosition])
|
|
{
|
|
this->values[this->currentPosition] += this->stepsPerInput[this->currentPosition];
|
|
} else {
|
|
this->values[this->currentPosition] = this->min[this->currentPosition];
|
|
}
|
|
|
|
this->printMenu();
|
|
}
|
|
|
|
void MenuIntInput::right() {
|
|
if (currentPosition < this->length - 1)
|
|
currentPosition++;
|
|
else
|
|
currentPosition = 0;
|
|
|
|
this->printMenu();
|
|
}
|
|
|
|
void MenuIntInput::left() {
|
|
if (currentPosition > 0)
|
|
currentPosition--;
|
|
else
|
|
currentPosition = this->length - 1;
|
|
|
|
this->printMenu();
|
|
}
|
|
|
|
void MenuIntInput::no() {
|
|
for (uint8_t i = 0; i < this->length; i++)
|
|
this->values[i] = this->originalValues[i];
|
|
this->currentPosition = 0;
|
|
|
|
if (this->parentMenu)
|
|
this->parentMenu->printMenu();
|
|
}
|
|
|
|
void MenuIntInput::yes() {
|
|
this->wrapper->action(this->values, this->length);
|
|
if (this->parentMenu && printParentMenu)
|
|
this->parentMenu->printMenu();
|
|
}
|
|
|
|
void MenuIntInput::printMenu() {
|
|
char bufferName[17];
|
|
|
|
if (this->currentPosition == 0 && this->length == 1) {
|
|
// No arrow
|
|
sprintf(bufferName, " %s ", this->names + 12 * this->currentPosition);
|
|
} else if (this->currentPosition > 0 && this->length - 1 == this->currentPosition) {
|
|
// Left arrow only
|
|
sprintf(bufferName, "< %s ", this->names + 12 * this->currentPosition);
|
|
} else if (this->currentPosition == 0 && this->length > 1) {
|
|
// Right arrow only
|
|
sprintf(bufferName, " %s >", this->names + 12 * this->currentPosition);
|
|
} else {
|
|
// Both arrow
|
|
sprintf(bufferName, "< %s >", this->names + 12 * this->currentPosition);
|
|
}
|
|
|
|
char bufferValue[17];
|
|
if (this->values[this->currentPosition])
|
|
sprintf(bufferValue, " -> %5.d", this->values[this->currentPosition]);
|
|
else
|
|
sprintf(bufferValue, " -> 0");
|
|
|
|
if (this->lcd) {
|
|
lcd->clear();
|
|
lcd->setCursor(0, 0);
|
|
lcd->print(bufferName);
|
|
lcd->setCursor(0, 1);
|
|
lcd->print(bufferValue);
|
|
}
|
|
std::cout << bufferName << " : " << bufferValue << std::endl;
|
|
}
|