work on Qt GUI, still not working properly
This commit is contained in:
@ -26,4 +26,5 @@ target_link_libraries(${PROJECT}
|
||||
Qt5::Qml
|
||||
Qt5::Quick
|
||||
calc
|
||||
decn
|
||||
)
|
||||
|
23
qt_gui/calc_main.h
Normal file
23
qt_gui/calc_main.h
Normal file
@ -0,0 +1,23 @@
|
||||
//header file for main.c for gui
|
||||
|
||||
#ifndef QT_GUI_CALC_MAIN_H
|
||||
#define QT_GUI_CALC_MAIN_H
|
||||
|
||||
|
||||
#include <QMutex>
|
||||
|
||||
extern const char KEY_MAP[20];
|
||||
|
||||
extern int8_t NewKeyBuf[4];
|
||||
|
||||
extern QMutex KeyMutex;
|
||||
|
||||
extern volatile uint8_t new_key_write_i;
|
||||
extern volatile uint8_t new_key_read_i;
|
||||
extern volatile uint8_t NewKeyEmpty;
|
||||
|
||||
extern uint8_t ExitCalcMain;
|
||||
|
||||
int calc_main(void);
|
||||
|
||||
#endif //include guard
|
@ -1,12 +1,70 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "calc_main.h"
|
||||
#include "../src/lcd.h"
|
||||
#include "calculator.h"
|
||||
|
||||
Calculator::Calculator(QObject *parent) :
|
||||
QObject(parent)
|
||||
QObject(parent),
|
||||
m_lcdText("calculator initial text"),
|
||||
m_timer(this)
|
||||
{
|
||||
NewKeyEmpty = 1;
|
||||
qDebug() << "Starting calculator thread";
|
||||
calc_thread.start();
|
||||
qDebug() << "calculator thread started";
|
||||
|
||||
QObject::connect(&m_timer, &QTimer::timeout, this, &Calculator::updateLcd);
|
||||
m_timer.start(50);
|
||||
}
|
||||
|
||||
Calculator::~Calculator(){
|
||||
ExitCalcMain = 1;
|
||||
while (!calc_thread.isFinished()); //TODO: timeout
|
||||
}
|
||||
|
||||
void Calculator::buttonClicked(const QString& in) {
|
||||
qDebug() << in;
|
||||
QStringList split = in.split(",");
|
||||
int8_t row = split[0].toInt();
|
||||
int8_t col = split[1].toInt();
|
||||
//translate column from left indexed, to right indexed
|
||||
static const int NUM_COLS = 4;
|
||||
col = (NUM_COLS - 1) - col;
|
||||
//get keycode
|
||||
int8_t keycode = col + NUM_COLS*row;
|
||||
// qDebug() << "keycode: " << keycode;
|
||||
// qDebug() << " row: " << row << ", col: " << col;
|
||||
//push keycode
|
||||
KeyMutex.lock();
|
||||
#define INCR_NEW_KEY_I(i) i = (i + 1) & 3
|
||||
if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
|
||||
printf("ERROR: key fifo full\n");
|
||||
KeyMutex.unlock();
|
||||
return;
|
||||
}
|
||||
NewKeyBuf[new_key_write_i] = keycode;
|
||||
INCR_NEW_KEY_I(new_key_write_i);
|
||||
NewKeyEmpty = 0;
|
||||
KeyMutex.unlock();
|
||||
}
|
||||
|
||||
void Calculator::updateLcd() {
|
||||
QString tmp("lcd text:\n");
|
||||
const char* lcd_buf = get_lcd_buf();
|
||||
for (int i = 0; i < MAX_ROWS; i++){
|
||||
for (int j = 0; j < MAX_CHARS_PER_LINE; j++){
|
||||
tmp += lcd_buf[j + i*MAX_CHARS_PER_LINE];
|
||||
}
|
||||
tmp += '\n';
|
||||
}
|
||||
// qDebug() << "update lcd:" << tmp.toStdString().c_str();
|
||||
|
||||
setLcdText(tmp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void CalcThread::run() {
|
||||
calc_main();
|
||||
}
|
||||
|
||||
|
@ -2,19 +2,50 @@
|
||||
#define QTGUI_CALCULATOR_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QThread>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
#include <QMutex>
|
||||
|
||||
class Calculator : public QObject
|
||||
class CalcThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Calculator(QObject *parent = 0);
|
||||
|
||||
public slots:
|
||||
void buttonClicked(const QString& in);
|
||||
|
||||
|
||||
protected:
|
||||
void run() override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class Calculator : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString lcdText READ lcdText WRITE setLcdText NOTIFY lcdTextChanged)
|
||||
|
||||
|
||||
public:
|
||||
explicit Calculator(QObject *parent = 0);
|
||||
~Calculator();
|
||||
|
||||
inline QString lcdText(){return m_lcdText;}
|
||||
inline void setLcdText(const QString &lcdText){m_lcdText = lcdText;}
|
||||
|
||||
signals:
|
||||
void lcdTextChanged();
|
||||
|
||||
public slots:
|
||||
void buttonClicked(const QString& in);
|
||||
void updateLcd();
|
||||
|
||||
private:
|
||||
CalcThread calc_thread;
|
||||
QString m_lcdText;
|
||||
QTimer m_timer;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //include guard
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <QApplication>
|
||||
#include <QObject>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQmlContext>
|
||||
#include "calculator.h"
|
||||
@ -6,11 +7,14 @@
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
qmlRegisterType<Calculator>("calculator.lcd", 1, 0, "CLcd");
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
|
||||
|
||||
Calculator calculator;
|
||||
engine.rootContext()->setContextProperty("_calculator", &calculator);
|
||||
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import QtQuick 2.0
|
||||
import QtQuick.Controls 1.0
|
||||
import Qt3D.Input 2.0
|
||||
import calculator.lcd 1.0
|
||||
|
||||
ApplicationWindow
|
||||
{
|
||||
@ -14,14 +14,38 @@ ApplicationWindow
|
||||
id: base;
|
||||
spacing: 5;
|
||||
width: 4 * (100 + 5)
|
||||
height: 5 * (100 + 5) + 200
|
||||
height: 5 * (100 + 5) + 200 + 30
|
||||
|
||||
//quit button
|
||||
Rectangle{
|
||||
width: 100
|
||||
height: 25
|
||||
color: "red"
|
||||
Text {text: "Quit"}
|
||||
MouseArea {
|
||||
onClicked: Qt.quit()
|
||||
anchors.fill: parent
|
||||
}
|
||||
}
|
||||
|
||||
//lcd text from C++
|
||||
CLcd {
|
||||
id: clcd
|
||||
}
|
||||
|
||||
//LCD
|
||||
Rectangle {
|
||||
id: lcd;
|
||||
color: "gray"
|
||||
objectName: "lcd";
|
||||
color: "lightgray"
|
||||
width: 4 * (100 + 5) - 5
|
||||
height: 200
|
||||
Text {
|
||||
objectName: "lcd_text";
|
||||
text: clcd.lcdText
|
||||
anchors.centerIn: parent
|
||||
|
||||
onTextChanged: clcd.lcdText = text
|
||||
}
|
||||
}
|
||||
|
||||
//Keyboard
|
||||
@ -29,8 +53,9 @@ ApplicationWindow
|
||||
model: 5;
|
||||
//row
|
||||
delegate: Row {
|
||||
id: key_row;
|
||||
spacing: 5;
|
||||
id: key_row
|
||||
spacing: 5
|
||||
objectName: index
|
||||
//keys within row
|
||||
Repeater {
|
||||
model: 4;
|
||||
@ -38,14 +63,15 @@ ApplicationWindow
|
||||
id: key_key;
|
||||
width: 100;
|
||||
height: 100;
|
||||
color: "blue"
|
||||
color: "gray"
|
||||
border { width: 1; color: "black" }
|
||||
Text {
|
||||
text: index
|
||||
anchors.centerIn: parent
|
||||
}
|
||||
MouseHandler {
|
||||
onClicked: _calculator.buttonClicked(index)
|
||||
MouseArea {
|
||||
onClicked: _calculator.buttonClicked(parent.parent.objectName + "," + index)
|
||||
anchors.fill: parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user