use semaphores for keyboard and lcd, add lcd thread for calling updateLcd()
This commit is contained in:
parent
9484dc01fc
commit
5e04a18cd2
@ -4,13 +4,14 @@
|
|||||||
#define QT_GUI_CALC_MAIN_H
|
#define QT_GUI_CALC_MAIN_H
|
||||||
|
|
||||||
|
|
||||||
#include <QMutex>
|
#include <QSemaphore>
|
||||||
|
|
||||||
extern const char KEY_MAP[20];
|
extern const char KEY_MAP[20];
|
||||||
|
|
||||||
extern int8_t NewKeyBuf[4];
|
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_write_i;
|
||||||
extern volatile uint8_t new_key_read_i;
|
extern volatile uint8_t new_key_read_i;
|
||||||
|
@ -5,15 +5,15 @@
|
|||||||
|
|
||||||
Calculator::Calculator(QObject *parent) :
|
Calculator::Calculator(QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
m_lcdText("calculator initial text"),
|
lcd_thread(*this),
|
||||||
m_timer(this)
|
m_lcdText("calculator initial text")
|
||||||
{
|
{
|
||||||
qDebug() << "Starting calculator thread";
|
qDebug() << "Starting calculator thread";
|
||||||
calc_thread.start();
|
calc_thread.start();
|
||||||
qDebug() << "calculator thread started";
|
qDebug() << "calculator thread started";
|
||||||
|
qDebug() << "Starting lcd thread";
|
||||||
QObject::connect(&m_timer, &QTimer::timeout, this, &Calculator::updateLcd);
|
lcd_thread.start();
|
||||||
m_timer.start(200);
|
qDebug() << "lcd thread started";
|
||||||
|
|
||||||
updateLcd();
|
updateLcd();
|
||||||
}
|
}
|
||||||
@ -21,6 +21,7 @@ Calculator::Calculator(QObject *parent) :
|
|||||||
Calculator::~Calculator(){
|
Calculator::~Calculator(){
|
||||||
ExitCalcMain = 1;
|
ExitCalcMain = 1;
|
||||||
while (!calc_thread.isFinished()); //TODO: timeout
|
while (!calc_thread.isFinished()); //TODO: timeout
|
||||||
|
while (!lcd_thread.isFinished()); //TODO: timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
void Calculator::buttonClicked(const QString& in) {
|
void Calculator::buttonClicked(const QString& in) {
|
||||||
@ -35,17 +36,15 @@ void Calculator::buttonClicked(const QString& in) {
|
|||||||
// qDebug() << "keycode: " << keycode;
|
// qDebug() << "keycode: " << keycode;
|
||||||
// qDebug() << " row: " << row << ", col: " << col;
|
// qDebug() << " row: " << row << ", col: " << col;
|
||||||
//push keycode
|
//push keycode
|
||||||
KeyMutex.lock();
|
|
||||||
#define INCR_NEW_KEY_I(i) i = (i + 1) & 3
|
#define INCR_NEW_KEY_I(i) i = (i + 1) & 3
|
||||||
if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
|
if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
|
||||||
printf("ERROR: key fifo full\n");
|
printf("ERROR: key fifo full\n");
|
||||||
KeyMutex.unlock();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NewKeyBuf[new_key_write_i] = keycode;
|
NewKeyBuf[new_key_write_i] = keycode;
|
||||||
INCR_NEW_KEY_I(new_key_write_i);
|
INCR_NEW_KEY_I(new_key_write_i);
|
||||||
NewKeyEmpty = 0;
|
NewKeyEmpty = 0;
|
||||||
KeyMutex.unlock();
|
KeysAvailable.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Calculator::updateLcd() {
|
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();
|
calc_main();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,10 +4,12 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QTimer>
|
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
|
|
||||||
class CalcThread : public QThread
|
|
||||||
|
class Calculator; //forward declare
|
||||||
|
|
||||||
|
class CalcMainThread : public QThread //thread runs code in main.c
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
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
|
class Calculator : public QObject
|
||||||
@ -39,13 +54,17 @@ public slots:
|
|||||||
void updateLcd();
|
void updateLcd();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CalcThread calc_thread;
|
CalcMainThread calc_thread;
|
||||||
|
CalcLcdThread lcd_thread;
|
||||||
QString m_lcdText;
|
QString m_lcdText;
|
||||||
QTimer m_timer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif //include guard
|
#endif //include guard
|
||||||
|
13
src/main.c
13
src/main.c
@ -10,7 +10,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#ifdef DESKTOP
|
#ifdef DESKTOP
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <QMutex>
|
#include <QSemaphore>
|
||||||
#else
|
#else
|
||||||
#include "stc15.h"
|
#include "stc15.h"
|
||||||
#endif
|
#endif
|
||||||
@ -28,7 +28,8 @@ static const char KEY_MAP[20] = {
|
|||||||
|
|
||||||
|
|
||||||
#ifdef DESKTOP
|
#ifdef DESKTOP
|
||||||
QMutex KeyMutex;
|
QSemaphore KeysAvailable(1);
|
||||||
|
QSemaphore LcdAvailable(1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int8_t NewKeyBuf[4];
|
int8_t NewKeyBuf[4];
|
||||||
@ -147,6 +148,7 @@ int main()
|
|||||||
uint8_t no_lift = 0;
|
uint8_t no_lift = 0;
|
||||||
uint8_t exp_i = 0;
|
uint8_t exp_i = 0;
|
||||||
int8_t disp_exponent;
|
int8_t disp_exponent;
|
||||||
|
NewKeyEmpty = 1; //initially empty
|
||||||
#ifdef DEBUG_KEYS
|
#ifdef DEBUG_KEYS
|
||||||
uint8_t j = 0;
|
uint8_t j = 0;
|
||||||
const uint8_t* keys;
|
const uint8_t* keys;
|
||||||
@ -209,7 +211,7 @@ int main()
|
|||||||
|
|
||||||
///get new key
|
///get new key
|
||||||
#ifdef DESKTOP
|
#ifdef DESKTOP
|
||||||
KeyMutex.lock();
|
KeysAvailable.acquire();
|
||||||
#endif
|
#endif
|
||||||
if (!NewKeyEmpty){
|
if (!NewKeyEmpty){
|
||||||
int8_t i_key = NewKeyBuf[new_key_read_i];
|
int8_t i_key = NewKeyBuf[new_key_read_i];
|
||||||
@ -218,7 +220,6 @@ int main()
|
|||||||
NewKeyEmpty = 1;
|
NewKeyEmpty = 1;
|
||||||
}
|
}
|
||||||
#ifdef DESKTOP
|
#ifdef DESKTOP
|
||||||
KeyMutex.unlock();
|
|
||||||
printf("\nprocessing key %c (r=%d, w=%d, e=%d)\n",
|
printf("\nprocessing key %c (r=%d, w=%d, e=%d)\n",
|
||||||
KEY_MAP[i_key], new_key_read_i, new_key_write_i, NewKeyEmpty);
|
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);
|
printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i);
|
||||||
@ -386,9 +387,6 @@ int main()
|
|||||||
} //switch(KEY_MAP[i_key])
|
} //switch(KEY_MAP[i_key])
|
||||||
} else { //else for (if found new key pressed)
|
} else { //else for (if found new key pressed)
|
||||||
//no new key pressed
|
//no new key pressed
|
||||||
#ifdef DESKTOP
|
|
||||||
KeyMutex.unlock();
|
|
||||||
#endif
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -474,6 +472,7 @@ int main()
|
|||||||
print_lcd();
|
print_lcd();
|
||||||
printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i);
|
printf("entry_i=%d,exp_i=%d\n", entry_i, exp_i);
|
||||||
print_entry_bufs();
|
print_entry_bufs();
|
||||||
|
LcdAvailable.release();
|
||||||
#endif
|
#endif
|
||||||
//turn backlight back on
|
//turn backlight back on
|
||||||
BACKLIGHT_ON();
|
BACKLIGHT_ON();
|
||||||
|
Loading…
Reference in New Issue
Block a user