diff --git a/src/main.cpp b/src/main.cpp
index cb45b5d..42cd82b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -15,12 +15,18 @@
* along with this program. If not, see .
*/
#include
+#include
+#include
#include
#include
+#include
+#include
#include
#include
#include
#include
+#include
+#include
#include "blt/gfx/renderer/batch_2d_renderer.h"
#include "blt/gfx/renderer/camera.h"
#include "blt/gfx/renderer/resource_manager.h"
@@ -40,6 +46,8 @@ std::array buffer;
size_t last_time_ran = 0;
size_t last_time_ran1 = 0;
+int send_socket = -1;
+
bool ready = false;
struct everything_t
@@ -66,8 +74,6 @@ struct needed_t
blt::vec2f position;
};
-blt::vec2 current_position;
-
struct boy_trust_t
{
std::deque point_cloud;
@@ -97,45 +103,13 @@ struct boy_trust_t
}
} 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(2 * blt::PI) - data.yaw;
- // blt::mem::fromBytes(result.data(), data.yaw);
- // blt::mem::fromBytes(result.data() + sizeof(float), data.distance);
- // blt::mem::fromBytes(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)
{
if (data.distance > 8000)
return;
blt::vec2f current_position;
- current_position[0] = data.position[0] + data.distance * std::cos(data.yaw + static_cast(blt::PI/2.0f));
- current_position[1] = data.position[1] + data.distance * std::sin(data.yaw + static_cast(blt::PI/2.0f));
+ current_position[0] = data.position[0] + data.distance * std::cos(data.yaw + static_cast(blt::PI / 2.0f));
+ current_position[1] = data.position[1] + data.distance * std::sin(data.yaw + static_cast(blt::PI / 2.0f));
point_data.point_cloud.push_back(current_position);
}
@@ -162,6 +136,76 @@ void update(const blt::gfx::window_data& data)
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(&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(&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(data.height)));
ImGui::SetNextWindowPos(ImVec2(0, 0));
if (ImGui::Begin("Settings", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize))
@@ -182,11 +226,6 @@ void update(const blt::gfx::window_data& data)
}
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.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});
@@ -207,6 +246,7 @@ void update(const blt::gfx::window_data& data)
void destroy(const blt::gfx::window_data&)
{
+ close(send_socket);
global_matrices.cleanup();
resources.cleanup();
renderer_2d.cleanup();
@@ -217,4 +257,5 @@ void destroy(const blt::gfx::window_data&)
int main()
{
blt::gfx::init(blt::gfx::window_data{"Draw Window", init, update, destroy}.setSyncInterval(1));
+ std::exit(0);
}