moved backspace and changed around the math for exponent

(cherry picked from commit c87a297a2f)
This commit is contained in:
tofasthacker 2023-09-28 20:35:36 -04:00 committed by Josh
parent a73933f5fa
commit 1114911926
2 changed files with 98 additions and 74 deletions

View File

@ -17,7 +17,7 @@ Calculator::~Calculator() {
} }
static const char* buttonMap[] = { static const char* buttonMap[] = {
"7", "8", "9", Symbols::backspace, "\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() { Calculator::Calculator() {
resultLabel = lv_label_create(lv_scr_act(), nullptr); resultLabel = lv_label_create(lv_scr_act(), nullptr);
@ -25,14 +25,14 @@ Calculator::Calculator() {
lv_label_set_align(resultLabel, LV_LABEL_ALIGN_RIGHT); lv_label_set_align(resultLabel, LV_LABEL_ALIGN_RIGHT);
lv_label_set_text_fmt(resultLabel, "%" PRId64, result); lv_label_set_text_fmt(resultLabel, "%" PRId64, result);
lv_obj_set_size(resultLabel, 200, 20); lv_obj_set_size(resultLabel, 200, 20);
lv_obj_set_pos(resultLabel, 10, 5); lv_obj_set_pos(resultLabel, -30, 5);
valueLabel = lv_label_create(lv_scr_act(), nullptr); valueLabel = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_long_mode(valueLabel, LV_LABEL_LONG_CROP); lv_label_set_long_mode(valueLabel, LV_LABEL_LONG_CROP);
lv_label_set_align(valueLabel, LV_LABEL_ALIGN_RIGHT); lv_label_set_align(valueLabel, LV_LABEL_ALIGN_RIGHT);
lv_label_set_text_fmt(valueLabel, "%" PRId64, value); lv_label_set_text_fmt(valueLabel, "%" PRId64, value);
lv_obj_set_size(valueLabel, 200, 20); lv_obj_set_size(valueLabel, 200, 20);
lv_obj_set_pos(valueLabel, 10, 35); lv_obj_set_pos(valueLabel, -30, 35);
buttonMatrix = lv_btnmatrix_create(lv_scr_act(), nullptr); buttonMatrix = lv_btnmatrix_create(lv_scr_act(), nullptr);
buttonMatrix->user_data = this; buttonMatrix->user_data = this;
@ -51,17 +51,84 @@ Calculator::Calculator() {
lv_obj_set_style_local_bg_opa(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, LV_OPA_COVER); lv_obj_set_style_local_bg_opa(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, LV_OPA_COVER);
lv_obj_set_style_local_bg_grad_stop(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, 128); lv_obj_set_style_local_bg_grad_stop(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, 128);
lv_obj_set_style_local_bg_main_stop(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, 128); lv_obj_set_style_local_bg_main_stop(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, 128);
// BackSpace Button
/*
lv_style_init(&btn_style);
lv_style_set_radius(&btn_style, LV_STATE_DEFAULT, 10);
lv_style_set_bg_color(&btn_style, LV_STATE_DEFAULT, Colors::bgAlt);
/*
back = lv_btn_create(lv_scr_act(), nullptr);
back->user_data = this;
lv_obj_set_event_cb(back, eventHandler);
lv_obj_set_style_local_text_font(back, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20);
//lv_label_set_text_static(back, "A");
//lv_label_set_align(back, LV_LABEL_ALIGN_CENTER);
lv_label_set_text_static(back, "A");
lv_obj_set_size(back, 59, 44);
lv_obj_align(back, nullptr, LV_ALIGN_IN_TOP_LEFT, 179, 16);
*/
back = lv_btn_create(lv_scr_act(), nullptr);
back->user_data = this;
lv_obj_set_event_cb(back, eventHandler);
lv_obj_set_size(back, 59, 44);
lv_obj_align(back, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 179, 16);
txtback = lv_label_create(back, nullptr);
lv_obj_set_style_local_bg_color(back, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt);
lv_label_set_text_static(txtback, Symbols::backspace);
} }
void Calculator::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { void Calculator::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
if ((obj == buttonMatrix) && (event == LV_EVENT_PRESSED)) { if ((obj == buttonMatrix) && (event == LV_EVENT_PRESSED)) {
HandleInput(); HandleInput();
} }
if (obj == back && event == LV_EVENT_PRESSED) {
if (value != 0) {
// delete one value digit
if (offset < FIXED_POINT_OFFSET) {
if (offset == 0) {
offset = 1;
} else {
offset *= 10;
}
} else {
value /= 10;
}
if (offset < FIXED_POINT_OFFSET) {
value -= value % (10 * offset);
} else {
value -= value % offset;
}
} else if (offset < FIXED_POINT_OFFSET) {
if (offset == 0) {
offset = 1;
} else {
offset *= 10;
}
} else {
// reset the result
result = 0;
}
operation = ' ';
UpdateOperation();
UpdateValueLabel();
UpdateResultLabel();
}
} }
void Calculator::HandleInput() { void Calculator::HandleInput() {
const char* buttonText = lv_btnmatrix_get_active_btn_text(buttonMatrix); const char* buttonText = lv_btnmatrix_get_active_btn_text(buttonMatrix);
if (buttonText == nullptr) { if (buttonText == nullptr) {
return; return;
} }
@ -72,7 +139,6 @@ void Calculator::HandleInput() {
} }
// we only compare the first char because it is enough // we only compare the first char because it is enough
NRF_LOG_INFO("Operation Start: %c", operation);
switch (*buttonText) { switch (*buttonText) {
case '0': case '0':
case '1': case '1':
@ -113,29 +179,11 @@ void Calculator::HandleInput() {
break; break;
// Decimal
case '.': case '.':
NRF_LOG_INFO("Button Pressed");
switch (operation) {
case ' ':
if (value != 0) {
Eval();
ResetInput();
}
operation = '^';
break;
case '^':
operation = ' ';
break;
default:
if (offset == FIXED_POINT_OFFSET) { if (offset == FIXED_POINT_OFFSET) {
offset /= 10; offset /= 10;
} }
operation = '.';
break;
}
UpdateOperation();
NRF_LOG_INFO(". offset: %" PRId64, offset); NRF_LOG_INFO(". offset: %" PRId64, offset);
NRF_LOG_INFO(". value: %" PRId64, value); NRF_LOG_INFO(". value: %" PRId64, value);
@ -146,6 +194,22 @@ void Calculator::HandleInput() {
// - eval the current operator if value > FIXED_POINT_OFFSET // - eval the current operator if value > FIXED_POINT_OFFSET
// - then set the new operator // - then set the new operator
// - + and - as well as * and / cycle on the same button // - + and - as well as * and / cycle on the same button
case '^':
if (value != 0) {
Eval();
ResetInput();
}
switch (operation) {
case ' ':
operation = '^';
break;
default:
operation = ' ';
break;
}
UpdateOperation();
break;
case '+': case '+':
if (value != 0) { if (value != 0) {
Eval(); Eval();
@ -186,43 +250,6 @@ void Calculator::HandleInput() {
UpdateOperation(); UpdateOperation();
break; break;
// this is a little hacky because it matches only the first char
case Symbols::backspace[0]:
if (value != 0) {
// delete one value digit
if (offset < FIXED_POINT_OFFSET) {
if (offset == 0) {
offset = 1;
} else {
offset *= 10;
}
} else {
value /= 10;
}
if (offset < FIXED_POINT_OFFSET) {
value -= value % (10 * offset);
} else {
value -= value % offset;
}
} else if (offset < FIXED_POINT_OFFSET) {
if (offset == 0) {
offset = 1;
} else {
offset *= 10;
}
} else {
// reset the result
result = 0;
}
NRF_LOG_INFO(". offset: %" PRId64, offset);
NRF_LOG_INFO(". value: %" PRId64, value);
NRF_LOG_INFO(". result: %" PRId64, result);
operation = ' ';
UpdateOperation();
break;
case '=': case '=':
equalSignPressed = true; equalSignPressed = true;
Eval(); Eval();
@ -271,16 +298,10 @@ void Calculator::UpdateOperation() const {
lv_btnmatrix_set_btn_ctrl(buttonMatrix, 11, LV_BTNMATRIX_CTRL_CHECK_STATE); lv_btnmatrix_set_btn_ctrl(buttonMatrix, 11, LV_BTNMATRIX_CTRL_CHECK_STATE);
break; break;
case '^': case '^':
lv_obj_set_style_local_bg_grad_dir(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, LV_GRAD_DIR_HOR);
lv_obj_set_style_local_bg_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::bgAlt);
lv_obj_set_style_local_bg_grad_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::deepOrange);
lv_btnmatrix_set_btn_ctrl(buttonMatrix, 13, LV_BTNMATRIX_CTRL_CHECK_STATE);
break;
case '.':
lv_obj_set_style_local_bg_grad_dir(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, LV_GRAD_DIR_HOR); lv_obj_set_style_local_bg_grad_dir(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, LV_GRAD_DIR_HOR);
lv_obj_set_style_local_bg_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::deepOrange); lv_obj_set_style_local_bg_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::deepOrange);
lv_obj_set_style_local_bg_grad_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::bgAlt); lv_obj_set_style_local_bg_grad_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::deepOrange);
lv_btnmatrix_set_btn_ctrl(buttonMatrix, 13, LV_BTNMATRIX_CTRL_CHECK_STATE); lv_btnmatrix_set_btn_ctrl(buttonMatrix, 3, LV_BTNMATRIX_CTRL_CHECK_STATE);
break; break;
default: default:
lv_btnmatrix_clear_btn_ctrl_all(buttonMatrix, LV_BTNMATRIX_CTRL_CHECK_STATE); lv_btnmatrix_clear_btn_ctrl_all(buttonMatrix, LV_BTNMATRIX_CTRL_CHECK_STATE);
@ -428,9 +449,9 @@ void Calculator::Eval() {
error = Error::ZeroDivision; error = Error::ZeroDivision;
break; break;
} }
place_holder = result;
result /= FIXED_POINT_OFFSET; place_holder /= FIXED_POINT_OFFSET;
result = pow(result, (value*0.0001))*10000; result = pow(place_holder, (value*0.0001))*10000;
break; break;
default: default:

View File

@ -31,6 +31,9 @@ namespace Pinetime {
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* back;
lv_obj_t* txtback;
double place_holder;
void Eval(); void Eval();
void ResetInput(); void ResetInput();