This commit is contained in:
minacode 2022-12-08 20:43:47 +01:00
parent 5af0a3410d
commit cacf107c11
2 changed files with 27 additions and 47 deletions

View file

@ -2,10 +2,8 @@
#include <cmath>
#include <libraries/log/nrf_log.h>
using namespace Pinetime::Applications::Screens;
static void eventHandler(lv_obj_t* obj, lv_event_t event) {
auto app = static_cast<Calculator*>(obj->user_data);
app->OnButtonEvent(obj, event);
@ -15,12 +13,7 @@ Calculator::~Calculator() {
lv_obj_clean(lv_scr_act());
}
static const char* buttonMap[] = {
"7", "8", "9", "<", "\n",
"4", "5", "6", "+-", "\n",
"1", "2", "3", "*/", "\n",
".", "0", "=", "^", ""
};
static const char* buttonMap[] = {"7", "8", "9", "<", "\n", "4", "5", "6", "+-", "\n", "1", "2", "3", "*/", "\n", ".", "0", "=", "^", ""};
Calculator::Calculator(DisplayApp* app) : Screen(app) {
resultLabel = lv_label_create(lv_scr_act(), nullptr);
@ -186,13 +179,7 @@ void Calculator::UpdateResultLabel() {
padding++;
}
lv_label_set_text_fmt(
resultLabel,
"%ld.%0*u",
integer,
padding,
printRemainder
);
lv_label_set_text_fmt(resultLabel, "%ld.%0*u", integer, padding, printRemainder);
}
void Calculator::UpdateValueLabel() {
@ -201,19 +188,19 @@ void Calculator::UpdateValueLabel() {
long int printRemainder;
int padding;
if (offset == 0) {
printRemainder = remainder;
padding = 3;
} else {
printRemainder = remainder / (10*offset);
printRemainder = remainder / (10 * offset);
padding = 0;
// calculate the padding length as the length difference
// between FIXED_POINT_OFFSET and offset
long int tmp = FIXED_POINT_OFFSET / (10*offset);
long int tmp = FIXED_POINT_OFFSET / (10 * offset);
while (tmp > 1) {
padding ++;
padding++;
tmp /= 10;
}
}
@ -223,13 +210,7 @@ void Calculator::UpdateValueLabel() {
} else if ((offset == FIXED_POINT_OFFSET / 10) && (remainder == 0)) {
lv_label_set_text_fmt(valueLabel, "%ld.", integer);
} else {
lv_label_set_text_fmt(
valueLabel,
"%ld.%0*u",
integer,
padding,
printRemainder
);
lv_label_set_text_fmt(valueLabel, "%ld.%0*u", integer, padding, printRemainder);
}
}

View file

@ -2,37 +2,36 @@
#include "Screen.h"
namespace Pinetime {
namespace Applications {
namespace Screens {
class Calculator : public Screen {
public:
~Calculator() override;
public:
~Calculator() override;
Calculator(DisplayApp* app);
Calculator(DisplayApp* app);
void OnButtonEvent(lv_obj_t* obj, lv_event_t event);
void OnButtonEvent(lv_obj_t* obj, lv_event_t event);
private:
lv_obj_t* buttonMatrix;
lv_obj_t* valueLabel;
lv_obj_t* resultLabel;
lv_obj_t* operationLabel;
private:
lv_obj_t* buttonMatrix;
lv_obj_t* valueLabel;
lv_obj_t* resultLabel;
lv_obj_t* operationLabel;
void Eval();
void UpdateValueLabel();
void UpdateResultLabel();
void Eval();
void UpdateValueLabel();
void UpdateResultLabel();
// offset is the current offset for new digits
// standard is FIXED_POINT_OFFSET for 3 decimal places
// after typing a . this gets divided by 10 with each input
static constexpr long int FIXED_POINT_OFFSET = 1000;
long int offset = FIXED_POINT_OFFSET;
// offset is the current offset for new digits
// standard is FIXED_POINT_OFFSET for 3 decimal places
// after typing a . this gets divided by 10 with each input
static constexpr long int FIXED_POINT_OFFSET = 1000;
long int offset = FIXED_POINT_OFFSET;
long int value = 0;
long int result = 0;
char operation[2] {" "};
long int value = 0;
long int result = 0;
char operation[2] {" "};
};
}
}