Compare commits

...

4 Commits

6 changed files with 82 additions and 6 deletions

View File

@ -1,5 +1,5 @@
PROG= sndlock
SRCS= imgui_impl_glfw.cpp imgui_impl_opengl3.cpp imgui_draw.cpp imgui_widgets.cpp imgui_tables.cpp imgui.cpp sndlock.cpp
SRCS= imgui_impl_glfw.cpp imgui_impl_opengl3.cpp imgui_draw.cpp imgui_widgets.cpp imgui_tables.cpp imgui.cpp kirdy.cpp sndlock.cpp
CXXOPTS= -Wall `pkg-config --cflags glfw3 gl`
LDFLAGS= `pkg-config --libs glfw3 gl`
LDADD= -lsndio -lm

View File

@ -82,14 +82,14 @@ class Clocker {
Clocker(std::chrono::milliseconds period): period(period) {};
void tick() {
if(next_tick == std::chrono::time_point<std::chrono::steady_clock>::min()) {
next_tick = std::chrono::steady_clock::now() + period;
next_tick = std::chrono::steady_clock::now();
} else {
next_tick += period;
auto duration = next_tick - std::chrono::steady_clock::now();
if(duration >= duration.zero())
std::this_thread::sleep_for(duration);
else
std::cerr << "missed tick" << std::endl;
next_tick += period;
}
}
};

View File

33
kirdy.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <asio.hpp>
#include <nlohmann/json.hpp>
#include "kirdy.hpp"
Kirdy::Kirdy(asio::string_view host, asio::string_view service)
{
asio::ip::tcp::resolver resolver(io_context);
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(host, service);
socket = new asio::ip::tcp::socket(io_context);
asio::connect(*socket, endpoints);
}
Kirdy::~Kirdy()
{
delete socket;
}
nlohmann::json Kirdy::command(nlohmann::json &request)
{
std::string reply;
asio::write(*socket, asio::buffer(request.dump() + "\n"));
asio::read_until(*socket, asio::dynamic_buffer(reply, 1024), '\n');
return nlohmann::json::parse(reply);
}
float Kirdy::get_laser_temp()
{
nlohmann::json json;
json["device_cmd"] = "GetStatusReport";
auto reply = command(json);
return reply["thermostat"]["temperature"];
}

15
kirdy.hpp Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <asio.hpp>
#include <nlohmann/json.hpp>
class Kirdy {
private:
asio::io_context io_context;
asio::ip::tcp::socket *socket;
nlohmann::json command(nlohmann::json &request);
public:
Kirdy(asio::string_view host, asio::string_view service);
~Kirdy();
float get_laser_temp();
};

View File

@ -14,8 +14,9 @@
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "dsp_lib.h"
#include "fifo.h"
#include "dsp_lib.hpp"
#include "fifo.hpp"
#include "kirdy.hpp"
#define SND_BITS 24
#define SND_PCHAN 2
@ -75,6 +76,16 @@ static void dsp_thread()
sio_close(hdl);
return;
}
if(par.sig != 1
|| par.bits != SND_BITS
|| par.pchan != SND_PCHAN
|| par.rchan != SND_RCHAN
|| par.rate != SND_RATE
|| par.le != SIO_LE_NATIVE) {
std::cerr << "sound device parameter mismatch" << std::endl;
sio_close(hdl);
return;
}
if(!sio_start(hdl)) {
std::cerr << "failed to start sound device" << std::endl;
@ -235,12 +246,20 @@ static void dsp_thread()
sio_close(hdl);
}
static const char *kirdies[SND_PCHAN][2] = {
{"192.168.1.128", "1337"},
{"192.168.1.126", "1337"},
};
static std::atomic<double> laser_temp[SND_PCHAN];
static void servo_thread(int channel)
{
Clocker clocker = Clocker(std::chrono::milliseconds(100));
Kirdy kirdy = Kirdy(kirdies[channel][0], kirdies[channel][1]);
while(!shutdown_threads) {
clocker.tick();
std::cout << "servo thread tick " << channel << std::endl;
laser_temp[channel] = kirdy.get_laser_temp();
}
}
@ -408,6 +427,15 @@ int main(int argc, char* argv[])
}
}
if(ImGui::CollapsingHeader("Laser servo")) {
for(int i=0;i<SND_PCHAN;i++) {
char str[64];
sprintf(str, "Channel %d", i);
ImGui::SeparatorText(str);
ImGui::Text("laser temperature: %.4f", (float)laser_temp[i]);
}
}
ImGui::End();
ImGui::PopStyleVar(1);