Scale Temperature
Lab 4: Scale Widget (Temperature Gauge)
1. โครงสร้างภาพรวมของ Lab
Why? - ทำไมต้องเรียนรู้เรื่องนี้
Professional Gauge: Scale widget สร้างเครื่องมือวัดที่ดูเหมือนเครื่องมือจริง มี tick marks และ labels
Color Zones: แสดง warning zones ด้วยสี (blue=cold, green=normal, red=hot) ซึ่งเป็นมาตรฐานในอุตสาหกรรม
Needle Display: เข็มชี้ค่าปัจจุบันเหมือน analog gauge ดั้งเดิม
Multiple Modes: ใช้ได้ทั้งแบบ vertical (thermometer) และ round (pressure gauge)
Industry Standard: Scale/Gauge เป็นองค์ประกอบหลักของ SCADA, HMI, และ BMS (Building Management System)
What? - จะได้เรียนรู้อะไร
Scale Widget: สร้าง gauge ด้วย
lv_scale_create()Tick Marks: กำหนด total ticks, major tick interval, tick length
Scale Sections: เพิ่ม color zones (blue, green, red)
Needle Line: สร้างเข็มชี้ด้วย
lv_line_create()บน scaleScale Modes: HORIZONTAL, VERTICAL, ROUND_INNER, ROUND_OUTER
How? - จะทำอย่างไร
สร้าง Scale widget ในโหมด
LV_SCALE_MODE_ROUND_OUTERกำหนด tick count, major tick interval, label format
เพิ่ม 3 sections สำหรับ color zones
สร้าง needle line ชี้ค่าปัจจุบัน
ใช้ timer อัพเดทค่าจาก simulated sensor

2. หลักการทำงานและ Flowchart
2.1 Scale Anatomy
2.2 Scale Modes
2.3 Color Sections (Temperature Zones)
3. ฟังก์ชันสำคัญ (API Reference)
3.1 Scale Creation & Range
lv_scale_create(parent)
parent: lv_obj_t*
สร้าง scale widget
lv_scale_set_mode(scale, mode)
mode: lv_scale_mode_t
HORIZONTAL/VERTICAL/ROUND_INNER/ROUND_OUTER
lv_scale_set_range(scale, min, max)
min, max: int32_t
กำหนดช่วงค่า
lv_scale_set_total_tick_count(scale, cnt)
cnt: uint32_t
จำนวน tick ทั้งหมด
lv_scale_set_major_tick_every(scale, nth)
nth: uint32_t
ทุกกี่ tick เป็น major
3.2 Tick Configuration
lv_scale_set_label_show(scale, show)
show: bool
แสดง/ซ่อน labels
lv_obj_set_style_length(scale, px, LV_PART_INDICATOR)
px: int32_t
ความยาว major tick
lv_obj_set_style_length(scale, px, LV_PART_ITEMS)
px: int32_t
ความยาว minor tick
3.3 Round Scale Angles
lv_scale_set_rotation(scale, deg)
deg: int32_t
มุมเริ่มต้น (เช่น 135)
lv_scale_set_angle_range(scale, deg)
deg: uint32_t
sweep angle (เช่น 270)
3.4 Scale Sections (Color Zones)
lv_scale_add_section(scale)
-
สร้าง section ใหม่ (return pointer)
lv_scale_section_set_range(sec, min, max)
min, max: int32_t
กำหนดช่วงค่าของ section
lv_scale_section_set_style(sec, part, style)
part, style
กำหนด style สำหรับ section
4. โค้ดตัวอย่าง (Step-by-Step Code)
4.1 Step 1: Global Variables และ Styles
4.2 Step 2: Initialize Section Styles
4.3 Step 3: Needle Update Function
4.4 Step 4: Timer Callback
4.5 Step 5: Main Function - สร้าง Gauge UI
4.6 UI Layout
5. องค์ความรู้และเทคนิค
5.1 Scale Modes Comparison
HORIZONTAL
แนวนอน
Ruler, level
Signal strength bar
VERTICAL
แนวตั้ง
Thermometer
Mercury thermometer
ROUND_OUTER
วงกลม (ticks นอก)
Gauge มาตรฐาน
Speedometer
ROUND_INNER
วงกลม (ticks ใน)
Gauge compact
Watch dial
5.2 Tick Count Formula
5.3 Needle Angle Calculation
5.4 Section Style Pattern
5.5 Line Points ต้อง Static
5.6 Multiple Scales ซ้อนกัน
6. แบบฝึกหัด (Exercises)
Exercise 1: Pressure Gauge (0-100 PSI with 3 Color Zones)
สร้าง Pressure Gauge สำหรับ Industrial Monitoring:
Requirements:
Scale mode: ROUND_OUTER, 270-degree sweep
Range: 0-100 PSI
3 Color zones:
0-30 PSI: สีเขียว (Normal operating pressure)
31-70 PSI: สีเหลือง (Elevated pressure)
71-100 PSI: สีแดง (Critical! Over-pressure warning)
Needle ชี้ค่าปัจจุบัน
Center label: "XX PSI"
Status label: "Normal" / "Warning" / "CRITICAL!"
Major tick ทุก 10 PSI (0, 10, 20, ..., 100)
Timer จำลองค่า pressure ที่เปลี่ยนแปลง
Hints:
total_tick_count = 51 (0 ถึง 100, ทุก 2 PSI)
major_tick_every = 5 (ทุก 10 PSI)
ใช้ pattern จาก Section 4.3 สำหรับ needle calculation
Exercise 2: Combined Temperature + Humidity Display with Dual Scales
สร้างจอแสดงผล Temperature และ Humidity แบบ dual gauge:
Requirements:
Gauge 1 (ซ้าย): Temperature Scale
Mode: ROUND_OUTER, size 130x130
Range: -10 to 50 degC
Sections: Blue (cold), Green (normal), Red (hot)
Needle + center value
Gauge 2 (ขวา): Humidity Scale
Mode: ROUND_OUTER, size 130x130
Range: 0-100 %RH
Sections: Red (dry), Green (normal), Blue (humid)
Needle + center value
Timer จำลองค่าที่เปลี่ยนสัมพันธ์กัน (temp สูง → humidity ต่ำ)
Label ด้านล่างแสดง "Comfort Index" (Good/Fair/Poor)
Hints:
ใช้
lv_obj_align()จัดวางซ้าย-ขวาแต่ละ gauge มี needle_points แยกกัน (static array แยก)
Comfort Index: Temp 20-26 + Humid 40-60 = "Good"
7. References
Last updated
Was this helpful?