GPIO Dashboard

Lab 9: Hardware GPIO Dashboard

1. โครงสร้างภาพรวมของ Lab

Why? - ทำไมต้องเรียนรู้เรื่องนี้

Dashboard ที่รวมทุก GPIO เป็นหัวใจของ Industrial HMI (Human-Machine Interface):

  • Complete Integration: รวม LED + Button + ADC + PWM ไว้ในหน้าจอเดียว

  • Mixed I/O: ควบคุม Output (LED) + อ่าน Input (Button, ADC) พร้อมกัน

  • Panel Layout: จัดหน้าจอแบบ Panel สำหรับ Inputs/Outputs แยกกัน

  • Industrial Application: คล้าย Control Panel ของเครื่องจักรในโรงงาน

  • Capstone Project: รวมทุกสิ่งที่เรียนมาจาก Lab 6-8

What? - จะได้เรียนรู้อะไร

  1. Multi-device Control: ควบคุม 3 LED + PWM + อ่าน Button + ADC

  2. Panel Layout: จัด UI เป็น Input Panel (ซ้าย) + Output Panel (ขวา)

  3. Mixed Polling: Timer callback อ่านทั้ง Button และ ADC พร้อมกัน

  4. Cross-control: ADC ควบคุม PWM (analog input -> analog output)

  5. Status Indicators: แสดงสถานะ Hardware ทั้งหมดแบบ Real-time

How? - ทำอย่างไร

  1. Initialize ทุก Hardware: GPIO, PWM, Sensors

  2. สร้าง Layout แบบ 2-panel: Input (ซ้าย) + Output (ขวา)

  3. สร้าง Timer poll ทุก 50ms อ่าน Button + ADC

  4. ใน timer callback อัพเดท UI + ควบคุม Hardware


2. หลักการทำงานและ Flowchart

2.1 Dashboard Layout

2.2 Control Architecture


3. ฟังก์ชันสำคัญ (API Reference)

3.1 Hardware API ทั้งหมดที่ใช้

Function
Description
Category

aic_gpio_init()

Initialize GPIO

Setup

aic_gpio_pwm_init(AIC_LED_BLUE)

Initialize PWM

Setup

aic_sensors_init()

Initialize sensors/ADC

Setup

aic_gpio_led_set(led, state)

Control LED ON/OFF

Output

aic_gpio_pwm_set_duty(led, duty)

Set PWM duty 0-100

Output

aic_gpio_button_read_raw(btn)

Read button (0=pressed)

Input

aic_adc_read()

Read ADC 0-4095

Input

3.2 LVGL Layout Functions

Function
Description

lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW)

จัดเรียงแนวนอน

lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_COLUMN)

จัดเรียงแนวตั้ง

lv_obj_set_flex_align(obj, main, cross, track)

จัดตำแหน่ง Flex

lv_obj_set_size(obj, w, h)

กำหนดขนาด

LV_PCT(50)

ขนาด 50% ของ parent

3.3 LVGL Widget Functions

Function
Description

lv_switch_create(parent)

สร้าง Switch (toggle)

lv_slider_create(parent)

สร้าง Slider

lv_bar_create(parent)

สร้าง Bar (progress)

lv_led_create(parent)

สร้าง LED indicator

lv_button_create(parent)

สร้าง Button

lv_label_create(parent)

สร้าง Label

lv_obj_add_state(sw, LV_STATE_CHECKED)

ตั้ง Switch เป็น ON

lv_obj_remove_state(sw, LV_STATE_CHECKED)

ตั้ง Switch เป็น OFF


4. โค้ดตัวอย่าง (Code Examples)

4.1 Full Dashboard Code

4.2 อธิบายโครงสร้างโค้ด

ส่วน
หน้าที่
จำนวนบรรทัด

Global Variables

เก็บ widget pointers ทั้งหมด

~15

poll_timer_cb()

อ่าน Button + ADC ทุก 50ms

~25

red/green_switch_cb()

ควบคุม Red/Green LED

~10 each

blue_slider_cb()

ควบคุม Blue LED PWM

~8

all_on/off_cb()

Batch control ทุก LED

~15 each

create_input_panel()

สร้าง UI ฝั่ง Input

~40

