working exponent math

This commit is contained in:
tofasthacker 2023-09-27 21:27:44 -04:00
parent c8de75ccd9
commit 86695db883

View File

@ -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", Symbols::backspace, "\n", "4", "5", "6", "+ -", "\n", "1", "2", "3", "* /", "\n", "0", ". ^", "(-)", "=", ""};
Calculator::Calculator() {
resultLabel = lv_label_create(lv_scr_act(), nullptr);
@ -72,6 +72,7 @@ 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,9 +114,28 @@ void Calculator::HandleInput() {
break;
case '.':
if (offset == FIXED_POINT_OFFSET) {
offset /= 10;
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;
}
UpdateOperation();
NRF_LOG_INFO(". offset: %" PRId64, offset);
NRF_LOG_INFO(". value: %" PRId64, value);
@ -219,7 +239,7 @@ void Calculator::HandleInput() {
break;
}
NRF_LOG_INFO("Operation End: %c", operation);
UpdateValueLabel();
UpdateResultLabel();
}
@ -250,6 +270,18 @@ void Calculator::UpdateOperation() const {
lv_obj_set_style_local_bg_grad_color(buttonMatrix, LV_BTNMATRIX_PART_BTN, LV_STATE_CHECKED, Colors::deepOrange);
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);
break;
default:
lv_btnmatrix_clear_btn_ctrl_all(buttonMatrix, LV_BTNMATRIX_CTRL_CHECK_STATE);
break;
@ -390,6 +422,16 @@ void Calculator::Eval() {
result *= FIXED_POINT_OFFSET;
result /= value;
break;
case '^':
// check for zero division
if (value == 0) {
error = Error::ZeroDivision;
break;
}
result /= FIXED_POINT_OFFSET;
result = pow(result, (value*0.0001))*10000;
break;
default:
break;