254 lines
6.6 KiB
C++
254 lines
6.6 KiB
C++
#pragma once
|
|
|
|
#include <fcntl.h>
|
|
#include <fmt/core.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/poll.h>
|
|
#include <unistd.h>
|
|
|
|
#include <CLI/CLI.hpp>
|
|
#include <chrono>
|
|
#include <cinttypes>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "Ads1115.hpp"
|
|
#include "MqttClient.hpp"
|
|
#include "coreExcept.hpp"
|
|
#include "gpioPort.hpp"
|
|
#include "i2cPort.hpp"
|
|
#include "logging.hpp"
|
|
#include "queue.hpp"
|
|
#include "textUtils.hpp"
|
|
#include "thread.hpp"
|
|
#include "defines.hpp"
|
|
|
|
class LinMot {
|
|
public:
|
|
LinMot(OutputPort& o1, OutputPort& o2)
|
|
: _out1(o1),
|
|
_out2(o2) {
|
|
}
|
|
|
|
void moveOut() { set(true); }
|
|
void moveIn() { set(false); }
|
|
bool isLocked() { return _locked; }
|
|
|
|
private:
|
|
OutputPort& _out1;
|
|
OutputPort& _out2;
|
|
bool _locked{};
|
|
void set(bool val) {
|
|
_out1.write(val);
|
|
_out2.write(!val);
|
|
_locked = val;
|
|
}
|
|
};
|
|
|
|
class CurrMon {
|
|
// https://www.monolithicpower.com/en/documentview/productdocument/index/version/2/document_type/Datasheet/lang/EN/sku/MCS1802/document_id/5066/
|
|
public:
|
|
CurrMon(AdcAds1115& adc)
|
|
: _adc(adc) {
|
|
adc.setPga(AdcAds1115::Pga::FSR_4_096V);
|
|
adc.startContinuousConversion();
|
|
|
|
float val{};
|
|
for (size_t i = 0; i < 8; i++) {
|
|
val += sample();
|
|
}
|
|
_refVal = val / 8;
|
|
}
|
|
|
|
float sampleCurrent() {
|
|
auto val = sample() - _refVal;
|
|
val = val / 0.264;
|
|
fmt::println("{:.2} A", val);
|
|
return val;
|
|
}
|
|
|
|
private:
|
|
float _refVal{};
|
|
AdcAds1115& _adc;
|
|
|
|
float sample() {
|
|
float volt;
|
|
_adc.readConversionVoltage(volt);
|
|
return volt;
|
|
}
|
|
};
|
|
|
|
class DeviceIoState {
|
|
public:
|
|
DeviceIoState(pal::InputTransAware& entry,
|
|
pal::InputTransAware& end,
|
|
pal::InputTransAware& pulled,
|
|
LinMot& latch)
|
|
: SwordEntry(entry),
|
|
SwordEnd(end),
|
|
SwordPulled(pulled),
|
|
SwordLatch(latch) {
|
|
}
|
|
|
|
void tick() {
|
|
SwordEntry.sample();
|
|
SwordEnd.sample();
|
|
SwordPulled.sample();
|
|
}
|
|
|
|
OpMode Mode{OpMode::Maintanance};
|
|
bool GameRunning{};
|
|
pal::InputTransAware& SwordEntry;
|
|
pal::InputTransAware& SwordEnd;
|
|
pal::InputTransAware& SwordPulled;
|
|
LinMot& SwordLatch;
|
|
|
|
std::chrono::seconds PromoDelay{150};
|
|
int Volume{50};
|
|
bool Mute{true};
|
|
};
|
|
|
|
struct SwordConfig {
|
|
SwordConfig(std::string base,
|
|
std::string brocker, std::string topic,
|
|
std::string user, std::string passwd)
|
|
: AssetBase(base),
|
|
SoundPromo{base + "/promo.wav"},
|
|
SoundPromoShort{base + "/promoShort.wav"},
|
|
SoundDeny{base + "/deny.wav"},
|
|
SoundAllow{base + "/allow.wav"},
|
|
BrockerUrl(brocker),
|
|
StationPath(topic),
|
|
MqttUser(user),
|
|
MqttPassword(passwd) {
|
|
using namespace std::filesystem;
|
|
|
|
try {
|
|
for (const auto& entry : directory_iterator(base)) {
|
|
if (!entry.is_regular_file()) {
|
|
continue;
|
|
}
|
|
const std::string filename = entry.path().filename().string();
|
|
if (filename.find("Excalibur_LOSE") != std::string::npos) {
|
|
SoundsDeny.push_back(entry.path().string());
|
|
} else if (filename.find("Excalibur_WIN_") != std::string::npos) {
|
|
SoundsAllow.push_back(entry.path().string());
|
|
}
|
|
}
|
|
} catch (const filesystem_error& e) {
|
|
std::cerr << "Filesystem error: " << e.what() << '\n';
|
|
}
|
|
}
|
|
|
|
const std::string AssetBase;
|
|
|
|
const std::string SoundPromo;
|
|
const std::string SoundPromoShort;
|
|
const std::string SoundDeny;
|
|
const std::string SoundAllow;
|
|
|
|
const std::vector<std::string>& getSoundsAllow() { return SoundsAllow; }
|
|
const std::vector<std::string>& getSoundsDeny() { return SoundsDeny; }
|
|
|
|
const std::string getNextAllow() {
|
|
if (SoundsAllow.size() < 1) {
|
|
return "";
|
|
}
|
|
|
|
auto ret = SoundsAllow[idxAllow];
|
|
idxAllow++;
|
|
if (idxAllow >= SoundsAllow.size()) {
|
|
idxAllow = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
const std::string getNextDeny() {
|
|
if (SoundsDeny.size() < 1) {
|
|
return "";
|
|
}
|
|
|
|
auto ret = SoundsDeny[idxDeny];
|
|
idxDeny++;
|
|
if (idxDeny >= SoundsDeny.size()) {
|
|
idxDeny = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
const std::string BrockerUrl;
|
|
const std::string StationPath;
|
|
const std::string MqttUser;
|
|
const std::string MqttPassword;
|
|
|
|
private:
|
|
std::vector<std::string> SoundsAllow;
|
|
std::vector<std::string> SoundsDeny;
|
|
int idxAllow{};
|
|
int idxDeny{};
|
|
};
|
|
|
|
class MediaPlayer {
|
|
public:
|
|
static void playWavFileAsync(const string& filePath) {
|
|
auto audioThread = playWavFileThread(filePath);
|
|
audioThread.detach();
|
|
}
|
|
static void playWavFileSync(const string& filePath) {
|
|
auto audioThread = playWavFileThread(filePath);
|
|
audioThread.join();
|
|
}
|
|
|
|
static void setVolume(int volume) {
|
|
if (volume < 0) {
|
|
volume = 0;
|
|
}
|
|
if (volume > 100) {
|
|
volume = 100;
|
|
}
|
|
static const std::string control = findVolumeControl();
|
|
if (control.empty()) {
|
|
fmt::println(stderr, "ERROR: No ALSA volume control found");
|
|
return;
|
|
}
|
|
auto cmd = fmt::format("amixer -q sset '{}' {}%", control, volume);
|
|
auto result = system(cmd.c_str());
|
|
if (result != 0) {
|
|
fmt::println(stderr, "ERROR: Failed to set volume to {}%", volume);
|
|
}
|
|
}
|
|
|
|
private:
|
|
static std::string findVolumeControl() {
|
|
for (const auto& name : {"Speaker", "PCM", "Master", "Headphones", "Headphone"}) {
|
|
auto cmd = fmt::format("amixer sget '{}' 2>/dev/null", name);
|
|
if (system(cmd.c_str()) == 0) {
|
|
return name;
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
static thread playWavFileThread(const string& filePath) {
|
|
if (filePath.empty()) {
|
|
fmt::println(stderr, "ERROR: Empty file path");
|
|
thread([](){ Thread::sleep(2s); });
|
|
}
|
|
return thread([filePath]() {
|
|
auto cmd = fmt::format("aplay '{}'", filePath);
|
|
auto result = system(cmd.c_str());
|
|
if (result != 0) {
|
|
fmt::println(stderr, "ERROR: Failed to play audio file: '{}'", filePath);
|
|
}
|
|
});
|
|
}
|
|
|
|
};
|