具体arduino代码
时间: 2023-07-03 08:21:55 浏览: 206
以下是一个使用LVGL库在Arduino上实现每行数量不一样的btnmatrix按键对齐的示例代码:
```c
#include <lvgl.h>
#define BTN_HEIGHT 50
#define BTN_WIDTH 100
void setup() {
lv_init();
// 创建主题
static lv_style_t style;
lv_style_init(&style);
lv_style_set_bg_color(&style, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_style_set_bg_color(&style, LV_STATE_PRESSED, LV_COLOR_GRAY);
lv_style_set_border_width(&style, LV_STATE_DEFAULT, 0);
lv_style_set_text_color(&style, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_theme_t *th = lv_theme_alien_init(100, NULL);
lv_theme_set_current(th);
// 创建屏幕
lv_obj_t *scr = lv_disp_get_scr_act(NULL);
// 创建btnmatrix
static const char *btnm_map[] = {"1", "2", "3", "4", "\n", "5", "6", "7", "\n", "8", "9", "0", ""};
lv_obj_t *btnm = lv_btnm_create(scr, NULL);
lv_obj_set_size(btnm, LV_HOR_RES, LV_VER_RES / 2);
lv_obj_align(btnm, NULL, LV_ALIGN_CENTER, 0, 0);
lv_btnm_set_map(btnm, btnm_map);
lv_btnm_set_action(btnm, btnm_action);
lv_btnm_set_style(btnm, LV_BTNM_STYLE_BG, &style);
lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_REL, &style);
lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_PR, &style);
lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_TGL_REL, &style);
lv_btnm_set_style(btnm, LV_BTNM_STYLE_BTN_TGL_PR, &style);
// 设置每个按键的大小和位置
lv_coord_t btn_x = 0;
lv_coord_t btn_y = 0;
lv_coord_t max_height = 0;
lv_coord_t max_width = 0;
uint8_t btn_count = 0;
uint8_t row_count = 0;
for (uint8_t i = 0; i < strlen(btnm_map); i++) {
char c = btnm_map[i][0];
if (c == '\n') {
row_count++;
btn_x = 0;
btn_y += max_height;
max_height = 0;
max_width = 0;
btn_count = 0;
continue;
}
lv_obj_t *btn = lv_btn_create(btnm, NULL);
lv_obj_set_size(btn, BTN_WIDTH, BTN_HEIGHT);
lv_obj_set_pos(btn, btn_x, btn_y);
lv_obj_set_style(btn, &style);
lv_obj_t *label = lv_label_create(btn, NULL);
lv_label_set_text(label, btnm_map[i]);
lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);
lv_coord_t w = lv_obj_get_width(btn);
lv_coord_t h = lv_obj_get_height(btn);
if (h > max_height) {
max_height = h;
}
if (w > max_width) {
max_width = w;
}
btn_x += max_width;
btn_count++;
}
// 将每个按键的高度设置为最大的按键高度
btn_y = 0;
for (uint8_t i = 0; i < row_count; i++) {
lv_coord_t row_max_height = 0;
for (uint8_t j = 0; j < btn_count; j++) {
lv_obj_t *btn = lv_obj_get_child(btnm, i * btn_count + j);
lv_obj_set_height(btn, max_height);
if (lv_obj_get_height(btn) > row_max_height) {
row_max_height = lv_obj_get_height(btn);
}
}
for (uint8_t j = 0; j < btn_count; j++) {
lv_obj_t *btn = lv_obj_get_child(btnm, i * btn_count + j);
lv_obj_set_y(btn, btn_y + (row_max_height - lv_obj_get_height(btn)) / 2);
}
btn_y += row_max_height;
}
// 将每个按键的宽度设置为最大的按键宽度
for (uint8_t i = 0; i < row_count; i++) {
lv_coord_t row_max_width = 0;
for (uint8_t j = 0; j < btn_count; j++) {
lv_obj_t *btn = lv_obj_get_child(btnm, i * btn_count + j);
if (lv_obj_get_width(btn) > row_max_width) {
row_max_width = lv_obj_get_width(btn);
}
}
for (uint8_t j = 0; j < btn_count; j++) {
lv_obj_t *btn = lv_obj_get_child(btnm, i * btn_count + j);
lv_obj_set_x(btn, lv_obj_get_x(btn) + (row_max_width - lv_obj_get_width(btn)) / 2);
lv_obj_set_width(btn, row_max_width);
}
}
lv_task_handler();
}
void loop() {
lv_task_handler();
delay(5);
}
void btnm_action(lv_obj_t *btnm, const char *txt) {
Serial.println(txt);
}
```
这个示例代码中,我们使用lv_obj_set_pos()函数设置每个按键的位置,并使用lv_obj_set_size()函数设置每个按键的大小。然后,我们在循环中计算每一行中最大的按键高度和宽度,并使用lv_obj_set_height()和lv_obj_set_width()函数将每个按键的高度和宽度设置为最大的按键高度和宽度。最后,我们使用lv_obj_set_x()和lv_obj_set_y()函数将每个按键放置在正确的位置。
阅读全文