Merge remote-tracking branch 'refs/remotes/origin/main'

main
ParkerTenBroeck 2025-04-16 23:33:42 -04:00
commit 423ff5db8a
1 changed files with 82 additions and 41 deletions

View File

@ -15,12 +15,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <deque> #include <deque>
#include <fcntl.h>
#include <future>
#include <imgui.h> #include <imgui.h>
#include <string> #include <string>
#include <unistd.h>
#include <arpa/inet.h>
#include <blt/gfx/window.h> #include <blt/gfx/window.h>
#include <blt/gfx/renderer/font_renderer.h> #include <blt/gfx/renderer/font_renderer.h>
#include <blt/std/requests.h> #include <blt/std/requests.h>
#include <blt/std/time.h> #include <blt/std/time.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "blt/gfx/renderer/batch_2d_renderer.h" #include "blt/gfx/renderer/batch_2d_renderer.h"
#include "blt/gfx/renderer/camera.h" #include "blt/gfx/renderer/camera.h"
#include "blt/gfx/renderer/resource_manager.h" #include "blt/gfx/renderer/resource_manager.h"
@ -40,6 +46,8 @@ std::array<char, 100> buffer;
size_t last_time_ran = 0; size_t last_time_ran = 0;
size_t last_time_ran1 = 0; size_t last_time_ran1 = 0;
int send_socket = -1;
bool ready = false; bool ready = false;
struct everything_t struct everything_t
@ -66,8 +74,6 @@ struct needed_t
blt::vec2f position; blt::vec2f position;
}; };
blt::vec2 current_position;
struct boy_trust_t struct boy_trust_t
{ {
std::deque<blt::vec2f> point_cloud; std::deque<blt::vec2f> point_cloud;
@ -97,38 +103,6 @@ struct boy_trust_t
} }
} point_data; } point_data;
bool check_for_request(needed_t& data)
{
if (!ready)
return false;
const auto cur_time = blt::system::getCurrentTimeMilliseconds();
if (cur_time - last_time_ran > 100)
{
last_time_ran = cur_time;
const std::string_view bad_code{buffer.data()};
if (bad_code.empty())
return false;
const std::string this_is_terrible{bad_code};
const auto result = blt::requests::send_get_request("http://" + this_is_terrible + "/get_stuff_bin");
if (result.size() != sizeof(needed_t))
{
BLT_WARN("Got string {}", result);
BLT_WARN("Size of string from ESP32 ({}) doesn't match the size of the struct ({})", result.size(), sizeof(needed_t));
return false;
}
std::memcpy(&data, result.data(), sizeof(needed_t));
data.position *= 25.4;
data.yaw = static_cast<float>(2 * blt::PI) - data.yaw;
// blt::mem::fromBytes<true>(result.data(), data.yaw);
// blt::mem::fromBytes<true>(result.data() + sizeof(float), data.distance);
// blt::mem::fromBytes<true>(result.data() + sizeof(float) * 2, data.position);
// BLT_TRACE("STRING {}", result);
// BLT_TRACE("GOT {} {} {} {}", data.yaw, data.distance, data.position.x(), data.position.y());
return true;
}
return false;
}
void handle_data(needed_t& data) void handle_data(needed_t& data)
{ {
if (data.distance > 8000) if (data.distance > 8000)
@ -162,6 +136,76 @@ void update(const blt::gfx::window_data& data)
static float point_size = 25; static float point_size = 25;
static sockaddr_in server_addr{};
if (send_socket == -1 && ready)
{
send_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (send_socket < 0)
throw std::runtime_error("Error creating socket");
std::memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(42069);
if (inet_pton(AF_INET, buffer.data(), &server_addr.sin_addr) <= 0)
{
close(send_socket);
throw std::runtime_error("Invalid address or address not supported.");
}
if (connect(send_socket, reinterpret_cast<sockaddr*>(&server_addr), sizeof(server_addr)) < 0)
{
close(send_socket);
throw std::runtime_error("Error connecting socket");
}
fcntl(send_socket, F_SETFL, O_NONBLOCK);
}
auto cur_time = blt::system::getCurrentTimeMilliseconds();
if (send_socket != -1 && (cur_time - last_time_ran > 100))
{
constexpr blt::u32 pack = 1;
// if (sendto(send_socket, &pack, sizeof(pack), 0, reinterpret_cast<const sockaddr*>(&server_addr), sizeof(server_addr)) < 0)
// {
// BLT_WARN("Failed to send packet!");
// }
if (send(send_socket, &pack, sizeof(pack), 0) < 0)
{
BLT_WARN("Failed to send packet!");
}
last_time_ran = cur_time;
}
static needed_t robot_data;
static char local_buff[1024];
ssize_t recv_size;
if ((recv_size = recv(send_socket, local_buff, sizeof(local_buff), 0)) > 0)
{
blt::u32 id;
std::memcpy(&id, local_buff, sizeof(blt::u32));
switch (id)
{
case 1:
{
std::memcpy(&robot_data, local_buff + sizeof(blt::u32)*2, sizeof(needed_t));
robot_data.position *= 25.4;
handle_data(robot_data);
}
break;
case 3: BLT_ERROR("Not implemented!");
break;
case 0:
point_data.point_cloud.clear();
break;
case 2: BLT_WARN("Received set target command, not supported on client!");
break;
default: BLT_WARN("Received unknown command '{}', not supported on client! Recv: {}", id, recv_size);
break;
}
}
ImGui::SetNextWindowSize(ImVec2(300, static_cast<float>(data.height))); ImGui::SetNextWindowSize(ImVec2(300, static_cast<float>(data.height)));
ImGui::SetNextWindowPos(ImVec2(0, 0)); ImGui::SetNextWindowPos(ImVec2(0, 0));
if (ImGui::Begin("Settings", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize)) if (ImGui::Begin("Settings", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize))
@ -182,11 +226,6 @@ void update(const blt::gfx::window_data& data)
} }
ImGui::End(); ImGui::End();
static needed_t robot_data;
if (check_for_request(robot_data))
{
handle_data(robot_data);
}
renderer_2d.drawPoint(blt::gfx::point2d_t{robot_data.position, 35}, blt::make_color(0, 0, 1), 3); renderer_2d.drawPoint(blt::gfx::point2d_t{robot_data.position, 35}, blt::make_color(0, 0, 1), 3);
renderer_2d.drawLine(blt::gfx::line2d_t{robot_data.position, {0, 0}}, blt::make_color(0, 0, 1), 2); renderer_2d.drawLine(blt::gfx::line2d_t{robot_data.position, {0, 0}}, blt::make_color(0, 0, 1), 2);
fr2d.render_text("Yaw " + std::to_string(robot_data.yaw), 32).setPosition(robot_data.position + blt::vec2{10, 0}); fr2d.render_text("Yaw " + std::to_string(robot_data.yaw), 32).setPosition(robot_data.position + blt::vec2{10, 0});
@ -207,6 +246,7 @@ void update(const blt::gfx::window_data& data)
void destroy(const blt::gfx::window_data&) void destroy(const blt::gfx::window_data&)
{ {
close(send_socket);
global_matrices.cleanup(); global_matrices.cleanup();
resources.cleanup(); resources.cleanup();
renderer_2d.cleanup(); renderer_2d.cleanup();
@ -217,4 +257,5 @@ void destroy(const blt::gfx::window_data&)
int main() int main()
{ {
blt::gfx::init(blt::gfx::window_data{"Draw Window", init, update, destroy}.setSyncInterval(1)); blt::gfx::init(blt::gfx::window_data{"Draw Window", init, update, destroy}.setSyncInterval(1));
std::exit(0);
} }