2022-01-05 16:10:18 +00:00
|
|
|
#include "lfrfid_view_tune_vm.h"
|
2021-05-04 13:21:16 +00:00
|
|
|
#include <callback-connector.h>
|
|
|
|
#include <gui/elements.h>
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
struct LfRfidViewTuneVMModel {
|
2021-05-04 13:21:16 +00:00
|
|
|
bool dirty;
|
|
|
|
bool fine;
|
|
|
|
uint32_t ARR;
|
|
|
|
uint32_t CCR;
|
|
|
|
int pos;
|
|
|
|
};
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
void LfRfidViewTuneVM::view_draw_callback(Canvas* canvas, void* _model) {
|
|
|
|
LfRfidViewTuneVMModel* model = reinterpret_cast<LfRfidViewTuneVMModel*>(_model);
|
2021-05-04 13:21:16 +00:00
|
|
|
canvas_clear(canvas);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
if(model->fine) {
|
|
|
|
canvas_draw_box(
|
|
|
|
canvas,
|
|
|
|
128 - canvas_string_width(canvas, "Fine") - 4,
|
|
|
|
0,
|
|
|
|
canvas_string_width(canvas, "Fine") + 4,
|
|
|
|
canvas_current_font_height(canvas) + 1);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_str_aligned(canvas, 128 - 2, 2, AlignRight, AlignTop, "Fine");
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
constexpr uint8_t buffer_size = 128;
|
|
|
|
char buffer[buffer_size + 1];
|
|
|
|
snprintf(
|
|
|
|
buffer,
|
|
|
|
buffer_size,
|
|
|
|
"%sARR: %lu\n"
|
|
|
|
"freq = %.4f\n"
|
|
|
|
"%sCCR: %lu\n"
|
|
|
|
"duty = %.4f",
|
|
|
|
model->pos == 0 ? ">" : "",
|
|
|
|
model->ARR,
|
|
|
|
(float)SystemCoreClock / ((float)model->ARR + 1),
|
|
|
|
model->pos == 1 ? ">" : "",
|
|
|
|
model->CCR,
|
|
|
|
((float)model->CCR + 1) / ((float)model->ARR + 1) * 100.0f);
|
|
|
|
elements_multiline_text_aligned(canvas, 2, 2, AlignLeft, AlignTop, buffer);
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
bool LfRfidViewTuneVM::view_input_callback(InputEvent* event, void* context) {
|
|
|
|
LfRfidViewTuneVM* _this = reinterpret_cast<LfRfidViewTuneVM*>(context);
|
2021-05-04 13:21:16 +00:00
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
// Process key presses only
|
|
|
|
if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
|
|
|
|
consumed = true;
|
|
|
|
|
|
|
|
switch(event->key) {
|
|
|
|
case InputKeyLeft:
|
|
|
|
_this->button_left();
|
|
|
|
break;
|
|
|
|
case InputKeyRight:
|
|
|
|
_this->button_right();
|
|
|
|
break;
|
|
|
|
case InputKeyUp:
|
|
|
|
_this->button_up();
|
|
|
|
break;
|
|
|
|
case InputKeyDown:
|
|
|
|
_this->button_down();
|
|
|
|
break;
|
|
|
|
case InputKeyOk:
|
|
|
|
_this->button_ok();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
consumed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
void LfRfidViewTuneVM::button_up() {
|
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
if(model->pos > 0) model->pos--;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
void LfRfidViewTuneVM::button_down() {
|
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
if(model->pos < 1) model->pos++;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
void LfRfidViewTuneVM::button_left() {
|
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
if(model->pos == 0) {
|
|
|
|
if(model->fine) {
|
|
|
|
model->ARR -= 1;
|
|
|
|
} else {
|
|
|
|
model->ARR -= 10;
|
|
|
|
}
|
|
|
|
} else if(model->pos == 1) {
|
|
|
|
if(model->fine) {
|
|
|
|
model->CCR -= 1;
|
|
|
|
} else {
|
|
|
|
model->CCR -= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
model->dirty = true;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
void LfRfidViewTuneVM::button_right() {
|
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
if(model->pos == 0) {
|
|
|
|
if(model->fine) {
|
|
|
|
model->ARR += 1;
|
|
|
|
} else {
|
|
|
|
model->ARR += 10;
|
|
|
|
}
|
|
|
|
} else if(model->pos == 1) {
|
|
|
|
if(model->fine) {
|
|
|
|
model->CCR += 1;
|
|
|
|
} else {
|
|
|
|
model->CCR += 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
model->dirty = true;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
void LfRfidViewTuneVM::button_ok() {
|
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
model->fine = !model->fine;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
LfRfidViewTuneVM::LfRfidViewTuneVM() {
|
2021-05-04 13:21:16 +00:00
|
|
|
view = view_alloc();
|
|
|
|
view_set_context(view, this);
|
2021-06-28 14:42:30 +00:00
|
|
|
view_allocate_model(view, ViewModelTypeLocking, sizeof(LfRfidViewTuneVMModel));
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
model->dirty = true;
|
|
|
|
model->fine = false;
|
|
|
|
model->ARR = 511;
|
|
|
|
model->CCR = 255;
|
|
|
|
model->pos = 0;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
view_set_draw_callback(
|
|
|
|
view, cbc::obtain_connector(this, &LfRfidViewTuneVM::view_draw_callback));
|
2021-05-04 13:21:16 +00:00
|
|
|
view_set_input_callback(
|
2021-06-28 14:42:30 +00:00
|
|
|
view, cbc::obtain_connector(this, &LfRfidViewTuneVM::view_input_callback));
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
LfRfidViewTuneVM::~LfRfidViewTuneVM() {
|
2021-05-04 13:21:16 +00:00
|
|
|
view_free(view);
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
View* LfRfidViewTuneVM::get_view() {
|
2021-05-04 13:21:16 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
void LfRfidViewTuneVM::clean() {
|
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
|
|
|
model->dirty = true;
|
|
|
|
model->fine = false;
|
|
|
|
model->ARR = 511;
|
|
|
|
model->CCR = 255;
|
|
|
|
model->pos = 0;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LfRfidViewTuneVM::is_dirty() {
|
2021-05-04 13:21:16 +00:00
|
|
|
bool result;
|
2021-06-28 14:42:30 +00:00
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
result = model->dirty;
|
|
|
|
model->dirty = false;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
uint32_t LfRfidViewTuneVM::get_ARR() {
|
2021-05-04 13:21:16 +00:00
|
|
|
uint32_t result;
|
2021-06-28 14:42:30 +00:00
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
result = model->ARR;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
uint32_t LfRfidViewTuneVM::get_CCR() {
|
2021-05-04 13:21:16 +00:00
|
|
|
uint32_t result;
|
2021-06-28 14:42:30 +00:00
|
|
|
with_view_model_cpp(view, LfRfidViewTuneVMModel, model, {
|
2021-05-04 13:21:16 +00:00
|
|
|
result = model->CCR;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|