mcu_ab568x/app/projects/AB5682C_240/functions/func_calculator.c
2025-05-30 18:03:10 +08:00

315 lines
11 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "include.h"
#include "func.h"
#include "func_calculator_alg.h"
#define TRACE_EN 1
#if TRACE_EN
#define TRACE(...) printf(__VA_ARGS__)
#else
#define TRACE(...)
#endif
enum {
COMPO_ID_NUM_DISP = 128, //单行为显示数 ,双行为被操作数
COMPO_ID_NUM_OPERAND_DISP, //操作数
COMPO_ID_OPERATOR, //操作符
};
typedef struct f_calculator_t_ {
} f_calculator_t;
#define CALCULATOR_DISP_BTN_ITEM_CNT ((int)(sizeof(tbl_calculator_disp_btn_item) / sizeof(tbl_calculator_disp_btn_item[0])))
#define CALCULATOR_DISP_NUMBER_MAX 32 //显示最大位数
#define CAL_FIRST_LINE_Y 92 //计算器按钮首行 中心点y的高度
#define CAL_FIRST_LINE_X 30 //计算器按钮首行 中心点x的水平距离
#define CAL_ITEM_OFF_Y 44 //按钮之间的高度间隔
#define CAL_ITEM_OFF_X 60 //按钮之间的宽度间隔
typedef struct calculator_disp_btn_item_t_ {
u32 res_addr;
u16 btn_id;
s16 x;
s16 y;
} calculator_disp_btn_item_t;
//按钮item创建时遍历一下
static const calculator_disp_btn_item_t tbl_calculator_disp_btn_item[] = {
{UI_BUF_CALCULATOR_0_CLICK_BIN, BTN_0, CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 4},
{UI_BUF_CALCULATOR_POINT_CLICK_BIN, BTN_POINT, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 4},
{UI_BUF_CALCULATOR_EQUAL_CLICK_BIN, BTN_EQUAL, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, 204+42},
{UI_BUF_CALCULATOR_7_CLICK_BIN, BTN_7, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 3},
{UI_BUF_CALCULATOR_8_CLICK_BIN, BTN_8, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 3},
{UI_BUF_CALCULATOR_9_CLICK_BIN, BTN_9, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 3},
{UI_BUF_CALCULATOR_4_CLICK_BIN, BTN_4, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
{UI_BUF_CALCULATOR_5_CLICK_BIN, BTN_5, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
{UI_BUF_CALCULATOR_6_CLICK_BIN, BTN_6, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
{UI_BUF_CALCULATOR_ADD_CLICK_BIN, BTN_ADD, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y * 2},
{UI_BUF_CALCULATOR_1_CLICK_BIN, BTN_1, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y},
{UI_BUF_CALCULATOR_2_CLICK_BIN, BTN_2, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y},
{UI_BUF_CALCULATOR_3_CLICK_BIN, BTN_3, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y + CAL_ITEM_OFF_Y},
{UI_BUF_CALCULATOR_REDUCE_CLICK_BIN, BTN_SUB, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, CAL_FIRST_LINE_Y+ CAL_ITEM_OFF_Y},
// {UI_BUF_CALCULATOR_MINUS_CLICK_BIN, BTN_OPPOSITE, 43, 332},
// {UI_BUF_CALCULATOR_CE_CLICK_BIN, BTN_CE, 43, 100},
{UI_BUF_CALCULATOR_C_CLICK_BIN, BTN_C, CAL_FIRST_LINE_X, CAL_FIRST_LINE_Y},
{UI_BUF_CALCULATOR_DIVIDED_CLICK_BIN, BTN_DIV, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X, CAL_FIRST_LINE_Y},
{UI_BUF_CALCULATOR_MULTIPLY_CLICK_BIN, BTN_MUL, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *2, CAL_FIRST_LINE_Y},
{UI_BUF_CALCULATOR_DEL_CLICK_BIN, BTN_DEL, CAL_FIRST_LINE_X + CAL_ITEM_OFF_X *3, CAL_FIRST_LINE_Y },
};
//创建计算器窗体,创建窗体中不要使用功能结构体 func_cb.f_cb
compo_form_t *func_calculator_form_create(void)
{
//新建窗体和背景
compo_form_t *frm = compo_form_create(true);
compo_picturebox_t *pic = compo_picturebox_create(frm, UI_BUF_CALCULATOR_BG_BIN);
compo_picturebox_set_pos(pic, GUI_SCREEN_CENTER_X, GUI_SCREEN_CENTER_Y);
//创建按钮
compo_button_t *btn;
for (u8 idx_btn = 0; idx_btn < CALCULATOR_DISP_BTN_ITEM_CNT; idx_btn++) {
if (tbl_calculator_disp_btn_item[idx_btn].res_addr) {
btn = compo_button_create_by_image(frm, tbl_calculator_disp_btn_item[idx_btn].res_addr);
compo_button_set_pos(btn, tbl_calculator_disp_btn_item[idx_btn].x, tbl_calculator_disp_btn_item[idx_btn].y);
} else {
btn = compo_button_create(frm);
compo_button_set_location(btn, tbl_calculator_disp_btn_item[idx_btn].x, tbl_calculator_disp_btn_item[idx_btn].y, 58, 40);
}
compo_setid(btn, tbl_calculator_disp_btn_item[idx_btn].btn_id);
compo_button_set_visible(btn, false);
}
#if defined(TJD_CALCULATOR_TWO_LINE_SHOW)
//创建文本
//被操作数
compo_textbox_t *txt;
txt = compo_textbox_create(frm, CALCULATOR_DISP_NUMBER_MAX);
compo_setid(txt, COMPO_ID_NUM_DISP);
compo_textbox_set_font(txt,UI_BUF_0FONT_FONT_NUM_24_BIN);
compo_textbox_set_right_align(txt,true);
// compo_textbox_set_location(txt, GUI_SCREEN_WIDTH-32, 20, GUI_SCREEN_WIDTH-64, 40);
compo_textbox_set_pos(txt, GUI_SCREEN_WIDTH-36, 20);
// compo_textbox_set_autosize(txt, true);
compo_textbox_set(txt, gcal_get_show_str());
//操作数
txt = compo_textbox_create(frm, CALCULATOR_DISP_NUMBER_MAX);
compo_setid(txt, COMPO_ID_NUM_OPERAND_DISP);
compo_textbox_set_font(txt,UI_BUF_0FONT_FONT_NUM_24_BIN);
compo_textbox_set_right_align(txt,true);
compo_textbox_set_pos(txt, GUI_SCREEN_WIDTH-36, 48);
// compo_textbox_set(txt, gcal_get_show_operand_str());
compo_textbox_set_visible(txt,false);
//操作符号
txt = compo_textbox_create(frm, 2);
compo_setid(txt, COMPO_ID_OPERATOR);
compo_textbox_set_font(txt,UI_BUF_0FONT_FONT_NUM_24_BIN);
compo_textbox_set_right_align(txt,true);
compo_textbox_set_pos(txt, 40, 48);
// compo_textbox_set(txt, "+");
// compo_textbox_set(txt, gcal_get_show_operand_str());
compo_textbox_set_visible(txt,false);
#else
//创建文本
compo_textbox_t *txt;
txt = compo_textbox_create(frm, CALCULATOR_DISP_NUMBER_MAX);
compo_setid(txt, COMPO_ID_NUM_DISP);
compo_textbox_set_font(txt,UI_BUF_0FONT_FONT_NUM_24_BIN);
compo_textbox_set_pos(txt, GUI_SCREEN_CENTER_X, 35);
compo_textbox_set_autosize(txt, true);
compo_textbox_set(txt, gcal_get_show_str());
#endif
return frm;
}
//计算器功能事件处理
static void func_calculator_process(void)
{
#if defined(TJD_CALCULATOR_TWO_LINE_SHOW)
compo_textbox_t *txt;
txt = compo_getobj_byid(COMPO_ID_NUM_OPERAND_DISP);
if(txt)
{
//如果操作数为0则不显示
if((strcmp(gcal_get_show_operand_str(),"0") ==0)||my_strchr_is_zero(gcal_get_show_operand_str()))
// if(a == 0)
{
compo_textbox_set_visible(txt,false);
}
// else if(my_strchr_is_zero(gcal_get_show_operand_str()))
// {
// compo_textbox_set_visible(txt,false);
// }
else{
compo_textbox_set_visible(txt,TRUE);
}
}
txt = compo_getobj_byid(COMPO_ID_OPERATOR);
if(txt)
{
if(gcal_get_holding_operator() != BTN_NULL)
{
compo_textbox_set_visible(txt,true);
}
else{
compo_textbox_set_visible(txt,false);
}
}
#endif
func_process();
}
//按钮触摸效果
static void func_calculator_button_press_handle(void)
{
compo_button_t *btn = compo_getobj_byid(compo_get_button_id());
if (btn) {
compo_button_set_visible(btn, true);
}
}
//按钮释放效果
static void func_calculator_button_release_handle(void)
{
u16 hold_id = gcal_get_holding_operator();
component_t *compo = (component_t *)compo_pool_get_top();
while (compo != NULL) {
if (compo->type == COMPO_TYPE_BUTTON && compo->id != hold_id) {
compo_button_t *btn = (compo_button_t *)compo;
compo_button_set_visible(btn, false);
}
compo = compo_get_next(compo);
}
}
//单击按钮
static void func_calculator_button_click_handler(void)
{
#if defined(TJD_CALCULATOR_TWO_LINE_SHOW)
int id = compo_get_button_id();
compo_textbox_t *txt;
gcalc_btn_click_handler(id);
txt = compo_getobj_byid(COMPO_ID_NUM_DISP);
compo_textbox_set(txt, gcal_get_show_str());
u8 len = strlen(gcal_get_show_str());
compo_textbox_set_pos(txt, GUI_SCREEN_WIDTH-36-len*6, 20);
txt = compo_getobj_byid(COMPO_ID_NUM_OPERAND_DISP);
compo_textbox_set(txt, gcal_get_show_operand_str());
u8 len1 = strlen(gcal_get_show_operand_str());
compo_textbox_set_pos(txt, GUI_SCREEN_WIDTH-36-len1*6, 48);
txt = compo_getobj_byid(COMPO_ID_OPERATOR);
if(txt){
switch (id) {
case BTN_DIV:
compo_textbox_set(txt, "÷");
break;
case BTN_MUL:
compo_textbox_set(txt, "×");
break;
case BTN_SUB:
compo_textbox_set(txt, "-");
break;
case BTN_ADD:
compo_textbox_set(txt, "+");
break;
default:
break;
}
}
func_calculator_button_release_handle();
#else
int id = compo_get_button_id();
compo_textbox_t *txt = compo_getobj_byid(COMPO_ID_NUM_DISP);
gcalc_btn_click_handler(id);
compo_textbox_set(txt, gcal_get_show_str());
func_calculator_button_release_handle();
#endif
}
//计算器功能消息处理
static void func_calculator_message(size_msg_t msg)
{
switch (msg) {
case MSG_CTP_TOUCH:
func_calculator_button_press_handle();
break;
case MSG_CTP_SHORT_UP:
case MSG_CTP_SHORT_DOWN:
case MSG_CTP_SHORT_LEFT:
case MSG_CTP_LONG:
func_calculator_button_release_handle();
if (func_cb.flag_sort) {
func_message(msg);
}
break;
case MSG_CTP_SHORT_RIGHT:
func_calculator_button_release_handle();
func_message(msg);
break;
case MSG_CTP_CLICK:
func_calculator_button_click_handler();
break;
default:
func_message(msg);
break;
}
}
//进入计算器功能
static void func_calculator_enter(void)
{
func_cb.f_cb = func_zalloc(sizeof(f_calculator_t));
func_cb.frm_main = func_calculator_form_create();
if (gcal_cb_init() == false) {
halt(HALT_MALLOC);
}
}
//退出计算器功能
static void func_calculator_exit(void)
{
gcal_cb_destroy();
}
//计算器功能
void func_calculator(void)
{
printf("%s\n", __func__);
func_calculator_enter();
while (func_cb.sta == FUNC_CALCULATOR) {
func_calculator_process();
func_calculator_message(msg_dequeue());
}
func_calculator_exit();
}