Shared Catalog
This commit is contained in:
+29
-68
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <vamp_pico/display.hpp>
|
||||
#include <vamp_pico/http_client.hpp>
|
||||
#include <vamp_pico/input.hpp>
|
||||
#include <vamp_pico/touch.hpp>
|
||||
#include <vamp_protocol/page_control.hpp>
|
||||
|
||||
@@ -15,14 +16,6 @@ enum class link_status {
|
||||
CONNECTED,
|
||||
};
|
||||
|
||||
struct gesture_state {
|
||||
std::bool_t touching{};
|
||||
std::bool_t hold_sent{};
|
||||
std::bool_t pending_tap{};
|
||||
absolute_time_t down_time{};
|
||||
absolute_time_t last_up_time{};
|
||||
};
|
||||
|
||||
auto color_for(link_status status) -> vamp_style::color {
|
||||
switch (status) {
|
||||
case link_status::CONNECTING:
|
||||
@@ -35,12 +28,8 @@ auto color_for(link_status status) -> vamp_style::color {
|
||||
return vamp_style::retrying;
|
||||
}
|
||||
|
||||
auto render(vamp_pico::display &display, link_status status, vamp_pico::remote_display_state const &remote) -> void {
|
||||
display.show_remote(color_for(status), remote.title, remote.part, remote.page, remote.pages, remote.active);
|
||||
}
|
||||
|
||||
auto clear_remote(vamp_pico::remote_display_state &remote) -> void {
|
||||
remote = vamp_pico::remote_display_state{};
|
||||
auto render(vamp_pico::display &display, link_status status, vamp_pico::remote_display_state const &remote, std::bool_t busy = false) -> void {
|
||||
display.show_remote(color_for(status), remote.title, remote.part, remote.page, remote.pages, remote.active, busy);
|
||||
}
|
||||
|
||||
auto connect_wifi(vamp_pico::display &display) -> std::bool_t {
|
||||
@@ -60,49 +49,6 @@ auto connect_wifi(vamp_pico::display &display) -> std::bool_t {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto send_command(vamp_protocol::page_command command, link_status &status, vamp_pico::remote_display_state &remote, vamp_pico::display &display) -> void {
|
||||
if (!vamp_pico::post_remote_command(command, remote)) {
|
||||
status = link_status::RETRYING;
|
||||
clear_remote(remote);
|
||||
render(display, status, remote);
|
||||
return;
|
||||
}
|
||||
status = link_status::CONNECTED;
|
||||
render(display, status, remote);
|
||||
}
|
||||
|
||||
auto update_gesture(gesture_state &gesture, std::bool_t touching, link_status &status, vamp_pico::remote_display_state &remote, vamp_pico::display &display) -> void {
|
||||
constexpr auto hold_us = std::int64_t{700000};
|
||||
constexpr auto double_tap_us = std::int64_t{220000};
|
||||
auto now = get_absolute_time();
|
||||
if (touching && !gesture.touching) {
|
||||
gesture.touching = true;
|
||||
gesture.hold_sent = false;
|
||||
gesture.down_time = now;
|
||||
}
|
||||
if (touching && !gesture.hold_sent && absolute_time_diff_us(gesture.down_time, now) >= hold_us) {
|
||||
gesture.hold_sent = true;
|
||||
gesture.pending_tap = false;
|
||||
send_command(vamp_protocol::page_command::SELECT, status, remote, display);
|
||||
}
|
||||
if (!touching && gesture.touching) {
|
||||
gesture.touching = false;
|
||||
if (!gesture.hold_sent) {
|
||||
if (gesture.pending_tap && absolute_time_diff_us(gesture.last_up_time, now) <= double_tap_us) {
|
||||
gesture.pending_tap = false;
|
||||
send_command(vamp_protocol::page_command::PREVIOUS, status, remote, display);
|
||||
} else {
|
||||
gesture.pending_tap = true;
|
||||
gesture.last_up_time = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!touching && gesture.pending_tap && absolute_time_diff_us(gesture.last_up_time, now) > double_tap_us) {
|
||||
gesture.pending_tap = false;
|
||||
send_command(vamp_protocol::page_command::NEXT, status, remote, display);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
@@ -118,25 +64,40 @@ auto main() -> int {
|
||||
}
|
||||
auto remote = vamp_pico::remote_display_state{};
|
||||
auto status = link_status::RETRYING;
|
||||
auto gesture = gesture_state{};
|
||||
auto last_poll = get_absolute_time();
|
||||
auto poll_now = std::bool_t{true};
|
||||
auto gesture = vamp_pico::gesture_state{};
|
||||
auto scheduler = vamp_pico::request_scheduler{};
|
||||
// Keep the owned request buffers off the Pico's small main stack.
|
||||
static auto client = vamp_pico::http_client{};
|
||||
auto busy_until_ms = std::uint64_t{};
|
||||
render(display, status, remote);
|
||||
while (true) {
|
||||
auto point = vamp_pico::touch_point{};
|
||||
update_gesture(gesture, touch.read(point), status, remote, display);
|
||||
auto now = get_absolute_time();
|
||||
if (poll_now || absolute_time_diff_us(last_poll, now) >= 1000000) {
|
||||
poll_now = false;
|
||||
last_poll = now;
|
||||
if (vamp_pico::fetch_remote_display(remote)) {
|
||||
auto touching = touch.read(point);
|
||||
auto now_ms = time_us_64() / 1000U;
|
||||
if (auto command = gesture.update(touching, now_ms); command) {
|
||||
if (scheduler.enqueue(*command) == vamp_pico::enqueue_result::BUSY) {
|
||||
busy_until_ms = now_ms + 1000U;
|
||||
}
|
||||
}
|
||||
auto network = client.poll(now_ms);
|
||||
if (network == vamp_pico::http_status::READY) {
|
||||
auto result = client.take_result();
|
||||
scheduler.complete(now_ms);
|
||||
network = vamp_pico::http_status::IDLE;
|
||||
if (result && result->success) {
|
||||
remote = result->display;
|
||||
status = link_status::CONNECTED;
|
||||
} else {
|
||||
status = link_status::RETRYING;
|
||||
clear_remote(remote);
|
||||
remote = vamp_pico::remote_display_state{};
|
||||
}
|
||||
render(display, status, remote);
|
||||
}
|
||||
if (network == vamp_pico::http_status::IDLE) {
|
||||
if (auto request = scheduler.next(now_ms); request) {
|
||||
client.start(*request, now_ms);
|
||||
}
|
||||
}
|
||||
render(display, status, remote, now_ms < busy_until_ms);
|
||||
sleep_ms(8);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user