2016-05-09 14:42:20 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
2016-05-10 01:20:24 -04:00
|
|
|
* Copyright (C) 2016 Furrtek
|
2016-05-09 14:42:20 -04:00
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_handwrite.hpp"
|
|
|
|
|
|
|
|
#include "portapack.hpp"
|
|
|
|
#include "hackrf_hal.hpp"
|
|
|
|
#include "portapack_shared_memory.hpp"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
2017-03-12 03:09:22 -04:00
|
|
|
|
2016-05-10 19:03:42 -04:00
|
|
|
void HandWriteView::paint(Painter& painter) {
|
|
|
|
_painter = &painter;
|
|
|
|
}
|
2016-05-09 14:42:20 -04:00
|
|
|
|
|
|
|
HandWriteView::HandWriteView(
|
|
|
|
NavigationView& nav,
|
2017-04-18 13:12:32 -04:00
|
|
|
std::string& txt,
|
2016-12-09 12:21:47 -05:00
|
|
|
size_t max_length
|
|
|
|
) : _max_length(max_length)
|
|
|
|
{
|
2016-05-10 19:03:42 -04:00
|
|
|
const char special_chars[5] = {'\'', '.', '?', '!', '='};
|
2016-07-28 22:52:51 -04:00
|
|
|
size_t n;
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2016-05-10 19:03:42 -04:00
|
|
|
// Handwriting alphabet definition here
|
|
|
|
handwriting = &handwriting_unistroke;
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2017-04-18 13:12:32 -04:00
|
|
|
txtidx = txt.length();
|
|
|
|
txtinput = txt;
|
2016-07-28 22:52:51 -04:00
|
|
|
n = txtidx;
|
|
|
|
while (n && (txtinput[n - 1] == ' ')) {
|
|
|
|
txtinput[--n] = 0;
|
|
|
|
txtidx--;
|
|
|
|
}
|
2016-05-10 19:03:42 -04:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2016-05-10 19:03:42 -04:00
|
|
|
&text_input,
|
|
|
|
&button_case,
|
|
|
|
&button_ok
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2016-05-09 14:42:20 -04:00
|
|
|
|
|
|
|
const auto button_fn = [this](Button& button) {
|
|
|
|
this->on_button(button);
|
|
|
|
};
|
|
|
|
|
2016-07-28 22:52:51 -04:00
|
|
|
n = 0;
|
2016-12-09 12:21:47 -05:00
|
|
|
for (auto& button : num_buttons) {
|
2016-05-09 14:42:20 -04:00
|
|
|
add_child(&button);
|
|
|
|
button.on_select = button_fn;
|
|
|
|
button.set_parent_rect({
|
|
|
|
static_cast<Coord>(n * 24),
|
2016-05-10 19:03:42 -04:00
|
|
|
static_cast<Coord>(236),
|
|
|
|
24, 28
|
2016-05-09 14:42:20 -04:00
|
|
|
});
|
|
|
|
const std::string label {
|
2016-12-09 12:21:47 -05:00
|
|
|
(char)(n + '0')
|
2016-05-09 14:42:20 -04:00
|
|
|
};
|
|
|
|
button.set_text(label);
|
2016-12-09 12:21:47 -05:00
|
|
|
button.id = n + '0';
|
2016-05-09 14:42:20 -04:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:03:42 -04:00
|
|
|
n = 0;
|
2016-12-09 12:21:47 -05:00
|
|
|
for (auto& button : special_buttons) {
|
2016-05-10 19:03:42 -04:00
|
|
|
add_child(&button);
|
|
|
|
button.on_select = button_fn;
|
|
|
|
button.set_parent_rect({
|
|
|
|
static_cast<Coord>(50 + n * 24),
|
|
|
|
static_cast<Coord>(270),
|
|
|
|
24, 28
|
|
|
|
});
|
|
|
|
const std::string label {
|
|
|
|
(char)(special_chars[n])
|
|
|
|
};
|
|
|
|
button.set_text(label);
|
|
|
|
button.id = special_chars[n];
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
button_case.on_select = [this, &nav](Button&) {
|
2016-05-09 14:42:20 -04:00
|
|
|
if (_lowercase == true) {
|
|
|
|
_lowercase = false;
|
2016-05-10 19:03:42 -04:00
|
|
|
button_case.set_text("LC");
|
2016-05-09 14:42:20 -04:00
|
|
|
} else {
|
|
|
|
_lowercase = true;
|
2016-05-10 19:03:42 -04:00
|
|
|
button_case.set_text("UC");
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
2016-05-10 19:03:42 -04:00
|
|
|
};
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2017-04-18 13:12:32 -04:00
|
|
|
button_ok.on_select = [this, &nav, &txt, max_length](Button&) {
|
|
|
|
txt = txtinput;
|
2016-12-09 12:21:47 -05:00
|
|
|
if (on_changed) on_changed(this->value());
|
2016-05-09 14:42:20 -04:00
|
|
|
nav.pop();
|
|
|
|
};
|
|
|
|
|
2016-05-29 06:06:47 -04:00
|
|
|
update_text();
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HandWriteView::on_touch(const TouchEvent event) {
|
|
|
|
if (event.type == ui::TouchEvent::Type::Start) {
|
2016-05-10 19:03:42 -04:00
|
|
|
stroke_index = 0;
|
|
|
|
move_wait = 3;
|
2016-05-09 14:42:20 -04:00
|
|
|
tracing = true;
|
|
|
|
}
|
|
|
|
if (event.type == ui::TouchEvent::Type::End) {
|
|
|
|
tracing = false;
|
2016-05-10 01:20:24 -04:00
|
|
|
guess_letter();
|
|
|
|
}
|
|
|
|
if (event.type == ui::TouchEvent::Type::Move) {
|
2016-12-09 12:21:47 -05:00
|
|
|
if (tracing)
|
2016-05-10 01:20:24 -04:00
|
|
|
current_pos = event.point;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:21:47 -05:00
|
|
|
void HandWriteView::clear_zone(const Color color, const bool flash) {
|
2016-05-10 19:03:42 -04:00
|
|
|
display.fill_rectangle(
|
|
|
|
{{0, 32}, {240, 216}},
|
|
|
|
color
|
|
|
|
);
|
|
|
|
if (flash) {
|
2016-12-25 19:31:38 -05:00
|
|
|
flash_timer = 8;
|
2016-05-10 19:03:42 -04:00
|
|
|
} else {
|
2016-12-09 12:21:47 -05:00
|
|
|
// Draw grid
|
2016-05-10 19:03:42 -04:00
|
|
|
_painter->draw_rectangle(
|
|
|
|
{{0, 32}, {80, 216}},
|
|
|
|
Color::grey()
|
|
|
|
);
|
|
|
|
_painter->draw_rectangle(
|
|
|
|
{{80, 32}, {80, 216}},
|
|
|
|
Color::grey()
|
|
|
|
);
|
|
|
|
_painter->draw_rectangle(
|
|
|
|
{{160, 32}, {80, 216}},
|
|
|
|
Color::grey()
|
|
|
|
);
|
|
|
|
_painter->draw_rectangle(
|
|
|
|
{{0, 104}, {240, 72}},
|
|
|
|
Color::grey()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:20:24 -04:00
|
|
|
void HandWriteView::guess_letter() {
|
2016-12-09 12:21:47 -05:00
|
|
|
uint32_t symbol, match, count, stroke_idx, stroke_data;
|
2016-05-10 01:20:24 -04:00
|
|
|
Condition cond;
|
|
|
|
Direction dir;
|
|
|
|
bool matched;
|
|
|
|
|
|
|
|
// Letter guessing
|
2016-05-10 19:03:42 -04:00
|
|
|
if (stroke_index) {
|
|
|
|
for (symbol = 0; symbol < handwriting->letter_count; symbol++) {
|
|
|
|
count = handwriting->letter[symbol].count;
|
2016-05-10 01:20:24 -04:00
|
|
|
matched = false;
|
|
|
|
if (count) {
|
|
|
|
// We have a count match to do
|
2016-05-10 19:03:42 -04:00
|
|
|
if ((count == 1) && (stroke_index == 1)) matched = true;
|
|
|
|
if ((count == 2) && (stroke_index == 2)) matched = true;
|
|
|
|
if ((count == 3) && (stroke_index > 2)) matched = true;
|
2016-05-10 01:20:24 -04:00
|
|
|
} else {
|
|
|
|
matched = true;
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
2016-05-10 01:20:24 -04:00
|
|
|
if (matched) {
|
|
|
|
for (match = 0; match < 3; match++) {
|
2016-05-10 19:03:42 -04:00
|
|
|
cond = handwriting->letter[symbol].match[match].cond;
|
|
|
|
dir = handwriting->letter[symbol].match[match].dir;
|
2016-05-10 01:20:24 -04:00
|
|
|
if ((cond != cond_empty) && (dir != dir_empty)) {
|
|
|
|
if (cond == last) {
|
2016-05-10 19:03:42 -04:00
|
|
|
if (stroke_index)
|
|
|
|
stroke_idx = stroke_index - 1;
|
2016-05-10 01:20:24 -04:00
|
|
|
else
|
|
|
|
stroke_idx = 0;
|
|
|
|
} else if (cond == stroke_a)
|
|
|
|
stroke_idx = 0;
|
|
|
|
else if (cond == stroke_b)
|
|
|
|
stroke_idx = 1;
|
|
|
|
else if (cond == stroke_c)
|
|
|
|
stroke_idx = 2;
|
|
|
|
else
|
|
|
|
stroke_idx = 3;
|
2016-05-10 19:03:42 -04:00
|
|
|
if (stroke_idx >= stroke_index) break;
|
|
|
|
stroke_data = stroke_list[stroke_idx];
|
2016-05-10 01:20:24 -04:00
|
|
|
if ((dir & 0xF0) == 0xF0) {
|
|
|
|
if ((dir & 0x0F) != (stroke_data & 0x0F)) break;
|
|
|
|
} else if ((dir & 0x0F) == 0x0F) {
|
|
|
|
if ((dir & 0xF0) != (stroke_data & 0xF0)) break;
|
|
|
|
} else {
|
2016-12-09 12:21:47 -05:00
|
|
|
if (dir != (int32_t)stroke_data) break;
|
2016-05-10 01:20:24 -04:00
|
|
|
}
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-10 01:20:24 -04:00
|
|
|
if (match == 3)
|
|
|
|
break;
|
2016-05-09 14:42:20 -04:00
|
|
|
else
|
2016-05-10 01:20:24 -04:00
|
|
|
matched = false;
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-10 01:20:24 -04:00
|
|
|
if (matched) {
|
2016-05-10 19:03:42 -04:00
|
|
|
if (symbol) {
|
|
|
|
if (_lowercase)
|
|
|
|
char_add('a' + symbol - 1);
|
|
|
|
else
|
|
|
|
char_add('A' + symbol - 1);
|
|
|
|
clear_zone(Color::green(), true); // Green flash
|
|
|
|
} else {
|
|
|
|
if (txtidx) {
|
2016-12-09 12:21:47 -05:00
|
|
|
txtidx--;
|
|
|
|
txtinput[txtidx] = 0; // Erase
|
2016-05-10 19:03:42 -04:00
|
|
|
clear_zone(Color::yellow(), true); // Yellow flash
|
|
|
|
} else {
|
|
|
|
clear_zone(Color::red(), true); // Red flash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clear_zone(Color::red(), true); // Red flash
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
2016-05-10 01:20:24 -04:00
|
|
|
} else {
|
2016-05-10 19:03:42 -04:00
|
|
|
// Short tap is space
|
2016-12-25 19:31:38 -05:00
|
|
|
char_add(' ');
|
2016-05-10 19:03:42 -04:00
|
|
|
clear_zone(Color::green(), true); // Green flash
|
2016-05-10 01:20:24 -04:00
|
|
|
}
|
|
|
|
update_text();
|
2016-05-10 19:03:42 -04:00
|
|
|
stroke_index = 0;
|
2016-05-10 01:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HandWriteView::add_stroke(uint8_t dir) {
|
2016-05-10 19:03:42 -04:00
|
|
|
if (stroke_index < 8) {
|
|
|
|
stroke_list[stroke_index] = dir;
|
|
|
|
stroke_index++;
|
2016-05-10 01:20:24 -04:00
|
|
|
} else {
|
|
|
|
guess_letter();
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandWriteView::sample_pen() {
|
|
|
|
int16_t diff_x, diff_y;
|
2016-05-10 19:03:42 -04:00
|
|
|
uint8_t dir, dir_ud, dir_lr, stroke_prev;
|
2016-05-09 22:24:19 -04:00
|
|
|
|
|
|
|
// Blink cursor
|
|
|
|
if (!(sample_skip & 15)) {
|
|
|
|
Point cursor_pos;
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
cursor_pos = {text_input.screen_rect().location().x() + (txtidx * 8),
|
|
|
|
text_input.screen_rect().location().y() + 16 - 4};
|
2016-05-09 22:24:19 -04:00
|
|
|
|
|
|
|
if (cursor) {
|
|
|
|
display.fill_rectangle(
|
2017-01-15 22:45:44 -05:00
|
|
|
{cursor_pos, {text_input.screen_rect().size().width() - cursor_pos.x(), 4}},
|
2016-05-09 22:24:19 -04:00
|
|
|
Color::black()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
display.fill_rectangle(
|
|
|
|
{cursor_pos, {8, 4}},
|
|
|
|
Color::white()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
cursor = !cursor;
|
|
|
|
}
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2016-05-10 19:03:42 -04:00
|
|
|
if (flash_timer) {
|
|
|
|
if (flash_timer == 1) clear_zone(Color::black(), false);
|
|
|
|
flash_timer--;
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:42:20 -04:00
|
|
|
if (!(sample_skip & 1)) {
|
|
|
|
if (tracing) {
|
|
|
|
if (move_wait) {
|
2016-05-10 19:03:42 -04:00
|
|
|
move_wait--; // ~100ms delay to get rid of jitter from touch start
|
2016-05-09 14:42:20 -04:00
|
|
|
} else {
|
2017-01-15 22:45:44 -05:00
|
|
|
diff_x = current_pos.x() - last_pos.x();
|
|
|
|
diff_y = current_pos.y() - last_pos.y();
|
2016-05-10 19:03:42 -04:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
if (current_pos.y() <= 240) {
|
2016-05-10 01:20:24 -04:00
|
|
|
display.fill_rectangle(
|
2017-01-15 22:45:44 -05:00
|
|
|
{{current_pos.x(), current_pos.y()}, {4, 4}},
|
2016-05-10 01:20:24 -04:00
|
|
|
Color::grey()
|
|
|
|
);
|
|
|
|
}
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2016-05-10 19:03:42 -04:00
|
|
|
dir = 0; // UL by default
|
2016-05-09 14:42:20 -04:00
|
|
|
if (abs(diff_x) > 7) {
|
|
|
|
if (diff_x > 0)
|
|
|
|
dir |= 0x01; // R
|
|
|
|
} else {
|
|
|
|
dir |= 0x02; // ?
|
|
|
|
}
|
|
|
|
if (abs(diff_y) > 7) {
|
|
|
|
if (diff_y > 0)
|
|
|
|
dir |= 0x10; // D
|
|
|
|
} else {
|
|
|
|
dir |= 0x20; // ?
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:03:42 -04:00
|
|
|
// Need at least two identical directions to validate stroke
|
2016-05-09 14:42:20 -04:00
|
|
|
if ((dir & 0x11) == (dir_prev & 0x11))
|
|
|
|
dir_cnt++;
|
|
|
|
else
|
|
|
|
dir_cnt = 0;
|
|
|
|
dir_prev = dir;
|
|
|
|
|
|
|
|
if (dir_cnt > 1) {
|
|
|
|
dir_cnt = 0;
|
2016-05-10 19:03:42 -04:00
|
|
|
if (stroke_index) {
|
|
|
|
if ((stroke_list[stroke_index - 1] != dir) && (dir != 0x22)) {
|
|
|
|
// Register stroke if different from last one
|
2016-05-10 01:20:24 -04:00
|
|
|
dir_ud = (dir & 0xF0);
|
|
|
|
dir_lr = (dir & 0x0F);
|
2016-05-10 19:03:42 -04:00
|
|
|
stroke_prev = stroke_list[stroke_index - 1];
|
2016-05-10 01:20:24 -04:00
|
|
|
if (dir_ud == 0x20) {
|
2016-05-10 19:03:42 -04:00
|
|
|
// LR changed
|
|
|
|
if ((stroke_prev & 0x0F) != dir_lr) add_stroke(dir);
|
2016-05-10 01:20:24 -04:00
|
|
|
} else if (dir_lr == 0x02) {
|
2016-05-10 19:03:42 -04:00
|
|
|
// UD changed
|
|
|
|
if ((stroke_prev & 0xF0) != dir_ud) add_stroke(dir);
|
2016-05-09 14:42:20 -04:00
|
|
|
} else {
|
2016-05-10 19:03:42 -04:00
|
|
|
// Add direction
|
|
|
|
if (((stroke_prev & 0xF0) == 0x20) && (dir_ud != 0x20)) {
|
|
|
|
// Add UD
|
|
|
|
if ((stroke_prev & 0x0F) == dir_lr) {
|
|
|
|
// Replace totally
|
|
|
|
stroke_list[stroke_index - 1] = dir;
|
|
|
|
} else if (dir_lr == 0x02) {
|
|
|
|
// Merge UD
|
|
|
|
stroke_list[stroke_index - 1] = dir_ud | (stroke_prev & 0x0F);
|
2016-05-09 14:42:20 -04:00
|
|
|
} else {
|
2016-05-10 01:20:24 -04:00
|
|
|
add_stroke(dir);
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
2016-05-10 19:03:42 -04:00
|
|
|
} else if (((stroke_prev & 0x0F) == 0x02) && (dir_lr != 0x02)) {
|
|
|
|
// Add LR
|
|
|
|
if ((stroke_prev & 0xF0) == dir_ud) {
|
|
|
|
// Replace totally
|
|
|
|
stroke_list[stroke_index - 1] = dir;
|
2016-05-10 01:20:24 -04:00
|
|
|
} else if (dir_ud == 0x20) {
|
2016-05-10 19:03:42 -04:00
|
|
|
// Merge LR
|
|
|
|
stroke_list[stroke_index - 1] = dir_lr | (stroke_prev & 0xF0);
|
2016-05-09 14:42:20 -04:00
|
|
|
} else {
|
2016-05-10 01:20:24 -04:00
|
|
|
add_stroke(dir);
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 01:20:24 -04:00
|
|
|
add_stroke(dir);
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 19:03:42 -04:00
|
|
|
// Register first stroke
|
2016-05-10 01:20:24 -04:00
|
|
|
if (dir != 0x22) add_stroke(dir);
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last_pos = current_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sample_skip++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandWriteView::on_show() {
|
2016-05-10 19:03:42 -04:00
|
|
|
clear_zone(Color::black(), false);
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
|
2017-04-18 13:12:32 -04:00
|
|
|
std::string HandWriteView::value() {
|
2016-12-09 12:21:47 -05:00
|
|
|
txtinput[txtidx] = 0;
|
2016-05-09 14:42:20 -04:00
|
|
|
return txtinput;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandWriteView::on_button(Button& button) {
|
2016-05-10 19:03:42 -04:00
|
|
|
char_add(button.id);
|
2016-05-09 14:42:20 -04:00
|
|
|
update_text();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandWriteView::char_add(const char c) {
|
2016-12-09 12:21:47 -05:00
|
|
|
if (txtidx >= _max_length) return;
|
|
|
|
|
|
|
|
txtinput[txtidx] = c;
|
|
|
|
txtidx++;
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HandWriteView::update_text() {
|
|
|
|
text_input.set(txtinput);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|