Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

touchscreen threshold (sensitivity) auto detect #2306

Merged
merged 17 commits into from
Oct 16, 2024
2 changes: 1 addition & 1 deletion firmware/application/apps/ui_battinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void BattinfoView::update_result() {
text_current.hidden(false);
text_charge.hidden(false);
text_current.set(to_string_dec_int(current) + " mA");
text_charge.set(current >= 0 ? "Charging" : "Discharging");
zxkmm marked this conversation as resolved.
Show resolved Hide resolved
text_charge.set(current > 0 ? "Charging" : (current < 0 ? "Discharging" : "Disconnected"));
labels_opt.hidden(false);

text_ttef.hidden(false);
Expand Down
69 changes: 69 additions & 0 deletions firmware/application/apps/ui_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,74 @@ void SetDisplayView::focus() {
button_save.focus();
}

/* SetTouchscreenSensitivityView ************************************/
/* sample max: 1023 sample_t AKA uint16_t
* touch_sensitivity: range: 1 to 128
* threshold = 1023 / sensitive
* threshold range: 1023/1 to 1023/128 = 1023 to 8
*/
SetTouchscreenThresholdView::SetTouchscreenThresholdView(NavigationView& nav) {
add_children({&labels,
&field_threshold,
&button_autodetect,
&button_reset,
&button_save,
&button_cancel});

field_threshold.set_value(pmem::touchscreen_threshold());

button_autodetect.on_select = [this, &nav](Button&) {
nav.display_modal("NOTICE",
"READ CAREFULLY:\n"
"now on don't touch screen\n"
"use buttons to select,\n"
"In auto detect, wait a few\n"
"until the number\n"
"stops increasing, then press\n"
"Save, then reboot to apply\n"
"read wiki for more info\n"
"Press YES to continue\n"
"Press NO to abort\n",
YESNO, [this, &nav](bool choice) {
if (choice)
{
in_auto_detect = true;
field_threshold.set_value(1);
portapack::touch_threshold = 1;
set_dirty();
} }, TRUE);
};

button_reset.on_select = [this](Button&) {
field_threshold.set_value(32);
};

button_save.on_select = [&nav, this](Button&) {
pmem::set_touchscreen_threshold(field_threshold.value());
send_system_refresh();
nav.pop();
};

button_cancel.on_select = [&nav, this](Button&) {
nav.pop();
};
}

void SetTouchscreenThresholdView::focus() {
button_autodetect.focus();
}

void SetTouchscreenThresholdView::on_frame_sync() {
if (get_touch_frame().touch) {
if (in_auto_detect) {
uint16_t sen = field_threshold.value();
sen++;
portapack::touch_threshold = sen;
field_threshold.set_value(sen);
}
}
}

/* SetMenuColorView ************************************/

void SetMenuColorView::paint_sample() {
Expand Down Expand Up @@ -1022,6 +1090,7 @@ void SettingsMenuView::on_populate() {
{"App Settings", ui::Color::dark_cyan(), &bitmap_icon_notepad, [this]() { nav_.push<AppSettingsView>(); }},
{"Audio", ui::Color::dark_cyan(), &bitmap_icon_speaker, [this]() { nav_.push<SetAudioView>(); }},
{"Calibration", ui::Color::dark_cyan(), &bitmap_icon_options_touch, [this]() { nav_.push<TouchCalibrationView>(); }},
{"TouchThreshold", ui::Color::dark_cyan(), &bitmap_icon_options_touch, [this]() { nav_.push<SetTouchscreenThresholdView>(); }},
{"Config Mode", ui::Color::dark_cyan(), &bitmap_icon_clk_ext, [this]() { nav_.push<SetConfigModeView>(); }},
{"Converter", ui::Color::dark_cyan(), &bitmap_icon_options_radio, [this]() { nav_.push<SetConverterSettingsView>(); }},
{"Date/Time", ui::Color::dark_cyan(), &bitmap_icon_options_datetime, [this]() { nav_.push<SetDateTimeView>(); }},
Expand Down
60 changes: 60 additions & 0 deletions firmware/application/apps/ui_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "bitmap.hpp"
#include "ff.h"
#include "portapack_persistent_memory.hpp"
#include "irq_controls.hpp"
htotoo marked this conversation as resolved.
Show resolved Hide resolved

#include <cstdint>

Expand Down Expand Up @@ -754,6 +755,65 @@ class SetDisplayView : public View {
};
};

using portapack::persistent_memory::touchscreen_threshold;

class SetTouchscreenThresholdView : public View {
public:
SetTouchscreenThresholdView(NavigationView& nav);

void focus() override;

std::string title() const override { return "Touch S"; };

private:
bool in_auto_detect = false;

Labels labels{
{{1 * 8, 1 * 16}, "Set touchscreen sensitivity", Theme::getInstance()->fg_light->foreground},
{{1 * 8, 2 * 16}, "Or press auto detect button", Theme::getInstance()->fg_light->foreground},
{{1 * 8, 3 * 16}, "FOLLOW INSTRUCTIONS", Theme::getInstance()->fg_light->foreground},
{{1 * 8, 4 * 16}, "REBOOT TO APPLY", Theme::getInstance()->fg_light->foreground},
{{1 * 8, 6 * 16}, "Threshold:", Theme::getInstance()->fg_light->foreground},
};

void on_frame_sync();

/* sample max: 1023 sample_t AKA uint16_t
* touch_sensitivity: range: 1 to 128
* threshold range: 1023/1 to 1023/128 = 1023 to 8
*/
NumberField field_threshold{
{1 * 8 + sizeof("Threshold:") * 8 + 8, 6 * 16},
3,
{1, 1023},
1,
' ',
};

Button button_autodetect{
{2 * 8, 13 * 16, 12 * 8, 32},
"Auto Detect"};
Button button_reset{
{16 * 8, 13 * 16, 12 * 8, 32},
"Reset",
};

Button button_save{
{2 * 8, 16 * 16, 12 * 8, 32},
"Save"};

Button button_cancel{
{16 * 8, 16 * 16, 12 * 8, 32},
"Cancel",
};

MessageHandlerRegistration message_handler_frame_sync{
Message::ID::DisplayFrameSync,
[this](const Message* const) {
this->on_frame_sync();
}};
};

class SetMenuColorView : public View {
public:
SetMenuColorView(NavigationView& nav);
Expand Down
3 changes: 2 additions & 1 deletion firmware/application/irq_controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "portapack_io.hpp"
#include "hackrf_hal.hpp"
#include "usb_serial_asyncmsg.hpp"

using namespace hackrf::one;
using namespace portapack;
Expand Down Expand Up @@ -85,7 +86,7 @@ static bool touch_update() {
case IO::TouchPinsConfig::SensePressure: {
const auto z1 = samples.xp - samples.xn;
const auto z2 = samples.yp - samples.yn;
const auto touch_raw = (z1 > touch::touch_threshold) || (z2 > touch::touch_threshold);
const auto touch_raw = (z1 > portapack::touch_threshold) || (z2 > portapack::touch_threshold);
touch_debounce = (touch_debounce << 1) | (touch_raw ? 1U : 0U);
touch_detected = ((touch_debounce & touch_debounce_mask) == touch_debounce_mask);
if (!touch_detected && !touch_cycle) {
Expand Down
6 changes: 6 additions & 0 deletions firmware/application/portapack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ TemperatureLogger temperature_logger;

bool antenna_bias{false};
uint32_t bl_tick_counter{0};
uint16_t touch_threshold{3};

void set_antenna_bias(const bool v) {
antenna_bias = v;
Expand Down Expand Up @@ -538,6 +539,11 @@ init_status_t init() {
set_cpu_clock_speed();

if (persistent_memory::config_lcd_inverted_mode()) display.set_inverted(true);
/* sample max: 1023 sample_t AKA uint16_t
* touch_sensitivity: range: 1 to 128
* threshold range: 1023/1 to 1023/128 = 1023 to 8
*/
touch_threshold = portapack::persistent_memory::touchscreen_threshold();

if (lcd_fast_setup)
draw_splash_screen_icon(0, ui::bitmap_icon_memory);
Expand Down
1 change: 1 addition & 0 deletions firmware/application/portapack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extern TransmitterModel transmitter_model;

extern uint32_t bl_tick_counter;
extern bool antenna_bias;
extern uint16_t touch_threshold;

extern TemperatureLogger temperature_logger;

Expand Down
8 changes: 0 additions & 8 deletions firmware/application/touch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ namespace touch {

using sample_t = uint16_t;

constexpr sample_t sample_max = 1023;

// If you have a dead bottom-left corner try to increase the sensitivity,
// but look for flickering touch indicator in the Buttons test screen
// in which case decrease sensitivity to avoid killing backlight timeout
constexpr sample_t touch_sensitivity = 32;
constexpr sample_t touch_threshold = sample_max / touch_sensitivity;

struct Samples {
sample_t xp;
sample_t xn;
Expand Down
11 changes: 11 additions & 0 deletions firmware/common/portapack_persistent_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ struct data_t {
// Menu Color Scheme
Color menu_color;

uint16_t touchscreen_threshold;

uint16_t UNUSED_16;

constexpr data_t()
Expand Down Expand Up @@ -313,6 +315,7 @@ struct data_t {
config_mode_storage(CONFIG_MODE_NORMAL_VALUE),
dst_config(),
menu_color(Color::grey()),
touchscreen_threshold(32),
UNUSED_16() {
}
};
Expand Down Expand Up @@ -1143,6 +1146,13 @@ void set_menu_color(Color v) {
data->menu_color = v;
}

uint16_t touchscreen_threshold() {
return data->touchscreen_threshold;
}
void set_touchscreen_threshold(uint16_t v) {
data->touchscreen_threshold = v;
}

// PMem to sdcard settings

bool should_use_sdcard_for_pmem() {
Expand Down Expand Up @@ -1250,6 +1260,7 @@ bool debug_dump() {
pmem_dump_file.write_line("dst_config: 0x" + to_string_hex((uint32_t)data->dst_config.v, 8));
pmem_dump_file.write_line("fake_brightness_level: " + to_string_dec_uint(data->fake_brightness_level));
pmem_dump_file.write_line("menu_color: 0x" + to_string_hex(data->menu_color.v, 4));
pmem_dump_file.write_line("touchscreen_threshold: " + to_string_dec_uint(data->touchscreen_threshold));

// ui_config bits
const auto backlight_timer = portapack::persistent_memory::config_backlight_timer();
Expand Down
4 changes: 4 additions & 0 deletions firmware/common/portapack_persistent_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ uint8_t fake_brightness_level();
void set_fake_brightness_level(uint8_t v);
void toggle_fake_brightness_level();

/* Touchscreen threshold */
uint16_t touchscreen_threshold();
void set_touchscreen_threshold(uint16_t v);

Color menu_color();
void set_menu_color(Color v);

Expand Down
Loading