Arc Gauge

Lab 2: Arc Widget (Circular Gauge)


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

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

  • Circular Display: แสดงค่าแบบวงกลมที่สวยงามและอ่านง่าย เหมาะกับ gauge แบบมืออาชีพ

  • Angular Data: เหมาะอย่างยิ่งกับข้อมูลที่มีขอบเขต เช่น Gyroscope, Compass, Temperature, Speed

  • Space Efficient: Arc ใช้พื้นที่แบบ compact กว่า bar แต่ให้ข้อมูลเท่ากัน

  • Dashboard UI: Circular gauge เป็นองค์ประกอบหลักของ dashboard สมัยใหม่ทั้งในรถยนต์และ Smart Home

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

  1. Arc Widget: สร้าง circular gauge ด้วย lv_arc_create()

  2. Rotation & Angles: กำหนดมุมเริ่มต้น, sweep angle (270 degree gauge)

  3. Arc Styling: ปรับความหนา, สี background, สี indicator

  4. Center Label: แสดงค่าตัวเลขตรงกลาง arc

  5. Timer Animation: อัพเดทค่าแบบ smooth ด้วย timer

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

  1. สร้าง Arc widget กำหนด rotation ที่ 135 degree (เริ่มจากล่างซ้าย)

  2. ตั้งค่า background angles เป็น 0-270 degree (sweep 270 degree)

  3. เพิ่ม label ตรงกลางแสดงค่าปัจจุบัน

  4. ใช้ timer อัพเดทค่าจาก simulated sensor

  5. ปรับ style ให้ดูเป็น professional gauge


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

2.1 Arc Geometry (ระบบมุมของ LVGL)

2.2 Gauge Configuration (270-degree sweep)

2.3 Arc Parts (สำหรับ styling)


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

3.1 Arc Creation & Configuration

Function
Parameters
Description

lv_arc_create(parent)

parent: lv_obj_t*

สร้าง arc widget

lv_arc_set_range(obj, min, max)

min, max: int32_t

กำหนดช่วงค่า

lv_arc_set_value(obj, val)

val: int32_t

ตั้งค่า indicator

lv_arc_get_value(obj)

-

อ่านค่าปัจจุบัน

3.2 Angle & Rotation

Function
Parameters
Description

lv_arc_set_rotation(obj, deg)

deg: int32_t

หมุนจุดเริ่มต้น (องศา)

lv_arc_set_bg_angles(obj, start, end)

start, end: uint32_t

กำหนดมุม background track

lv_arc_set_angles(obj, start, end)

start, end: uint32_t

กำหนดมุม indicator

lv_arc_set_mode(obj, mode)

mode: lv_arc_mode_t

NORMAL / REVERSE / SYMMETRICAL

3.3 Arc Styling

Function / Property
Description

lv_obj_set_size(arc, w, h)

ขนาด (ต้องเป็นสี่เหลี่ยมจัตุรัส w=h)

lv_obj_set_style_arc_width(obj, px, part)

ความหนาของ arc (pixels)

lv_obj_set_style_arc_color(obj, color, part)

สีของ arc

lv_obj_set_style_arc_rounded(obj, bool, part)

ปลาย arc โค้งมน

3.4 Make Arc Non-Interactive (Gauge Mode)


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

4.1 Step 1: Global Variables

4.2 Step 2: Simulated Sensor Data

4.3 Step 3: Timer Callback

4.4 Step 4: Main Function - สร้าง Gauge UI

4.5 UI Layout Summary


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

5.1 Arc Size ต้องเป็นสี่เหลี่ยมจัตุรัส

5.2 Common Rotation Patterns

5.3 Center Label Pattern

5.4 Multiple Arcs (Nested Gauges)

5.5 Color Zones ด้วย Multiple Indicator Colors

5.6 Performance Tips


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

Exercise 1: Speedometer (0-200 km/h)

สร้าง Speedometer ที่แสดงความเร็ว 0-200 km/h:

Requirements:

  • Arc 270-degree gauge แสดงความเร็ว

  • Center label แสดง "120 km/h" (ค่าปัจจุบัน)

  • 3 Color zones บน indicator:

    • 0-80 km/h: สีเขียว (Safe zone)

    • 81-140 km/h: สีเหลือง (Caution zone)

    • 141-200 km/h: สีแดง (Danger zone)

  • Timer จำลองความเร็วที่เปลี่ยนแปลง (เร่ง/ลดสลับกัน)

  • Min/Max labels ที่ขอบ gauge ("0" และ "200")

Hints:

  • lv_arc_set_range(arc, 0, 200)

  • ใช้ update_arc_color_zone() pattern จาก Section 5.5

  • จำลองความเร็ว: เริ่ม 0, เพิ่มทีละ 3-5, ถึง 200 แล้วลดกลับ


Exercise 2: Temperature Gauge with Animated Needle (-10 to 50 degC)

สร้าง Temperature Gauge ที่มี animation แบบ smooth:

Requirements:

  • Arc 270-degree แสดงอุณหภูมิ -10 ถึง 50 degC

  • Center label แสดงค่าอุณหภูมิ + unit

  • Line needle ชี้ค่าปัจจุบัน (ใช้ lv_line_create())

  • Color zones ตามอุณหภูมิ:

    • -10 ถึง 5 degC: สีฟ้า (Cold)

    • 6 ถึง 35 degC: สีเขียว (Normal)

    • 36 ถึง 50 degC: สีแดง (Hot)

  • Status label แสดง "Cold" / "Normal" / "Hot"

  • Timer จำลองอุณหภูมิเปลี่ยนทุก 300ms

Hints:

  • Needle: ใช้ lv_line_create() กับ static point array

  • คำนวณมุมเข็ม: angle = 135 + (value - min) * 270 / (max - min)

  • ใช้ sin() / cos() คำนวณ endpoint ของ line

  • อัพเดท line points ใน timer callback


7. References


Last updated

Was this helpful?