create_output_panel()

สร้าง UI ฝั่ง Output

~60

part1_ex9_...()

Main function init + layout

~30


5. องค์ความรู้และเทคนิค

5.1 Pattern: Panel-based Layout

5.2 Pattern: Batch Control with UI Sync

5.3 Pattern: Mixed Poll + Event

5.4 สิ่งที่ต้องระวัง

หัวข้อ
รายละเอียด

Init Order

aic_gpio_init() -> aic_gpio_pwm_init() -> aic_sensors_init()

UI Sync

เมื่อ All ON/OFF ต้อง sync ทั้ง Hardware + Switch states + LED indicators

Single Timer

ใช้ timer เดียวอ่านทั้ง Button + ADC (ไม่ต้องแยก timer)

LVGL Thread

ทุก UI update ต้องอยู่ใน LVGL callback context

PWM Blue Only

Red/Green ใช้ Switch (ON/OFF), Blue ใช้ Slider (0-100%)

5.5 Application ในอุตสาหกรรม

  • Factory Control Panel: ควบคุมมอเตอร์ + อ่าน sensor + แสดงสถานะ

  • Building Management: ควบคุมไฟ + อ่าน occupancy sensor + แสดง energy

  • Process Control: ปรับ valve (slider) + อ่าน pressure (ADC) + alarm (LED)

  • Medical Device: ตั้งค่า parameter + monitor patient vitals


6. แบบฝึกหัด (Exercises)

แบบฝึกหัดที่ 1: ADC-controlled LED Brightness (Auto Mode)

โจทย์: เพิ่มโหมด "Auto" ที่ ADC Potentiometer ควบคุมความสว่าง Blue LED โดยอัตโนมัติ

ข้อกำหนด:

  • เพิ่ม Switch "Auto Mode" บน Output Panel

  • เมื่อ Auto Mode = ON:

    • Blue Slider ถูก disable (สีจาง)

    • ค่า ADC (0-4095) ถูก map เป็น PWM duty (0-100)

    • Blue LED เปลี่ยนความสว่างตาม Potentiometer แบบ real-time

    • Slider เคลื่อนตามค่า ADC (read-only visual)

  • เมื่อ Auto Mode = OFF:

    • Blue Slider กลับมาใช้งานได้ปกติ (Manual control)

  • แสดงโหมดปัจจุบัน: "Auto: POT->Blue" หรือ "Manual"

Hints:

  • ใช้ static bool auto_mode = false;

  • lv_obj_add_state(slider, LV_STATE_DISABLED) / lv_obj_remove_state()

  • ใน poll_timer_cb: ถ้า auto_mode ให้ set PWM จาก ADC

แบบฝึกหัดที่ 2: Mini Alarm System

โจทย์: สร้างระบบ Alarm ย่อที่ใช้ Button arm/disarm และ ADC trigger

ข้อกำหนด:

  • สถานะ Disarmed (เริ่มต้น):

    • แสดง "DISARMED" สีเขียว

    • LED ดับหมด

    • กดปุ่ม USER -> เปลี่ยนเป็น Armed

  • สถานะ Armed:

    • แสดง "ARMED" สีเหลือง

    • Green LED ติด (indicator ว่า armed)

    • ถ้า ADC > 80% -> เปลี่ยนเป็น Alarm Triggered

    • กดปุ่ม USER -> กลับเป็น Disarmed

  • สถานะ Alarm Triggered:

    • แสดง "!! ALARM !!" สีแดง กะพริบ

    • Red LED กะพริบ (toggle ทุก 200ms ใน timer)

    • Blue LED สว่าง 100%

    • กดปุ่ม USER -> กลับเป็น Disarmed

  • แสดง ADC value เป็น Bar graph ตลอดเวลา

  • แสดง Threshold line บน Bar (เส้นที่ 80%)

Hints:

  • ใช้ enum { DISARMED, ARMED, TRIGGERED } สำหรับ state machine

  • Edge detection สำหรับ button press (เปลี่ยนสถานะเฉพาะจังหวะกด)

  • Timer 50ms สำหรับ poll; ใช้ counter นับ tick สำหรับกะพริบ (4 ticks = 200ms)

Last updated

Was this helpful?