Copy #include "aic-eec.h"
/* ===== Global Variables ===== */
static lv_obj_t * led_red_ind;
static lv_obj_t * led_green_ind;
static lv_obj_t * led_blue_ind;
static lv_obj_t * duty_label;
/* ===== Callback: Red LED Switch ===== */
static void ex6_red_cb(lv_event_t * e)
{
if (lv_event_get_code(e) != LV_EVENT_VALUE_CHANGED) return;
lv_obj_t * sw = lv_event_get_target(e);
bool on = lv_obj_has_state(sw, LV_STATE_CHECKED);
aic_gpio_led_set(AIC_LED_RED, on); /* Hardware */
on ? lv_led_on(led_red_ind) : lv_led_off(led_red_ind); /* UI */
}
/* ===== Callback: Green LED Switch ===== */
static void ex6_green_cb(lv_event_t * e)
{
if (lv_event_get_code(e) != LV_EVENT_VALUE_CHANGED) return;
lv_obj_t * sw = lv_event_get_target(e);
bool on = lv_obj_has_state(sw, LV_STATE_CHECKED);
aic_gpio_led_set(AIC_LED_GREEN, on); /* Hardware */
on ? lv_led_on(led_green_ind) : lv_led_off(led_green_ind);
}
/* ===== Callback: Blue LED PWM Slider ===== */
static void ex6_slider_cb(lv_event_t * e)
{
if (lv_event_get_code(e) != LV_EVENT_VALUE_CHANGED) return;
lv_obj_t * slider = lv_event_get_target(e);
int duty = lv_slider_get_value(slider);
/* [1] Hardware: PWM duty cycle */
aic_gpio_pwm_set_duty(AIC_LED_BLUE, duty);
/* [2] UI: LED indicator brightness (scale 0-100 -> 0-255) */
lv_led_set_brightness(led_blue_ind, (uint8_t)(duty * 255 / 100));
/* [3] UI: Label update */
lv_label_set_text_fmt(duty_label, "Duty: %d%%", duty);
}
/* ===== Main Function ===== */
void part1_ex6_hw_led_control(void)
{
/* ============================== */
/* HARDWARE INITIALIZATION */
/* ============================== */
aic_gpio_init(); /* [1] GPIO subsystem */
aic_gpio_pwm_init(AIC_LED_BLUE); /* [2] PWM for Blue LED */
/* ============================== */
/* LVGL UI SETUP */
/* ============================== */
lv_obj_t * scr = lv_screen_active();
lv_obj_set_style_bg_color(scr, lv_color_hex(0x1a1a2e), 0);
/* --- Title --- */
lv_obj_t * title = lv_label_create(scr);
lv_label_set_text(title, "Lab 6: HW LED Control");
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(title, &lv_font_montserrat_18, 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
/* --- Container for LED controls --- */
lv_obj_t * cont = lv_obj_create(scr);
lv_obj_set_size(cont, 440, 200);
lv_obj_align(cont, LV_ALIGN_CENTER, 0, 10);
lv_obj_set_style_bg_color(cont, lv_color_hex(0x16213e), 0);
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_row(cont, 10, 0);
lv_obj_set_style_pad_all(cont, 15, 0);
/* --- Row 1: Red LED --- */
lv_obj_t * row1 = lv_obj_create(cont);
lv_obj_set_size(row1, LV_PCT(100), 45);
lv_obj_set_flex_flow(row1, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_all(row1, 5, 0);
lv_obj_remove_flag(row1, LV_OBJ_FLAG_SCROLLABLE);
led_red_ind = lv_led_create(row1);
lv_led_set_color(led_red_ind, lv_palette_main(LV_PALETTE_RED));
lv_obj_set_size(led_red_ind, 25, 25);
lv_led_off(led_red_ind);
lv_obj_t * lbl_red = lv_label_create(row1);
lv_label_set_text(lbl_red, "Red LED");
lv_obj_set_style_text_color(lbl_red, lv_color_hex(0xFFFFFF), 0);
lv_obj_t * sw_red = lv_switch_create(row1);
lv_obj_add_event_cb(sw_red, ex6_red_cb, LV_EVENT_VALUE_CHANGED, NULL);
/* --- Row 2: Green LED --- */
lv_obj_t * row2 = lv_obj_create(cont);
lv_obj_set_size(row2, LV_PCT(100), 45);
lv_obj_set_flex_flow(row2, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_all(row2, 5, 0);
lv_obj_remove_flag(row2, LV_OBJ_FLAG_SCROLLABLE);
led_green_ind = lv_led_create(row2);
lv_led_set_color(led_green_ind, lv_palette_main(LV_PALETTE_GREEN));
lv_obj_set_size(led_green_ind, 25, 25);
lv_led_off(led_green_ind);
lv_obj_t * lbl_green = lv_label_create(row2);
lv_label_set_text(lbl_green, "Green LED");
lv_obj_set_style_text_color(lbl_green, lv_color_hex(0xFFFFFF), 0);
lv_obj_t * sw_green = lv_switch_create(row2);
lv_obj_add_event_cb(sw_green, ex6_green_cb, LV_EVENT_VALUE_CHANGED, NULL);
/* --- Row 3: Blue LED + PWM Slider --- */
lv_obj_t * row3 = lv_obj_create(cont);
lv_obj_set_size(row3, LV_PCT(100), 50);
lv_obj_set_flex_flow(row3, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_all(row3, 5, 0);
lv_obj_remove_flag(row3, LV_OBJ_FLAG_SCROLLABLE);
led_blue_ind = lv_led_create(row3);
lv_led_set_color(led_blue_ind, lv_palette_main(LV_PALETTE_BLUE));
lv_obj_set_size(led_blue_ind, 25, 25);
lv_led_off(led_blue_ind);
lv_obj_t * lbl_blue = lv_label_create(row3);
lv_label_set_text(lbl_blue, "Blue PWM");
lv_obj_set_style_text_color(lbl_blue, lv_color_hex(0xFFFFFF), 0);
lv_obj_t * slider = lv_slider_create(row3);
lv_obj_set_width(slider, 180);
lv_slider_set_range(slider, 0, 100);
lv_slider_set_value(slider, 0, LV_ANIM_OFF);
lv_obj_add_event_cb(slider, ex6_slider_cb, LV_EVENT_VALUE_CHANGED, NULL);
duty_label = lv_label_create(row3);
lv_label_set_text(duty_label, "Duty: 0%");
lv_obj_set_style_text_color(duty_label, lv_color_hex(0x00BFFF), 0);
}