v0.0.3
This commit is contained in:
parent
2b4f09bb49
commit
a934520cd9
6 changed files with 180 additions and 36 deletions
98
Main.qml
98
Main.qml
|
|
@ -15,7 +15,7 @@ Window {
|
|||
property color water_color: "#F00F7FAF"
|
||||
Material.accent: root.accent_color
|
||||
property int font_enlarged_text_size: 20
|
||||
property int font_normal_text_size: 18
|
||||
property int font_normal_text_size: 16
|
||||
property double distance: 0
|
||||
property int distance_count: 0
|
||||
// visibility: Window.FullScreen
|
||||
|
|
@ -180,7 +180,7 @@ Window {
|
|||
height: max_thres_input.height
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
color: root.accent_color
|
||||
font.pixelSize: root.font_normal_text_size
|
||||
font.pointSize: root.font_normal_text_size
|
||||
}
|
||||
|
||||
TextField {
|
||||
|
|
@ -192,8 +192,9 @@ Window {
|
|||
top: settings_label.bottom
|
||||
topMargin: 20
|
||||
}
|
||||
width: parent.width / 2
|
||||
color: root.accent_color
|
||||
font.pixelSize: root.font_normal_text_size
|
||||
font.pointSize: root.font_normal_text_size
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -208,7 +209,7 @@ Window {
|
|||
height: min_thres_input.height
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
color: root.accent_color
|
||||
font.pixelSize: root.font_normal_text_size
|
||||
font.pointSize: root.font_normal_text_size
|
||||
}
|
||||
|
||||
TextField {
|
||||
|
|
@ -220,8 +221,9 @@ Window {
|
|||
top: max_thres_label.bottom
|
||||
topMargin: 20
|
||||
}
|
||||
width: parent.width / 2
|
||||
color: root.accent_color
|
||||
font.pixelSize: root.font_normal_text_size
|
||||
font.pointSize: root.font_normal_text_size
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -236,7 +238,7 @@ Window {
|
|||
height: alert_thres_input.height
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
color: root.accent_color
|
||||
font.pixelSize: root.font_normal_text_size
|
||||
font.pointSize: root.font_normal_text_size
|
||||
}
|
||||
|
||||
TextField {
|
||||
|
|
@ -248,9 +250,29 @@ Window {
|
|||
top: min_thres_label.bottom
|
||||
topMargin: 20
|
||||
}
|
||||
width: parent.width / 2
|
||||
color: root.accent_color
|
||||
font.pixelSize: root.font_normal_text_size
|
||||
font.pointSize: root.font_normal_text_size
|
||||
}
|
||||
CheckBox {
|
||||
id: flip_bits_box
|
||||
text: "Flip Bits"
|
||||
checked: config.flipBits
|
||||
anchors {
|
||||
topMargin: 20
|
||||
top: alert_thres_label.bottom
|
||||
left: parent.left
|
||||
}
|
||||
width: parent.width - 20
|
||||
contentItem: Text {
|
||||
text: flip_bits_box.text
|
||||
font.pointSize: root.font_normal_text_size
|
||||
color: root.accent_color
|
||||
leftPadding: flip_bits_box.indicator.width + flip_bits_box.spacing
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Text {
|
||||
id: settings_error_label
|
||||
|
|
@ -259,7 +281,9 @@ Window {
|
|||
visible: settings_error_text === "" ? false : true
|
||||
anchors {
|
||||
topMargin: 20
|
||||
top: alert_thres_label.bottom
|
||||
top: flip_bits_box.bottom
|
||||
bottom: flip_bits_box.top
|
||||
bottomMargin: 10
|
||||
left: parent.left
|
||||
horizontalCenter: parent.horizontalCenter
|
||||
}
|
||||
|
|
@ -268,7 +292,52 @@ Window {
|
|||
font.pixelSize: root.font_normal_text_size
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
id: reset_averages
|
||||
anchors {
|
||||
topMargin: 10
|
||||
bottom: save_config.top
|
||||
horizontalCenter: parent.horizontalCenter
|
||||
}
|
||||
color: "#F0121212"
|
||||
width: parent.width / 2.5
|
||||
height: 60
|
||||
border {
|
||||
width: 1
|
||||
color: root.accent_color
|
||||
}
|
||||
radius: 3
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: "Reset Averages"
|
||||
color: root.accent_color
|
||||
font.pointSize: root.font_enlarged_text_size
|
||||
}
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
property color settings_rect_pressed_prev: "#00000000"
|
||||
property color settings_rect_entered_prev: "#00000000"
|
||||
onEntered: {
|
||||
settings_rect_entered_prev = save_config.color
|
||||
save_config.color = "#5A1F1F1F"
|
||||
}
|
||||
onExited: {
|
||||
save_config.color = settings_rect_entered_prev
|
||||
}
|
||||
onPressed: {
|
||||
settings_rect_pressed_prev = save_config.color
|
||||
save_config.color = "#5A1F4F1F"
|
||||
settings_popup.close();
|
||||
listener.resetAverages();
|
||||
}
|
||||
onReleased: {
|
||||
save_config.color = settings_rect_pressed_prev
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
id: save_config
|
||||
|
|
@ -310,7 +379,8 @@ Window {
|
|||
let max = parseFloat(min_thres_input.text);
|
||||
let min = parseFloat(min_thres_input.text);
|
||||
let alert = parseFloat(alert_thres_input.text);
|
||||
console.log("Parsed values:", max, min, alert);
|
||||
let flipBits = flip_bits_box.checked;
|
||||
console.log("Parsed values:", max, min, alert, flip_bits_box.checked);
|
||||
if(max < min) {
|
||||
settings_error_label.settings_error_text = "Max cannot be less than min";
|
||||
return;
|
||||
|
|
@ -319,20 +389,19 @@ Window {
|
|||
settings_error_label.settings_error_text = "Alert must be between 0 to 100";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let current_min_reading = Math.max(0, (parseFloat(min_thres_input.text)));
|
||||
let effective_max_reading = Math.max(0, (parseFloat(max_thres_input.text)) - current_min_reading);
|
||||
let alert_effective_reading = (effective_max_reading * (100 - alert) / 100) + current_min_reading;
|
||||
console.log("Alert effective reading is: " + alert_effective_reading + " for value: " + alert);
|
||||
listener.setAlert(alert_effective_reading);
|
||||
config.saveConfig(max_thres_input.text, min_thres_input.text, alert_thres_input.text);
|
||||
listener.setConfig(alert_effective_reading, flipBits);
|
||||
config.saveConfig(max_thres_input.text, min_thres_input.text, alert_thres_input.text, flip_bits_box.checked);
|
||||
settings_popup.close();
|
||||
}
|
||||
onReleased: {
|
||||
save_config.color = settings_rect_pressed_prev
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -507,7 +576,8 @@ Window {
|
|||
let alert = parseFloat(alert_thres_input.text);
|
||||
let alert_effective_reading = (effective_max_reading * (100 - alert) / 100) + current_min_reading;
|
||||
console.log("Alert effective reading is: " + alert_effective_reading + " for value: " + alert);
|
||||
listener.setAlert(alert_effective_reading);
|
||||
|
||||
listener.setConfig(alert_effective_reading, flip_bits_box.checked);
|
||||
}
|
||||
|
||||
distance_count += 1;
|
||||
|
|
|
|||
|
|
@ -22,12 +22,15 @@ ConfigInterface::ConfigInterface(QObject *parent)
|
|||
config.close();
|
||||
QJsonObject top = QJsonDocument::fromJson(data).object();
|
||||
QString max, min, alert;
|
||||
bool flipbits;
|
||||
max = top["max_threshold"].toString();
|
||||
min = top["min_threshold"].toString();
|
||||
alert = top["alert_threshold"].toString();
|
||||
flipbits = top["flip_bits"].toBool();
|
||||
setMax(max);
|
||||
setMin(min);
|
||||
setAlert(alert);
|
||||
setFlipBits(flipbits);
|
||||
}
|
||||
else {
|
||||
qDebug() << "File does not exist creating one...";
|
||||
|
|
@ -47,7 +50,7 @@ ConfigInterface::ConfigInterface(QObject *parent)
|
|||
}
|
||||
}
|
||||
|
||||
void ConfigInterface::saveConfig(QString max, QString min, QString alert)
|
||||
void ConfigInterface::saveConfig(QString max, QString min, QString alert, bool flipbits)
|
||||
{
|
||||
auto paths = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
qDebug() << "Got writable path as: " << paths;
|
||||
|
|
@ -58,6 +61,7 @@ void ConfigInterface::saveConfig(QString max, QString min, QString alert)
|
|||
top["max_threshold"] = max;
|
||||
top["min_threshold"] = min;
|
||||
top["alert_threshold"] = alert;
|
||||
top["flip_bits"] = flipbits;
|
||||
setMax(max);
|
||||
setMin(min);
|
||||
setAlert(alert);
|
||||
|
|
@ -106,3 +110,16 @@ void ConfigInterface::setMin(const QString &newMin)
|
|||
m_min = newMin;
|
||||
emit minChanged();
|
||||
}
|
||||
|
||||
bool ConfigInterface::flipBits() const
|
||||
{
|
||||
return m_flipBits;
|
||||
}
|
||||
|
||||
void ConfigInterface::setFlipBits(bool newFlipBits)
|
||||
{
|
||||
if (m_flipBits == newFlipBits)
|
||||
return;
|
||||
m_flipBits = newFlipBits;
|
||||
emit flipBitsChanged();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ class ConfigInterface : public QObject
|
|||
Q_PROPERTY(QString min READ min WRITE setMin NOTIFY minChanged FINAL)
|
||||
Q_PROPERTY(QString max READ max WRITE setMax NOTIFY maxChanged FINAL)
|
||||
Q_PROPERTY(QString alert READ alert WRITE setAlert NOTIFY alertChanged FINAL)
|
||||
Q_PROPERTY(bool flipBits READ flipBits WRITE setFlipBits NOTIFY flipBitsChanged FINAL)
|
||||
public:
|
||||
explicit ConfigInterface(QObject *parent = nullptr);
|
||||
QString alert() const;
|
||||
|
|
@ -28,18 +29,24 @@ public:
|
|||
QString min() const;
|
||||
void setMin(const QString &newMin);
|
||||
|
||||
bool flipBits() const;
|
||||
void setFlipBits(bool newFlipBits);
|
||||
|
||||
signals:
|
||||
void configData(QString max, QString min, QString alert);
|
||||
void configData(QString max, QString min, QString alert, bool flipbits);
|
||||
void alertChanged();
|
||||
void maxChanged();
|
||||
void minChanged();
|
||||
|
||||
void flipBitsChanged();
|
||||
|
||||
public slots:
|
||||
void saveConfig(QString max, QString min, QString alert);
|
||||
void saveConfig(QString max, QString min, QString alert, bool flipbits);
|
||||
private:
|
||||
QString m_alert;
|
||||
QString m_max;
|
||||
QString m_min;
|
||||
bool m_flipBits;
|
||||
};
|
||||
|
||||
#endif // CONFIGINTERFACE_H
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef MOVING_CONTAINER
|
||||
#define MOVING_CONTAINER
|
||||
|
||||
|
||||
#include <QDebug>
|
||||
using ld = long double;
|
||||
|
||||
template<typename T> class average {
|
||||
|
|
@ -21,11 +21,16 @@ public:
|
|||
|
||||
~average() {
|
||||
curr = head;
|
||||
if(!curr)
|
||||
return;
|
||||
while(curr != nullptr) {
|
||||
info *now = curr;
|
||||
curr = curr->next;
|
||||
delete now;
|
||||
}
|
||||
curr = nullptr;
|
||||
head = nullptr;
|
||||
tail = nullptr;
|
||||
}
|
||||
|
||||
unsigned int size() {
|
||||
|
|
@ -36,6 +41,22 @@ public:
|
|||
return this->avg;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
curr = head;
|
||||
if(!curr)
|
||||
return;
|
||||
while(curr != nullptr) {
|
||||
info *now = curr;
|
||||
curr = curr->next;
|
||||
delete now;
|
||||
}
|
||||
qDebug() << "Clearing";
|
||||
curr = nullptr;
|
||||
head = nullptr;
|
||||
tail = nullptr;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
void recalculate_average() {
|
||||
curr = head;
|
||||
this->avg = 0;
|
||||
|
|
|
|||
|
|
@ -39,8 +39,19 @@ void UDPListener::setDistance(double newDistance)
|
|||
emit distanceChanged();
|
||||
}
|
||||
|
||||
void UDPListener::setAlert(float distance)
|
||||
void UDPListener::resetAverages()
|
||||
{
|
||||
averages.clear();
|
||||
}
|
||||
|
||||
void UDPListener::setFlipBits(bool flip_bits)
|
||||
{
|
||||
m_flip_bits = flip_bits;
|
||||
}
|
||||
|
||||
void UDPListener::setConfig(float distance, bool flip_bits)
|
||||
{
|
||||
setFlipBits(flip_bits);
|
||||
if(!command_socket) {
|
||||
command_socket = new QUdpSocket(this);
|
||||
bool bind = command_socket->bind(8181);
|
||||
|
|
@ -57,6 +68,7 @@ void UDPListener::setAlert(float distance)
|
|||
command_socket->close();
|
||||
}
|
||||
}
|
||||
averages.clear();
|
||||
}
|
||||
|
||||
void UDPListener::socketError(QString err)
|
||||
|
|
@ -73,12 +85,25 @@ void UDPListener::startService()
|
|||
|
||||
}
|
||||
|
||||
void UDPListener::datagramRecieved(float distance)
|
||||
void UDPListener::datagramRecieved(QByteArray data1, QByteArray data2)
|
||||
{
|
||||
qDebug() << "Data received in main class: " << distance;
|
||||
averages.push_back(distance);
|
||||
std::string correct_order = "";
|
||||
#ifdef Q_OS_ANDROID
|
||||
if(!m_flip_bits)
|
||||
correct_order = "0x" + data2.toHex().toStdString();
|
||||
else
|
||||
correct_order = "0x" + data1.toHex().toStdString();
|
||||
#else
|
||||
if(!m_flip_bits)
|
||||
correct_order = "0x" + data1.toHex().toStdString();
|
||||
else
|
||||
correct_order = "0x" + data2.toHex().toStdString();
|
||||
#endif
|
||||
float data = convertToFloat(correct_order);
|
||||
qDebug() << "Data received in main class: " << data;
|
||||
averages.push_back(data);
|
||||
prev_time_point = std::chrono::high_resolution_clock::now();
|
||||
emit distanceReceived(distance, QDateTime::currentDateTime().toString("dd/mm/yyyy hh:mm:ss.z ap"));
|
||||
emit distanceReceived(data, QDateTime::currentDateTime().toString("dd/mm/yyyy hh:mm:ss.z ap"));
|
||||
if(averages.size() >= 5) {
|
||||
this->m_distance = averages.get_average();
|
||||
emit distanceChanged();
|
||||
|
|
@ -111,6 +136,14 @@ void UDPListener::connectionTimerTimedOut()
|
|||
}
|
||||
}
|
||||
|
||||
float UDPListener::convertToFloat(std::string hex)
|
||||
{
|
||||
uint32_t num;
|
||||
sscanf(hex.c_str(), "%x", &num);
|
||||
float dist = *((float*)&num);
|
||||
return dist;
|
||||
}
|
||||
|
||||
Worker::Worker(QObject *parent)
|
||||
{
|
||||
|
||||
|
|
@ -146,16 +179,8 @@ void Worker::socketDataReceived()
|
|||
ch[i] = buff[3-i];
|
||||
}
|
||||
auto data2 = QByteArray(ch,4);
|
||||
#ifdef Q_OS_ANDROID
|
||||
std::string correct_order = "0x" + data2.toHex().toStdString();
|
||||
#else
|
||||
std::string correct_order = "0x" + data2.toHex().toStdString();
|
||||
#endif
|
||||
std::string correct_order = "";
|
||||
qDebug() << "Distance received: " << correct_order;
|
||||
uint32_t num;
|
||||
sscanf(correct_order.c_str(), "%x", &num);
|
||||
float dist = *((float*)&num);
|
||||
qDebug() << "Float is: " << dist;
|
||||
emit datagramReceived(dist);
|
||||
emit datagramReceived(data, data2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public:
|
|||
explicit Worker(QObject *parent = nullptr);
|
||||
signals:
|
||||
void socketError(QString error);
|
||||
void datagramReceived(float distance);
|
||||
void datagramReceived(QByteArray data, QByteArray data2);
|
||||
public slots:
|
||||
void run();
|
||||
private slots:
|
||||
|
|
@ -41,13 +41,16 @@ signals:
|
|||
void disconnected();
|
||||
void distanceReceived(double dist, QString timestamp);
|
||||
public slots:
|
||||
void setAlert(float distance);
|
||||
void resetAverages();
|
||||
void setFlipBits(bool flip_bits);
|
||||
void setConfig(float distance, bool flip_bits);
|
||||
void socketError(QString err);
|
||||
void startService();
|
||||
void datagramRecieved(float distance);
|
||||
void datagramRecieved(QByteArray data, QByteArray data2);
|
||||
private slots:
|
||||
void destroy();
|
||||
void connectionTimerTimedOut();
|
||||
float convertToFloat(std::string hex);
|
||||
private:
|
||||
QUdpSocket *command_socket = nullptr;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> prev_time_point;
|
||||
|
|
@ -56,6 +59,7 @@ private:
|
|||
Worker *worker = nullptr;
|
||||
QThread *thread = nullptr;
|
||||
QTimer *timer = nullptr;
|
||||
bool m_flip_bits;
|
||||
};
|
||||
|
||||
#endif // UDPLISTENER_H
|
||||
|
|
|
|||
Loading…
Reference in a new issue