2019-04-03 06:07:15 +02:00
|
|
|
#include <QDebug>
|
2019-04-04 06:33:42 +02:00
|
|
|
#include "calc_main.h"
|
|
|
|
#include "../src/lcd.h"
|
2019-04-03 06:07:15 +02:00
|
|
|
#include "calculator.h"
|
|
|
|
|
|
|
|
Calculator::Calculator(QObject *parent) :
|
2019-04-04 06:33:42 +02:00
|
|
|
QObject(parent),
|
2019-04-04 07:54:02 +02:00
|
|
|
lcd_thread(*this),
|
|
|
|
m_lcdText("calculator initial text")
|
2019-04-03 06:07:15 +02:00
|
|
|
{
|
2019-04-04 07:54:02 +02:00
|
|
|
qDebug() << "Starting lcd thread";
|
|
|
|
lcd_thread.start();
|
|
|
|
qDebug() << "lcd thread started";
|
2019-04-05 08:10:10 +02:00
|
|
|
qDebug() << "Starting calculator thread";
|
|
|
|
calc_thread.start();
|
|
|
|
qDebug() << "calculator thread started";
|
2019-04-04 07:32:15 +02:00
|
|
|
|
|
|
|
updateLcd();
|
2019-04-04 06:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Calculator::~Calculator(){
|
2019-04-04 08:04:20 +02:00
|
|
|
quit();
|
2019-04-04 06:33:42 +02:00
|
|
|
while (!calc_thread.isFinished()); //TODO: timeout
|
2019-04-04 07:54:02 +02:00
|
|
|
while (!lcd_thread.isFinished()); //TODO: timeout
|
2019-04-03 06:07:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 08:04:20 +02:00
|
|
|
void Calculator::quit(){
|
|
|
|
ExitCalcMain = 1;
|
|
|
|
LcdAvailable.release();
|
|
|
|
KeysAvailable.release();
|
|
|
|
qDebug() << "quitting...";
|
|
|
|
}
|
|
|
|
|
2019-04-03 06:07:15 +02:00
|
|
|
void Calculator::buttonClicked(const QString& in) {
|
2019-04-04 06:33:42 +02:00
|
|
|
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
|
|
|
|
#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");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NewKeyBuf[new_key_write_i] = keycode;
|
|
|
|
INCR_NEW_KEY_I(new_key_write_i);
|
|
|
|
NewKeyEmpty = 0;
|
2019-04-04 07:54:02 +02:00
|
|
|
KeysAvailable.release();
|
2019-04-04 06:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Calculator::updateLcd() {
|
|
|
|
QString tmp("lcd text:\n");
|
|
|
|
const char* lcd_buf = get_lcd_buf();
|
|
|
|
for (int i = 0; i < MAX_ROWS; i++){
|
2019-04-04 07:32:15 +02:00
|
|
|
tmp += "|";
|
2019-04-04 06:33:42 +02:00
|
|
|
for (int j = 0; j < MAX_CHARS_PER_LINE; j++){
|
|
|
|
tmp += lcd_buf[j + i*MAX_CHARS_PER_LINE];
|
|
|
|
}
|
2019-04-04 07:32:15 +02:00
|
|
|
tmp += "|\n";
|
2019-04-04 06:33:42 +02:00
|
|
|
}
|
|
|
|
// qDebug() << "update lcd:" << tmp.toStdString().c_str();
|
|
|
|
|
|
|
|
setLcdText(tmp);
|
2019-04-03 06:07:15 +02:00
|
|
|
}
|
2019-04-04 06:33:42 +02:00
|
|
|
|
2019-04-04 07:32:15 +02:00
|
|
|
void Calculator::setLcdText(const QString &lcdText){
|
|
|
|
m_lcdText = lcdText;
|
|
|
|
emit lcdTextChanged();
|
|
|
|
}
|
2019-04-04 06:33:42 +02:00
|
|
|
|
|
|
|
|
2019-04-04 07:54:02 +02:00
|
|
|
CalcLcdThread::CalcLcdThread(Calculator &calc) :
|
|
|
|
m_calc(calc)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalcLcdThread::run(){
|
|
|
|
while(1){
|
|
|
|
if (ExitCalcMain){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LcdAvailable.acquire();
|
|
|
|
m_calc.updateLcd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 06:33:42 +02:00
|
|
|
|
2019-04-04 07:54:02 +02:00
|
|
|
void CalcMainThread::run() {
|
2019-04-04 06:33:42 +02:00
|
|
|
calc_main();
|
|
|
|
}
|
|
|
|
|