Initial demo for Promo-Operation
This commit is contained in:
1 parent
55b7909717
commit
eb1c84cf05
25 files changed
+24615
-2
No files matched your search
@@ -0,0 +1,169 @@
|
||||
#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 <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "MqttClient.hpp"
|
||||
#include "coreExcept.hpp"
|
||||
#include "gpioPort.hpp"
|
||||
#include "logging.hpp"
|
||||
#include "queue.hpp"
|
||||
#include "textUtils.hpp"
|
||||
#include "thread.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 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();
|
||||
}
|
||||
|
||||
pal::InputTransAware& SwordEntry;
|
||||
pal::InputTransAware& SwordEnd;
|
||||
pal::InputTransAware& SwordPulled;
|
||||
LinMot& SwordLatch;
|
||||
|
||||
std::chrono::seconds PromoDelay{150};
|
||||
int Volume{50};
|
||||
bool Mute{false};
|
||||
};
|
||||
|
||||
class SwordConfig {
|
||||
public:
|
||||
std::string getSoundPromo() { return soundPromo; }
|
||||
std::string getSoundDeny() { return soundDeny; }
|
||||
std::string getSoundAllow() { return soundAllow; }
|
||||
std::string getBrockerUrl() { return brockerUrl; }
|
||||
std::string getMqttUser() { return mqttUser; }
|
||||
std::string getMqttPassword() { return mqttPassword; }
|
||||
std::string getStationPath() { return stationPath; }
|
||||
|
||||
void setVar(std::string base,
|
||||
std::string brocker, std::string topic,
|
||||
std::string user, std::string passwd) {
|
||||
if (wasSet) {
|
||||
return;
|
||||
}
|
||||
soundPromo = base + soundPromo;
|
||||
soundDeny = base + soundDeny;
|
||||
soundAllow = base + soundAllow;
|
||||
|
||||
brockerUrl = brocker;
|
||||
stationPath = topic;
|
||||
mqttUser = user;
|
||||
mqttPassword = passwd;
|
||||
wasSet = true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool wasSet{};
|
||||
std::string assetBase{};
|
||||
std::string soundPromo{"/promo.wav"};
|
||||
std::string soundPromoShort{"/promoShort.wav"};
|
||||
std::string soundDeny{"/deny.wav"};
|
||||
std::string soundAllow{"/allow.wav"};
|
||||
|
||||
std::string brockerUrl;
|
||||
std::string mqttUser;
|
||||
std::string mqttPassword;
|
||||
std::string stationPath;
|
||||
};
|
||||
|
||||
class MediaPlayer {
|
||||
public:
|
||||
static void playWavFile(const string& filePath) {
|
||||
if (filePath.empty()) {
|
||||
fmt::println(stderr, "ERROR: Empty file path");
|
||||
return;
|
||||
}
|
||||
thread audioThread([filePath]() {
|
||||
auto cmd = fmt::format("aplay '{}' 2>/dev/null", filePath);
|
||||
auto result = system(cmd.c_str());
|
||||
if (result != 0) {
|
||||
fmt::println(stderr, "ERROR: Failed to play audio file: '{}'", filePath);
|
||||
}
|
||||
});
|
||||
audioThread.detach();
|
||||
}
|
||||
|
||||
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 '{}' {}% 2>/dev/null", 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 {};
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user