Event Bus
Lab 2: Event Bus Integration (EventBus + Advanced IPC)
Part 4 - IPC & Event Bus
1. โครงสร้างภาพรวม (Overview)
Why? - ทำไมต้องเรียนรู้เรื่องนี้
Event-Driven Architecture คือรูปแบบสถาปัตยกรรมที่ใช้ในระบบ Industrial, SCADA, IoT ทุกระดับ:
Loose Coupling: ลด dependency ระหว่างโมดูล ทำให้ขยายระบบได้ง่าย
Publish-Subscribe: หลายโมดูลสามารถ subscribe event เดียวกัน ไม่ต้องรู้จักกัน
Scalability: เพิ่ม subscriber ได้โดยไม่ต้องแก้ publisher
ในระบบ Embedded จริง ไม่มี feature ใดทำงานอิสระ ทุกอย่างต้อง บูรณาการ เข้าด้วยกัน:
Smart Factory HMI: หน้าจอเดียวต้องแสดง sensor data + ควบคุม actuator + แสดง event log + monitor สถานะระบบ
Medical Device: Patient monitor ต้องรวม ECG + SpO2 + Blood Pressure + Alarm ทั้งหมด
Automotive Dashboard: Instrument cluster ต้องรวม speed + RPM + fuel + warning lights
Building Management System: HVAC control + lighting + security + energy monitoring ในระบบเดียว
Lab นี้เป็นแบบ progressive learning -- เริ่มจาก Part A ที่สร้าง Event Bus ด้วย simulated/IPC data แล้วก้าวสู่ Part B ที่บูรณาการ Event Bus + IPC + Logging + GPIO real hardware เข้าด้วยกัน
What? - จะได้เรียนรู้อะไร
Part A (Ex4 - Event Bus):
Event Bus Pattern: Publish-Subscribe สำหรับ Embedded Systems
Event Types: EVENT_IMU, EVENT_ADC, EVENT_BUTTON
Multiple Subscribers: หลาย handler ต่อ 1 event
State Machine: ออกแบบ state machine ที่ขับเคลื่อนด้วย event
Part B (Ex8 - Advanced Integration):
System Integration: รวม 4 subsystems (IPC, EventBus, Logging, Hardware)
Layout Helpers: ใช้
aic_col_create(),aic_row_create(),aic_full_size(),aic_pad()สร้าง UI อย่างรวดเร็วEvent Bus Pub/Sub: Decouple sensor reads จาก UI updates ผ่าน publish/subscribe
IPC Statistics Monitoring: ติดตาม TX/RX/Error counts แบบ real-time
Multi-Sensor Dashboard: แสดง IMU + ADC + Button + LED ในหน้าเดียว
Combined Init Pattern: ลำดับการ initialize subsystems ที่ถูกต้อง
How? - จะทำอย่างไร
Part A: สร้าง Event Bus infrastructure (subscribe, publish, dispatch) + UI แสดง event log + statistics โดยรับข้อมูลจาก IPC callback ผ่าน flag-based pattern
Part B: สร้าง Dashboard บน CM55 ด้วย layout helpers ที่ประกอบด้วย LED row, Button state, IMU display, ADC display, Stats row -- โดยรวม IPC + EventBus + Logging + GPIO เข้าด้วยกัน
2. หลักการทางเทคนิค (Technical Principles)
2.1 Event-Driven Architecture
2.2 Publish-Subscribe Flow
2.3 Event Bus Architecture (Part A)
2.4 Integration Architecture (Part B)
2.5 Initialization Sequence (Part B -- Critical!)
3. Part A: Event Bus (Ex4 - Simulated + IPC Data)
Part A สอนการสร้าง Event Bus infrastructure ตั้งแต่ต้น -- ออกแบบ event types, subscriber system, publish/dispatch mechanism แล้วทดสอบด้วย IPC data + timer events
3.1 Event Types
3.2 Event Data Structure
3.3 Event Bus Core
3.4 Step-by-Step Implementation
Step 1: ประกาศ Variables
Step 2: IPC Callback (Bridge to Event Bus)
Step 3: Event Handlers (Subscribers)
Step 4: Bridge Timer (Flag -> Event Bus)
Step 5: สร้าง UI Layout + Main Entry
3.5 Subscriber Registration Map
4. Part B: Advanced IPC Integration (Ex8 - Real Hardware)
Part B ยกระดับจาก Part A โดยใช้ AIC Library APIs (aic_event_*, aic_gpio_*, layout helpers) แทน manual Event Bus -- รวม IPC + EventBus + Logging + GPIO real hardware เข้าด้วยกัน
4.1 Layout Helpers (AIC Layout System)
aic_col_create(parent)
สร้าง container แนวตั้ง (Flex Column)
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_COLUMN)
aic_row_create(parent)
สร้าง container แนวนอน (Flex Row)
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW)
aic_full_size(obj)
ขยาย obj เต็ม parent
lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100))
aic_pad(obj, px)
ตั้ง padding ทุกด้าน
lv_obj_set_style_pad_all(obj, px, 0)
aic_apply_dark_theme(scr)
ตั้งค่า dark theme
Multiple style calls
aic_create_footer(scr)
สร้าง footer bar
Custom footer creation
aic_xyz_display_create(...)
สร้าง XYZ display
3 labels + header
4.2 Layout Helpers vs Manual Positioning
4.3 Layout Structure ของ Part B
4.4 Data Flow Diagram
4.5 Event Bus Pub/Sub ใน Part B
4.6 Static Variables
4.7 Event Bus Callbacks (Subscribers)
4.8 LED Toggle Callback (IPC + GPIO + Logging)
4.9 Timer Callback (Heart of the System)
4.10 Main Function (Full UI Build)
5. องค์ความรู้และเทคนิค (Patterns & Tips)
5.1 Integration Checklist Pattern
5.2 Graceful Degradation Pattern
5.3 Button Edge Detection
5.4 Event Count as System Health Metric
5.5 Design Patterns ที่เกี่ยวข้อง
6. แบบฝึกหัด (Exercises)
Exercise 1: Event Counter Dashboard (Bar Chart) -- Part A
สร้าง dashboard แสดง bar chart ของจำนวน event แต่ละ type:
แกน X: EVENT_IMU, EVENT_ADC, EVENT_BUTTON, EVENT_TIMER, EVENT_THRESHOLD
แกน Y: จำนวน events
อัปเดตทุก 1 วินาที
แสดง event rate (events/sec) สำหรับแต่ละ type
Hints:
Exercise 2: Event-Driven State Machine -- Part A
สร้าง state machine ที่มี 3 states:
IDLE: ปกติ (สีเขียว) -- เมื่อได้ EVENT_BUTTON --> ไป ACTIVE
ACTIVE: กำลังทำงาน (สีเหลือง) -- เมื่อได้ EVENT_THRESHOLD --> ไป ALERT
ALERT: แจ้งเตือน (สีแดง กระพริบ) -- เมื่อได้ EVENT_BUTTON --> กลับ IDLE
Hints:
Exercise 3: Sensor Threshold Alerts -- Part B
เพิ่มระบบ alert เมื่อค่า sensor เกิน threshold:
Requirements:
เพิ่ม label "Alert: NONE" ใต้ ADC label
ADC > 80% (raw > 3276): "Alert: HIGH VOLTAGE" สีแดง + LED1 on
IMU |accel| > 15 m/s^2: "Alert: VIBRATION" สีส้ม + LED2 on
ค่ากลับปกติ: "Alert: NONE" สีเขียว + LED off
ส่ง
CM55_LOGW("ALERT: ...")เมื่อ alert เปลี่ยนสถานะ (edge detect)
Hints:
ใช้
static bool alert_active = false;สำหรับ edge detect (log เฉพาะตอนเปลี่ยน)Check thresholds ใน
ex8_timer_cb()หลัง sensor read
Exercise 4: IPC Health Card -- Part B
เพิ่ม IPC statistics card:
Requirements:
TX Rate / RX Rate: messages/sec (delta per second)
Error Rate: % (errors / total * 100)
Status LED: green (OK) / red (error > 5%)
Uptime: seconds since start
ถ้า error > 5%:
CM55_LOGW("IPC health degraded")
Hints:
เก็บ
last_tx, last_rxสำหรับ rate calculationสร้าง timer แยก 1 วินาที สำหรับ rate update
7. Quick Reference
Event Bus API (Part A)
AIC Event Bus API (Part B)
aic_event_init()
Initialize Event Bus
aic_event_subscribe(event, cb, user_data)
Subscribe to event type
aic_event_publish_imu(ax, ay, az, gx, gy, gz)
Publish IMU data event
aic_event_publish_adc(ch, raw, mv)
Publish ADC data event
Layout Helper Functions (Part B)
aic_col_create(parent)
สร้าง Flex Column container
aic_row_create(parent)
สร้าง Flex Row container
aic_full_size(obj)
ขยาย 100% x 100%
aic_pad(obj, px)
ตั้ง padding ทุกด้าน
aic_apply_dark_theme(scr)
ตั้ง dark background + colors
aic_xyz_display_create(par, title, labels[3])
สร้าง XYZ display panel
aic_create_footer(scr)
สร้าง footer bar
IPC + Hardware Functions (Part B)
cm55_ipc_is_init()
ตรวจว่า IPC พร้อมหรือยัง
cm55_ipc_set_led(id, state)
ส่งคำสั่ง LED ไป CM33
cm55_ipc_get_stats(&tx, &rx, &err)
ดึง IPC statistics
CM55_LOGI(fmt, ...)
Send INFO log to CM33 via IPC
aic_gpio_init()
Init GPIO, return bool (success)
aic_gpio_led_set(led, state)
Set LED ON/OFF
aic_gpio_button_read_raw(btn)
Read button state (true=pressed)
aic_sensors_init()
Init sensor subsystem
imu_shared_read_accel(&ax, &ay, &az)
Read accelerometer (float, g units)
aic_adc_read(channel)
Read ADC (uint16_t, 0-4095)
Architecture Principles
Application ในงาน Industrial
SCADA System
Event-driven monitoring ของโรงงานทั้งหมด
PLC Programming
State machine สำหรับควบคุมกระบวนการผลิต
Alarm Management
จัดการ alarm ตามมาตรฐาน ISA-18.2
Industrial IoT
Edge computing ที่ประมวลผล event ก่อนส่งขึ้น cloud
Automotive ECU
Event-driven architecture สำหรับ ADAS, Body Control
Building Automation
BACnet/BMS event management สำหรับ HVAC, Lighting
Medical Device
Monitor: vitals + pump control + alarm + audit log
Last updated