feat(agb02): protocol change

This commit is contained in:
catvayor 2024-10-13 10:42:30 +02:00
parent 012e5fb772
commit 8de7773129
Signed by: lbailly
GPG key ID: CE3E645251AC63F3
6 changed files with 37 additions and 42 deletions

View file

@ -1,6 +1,6 @@
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <gpiod.hpp>
#include <iostream>
#include <fstream>
@ -11,8 +11,7 @@ using namespace std::literals::chrono_literals;
constexpr std::chrono::microseconds debounce = 40ms;
constexpr std::chrono::microseconds poll_period = 5ms;
constexpr std::chrono::microseconds server_ratelimit = 50ms;
constexpr const char* url = "https://agb.hackens.org/api/control-box";
constexpr std::chrono::microseconds retry_timeout = 500ms;
constexpr std::pair<double, double> joystick_movement(1.0, 1.0);
@ -20,14 +19,14 @@ const gpiod::line::offsets drive_down = { 21, 13, 6 };
const gpiod::line::offsets decoder = { 2, 3, 4, 14, 15, 18, 23, 24 }; // lsbf
const gpiod::line::offsets joystick = { 0, 0, 0, 0 }; // x+, y+, x-, y-
const gpiod::line::offset black_button = 0;
const gpiod::line::offset white_button = 0;
const gpiod::line::offset black_button = 16;
const gpiod::line::offset white_button = 20;
const gpiod::line_settings input_settings =
gpiod::line_settings()
.set_direction(gpiod::line::direction::INPUT)
.set_bias(gpiod::line::bias::PULL_UP)
.set_active_low(true)
.set_active_low(false)
.set_debounce_period(debounce);
constexpr std::array<uint8_t, 256> decoder_table =
@ -46,8 +45,8 @@ inline void clamp_decoder(uint8_t& decoder, int move){
}
int main(const int argc, char const* const* const argv) {
if(argc < 3) {
std::cerr << "usage: agb gpiodevice tokenfile" << std::endl;
if(argc < 2) {
std::cerr << "usage: agb gpiodevice" << std::endl;
return 1;
}
@ -64,10 +63,11 @@ int main(const int argc, char const* const* const argv) {
.set_output_value(gpiod::line::value::INACTIVE)
)
.add_line_settings({ black_button, white_button }, input_settings)
.add_line_settings(joystick, input_settings)
.add_line_settings(joystick,
gpiod::line_settings(input_settings)
.set_active_low(true))
.add_line_settings(decoder,
gpiod::line_settings(input_settings)
.set_active_low(false)
.set_debounce_period(0ms))
.do_request();
@ -76,15 +76,21 @@ int main(const int argc, char const* const* const argv) {
/// init server communication ///
cURLpp::initialize();
std::string token;
int socket_file_desc = socket(AF_INET, SOCK_STREAM, 0);
{
std::ifstream tokenFile(argv[2]);
std::getline(tokenFile, token);
sockaddr_in socket_addr = {
.sin_family = AF_INET,
.sin_port = htons(1235),
.sin_addr = { .s_addr = inet_addr("10.10.10.1") }
};
int con_ret = connect(socket_file_desc,
reinterpret_cast<const sockaddr*>(&socket_addr),
sizeof(socket_addr));
if(con_ret < 0) {
std::cerr << "Failed to open tcp socket." << std::endl;
return 1;
}
}
std::list<std::string> header;
header.push_back("Content-Type: application/json");
header.push_back("Authorization: Bearer " + token);
/// internal state and buffers ///
@ -97,7 +103,7 @@ int main(const int argc, char const* const* const argv) {
bool white_pressed = false;
bool black_pressed = false;
bool has_changed = false;
bool has_changed = true;
std::chrono::time_point last_send = std::chrono::system_clock::now();
std::string postData;
@ -150,10 +156,6 @@ int main(const int argc, char const* const* const argv) {
/// server notification
std::chrono::time_point now = std::chrono::system_clock::now();
if(has_changed && (now - last_send > server_ratelimit)){
curlpp::Easy request;
request.setOpt(curlpp::options::Url(url));
request.setOpt(curlpp::options::HttpHeader(header));
postData.clear();
std::format_to(std::back_inserter(postData), "{{"
"\"pan\": {},"
@ -161,22 +163,22 @@ int main(const int argc, char const* const* const argv) {
"\"focus\": {},"
"\"whiteButton\": {},"
"\"blackButton\": {}"
"}}",
"}}\n",
spot_pos.first,
spot_pos.second,
int(decoder_pos),
white_pressed,
black_pressed
);
request.setOpt(curlpp::options::PostFields(postData));
request.setOpt(curlpp::options::PostFieldSize(postData.size()));
//request.perform();
// std::cout << std::chrono::system_clock::now() - now << std::endl;
std::cout << postData << std::endl;
has_changed = false;
last_send = now;
int wrote = write(socket_file_desc, postData.data(), postData.size());
if(wrote < postData.size()){
std::cerr << "Failed to send data, retrying in " << retry_timeout << "." << std::endl;
last_send = now + retry_timeout - server_ratelimit;
} else {
has_changed = false;
last_send = now;
}
}
}
}

View file

@ -1,4 +1,4 @@
{ stdenv, libgpiod, curlpp, curl }:
{ stdenv, libgpiod }:
stdenv.mkDerivation rec {
pname = "agb";
version = "oct-24";
@ -7,8 +7,6 @@ stdenv.mkDerivation rec {
buildPhase = ''
g++ --std=c++23 agb.cpp -o agb \
-L${libgpiod}/lib -lgpiodcxx -I${libgpiod}/include \
-L${curl.out}/lib -lcurl -I${curl.dev}/include \
-L${curlpp}/lib -lcurlpp -I${curlpp}/include
'';
installPhase = ''
mkdir -p $out/bin

View file

@ -8,8 +8,4 @@
file = ./wg.age;
owner = "systemd-network";
};
age.secrets."token" = {
file = ./token.age;
# owner = "systemd-network";
};
}

View file

@ -5,5 +5,4 @@ let
(builtins.readFile (../../../pubkeys + "/${user}.keys")));
in {
"wg.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02");
"token.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02");
}

Binary file not shown.

View file

@ -41,7 +41,7 @@ let
agb02 = {
deployment = {
targetHost = null;
targetHost = "10.10.10.6";
};
arch = "aarch64-linux";
imports = [ agenix ];