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

View file

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