Initial Commit
This commit is contained in:
commit
c13016275b
41 changed files with 3596 additions and 0 deletions
123
src/VUtils/Environment.cpp
Normal file
123
src/VUtils/Environment.cpp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
#include <VUtils/Environment.h>
|
||||
#include <VUtils/FileHandler.h>
|
||||
#include <VUtils/StringUtils.h>
|
||||
#include <VUtils/Logging.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace VUtils {
|
||||
Environment::Environment(const char *filename) : m_filename(filename) {
|
||||
m_env[""] = "";
|
||||
}
|
||||
Environment::Environment() {
|
||||
m_env[""] = "";
|
||||
}
|
||||
|
||||
void Environment::loadFile() {
|
||||
DBG("Load ENV-File: %s", m_filename.c_str());
|
||||
if (!FileHandler::fileExists(m_filename)) {
|
||||
WARN("Cannot Load Env-File %s! File not found", m_filename.c_str());
|
||||
return;
|
||||
}
|
||||
auto content = StringUtils::trimCopy(FileHandler::readFile(m_filename));
|
||||
auto lines = StringUtils::split(content, "\n");
|
||||
for (auto &line : lines) {
|
||||
if (line.empty()) {
|
||||
continue;
|
||||
}
|
||||
auto split = StringUtils::split(line, "=");
|
||||
if (split.size() >= 2) {
|
||||
m_env[StringUtils::trimCopy(split[0])] = StringUtils::trimCopy(split[1]);
|
||||
} else {
|
||||
m_env[StringUtils::trimCopy(split[0])] = "true";
|
||||
}
|
||||
}
|
||||
DBG("Found %d Elements for Environment File %s", m_env.size(), m_filename.c_str());
|
||||
}
|
||||
|
||||
std::string &Environment::getEnv(const std::string &name, std::string def) {
|
||||
if (m_env.contains(name)) {
|
||||
return m_env[name];
|
||||
}
|
||||
auto *val = std::getenv(std::string(m_prefix + name).c_str());
|
||||
if (val) {
|
||||
m_env[name] = std::string(val);
|
||||
return m_env[name];
|
||||
}
|
||||
if (def.empty()) {
|
||||
return m_env[""];
|
||||
}
|
||||
m_env[name] = std::move(def);
|
||||
return m_env[name];
|
||||
}
|
||||
|
||||
bool Environment::hasEnv(const std::string &name) {
|
||||
return m_env.contains(name) || std::getenv(name.c_str()) != nullptr;
|
||||
}
|
||||
|
||||
int Environment::getAsInt(const std::string &name, int def) {
|
||||
return (int) getAsDouble(name, def);
|
||||
}
|
||||
|
||||
double Environment::getAsDouble(const std::string &name, double def) {
|
||||
char *end;
|
||||
auto *v = getEnv(name, "").c_str();
|
||||
double val = (int) std::strtod(v, &end);
|
||||
if (end == v) {
|
||||
setNumber(name.c_str(), def);
|
||||
return def;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
bool Environment::getAsBool(const std::string &name) {
|
||||
return getEnv(name, "false") == "true";
|
||||
}
|
||||
|
||||
void Environment::setPrefix(std::string prefix) {
|
||||
m_prefix = std::move(prefix);
|
||||
}
|
||||
|
||||
bool Environment::saveFile() {
|
||||
// override file if not exists!
|
||||
std::stringstream stream{};
|
||||
stream << std::setprecision(4);
|
||||
for (auto &element : m_env) {
|
||||
if (element.first.empty())
|
||||
continue;
|
||||
stream << element.first << "=" << element.second << "\n";
|
||||
}
|
||||
if (!FileHandler::createDirectoryIfNotExist(m_filename)) {
|
||||
ERR("Directory not exists or cannot create for: %s", m_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
if (!FileHandler::writeFile(m_filename, stream.str())) {
|
||||
WARN("Cannot Save Env-File %s! Write failed", m_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
DBG("Saved file to: %s", m_filename.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void Environment::setFile(const char *filename) {
|
||||
m_filename = filename;
|
||||
}
|
||||
|
||||
void Environment::set(const char *name, const char *value) {
|
||||
m_env[name] = value;
|
||||
}
|
||||
|
||||
// Small hack that set numbers to max precision
|
||||
void Environment::setNumber(const char *name, double value) {
|
||||
int newValue = (int) value;
|
||||
std::ostringstream out;
|
||||
|
||||
if (value != newValue) {
|
||||
out.precision(4);
|
||||
} else {
|
||||
out.precision(0);
|
||||
}
|
||||
out << std::fixed << value;
|
||||
m_env[name] = out.str();
|
||||
}
|
||||
}
|
||||
91
src/VUtils/FileHandler.cpp
Normal file
91
src/VUtils/FileHandler.cpp
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <VUtils/FileHandler.h>
|
||||
#include <VUtils/Logging.h>
|
||||
#include <pwd.h>
|
||||
|
||||
namespace VUtils {
|
||||
|
||||
bool FileHandler::fileExists(const std::string &fileName) {
|
||||
return std::filesystem::exists(fileName);
|
||||
}
|
||||
|
||||
std::string FileHandler::readFile(const std::string &fileName) {
|
||||
std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||
std::ifstream::pos_type fileSize = ifs.tellg();
|
||||
ifs.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<char> bytes(fileSize);
|
||||
ifs.read(bytes.data(), fileSize);
|
||||
|
||||
return std::string(bytes.data(), fileSize);
|
||||
}
|
||||
|
||||
bool FileHandler::writeFile(const std::string &fileName, const std::string &content) {
|
||||
try {
|
||||
std::ofstream ofs(fileName);
|
||||
ofs << content;
|
||||
ofs.close();
|
||||
} catch (std::exception &e) {
|
||||
ERR("Save Failed: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool FileHandler::isDirectory(const std::string &fileName) {
|
||||
return std::filesystem::is_directory(fileName);
|
||||
}
|
||||
|
||||
std::string FileHandler::getExtension(const std::string &fileName) {
|
||||
auto ext = std::filesystem::path(fileName).extension().string();
|
||||
if (ext.empty()) {
|
||||
return ext;
|
||||
}
|
||||
// remove first dot!
|
||||
return ext.erase(0, 1);
|
||||
}
|
||||
|
||||
int FileHandler::getFileID(const std::string &fileName) {
|
||||
return open(fileName.c_str(), O_RDONLY);
|
||||
}
|
||||
|
||||
long FileHandler::getFileSize(int fd) {
|
||||
struct stat stat_buf;
|
||||
int rc = fstat(fd, &stat_buf);
|
||||
return rc == 0 ? stat_buf.st_size : -1;
|
||||
}
|
||||
|
||||
void FileHandler::closeFD(int fd) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
std::string FileHandler::getFileName(const std::basic_string<char> &name) {
|
||||
auto p = std::filesystem::path(name);
|
||||
return p.filename().replace_extension("");
|
||||
}
|
||||
|
||||
bool FileHandler::createDirectoryIfNotExist(const std::basic_string<char> &fileName) {
|
||||
auto p = std::filesystem::path(fileName);
|
||||
if (!p.has_parent_path()) {
|
||||
return false;
|
||||
}
|
||||
if (FileHandler::isDirectory(p.parent_path())) {
|
||||
return true;
|
||||
}
|
||||
return std::filesystem::create_directories(p.parent_path());
|
||||
}
|
||||
char *FileHandler::getHomeDirectory() {
|
||||
char *homedir;
|
||||
if ((homedir = getenv("HOME")) == nullptr) {
|
||||
homedir = getpwuid(getuid())->pw_dir;
|
||||
}
|
||||
return homedir;
|
||||
}
|
||||
std::string FileHandler::getFromHomeDir(const std::basic_string<char> &path) {
|
||||
return std::string(getHomeDirectory() + path);
|
||||
}
|
||||
}
|
||||
68
src/VUtils/Logging.cpp
Normal file
68
src/VUtils/Logging.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include <VUtils/Logging.h>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
|
||||
#ifndef ERROR_COLOR
|
||||
#define ERROR_COLOR 31
|
||||
#endif
|
||||
#ifndef INFO_COLOR
|
||||
#define INFO_COLOR 34
|
||||
#endif
|
||||
#ifndef DEBUG_COLOR
|
||||
#define DEBUG_COLOR 32
|
||||
#endif
|
||||
#ifndef WARN_COLOR
|
||||
#define WARN_COLOR 33
|
||||
#endif
|
||||
|
||||
void Logging::debug(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::DBG, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logging::log(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::LOG, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logging::error(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::ERROR, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logging::warn(bool newLine, const char *file, const char *func, ...) noexcept {
|
||||
va_list args;
|
||||
va_start(args, func);
|
||||
auto log = va_arg(args, const char*);
|
||||
vprintf(Logging::format(newLine, PrintType::WARN, log, file, func).c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
std::string Logging::format(bool newLine, PrintType type, const char *log, const char *file, const char *func) {
|
||||
auto pre = getPrefix(type, std::string(VUtils::FileHandler::getFileName(file) + "::" + func).c_str());
|
||||
pre += log;
|
||||
pre += "\033[0m";
|
||||
if (newLine) pre += "\n";
|
||||
return pre;
|
||||
}
|
||||
std::string Logging::getPrefix(PrintType type, const char *s) {
|
||||
switch (type) {
|
||||
case PrintType::ERROR:
|
||||
return "\033[1;" + std::to_string(ERROR_COLOR) + "m[ERROR][" + std::string(s) + "] >> ";
|
||||
case PrintType::DBG:
|
||||
return "\033[1;" + std::to_string(DEBUG_COLOR) + "m[DEBUG][" + std::string(s) + "] >> ";
|
||||
case PrintType::WARN:
|
||||
return "\033[1;" + std::to_string(WARN_COLOR) + "m[WARN][" + std::string(s) + "] >> ";
|
||||
default:
|
||||
return "\033[1;" + std::to_string(INFO_COLOR) + "m[LOG][" + std::string(s) + "] >> ";
|
||||
}
|
||||
}
|
||||
39
src/VUtils/Pool.cpp
Normal file
39
src/VUtils/Pool.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <VUtils/Pool.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
namespace VUtils {
|
||||
Pool::Pool(const char *name) : m_name(name) {
|
||||
}
|
||||
|
||||
Pool::~Pool() {
|
||||
delete[] m_threads;
|
||||
}
|
||||
|
||||
void Pool::setThreadCount(int count) {
|
||||
if (m_isCreated) return;
|
||||
m_count = count == -1 ? std::thread::hardware_concurrency() : count;
|
||||
}
|
||||
|
||||
void Pool::joinFirstThread() {
|
||||
if (!m_isCreated) return;
|
||||
if (m_threads[0].joinable()) {
|
||||
m_threads[0].join();
|
||||
}
|
||||
}
|
||||
|
||||
void Pool::create(PoolWorker &worker) {
|
||||
if (m_isCreated) return;
|
||||
m_isCreated = true;
|
||||
m_worker = &worker;
|
||||
m_threads = new std::thread[m_count];
|
||||
for (int i = 0; i < m_count; ++i) {
|
||||
m_threads[i] = std::thread(&Pool::execute, this);
|
||||
}
|
||||
DBG("Created %d Threads for ThreadPool %s", m_count, m_name)
|
||||
}
|
||||
|
||||
void Pool::execute() {
|
||||
m_worker->run();
|
||||
}
|
||||
}
|
||||
|
||||
106
src/VUtils/StringUtils.cpp
Normal file
106
src/VUtils/StringUtils.cpp
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#include <VUtils/StringUtils.h>
|
||||
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
namespace VUtils {
|
||||
void StringUtils::leftTrim(std::string &s) {
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace))));
|
||||
}
|
||||
|
||||
void StringUtils::rightTrim(std::string &s) {
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace)))
|
||||
.base(),
|
||||
s.end());
|
||||
}
|
||||
|
||||
void StringUtils::trim(std::string &s) {
|
||||
leftTrim(s);
|
||||
rightTrim(s);
|
||||
}
|
||||
|
||||
std::string StringUtils::leftTrimCopy(std::string s) {
|
||||
leftTrim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string StringUtils::rightTrimCopy(std::string s) {
|
||||
rightTrim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string StringUtils::trimCopy(std::string s) {
|
||||
trim(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<std::string> StringUtils::split(const std::string &s, const std::string &delimiter) {
|
||||
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
|
||||
std::string token;
|
||||
std::vector<std::string> res;
|
||||
|
||||
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
|
||||
token = s.substr(pos_start, pos_end - pos_start);
|
||||
pos_start = pos_end + delim_len;
|
||||
res.push_back(token);
|
||||
}
|
||||
|
||||
res.push_back(s.substr(pos_start));
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string StringUtils::urlDecode(const std::string &val) {
|
||||
std::string ret;
|
||||
char ch;
|
||||
int i, ii;
|
||||
for (i = 0; i < val.length(); i++) {
|
||||
if (int(val[ i ]) == 37) {
|
||||
sscanf(val.substr(i + 1, 2).c_str(), "%x", &ii);
|
||||
ch = static_cast<char>(ii);
|
||||
ret += ch;
|
||||
i = i + 2;
|
||||
} else {
|
||||
ret += val[ i ];
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
std::string StringUtils::urlEncode(const std::string &value) {
|
||||
std::ostringstream escaped;
|
||||
escaped.fill('0');
|
||||
escaped << std::hex;
|
||||
|
||||
for (char c : value) {
|
||||
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
|
||||
escaped << c;
|
||||
continue;
|
||||
}
|
||||
escaped << std::uppercase;
|
||||
escaped << '%' << std::setw(2) << int((unsigned char) c);
|
||||
escaped << std::nouppercase;
|
||||
}
|
||||
|
||||
return escaped.str();
|
||||
}
|
||||
std::string StringUtils::join(const std::vector<std::string> &vector, const std::string &delimiter) {
|
||||
std::stringstream string;
|
||||
for (int i = 0; i < vector.size(); ++i) {
|
||||
if (i != 0)
|
||||
string << delimiter;
|
||||
string << vector[ i ];
|
||||
|
||||
}
|
||||
return string.str();
|
||||
}
|
||||
bool StringUtils::hasNullByte(int size, const char *string) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (string[ i ] == '\0') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}// namespace VUtils
|
||||
164
src/VulcanoLE/API/HIDHelper.cpp
Normal file
164
src/VulcanoLE/API/HIDHelper.cpp
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <fcntl.h>
|
||||
#include <execution>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <VulcanoLE/API/HIDHelper.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
#define RV_CTRL_INTERFACE 1
|
||||
#define RV_LED_INTERFACE 3
|
||||
#define RV_VENDOR 0x1e7d
|
||||
|
||||
|
||||
HIDHelper::HIDHelper() = default;
|
||||
HIDHelper::~HIDHelper() = default;
|
||||
int HIDHelper::openDevices() {
|
||||
int p = 0;
|
||||
while (m_products[ p ]) {
|
||||
unsigned short product_id = m_products[ p ];
|
||||
// For LED device, use hidapi-libusb, since we need to disconnect it
|
||||
// from the default kernel driver.
|
||||
struct hid_device_info *devs = nullptr;
|
||||
m_ledDevice = nullptr;
|
||||
devs = hid_enumerate(RV_VENDOR, product_id);
|
||||
if (!findLED(devs, product_id)) {
|
||||
if (devs) {
|
||||
hid_free_enumeration(devs);
|
||||
devs = nullptr;
|
||||
};
|
||||
if (m_ledDevice) hid_close(m_ledDevice);
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// For CTRL device, use native HIDRAW access. After sending the init
|
||||
// sequence, we will close it.
|
||||
|
||||
struct udev *udev = udev_new();
|
||||
struct udev_enumerate *enumerate = udev_enumerate_new(udev);
|
||||
udev_enumerate_add_match_subsystem(enumerate, "hidraw");
|
||||
udev_enumerate_scan_devices(enumerate);
|
||||
struct udev_list_entry *entries = udev_enumerate_get_list_entry(enumerate);
|
||||
struct udev_list_entry *cur;
|
||||
udev_list_entry_foreach(cur, entries) {
|
||||
struct udev_device *usb_dev = nullptr;
|
||||
struct udev_device *raw_dev = nullptr;
|
||||
if (ctrlDeviceWork(cur, usb_dev, raw_dev, udev, product_id)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctrl_device < 1) {
|
||||
ERR("open_device(%04hx, %04hx): No CTRL device found", RV_VENDOR, product_id);
|
||||
if (devs) {
|
||||
hid_free_enumeration(devs);
|
||||
devs = nullptr;
|
||||
};
|
||||
if (m_ledDevice) hid_close(m_ledDevice);
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
bool HIDHelper::ctrlDeviceWork(
|
||||
udev_list_entry *cur,
|
||||
udev_device *usb_dev,
|
||||
udev_device *raw_dev,
|
||||
udev *udev,
|
||||
unsigned int product_id
|
||||
) {
|
||||
const char *sysfs_path = udev_list_entry_get_name(cur);
|
||||
if (!sysfs_path)
|
||||
return false;
|
||||
|
||||
raw_dev = udev_device_new_from_syspath(udev, sysfs_path);
|
||||
const char *dev_path = udev_device_get_devnode(raw_dev);
|
||||
if (!dev_path)
|
||||
return false;
|
||||
|
||||
usb_dev = udev_device_get_parent_with_subsystem_devtype(
|
||||
raw_dev,
|
||||
"usb",
|
||||
"usb_interface");
|
||||
if (!usb_dev)
|
||||
return false;
|
||||
|
||||
const char *info = udev_device_get_sysattr_value(usb_dev, "uevent");
|
||||
if (!info)
|
||||
return false;
|
||||
|
||||
const char *itf = udev_device_get_sysattr_value(usb_dev, "bInterfaceNumber");
|
||||
if (!itf)
|
||||
return false;
|
||||
|
||||
// We're looking for vid/pid and interface number
|
||||
if (atoi(itf) == RV_CTRL_INTERFACE) {
|
||||
char searchstr[64];
|
||||
snprintf(searchstr, 64, "PRODUCT=%hx/%hx", RV_VENDOR, product_id);
|
||||
|
||||
if (strstr(info, searchstr) != nullptr) {
|
||||
ctrl_device = open(dev_path, O_RDWR);
|
||||
if (!ctrl_device) {
|
||||
ERR("open_device(%04hx, %04hx): Unable to open CTRL device at %s", RV_VENDOR, product_id,
|
||||
dev_path);
|
||||
return false;
|
||||
}
|
||||
LOG("open_device(%04hx, %04hx): CTRL interface at %s", RV_VENDOR, product_id, dev_path);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (raw_dev) udev_device_unref(raw_dev);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool HIDHelper::findLED(hid_device_info *devs, unsigned int product_id) {
|
||||
struct hid_device_info *dev = nullptr;
|
||||
dev = devs;
|
||||
while (dev) {
|
||||
if (dev->interface_number == RV_LED_INTERFACE) {
|
||||
LOG("open_device(%04hx, %04hx): LED interface at USB path %s", RV_VENDOR, product_id, dev->path);
|
||||
m_ledDevice = hid_open_path(dev->path);
|
||||
if (!m_ledDevice || hid_set_nonblocking(m_ledDevice, 1) < 0) {
|
||||
ERR("open_device(%04hx, %04hx): Unable to open LED interface %s", RV_VENDOR, product_id,
|
||||
dev->path);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
DBG("open_device(%04hx, %04hx): ignoring non-LED interface #%d", RV_VENDOR, product_id,
|
||||
dev->interface_number);
|
||||
}
|
||||
dev = dev->next;
|
||||
}
|
||||
|
||||
if (!m_ledDevice) {
|
||||
DBG("open_device(%04hx, %04hx): No LED device found", RV_VENDOR, product_id);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int HIDHelper::get_feature_report(unsigned char *buf, int size) const {
|
||||
int res = ioctl(ctrl_device, HIDIOCGFEATURE(size), buf);
|
||||
if (res < 0) {
|
||||
return 0;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int HIDHelper::send_feature_report(unsigned char *buf, int size) const {
|
||||
int res = ioctl(ctrl_device, HIDIOCSFEATURE(size), buf);
|
||||
if (res < 0) {
|
||||
return 0;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void HIDHelper::close_ctrl_device() {
|
||||
if (ctrl_device) close(ctrl_device);
|
||||
ctrl_device = 0;
|
||||
}
|
||||
194
src/VulcanoLE/Audio/AudioGrabber.cpp
Normal file
194
src/VulcanoLE/Audio/AudioGrabber.cpp
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <VulcanoLE/Audio/AudioGrabber.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
|
||||
AudioGrabber::AudioGrabber() = default;
|
||||
AudioGrabber::~AudioGrabber() = default;
|
||||
|
||||
bool AudioGrabber::read(pcm_stereo_sample *buffer, uint32_t buffer_size) {
|
||||
auto buffer_size_bytes = static_cast<size_t>(sizeof(pcm_stereo_sample) * buffer_size);
|
||||
if (m_pulseaudio_simple == nullptr) {
|
||||
open_pulseaudio_source(static_cast<uint32_t>(buffer_size_bytes));
|
||||
}
|
||||
|
||||
if (m_pulseaudio_simple != nullptr) {
|
||||
memset(buffer, 0, buffer_size_bytes);
|
||||
int32_t error_code;
|
||||
auto return_code = pa_simple_read(m_pulseaudio_simple, buffer,
|
||||
buffer_size_bytes, &error_code);
|
||||
if (return_code < 0) {
|
||||
WARN("Could not finish reading pulse Audio stream buffer\n bytes read: %d buffer\n size: %d", return_code,
|
||||
buffer_size_bytes)
|
||||
// zero out buffer
|
||||
memset(buffer, 0, buffer_size_bytes);
|
||||
pa_simple_free(m_pulseaudio_simple);
|
||||
m_pulseaudio_simple = nullptr;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Success fully read entire buffer
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void AudioGrabber::populate_default_source_name() {
|
||||
pa_mainloop_api *mainloop_api;
|
||||
pa_context *pulseaudio_context;
|
||||
m_pulseaudio_mainloop = pa_mainloop_new();
|
||||
mainloop_api = pa_mainloop_get_api(m_pulseaudio_mainloop);
|
||||
pulseaudio_context = pa_context_new(mainloop_api, "VulcanoLE device list");
|
||||
pa_context_connect(pulseaudio_context, nullptr, PA_CONTEXT_NOFLAGS,
|
||||
nullptr);
|
||||
pa_context_set_state_callback(pulseaudio_context,
|
||||
pulseaudio_context_state_callback,
|
||||
reinterpret_cast<void *>(this));
|
||||
|
||||
int ret;
|
||||
if (pa_mainloop_run(m_pulseaudio_mainloop, &ret) < 0) {
|
||||
ERR("Could not open pulseaudio mainloop to find default device name: %d", ret)
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioGrabber::open_pulseaudio_source(uint32_t max_buffer_size) {
|
||||
int32_t error_code = 0;
|
||||
static const pa_sample_spec sample_spec = { PA_SAMPLE_FLOAT32NE, k_sample_rate,
|
||||
k_channels };
|
||||
static const pa_buffer_attr buffer_attr = { max_buffer_size, 0, 0, 0,
|
||||
(max_buffer_size / 2) };
|
||||
populate_default_source_name();
|
||||
if (!m_pulseaudio_default_source_name.empty()) {
|
||||
m_pulseaudio_simple =
|
||||
pa_simple_new(nullptr, k_record_stream_name, PA_STREAM_RECORD,
|
||||
m_pulseaudio_default_source_name.c_str(),
|
||||
k_record_stream_description, &sample_spec,
|
||||
nullptr, &buffer_attr, &error_code);
|
||||
}
|
||||
if (m_pulseaudio_simple == nullptr) {
|
||||
m_pulseaudio_simple =
|
||||
pa_simple_new(nullptr, k_record_stream_name, PA_STREAM_RECORD,
|
||||
nullptr, k_record_stream_description,
|
||||
&sample_spec, nullptr, &buffer_attr, &error_code);
|
||||
}
|
||||
if (m_pulseaudio_simple == nullptr) {
|
||||
m_pulseaudio_simple =
|
||||
pa_simple_new(nullptr, k_record_stream_name, PA_STREAM_RECORD,
|
||||
"0", k_record_stream_description, &sample_spec,
|
||||
nullptr, &buffer_attr, &error_code);
|
||||
}
|
||||
if (m_pulseaudio_simple != nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ERR("Could not open pulseaudio source: %s", pa_strerror(error_code))
|
||||
return false;
|
||||
}
|
||||
|
||||
void AudioGrabber::pulseaudio_context_state_callback(pa_context *c, void *userdata) {
|
||||
switch (pa_context_get_state(c)) {
|
||||
case PA_CONTEXT_UNCONNECTED:
|
||||
case PA_CONTEXT_CONNECTING:
|
||||
case PA_CONTEXT_AUTHORIZING:
|
||||
case PA_CONTEXT_SETTING_NAME:
|
||||
break;
|
||||
|
||||
case PA_CONTEXT_READY: {
|
||||
pa_operation_unref(pa_context_get_server_info(
|
||||
c, pulseaudio_server_info_callback, userdata));
|
||||
break;
|
||||
}
|
||||
|
||||
case PA_CONTEXT_FAILED:
|
||||
case PA_CONTEXT_TERMINATED:
|
||||
auto *src =
|
||||
reinterpret_cast<AudioGrabber *>(userdata);
|
||||
pa_mainloop_quit(src->m_pulseaudio_mainloop, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioGrabber::pulseaudio_server_info_callback(pa_context *context, const pa_server_info *i, void *userdata) {
|
||||
if (i != nullptr) {
|
||||
auto *src = reinterpret_cast<AudioGrabber *>(userdata);
|
||||
std::string name = i->default_sink_name;
|
||||
name.append(k_default_monitor_postfix);
|
||||
|
||||
src->m_pulseaudio_default_source_name = name;
|
||||
|
||||
// stop mainloop after finding default name
|
||||
pa_mainloop_quit(src->m_pulseaudio_mainloop, 0);
|
||||
}
|
||||
}
|
||||
|
||||
AudioGrabber *AudioGrabber::createAudioGrabber() {
|
||||
auto *grabber = new AudioGrabber();
|
||||
return grabber;
|
||||
}
|
||||
|
||||
void AudioGrabber::init() {
|
||||
m_pcm_buffer = static_cast<pcm_stereo_sample *>(calloc(BUFFER_SIZE, sizeof(pcm_stereo_sample)));
|
||||
if (env != nullptr)
|
||||
m_scale = env->getAsDouble("audio_scale", 1.0);
|
||||
DBG("SET Audio Scale: %.3f", m_scale)
|
||||
loudness = 0.0;
|
||||
}
|
||||
|
||||
void AudioGrabber::calculateRMS(pcm_stereo_sample *pFrame) {
|
||||
float square = 0, mean;
|
||||
for (int i = 0; i < BUFFER_SIZE; i++) {
|
||||
float left = pFrame[0].l;
|
||||
square += std::pow(left, 2);
|
||||
}
|
||||
mean = (square / (float) (BUFFER_SIZE));
|
||||
loudness = std::sqrt(mean);
|
||||
}
|
||||
|
||||
void AudioGrabber::calculatePEAK(pcm_stereo_sample *pFrame) {
|
||||
float max = 0;
|
||||
for (int i = 0; i < BUFFER_SIZE; i++) {
|
||||
float left = std::abs(pFrame[0].l);
|
||||
if (left > max) {
|
||||
max = left;
|
||||
}
|
||||
}
|
||||
loudness = max;
|
||||
}
|
||||
|
||||
float AudioGrabber::getLoudness() {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
return (float) loudness;
|
||||
}
|
||||
|
||||
bool AudioGrabber::doWork() {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
if (this->read(m_pcm_buffer, BUFFER_SIZE)) {
|
||||
for (int i = 0; i < BUFFER_SIZE; ++i) {
|
||||
// my system is fucking quite
|
||||
m_pcm_buffer[i].l *= m_scale;
|
||||
m_pcm_buffer[i].r *= m_scale;
|
||||
}
|
||||
switch (requestMode) {
|
||||
case ReqMode::FFT:
|
||||
fft.process(m_pcm_buffer);
|
||||
break;
|
||||
case ReqMode::RMS:
|
||||
calculateRMS(m_pcm_buffer);
|
||||
break;
|
||||
case ReqMode::PEAK:
|
||||
calculatePEAK(m_pcm_buffer);
|
||||
break;
|
||||
default:
|
||||
fft.process(m_pcm_buffer);
|
||||
calculateRMS(m_pcm_buffer);
|
||||
calculatePEAK(m_pcm_buffer);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
DBG("Wait for Data")
|
||||
return false;
|
||||
}
|
||||
}
|
||||
55
src/VulcanoLE/Audio/FFT.cpp
Normal file
55
src/VulcanoLE/Audio/FFT.cpp
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include <VulcanoLE/Audio/FFT.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
void FFT::process(pcm_stereo_sample *pFrame) {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
prepareInput(pFrame, FFT_SIZE);
|
||||
m_fftw_plan_left = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftw_input_left,
|
||||
m_fftw_output_left, FFTW_ESTIMATE);
|
||||
m_fftw_plan_right = fftw_plan_dft_r2c_1d(static_cast<int>(BUFFER_SIZE), m_fftw_input_right,
|
||||
m_fftw_output_right, FFTW_ESTIMATE);
|
||||
fftw_execute(m_fftw_plan_left);
|
||||
fftw_execute(m_fftw_plan_right);
|
||||
fftw_destroy_plan(m_fftw_plan_left);
|
||||
fftw_destroy_plan(m_fftw_plan_right);
|
||||
for (int i = 0; i < FFT_SIZE; ++i) {
|
||||
double left = (double(*m_fftw_output_left[i]));
|
||||
double right = (double(*m_fftw_output_right[i]));
|
||||
m_sample->leftChannel[i] = left;
|
||||
m_sample->rightChannel[i] = right;
|
||||
}
|
||||
}
|
||||
|
||||
// return vector of floats!
|
||||
outputSample *FFT::getData() {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
return m_sample;
|
||||
}
|
||||
|
||||
bool FFT::prepareInput(pcm_stereo_sample *buffer, uint32_t sample_size) {
|
||||
bool is_silent = true;
|
||||
for (auto i = 0u; i < sample_size; ++i) {
|
||||
m_fftw_input_left[i] = buffer[i].l;
|
||||
m_fftw_input_right[i] = buffer[i].r;
|
||||
if (is_silent && (m_fftw_input_left[i] > 0 || m_fftw_input_right[i] > 0)) is_silent = false;
|
||||
}
|
||||
return is_silent;
|
||||
}
|
||||
|
||||
FFT::FFT() {
|
||||
m_fftw_results = (static_cast<size_t>(BUFFER_SIZE) / 2) + 1;
|
||||
|
||||
m_fftw_input_left = static_cast<double *>(
|
||||
fftw_malloc(sizeof(double) * BUFFER_SIZE));
|
||||
m_fftw_input_right = static_cast<double *>(
|
||||
fftw_malloc(sizeof(double) * BUFFER_SIZE));
|
||||
|
||||
m_fftw_output_left = static_cast<fftw_complex *>(
|
||||
fftw_malloc(sizeof(fftw_complex) * m_fftw_results));
|
||||
m_fftw_output_right = static_cast<fftw_complex *>(
|
||||
fftw_malloc(sizeof(fftw_complex) * m_fftw_results));
|
||||
}
|
||||
FFT::~FFT() {
|
||||
delete m_sample;
|
||||
}
|
||||
|
||||
37
src/VulcanoLE/Audio/VisAudioRunner.cpp
Normal file
37
src/VulcanoLE/Audio/VisAudioRunner.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include <VulcanoLE/Audio/VisAudioRunner.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
VisAudioRunner::VisAudioRunner(AudioGrabber *ag, VIZ::VisPlugins *vp) : grabber(ag), plugins(vp) {}
|
||||
|
||||
VisAudioRunner::~VisAudioRunner() {
|
||||
delete plugins;
|
||||
delete grabber;
|
||||
}
|
||||
|
||||
VisAudioRunner *VisAudioRunner::create() {
|
||||
return new VisAudioRunner(AudioGrabber::createAudioGrabber(), new VIZ::VisPlugins());
|
||||
}
|
||||
|
||||
|
||||
void VisAudioRunner::init() {
|
||||
grabber->env = env;
|
||||
plugins->env = env;
|
||||
grabber->init();
|
||||
plugins->on_startup();
|
||||
LOG("Create Visual Audio Runner Thread")
|
||||
thread = std::thread(&VisAudioRunner::run, this);
|
||||
}
|
||||
|
||||
void VisAudioRunner::run() const {
|
||||
usleep(5000);
|
||||
while (isActive) {
|
||||
if (grabber->doWork()) {
|
||||
plugins->on_tick();
|
||||
} else {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100ul));
|
||||
}
|
||||
}
|
||||
usleep(50000);
|
||||
DBG("SHUTDOWN HOOk")
|
||||
plugins->on_shutdown();
|
||||
}
|
||||
242
src/VulcanoLE/Keyboards/Vulcan121.cpp
Normal file
242
src/VulcanoLE/Keyboards/Vulcan121.cpp
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
#include <cstring>
|
||||
#include <execution>
|
||||
#include <VulcanoLE/Keyboards/Vulcan121.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
Vulcan121::Vulcan121(HIDHelper *helper)
|
||||
: m_helper(helper) {}
|
||||
|
||||
int Vulcan121::send_led_map(led_map *src, bool deleteMap) {
|
||||
int i, k;
|
||||
rgba rgb;
|
||||
unsigned char hwmap[444]{};
|
||||
unsigned char workbuf[65];
|
||||
memset(hwmap, 0, sizeof(hwmap));
|
||||
for (k = 0; k < NUM_KEYS; k++) {
|
||||
rgb = rv_fixed[ k ] ? *(rv_fixed[ k ]) : (src ? src->key[ k ] : rv_color_off);
|
||||
|
||||
rgb.r = (rgb.r > 255) ? 255 : (rgb.r < 0) ? 0 : rgb.r;
|
||||
rgb.g = (rgb.g > 255) ? 255 : (rgb.g < 0) ? 0 : rgb.g;
|
||||
rgb.b = (rgb.b > 255) ? 255 : (rgb.b < 0) ? 0 : rgb.b;
|
||||
rgb.a = (rgb.a > 255) ? 255 : (rgb.a < 0) ? 0 : rgb.a;
|
||||
double factor = rgb.a / 255.0;
|
||||
rgb.r *= factor;
|
||||
rgb.g *= factor;
|
||||
rgb.b *= factor;
|
||||
|
||||
int offset = ((k / 12) * 36) + (k % 12);
|
||||
hwmap[ offset + 0 ] = (unsigned char) rgb.r;
|
||||
hwmap[ offset + 12 ] = (unsigned char) rgb.g;
|
||||
hwmap[ offset + 24 ] = (unsigned char) rgb.b;
|
||||
}
|
||||
|
||||
// First chunk comes with header
|
||||
workbuf[ 0 ] = 0x00;
|
||||
workbuf[ 1 ] = 0xa1;
|
||||
workbuf[ 2 ] = 0x01;
|
||||
workbuf[ 3 ] = 0x01;
|
||||
workbuf[ 4 ] = 0xb4;
|
||||
memcpy(&workbuf[ 5 ], hwmap, 60);
|
||||
if (hid_write(m_helper->m_ledDevice, workbuf, 65) != 65) {
|
||||
if (deleteMap) {
|
||||
delete src;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Six more chunks
|
||||
for (i = 1; i < 7; i++) {
|
||||
workbuf[ 0 ] = 0x00;
|
||||
memcpy(&workbuf[ 1 ], &hwmap[ (i * 64) - 4 ], 64);
|
||||
if (hid_write(m_helper->m_ledDevice, workbuf, 65) != 65) {
|
||||
if (deleteMap) {
|
||||
delete src;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (deleteMap) {
|
||||
delete src;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int Vulcan121::send_led_to(rgba rgb) {
|
||||
auto *map = new led_map();
|
||||
for (auto &i : map->key) {
|
||||
i = rgb;
|
||||
}
|
||||
int st = send_led_map(map, true);
|
||||
return st;
|
||||
}
|
||||
|
||||
led_map *Vulcan121::createEmptyLEDMap() {
|
||||
return new led_map();
|
||||
}
|
||||
|
||||
bool Vulcan121::send_init_sequence() {
|
||||
LOG("Sending device init sequence...")
|
||||
unsigned char a[9] = { 0x15, 0x05, 0x07, 0x0a, 0x0b, 0x06, 0x09, 0x0d, 0x13 };
|
||||
if (!query_ctrl_report(0x0f))
|
||||
return false;
|
||||
for (auto i : a) {
|
||||
if (!send_ctrl_report(i) || !wait_for_ctrl_dev()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_helper->close_ctrl_device();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Vulcan121::query_ctrl_report(unsigned char id) {
|
||||
if (id != 0x0f) return false;
|
||||
unsigned char buffer[8] = {};
|
||||
int length = 8;
|
||||
buffer[ 0 ] = id;
|
||||
int res = m_helper->get_feature_report(buffer, length);
|
||||
if (res) {
|
||||
return true;
|
||||
}
|
||||
ERR("query_ctrl_report(%02hhx) failed", id);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Vulcan121::send_ctrl_report(unsigned char id) {
|
||||
unsigned char *buffer;
|
||||
int length;
|
||||
|
||||
switch (id) {
|
||||
case 0x15:
|
||||
buffer = (unsigned char *) "\x15\x00\x01";
|
||||
length = 3;
|
||||
break;
|
||||
case 0x05:
|
||||
buffer = (unsigned char *) "\x05\x04\x00\x04";
|
||||
length = 4;
|
||||
break;
|
||||
case 0x07:
|
||||
buffer = (unsigned char *) "\x07\x5f\x00\x3a\x00\x00\x3b\x00\x00\x3c\x00\x00\x3d\x00\x00\x3e" \
|
||||
"\x00\x00\x3f\x00\x00\x40\x00\x00\x41\x00\x00\x42\x00\x00\x43\x00" \
|
||||
"\x00\x44\x00\x00\x45\x00\x00\x46\x00\x00\x47\x00\x00\x48\x00\x00" \
|
||||
"\xb3\x00\x00\xb4\x00\x00\xb5\x00\x00\xb6\x00\x00\xc2\x00\x00\xc3" \
|
||||
"\x00\x00\xc0\x00\x00\xc1\x00\x00\xce\x00\x00\xcf\x00\x00\xcc\x00" \
|
||||
"\x00\xcd\x00\x00\x46\x00\x00\xfc\x00\x00\x48\x00\x00\xcd\x0e";
|
||||
length = 95;
|
||||
break;
|
||||
case 0x0a:
|
||||
buffer = (unsigned char *) "\x0a\x08\x00\xff\xf1\x00\x02\x02";
|
||||
length = 8;
|
||||
break;
|
||||
case 0x0b:
|
||||
buffer = (unsigned char *) "\x0b\x41\x00\x1e\x00\x00\x1f\x00\x00\x20\x00\x00\x21\x00\x00\x22" \
|
||||
"\x00\x00\x14\x00\x00\x1a\x00\x00\x08\x00\x00\x15\x00\x00\x17\x00" \
|
||||
"\x00\x04\x00\x00\x16\x00\x00\x07\x00\x00\x09\x00\x00\x0a\x00\x00" \
|
||||
"\x1d\x00\x00\x1b\x00\x00\x06\x00\x00\x19\x00\x00\x05\x00\x00\xde\x01";
|
||||
length = 65;
|
||||
break;
|
||||
case 0x06:
|
||||
buffer = (unsigned char *) "\x06\x85\x00\x3a\x29\x35\x1e\x2b\x39\xe1\xe0\x3b\x1f\x14\x1a\x04" \
|
||||
"\x64\x00\x00\x3d\x3c\x20\x21\x08\x16\x1d\xe2\x3e\x23\x22\x15\x07" \
|
||||
"\x1b\x06\x8b\x3f\x24\x00\x17\x0a\x09\x19\x91\x40\x41\x00\x1c\x18" \
|
||||
"\x0b\x05\x2c\x42\x26\x25\x0c\x0d\x0e\x10\x11\x43\x2a\x27\x2d\x12" \
|
||||
"\x0f\x36\x8a\x44\x45\x89\x2e\x13\x33\x37\x90\x46\x49\x4c\x2f\x30" \
|
||||
"\x34\x38\x88\x47\x4a\x4d\x31\x32\x00\x87\xe6\x48\x4b\x4e\x28\x52" \
|
||||
"\x50\xe5\xe7\xd2\x53\x5f\x5c\x59\x51\x00\xf1\xd1\x54\x60\x5d\x5a" \
|
||||
"\x4f\x8e\x65\xd0\x55\x61\x5e\x5b\x62\xa4\xe4\xfc\x56\x57\x85\x58" \
|
||||
"\x63\x00\x00\xc2\x24";
|
||||
length = 133;
|
||||
break;
|
||||
case 0x09:
|
||||
buffer = (unsigned char *) "\x09\x2b\x00\x49\x00\x00\x4a\x00\x00\x4b\x00\x00\x4c\x00\x00\x4d" \
|
||||
"\x00\x00\x4e\x00\x00\xa4\x00\x00\x8e\x00\x00\xd0\x00\x00\xd1\x00" \
|
||||
"\x00\x00\x00\x00\x01\x00\x00\x00\x00\xcd\x04";
|
||||
length = 43;
|
||||
break;
|
||||
case 0x0d:
|
||||
length = 443;
|
||||
buffer = (unsigned char *) "\x0d\xbb\x01\x00\x06\x0b\x05\x45\x83\xca\xca\xca\xca\xca\xca\xce" \
|
||||
"\xce\xd2\xce\xce\xd2\x19\x19\x19\x19\x19\x19\x23\x23\x2d\x23\x23" \
|
||||
"\x2d\xe0\xe0\xe0\xe0\xe0\xe0\xe3\xe3\xe6\xe3\xe3\xe6\xd2\xd2\xd5" \
|
||||
"\xd2\xd2\xd5\xd5\xd5\xd9\xd5\x00\xd9\x2d\x2d\x36\x2d\x2d\x36\x36" \
|
||||
"\x36\x40\x36\x00\x40\xe6\xe6\xe9\xe6\xe6\xe9\xe9\xe9\xec\xe9\x00" \
|
||||
"\xec\xd9\xd9\xdd\xd9\xdd\xdd\xe0\xe0\xdd\xe0\xe4\xe4\x40\x40\x4a" \
|
||||
"\x40\x4a\x4a\x53\x53\x4a\x53\x5d\x5d\xec\xec\xef\xec\xef\xef\xf2" \
|
||||
"\xf2\xef\xf2\xf5\xf5\xe4\xe4\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
|
||||
"\x00\x5d\x5d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf5\xf5\x00" \
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe4\xe4\xe8\xe8\xe8\xe8\xe8" \
|
||||
"\xeb\xeb\xeb\x00\xeb\x5d\x5d\x67\x67\x67\x67\x67\x70\x70\x70\x00" \
|
||||
"\x70\xf5\xf5\xf8\xf8\xf8\xf8\xf8\xfb\xfb\xfb\x00\xfb\xeb\xef\xef" \
|
||||
"\xef\x00\xef\xf0\xf0\xed\xf0\xf0\x00\x70\x7a\x7a\x7a\x00\x7a\x7a" \
|
||||
"\x7a\x6f\x7a\x7a\x00\xfb\xfd\xfd\xfd\x00\xfd\xf8\xf8\xea\xf8\xf8" \
|
||||
"\x00\xed\xed\xea\xed\xed\x00\xed\xea\xea\xf6\xe7\xea\x6f\x6f\x65" \
|
||||
"\x6f\x6f\x00\x6f\x65\x65\x66\x5a\x65\xea\xea\xdc\xea\xea\x00\xea" \
|
||||
"\xdc\xdc\x00\xce\xdc\xea\xe7\xe5\xe7\xe5\xe5\x00\x00\x00\x00\x00" \
|
||||
"\x00\x65\x5a\x50\x5a\x50\x50\x00\x00\x00\x00\x00\x00\xdc\xce\xc0" \
|
||||
"\xce\xc0\xc0\x00\x00\x00\x00\x00\x00\xe7\x00\x00\xe2\xe2\xe2\xe2" \
|
||||
"\xdf\xdf\xdf\xdf\xdf\x5a\x00\x00\x45\x45\x45\x45\x3b\x3b\x3b\x3b" \
|
||||
"\x3b\xce\x00\x00\xb2\xb2\xb2\xb2\xa4\xa4\xa4\xa4\xa4\xdc\xdc\xdc" \
|
||||
"\xdc\x00\xda\xda\xda\xda\xda\x00\xd7\x30\x30\x30\x30\x00\x26\x26" \
|
||||
"\x26\x26\x26\x00\x1c\x96\x96\x96\x96\x00\x88\x88\x88\x88\x88\x00" \
|
||||
"\x7a\xd7\xd7\xd7\x00\xd4\xd4\xd4\xd4\xd4\xd1\xd1\xd1\x1c\x1c\x1c" \
|
||||
"\x00\x11\x11\x11\x11\x11\x06\x06\x06\x7a\x7a\x7a\x00\x6c\x6c\x6c" \
|
||||
"\x6c\x6c\x5e\x5e\x5e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\xcf";
|
||||
break;
|
||||
case 0x13:
|
||||
buffer = (unsigned char *) "\x13\x08\x01\x00\x00\x00\x00\x00";
|
||||
length = 8;
|
||||
break;
|
||||
default: ERR("UNKNOWN BUFFER OPTION")
|
||||
return false;
|
||||
}
|
||||
int res = m_helper->send_feature_report(buffer, length);
|
||||
if (res == length) return true;
|
||||
ERR("send_ctrl_report(%02hhx) failed", id)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Vulcan121::wait_for_ctrl_dev() {
|
||||
unsigned char buffer[] = { 0x04, 0x00, 0x00, 0x00 };
|
||||
int res;
|
||||
while (true) {
|
||||
// 150ms is the magic number here, should suffice on first try.
|
||||
usleep(10000);
|
||||
res = m_helper->get_feature_report(buffer, sizeof(buffer));
|
||||
if (res) {
|
||||
if (buffer[ 1 ] == 0x01) break;
|
||||
} else {
|
||||
ERR("rv_wait_for_ctrl_device() failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int Vulcan121::getColumnsForRow(int row) {
|
||||
if (row > 5) {
|
||||
WARN("Try to Access out of Bound %d max %d", row, 5)
|
||||
return 0;
|
||||
}
|
||||
return keyMapRow[row]->count;
|
||||
}
|
||||
|
||||
// @Todo Add Columngs
|
||||
int Vulcan121::getRowsForColumns(int col) {
|
||||
if (col > 5) {
|
||||
WARN("Try to Access out of Bound %d max %d", col, 5)
|
||||
return 0;
|
||||
}
|
||||
return keyMapCols[col]->count;
|
||||
}
|
||||
|
||||
int Vulcan121::getIndex(int row, int col) {
|
||||
if (row > 5) {
|
||||
WARN("Try to Access out of Bound %d max %d", row, 5)
|
||||
return 0;
|
||||
}
|
||||
if (col > keyMapRow[ row ]->count) {
|
||||
WARN("Try to Access out of Bound %d max %d", col, keyMapRow[row]->count)
|
||||
return 0;
|
||||
}
|
||||
return keyMapRow[row]->index[col];
|
||||
}
|
||||
33
src/VulcanoLE/Scripts/Loudness.cpp
Normal file
33
src/VulcanoLE/Scripts/Loudness.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <VulcanoLE/Scripts/Loudness.h>
|
||||
|
||||
namespace VIZ {
|
||||
Loudness::Loudness(AudioGrabber *pGrabber, Vulcan121 *pVulcan121) : VIZ(pGrabber, pVulcan121) {}
|
||||
|
||||
void Loudness::on_setup() {
|
||||
keyboard->send_led_to({ 0, 0, 0, 0 });
|
||||
grabber->requestMode = AudioGrabber::ReqMode::PEAK;
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
void Loudness::on_tick() {
|
||||
auto data = Vulcan121::createEmptyLEDMap();
|
||||
float val = grabber->getLoudness();
|
||||
val = val > 1.0f ? 1.0f : val;
|
||||
double newVal = (val + lastVal) * 0.5;
|
||||
int maxCol = newVal * 24;
|
||||
for (int col = 0; col < maxCol; ++col) {
|
||||
for (int i = 0; i < keyboardData.num_rows; ++i) {
|
||||
auto index = col * i;
|
||||
if (col >= maxCol - 1) data[ 0 ].key[ index ] = { 255, 0, 0, 100 };
|
||||
else data[ 0 ].key[ index ] = { 0, 0, 255, 80 };
|
||||
}
|
||||
}
|
||||
// delete map! ;)
|
||||
lastVal = val;
|
||||
keyboard->send_led_map(data, true);
|
||||
}
|
||||
|
||||
const char *Loudness::name() {
|
||||
return m_name.c_str();
|
||||
}
|
||||
}
|
||||
43
src/VulcanoLE/Scripts/Spectrum.cpp
Normal file
43
src/VulcanoLE/Scripts/Spectrum.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <VulcanoLE/Scripts/Spectrum.h>
|
||||
|
||||
namespace VIZ {
|
||||
Spectrum::Spectrum(AudioGrabber *pGrabber, Vulcan121 *pVulcan121) : VIZ(pGrabber, pVulcan121) {}
|
||||
|
||||
void Spectrum::on_setup() {
|
||||
keyboard->send_led_to({ 0, 0, 0, 0 });
|
||||
grabber->requestMode = AudioGrabber::ReqMode::FFT;
|
||||
usleep(100000);
|
||||
}
|
||||
void Spectrum::on_tick() {
|
||||
auto map = Vulcan121::createEmptyLEDMap();
|
||||
auto data = grabber->fft.getData()->leftChannel;
|
||||
// find largest bass ~43hz (44100 / FFT_SIZE) range
|
||||
auto val = 0.0;
|
||||
for (int i = 1; i < 4; ++i) {
|
||||
if (data[ i ] > val) {
|
||||
val = data[ i ];
|
||||
}
|
||||
}
|
||||
double newVal = (val + lastVal) / 2.0;
|
||||
lastVal = val;
|
||||
int split = keyboardData.num_keys / 3;
|
||||
int x = 0;
|
||||
int colorInd = 0;
|
||||
colors[0].a = newVal;
|
||||
colors[1].a = newVal;
|
||||
colors[2].a = newVal;
|
||||
for (int key = 0; key < keyboardData.num_keys; ++key) {
|
||||
map[ 0 ].key[ key ] = colors[ colorInd ];
|
||||
x++;
|
||||
if (x > split) {
|
||||
colorInd++;
|
||||
x = 0;
|
||||
}
|
||||
}
|
||||
keyboard->send_led_map(map, true);
|
||||
}
|
||||
|
||||
const char *Spectrum::name() {
|
||||
return m_name.c_str();
|
||||
}
|
||||
}
|
||||
65
src/VulcanoLE/Scripts/WeirdSpec.cpp
Normal file
65
src/VulcanoLE/Scripts/WeirdSpec.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#include <VulcanoLE/Scripts/WeirdSpec.h>
|
||||
|
||||
namespace VIZ {
|
||||
WeirdSpec::WeirdSpec(AudioGrabber *pGrabber, Vulcan121 *pVulcan121) : VIZ(pGrabber, pVulcan121) {
|
||||
|
||||
}
|
||||
|
||||
void WeirdSpec::on_setup() {
|
||||
keyboard->send_led_to({ 0, 0, 0, 0 });
|
||||
grabber->requestMode = AudioGrabber::ReqMode::FFT;
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
void WeirdSpec::on_tick() {
|
||||
auto map = Vulcan121::createEmptyLEDMap();
|
||||
auto data = grabber->fft.getData()->leftChannel;
|
||||
auto val = 0.0;
|
||||
for (int i = 1; i < 4; ++i) {
|
||||
if (data[ i ] > val) {
|
||||
val = data[ i ];
|
||||
}
|
||||
}
|
||||
switchOnPeak(val);
|
||||
tick++;
|
||||
int colorInd = left ? 0 : 1;
|
||||
colors[ colorInd ].a = val;
|
||||
if (left) {
|
||||
for (int i = 0; i < 62; ++i) {
|
||||
int ind = i;
|
||||
map[ 0 ].key[ ind ] = colors[ colorInd ];
|
||||
}
|
||||
} else {
|
||||
for (int i = 62; i < keyboardData.num_keys; ++i) {
|
||||
int ind = i;
|
||||
map[ 0 ].key[ ind ] = colors[ colorInd ];
|
||||
}
|
||||
}
|
||||
keyboard->send_led_map(map, true);
|
||||
}
|
||||
|
||||
void WeirdSpec::switchOnPeak(double peak) {
|
||||
if (tick < 3) {
|
||||
return;
|
||||
}
|
||||
if (peak < 20) {
|
||||
lastPeak = -1;
|
||||
return;
|
||||
}
|
||||
// we dont have any data! reset ;)
|
||||
if (lastPeak == -1) {
|
||||
lastPeak = peak;
|
||||
return;
|
||||
}
|
||||
lastPeak -= decayRate;
|
||||
if (peak > 100 && peak > lastPeak + threshold) {
|
||||
left = !left;
|
||||
lastPeak = peak;
|
||||
tick = 0;
|
||||
}
|
||||
}
|
||||
|
||||
const char *WeirdSpec::name() {
|
||||
return m_name.c_str();
|
||||
}
|
||||
}
|
||||
59
src/VulcanoLE/Visual/VisPlugins.cpp
Normal file
59
src/VulcanoLE/Visual/VisPlugins.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include <VulcanoLE/Visual/VisPlugins.h>
|
||||
#include <VulcanoLE/Scripts/Spectrum.h>
|
||||
#include <VulcanoLE/Scripts/Loudness.h>
|
||||
#include <VulcanoLE/Scripts/WeirdSpec.h>
|
||||
#include <VUtils/Logging.h>
|
||||
|
||||
namespace VIZ {
|
||||
void VisPlugins::init(HIDHelper *hidHelper, AudioGrabber *audioGrabber) {
|
||||
grabber = audioGrabber;
|
||||
keyboard = new Vulcan121(hidHelper);
|
||||
viz[0] = new Spectrum(grabber, keyboard);
|
||||
viz[1] = new Loudness(grabber, keyboard);
|
||||
viz[2] = new WeirdSpec(grabber, keyboard);
|
||||
currentVis = viz[mode];
|
||||
}
|
||||
|
||||
void VisPlugins::on_startup() {
|
||||
if (!keyboard->send_init_sequence()) {
|
||||
ERR("FAILED TO INIT KEYBOARD")
|
||||
exit(1);
|
||||
}
|
||||
currentVis->on_setup();
|
||||
}
|
||||
|
||||
void VisPlugins::on_tick() {
|
||||
currentVis->on_tick();
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
void VisPlugins::on_shutdown() {
|
||||
int16_t r = env->getAsInt("shutdown_color_red", 0);
|
||||
int16_t g = env->getAsInt("shutdown_color_green", 0);
|
||||
int16_t b = env->getAsInt("shutdown_color_blue", 150);
|
||||
int16_t a = env->getAsInt("shutdown_brightness", 100);
|
||||
keyboard->send_led_to({ r, g, b, a });
|
||||
}
|
||||
|
||||
VisPlugins::~VisPlugins() {
|
||||
delete grabber;
|
||||
delete keyboard;
|
||||
for (auto &i : viz) {
|
||||
delete i;
|
||||
}
|
||||
}
|
||||
|
||||
void VisPlugins::setCurrentMode(int m) {
|
||||
if (m == 0 || m > VIZSIZE) {
|
||||
ERR("Mode Setting Failed >> Mode is not in the available range 1 - %d", VIZSIZE)
|
||||
return;
|
||||
}
|
||||
grabber->env->setNumber("visual_mode", m);
|
||||
m -= 1;
|
||||
currentVis = viz[m];
|
||||
LOG("Now Using: %s", currentVis->name());
|
||||
currentVis->on_setup();
|
||||
mode = m;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue