diff --git a/qt_gui/calc_main.h b/qt_gui/calc_main.h index 3e01196..9f89bbb 100644 --- a/qt_gui/calc_main.h +++ b/qt_gui/calc_main.h @@ -4,13 +4,14 @@ #define QT_GUI_CALC_MAIN_H -#include +#include extern const char KEY_MAP[20]; extern int8_t NewKeyBuf[4]; -extern QMutex KeyMutex; +extern QSemaphore KeysAvailable; +extern QSemaphore LcdAvailable; extern volatile uint8_t new_key_write_i; extern volatile uint8_t new_key_read_i; diff --git a/qt_gui/calculator.cpp b/qt_gui/calculator.cpp index ee4e56b..7c3305b 100644 --- a/qt_gui/calculator.cpp +++ b/qt_gui/calculator.cpp @@ -5,15 +5,15 @@ Calculator::Calculator(QObject *parent) : QObject(parent), - m_lcdText("calculator initial text"), - m_timer(this) + lcd_thread(*this), + m_lcdText("calculator initial text") { qDebug() << "Starting calculator thread"; calc_thread.start(); qDebug() << "calculator thread started"; - - QObject::connect(&m_timer, &QTimer::timeout, this, &Calculator::updateLcd); - m_timer.start(200); + qDebug() << "Starting lcd thread"; + lcd_thread.start(); + qDebug() << "lcd thread started"; updateLcd(); } @@ -21,6 +21,7 @@ Calculator::Calculator(QObject *parent) : Calculator::~Calculator(){ ExitCalcMain = 1; while (!calc_thread.isFinished()); //TODO: timeout + while (!lcd_thread.isFinished()); //TODO: timeout } void Calculator::buttonClicked(const QString& in) { @@ -35,17 +36,15 @@ void Calculator::buttonClicked(const QString& in) { // 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(); + KeysAvailable.release(); } void Calculator::updateLcd() { @@ -69,8 +68,24 @@ void Calculator::setLcdText(const QString &lcdText){ } +CalcLcdThread::CalcLcdThread(Calculator &calc) : + m_calc(calc) +{ -void CalcThread::run() { +} + +void CalcLcdThread::run(){ + while(1){ + if (ExitCalcMain){ + return; + } + LcdAvailable.acquire(); + m_calc.updateLcd(); + } +} + + +void CalcMainThread::run() { calc_main(); } diff --git a/qt_gui/calculator.h b/qt_gui/calculator.h index 3de2395..79c11cf 100644 --- a/qt_gui/calculator.h +++ b/qt_gui/calculator.h @@ -4,10 +4,12 @@ #include #include #include -#include #include -class CalcThread : public QThread + +class Calculator; //forward declare + +class CalcMainThread : public QThread //thread runs code in main.c { Q_OBJECT @@ -16,6 +18,19 @@ protected: }; +class CalcLcdThread : public QThread //thread just checks if we need to update lcd +{ + Q_OBJECT + +public: + explicit CalcLcdThread(Calculator& calc); +protected: + void run() override; +private: + Calculator& m_calc; +}; + + class Calculator : public QObject @@ -39,13 +54,17 @@ public slots: void updateLcd(); private: - CalcThread calc_thread; + CalcMainThread calc_thread; + CalcLcdThread lcd_thread; QString m_lcdText; - QTimer m_timer; }; + + + + #endif //include guard diff --git a/src/main.c b/src/main.c index 226f847..227b1e5 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ #include "utils.h" #ifdef DESKTOP #include -#include +#include #else #include "stc15.h" #endif @@ -28,7 +28,8 @@ static const char KEY_MAP[20] = { #ifdef DESKTOP -QMutex KeyMutex; +QSemaphore KeysAvailable(1); +QSemaphore LcdAvailable(1); #endif int8_t NewKeyBuf[4]; @@ -147,6 +148,7 @@ int main() uint8_t no_lift = 0; uint8_t exp_i = 0; int8_t disp_exponent; + NewKeyEmpty = 1; //initially empty #ifdef DEBUG_KEYS uint8_t j = 0; const uint8_t* keys; @@ -209,7 +211,7 @@ int main() ///get new key #ifdef DESKTOP - KeyMutex.lock(); + KeysAvailable.acquire(); #endif if (!NewKeyEmpty){ int8_t i_key = NewKeyBuf[new_key_read_i]; @@ -218,7 +220,6 @@ int main() NewKeyEmpty = 1; } #ifdef DESKTOP - KeyMutex.unlock(); printf("\nprocessing key %c (r=%d, w=%d, e=%d)\n", KEY_MAP[i_key], new_key_read_i, new_key_write_i, NewKeyEmpty); printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i); @@ -386,9 +387,6 @@ int main() } //switch(KEY_MAP[i_key]) } else { //else for (if found new key pressed) //no new key pressed -#ifdef DESKTOP - KeyMutex.unlock(); -#endif continue; } @@ -474,6 +472,7 @@ int main() print_lcd(); printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i); print_entry_bufs(); + LcdAvailable.release(); #endif //turn backlight back on BACKLIGHT_ON();