unleashed-firmware/applications/subghz/view/subghz-view-spectrum-settings.cpp
SG 7afdd14a4c
Subghz app example (#365)
* Gui: ported submenu and view_dispatcher_remove_view from iButton branch

* App gui-test: use backported submenu api

* App subghz: initial commit

* App subghz: syntax fix

* App gui-test: fix submenu callback

* App subghz: add subfolders to build

* Gui view: c++ verison of with_view_model

* Subghz app: simple spectrum settings view

* Subghz app: add spectrum settings view to view manager

* Subghz app: spectrum settings scene

Co-authored-by: coreglitch <mail@s3f.ru>
2021-03-05 12:47:27 +03:00

95 lines
No EOL
2.7 KiB
C++

#include "subghz-view-spectrum-settings.h"
#include <callback-connector.h>
struct SpectrumSettingsModel {
uint32_t start_freq;
};
/***************************************************************************************/
static void draw_callback(Canvas* canvas, void* _model) {
SpectrumSettingsModel* model = static_cast<SpectrumSettingsModel*>(_model);
const uint8_t str_size = 64;
char str_buffer[str_size];
canvas_clear(canvas);
snprintf(str_buffer, str_size, "Start freq < %ld > MHz", model->start_freq);
canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, str_buffer);
}
static bool input_callback(InputEvent* event, void* context) {
SubghzViewSpectrumSettings* _this = static_cast<SubghzViewSpectrumSettings*>(context);
bool consumed = false;
// Process key presses only
if(event->type == InputTypeShort) {
if(event->key == InputKeyOk) {
_this->call_ok_callback();
consumed = true;
} else if(event->key == InputKeyLeft) {
with_view_model_cpp(_this->get_view(), SpectrumSettingsModel, model, {
model->start_freq--;
return true;
});
consumed = true;
} else if(event->key == InputKeyRight) {
with_view_model_cpp(_this->get_view(), SpectrumSettingsModel, model, {
model->start_freq++;
return true;
});
consumed = true;
}
}
return consumed;
}
/***************************************************************************************/
View* SubghzViewSpectrumSettings::get_view() {
return view;
}
void SubghzViewSpectrumSettings::set_ok_callback(OkCallback callback, void* context) {
ok_callback = callback;
ok_callback_context = context;
}
void SubghzViewSpectrumSettings::call_ok_callback() {
if(ok_callback != nullptr) {
ok_callback(ok_callback_context);
}
}
void SubghzViewSpectrumSettings::set_start_freq(uint32_t start_freq) {
with_view_model_cpp(view, SpectrumSettingsModel, model, {
model->start_freq = start_freq;
return true;
});
}
uint32_t SubghzViewSpectrumSettings::get_start_freq() {
uint32_t result;
with_view_model_cpp(view, SpectrumSettingsModel, model, {
result = model->start_freq;
return false;
});
return result;
}
SubghzViewSpectrumSettings::SubghzViewSpectrumSettings() {
view = view_alloc();
view_set_context(view, this);
view_allocate_model(view, ViewModelTypeLocking, sizeof(SpectrumSettingsModel));
view_set_draw_callback(view, draw_callback);
view_set_input_callback(view, input_callback);
}
SubghzViewSpectrumSettings::~SubghzViewSpectrumSettings() {
view_free(view);
}