diff --git a/src/displayapp/screens/Calculator.cpp b/src/displayapp/screens/Calculator.cpp index ed81d093..f2bbae6e 100644 --- a/src/displayapp/screens/Calculator.cpp +++ b/src/displayapp/screens/Calculator.cpp @@ -17,7 +17,7 @@ Calculator::~Calculator() { } 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() { 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_text_fmt(resultLabel, "%" PRId64, result); 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); lv_label_set_long_mode(valueLabel, LV_LABEL_LONG_CROP); lv_label_set_align(valueLabel, LV_LABEL_ALIGN_RIGHT); lv_label_set_text_fmt(valueLabel, "%" PRId64, value); 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->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_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); + + // 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) { if ((obj == buttonMatrix) && (event == LV_EVENT_PRESSED)) { 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() { const char* buttonText = lv_btnmatrix_get_active_btn_text(buttonMatrix); + if (buttonText == nullptr) { return; } @@ -72,7 +139,6 @@ void Calculator::HandleInput() { } // we only compare the first char because it is enough - NRF_LOG_INFO("Operation Start: %c", operation); switch (*buttonText) { case '0': case '1': @@ -113,30 +179,12 @@ void Calculator::HandleInput() { break; + // Decimal 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) { - offset /= 10; - } - operation = '.'; - - break; + if (offset == FIXED_POINT_OFFSET) { + offset /= 10; } - UpdateOperation(); - + NRF_LOG_INFO(". offset: %" PRId64, offset); NRF_LOG_INFO(". value: %" PRId64, value); NRF_LOG_INFO(". result: %" PRId64, result); @@ -146,6 +194,22 @@ void Calculator::HandleInput() { // - eval the current operator if value > FIXED_POINT_OFFSET // - then set the new operator // - + 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 '+': if (value != 0) { Eval(); @@ -186,43 +250,6 @@ void Calculator::HandleInput() { UpdateOperation(); 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 '=': equalSignPressed = true; Eval(); @@ -271,16 +298,10 @@ void Calculator::UpdateOperation() const { lv_btnmatrix_set_btn_ctrl(buttonMatrix, 11, 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_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_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_btnmatrix_set_btn_ctrl(buttonMatrix, 13, LV_BTNMATRIX_CTRL_CHECK_STATE); + lv_obj_set_style_local_bg_grad_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::deepOrange); + lv_btnmatrix_set_btn_ctrl(buttonMatrix, 3, LV_BTNMATRIX_CTRL_CHECK_STATE); break; default: lv_btnmatrix_clear_btn_ctrl_all(buttonMatrix, LV_BTNMATRIX_CTRL_CHECK_STATE); @@ -428,9 +449,9 @@ void Calculator::Eval() { error = Error::ZeroDivision; break; } - - result /= FIXED_POINT_OFFSET; - result = pow(result, (value*0.0001))*10000; + place_holder = result; + place_holder /= FIXED_POINT_OFFSET; + result = pow(place_holder, (value*0.0001))*10000; break; default: diff --git a/src/displayapp/screens/Calculator.h b/src/displayapp/screens/Calculator.h index c69807a0..852be95e 100644 --- a/src/displayapp/screens/Calculator.h +++ b/src/displayapp/screens/Calculator.h @@ -31,6 +31,9 @@ namespace Pinetime { lv_obj_t* buttonMatrix {}; lv_obj_t* valueLabel {}; lv_obj_t* resultLabel {}; + lv_obj_t* back; + lv_obj_t* txtback; + double place_holder; void Eval(); void ResetInput();