Added connection management

This commit is contained in:
2022-07-08 16:33:42 -07:00
parent fe00a30268
commit 4aa558eda7
4 changed files with 141 additions and 52 deletions
+45 -22
View File
@@ -5,6 +5,9 @@
// For reading FIFO
#include <fcntl.h>
#include <unistd.h>
// For sleep
#include <chrono>
#include <thread>
// Local files
#include "utility/bitmap.hpp"
@@ -82,37 +85,57 @@ int main(int argc, char *argv[]) {
);
// Write buffer
uint8_t hid_buf[12];
uint8_t hid_buf[Dox.packet_size];
wprintf(L"Trying to connect...\n");
while (!Dox.is_connected()) {
Dox.try_connect();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
while (1) {
if (Dox.is_connected()) {
if (Dox.get_animation_mode() == 0x02) {
buf.update();
fft.update(buf);
if (Dox.get_animation_mode() == 0x02) {
buf.update();
fft.update(buf);
hid_buf[0] = CMD_ANIM_DATA_fft;
for (size_t i = 0; i < 10; i++) {
// Get height from fft, apply bottom_skip
ssize_t h = fft.get_output()[i] - bottom_skip;
hid_buf[0] = CMD_ANIM_DATA_fft;
for (size_t i = 0; i < 10; i++) {
// Get height from fft, apply bottom_skip
ssize_t h = fft.get_output()[i] - bottom_skip;
// Enforce max and min
// max implicitly enforces top_skip
h = h>kb_resolution ? kb_resolution : h;
h = h<0 ? 0 : h;
// Enforce max and min
// max implicitly enforces top_skip
h = h>kb_resolution ? kb_resolution : h;
h = h<0 ? 0 : h;
hid_buf[i + 1] = h;
hid_buf[i + 1] = h;
}
}
Dox.write(CMD_ANIM_DATA, hid_buf, 13);
}
// Read a packet if there is a packet to read
if (Dox.read()) {
uint8_t cmd = Dox.read_buf[0];
Dox.write(CMD_ANIM_DATA, hid_buf, Dox.packet_size);
switch(cmd) {
case CMD_SEND_STATE:
break;
// Dox.write might detect that we've been disconnected,
// and Dox.read will fail if we are.
// This check prevents it from doing that, and instead jumps to reconnect.
if (!Dox.is_connected()) { continue; }
// Read a packet if there is a packet to read
if (Dox.read()) {
uint8_t cmd = Dox.read_buf[0];
switch(cmd) {
case CMD_SEND_STATE:
break;
}
}
} else {
wprintf(L"Trying to connect...\n");
while (!Dox.is_connected()) {
Dox.try_connect();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
}