# Hardware Interfacing Workshops

## Part I: GPIO Hardware Integration

***

## โค้ดโปรเจคสำหรับ GPIO-to-HMI Display

{% embed url="<https://github.com/Advance-Innovation-Centre-AIC/psoc-e84-e2-lvgl-aic-eec>" %}

{% hint style="info" %}

### **Part 1 Examples (LVGL Basics + GPIO):**

Section II (Hardware Integration):

* 6 = Hardware LED Control (3 LEDs)
* 7 = Hardware Button Status
* 8 = Hardware ADC (Potentiometer)
* 9 = Hardware GPIO Dashboard
* 10 = CAPSENSE UI Mockup
* 11 = CAPSENSE Hardware (I2C Direct)
  {% endhint %}

### ขั้นตอนการดาวโหลดจาก Github Repo และรันโปรแกรมตัวแรก - Label Demo

{% embed url="<https://www.youtube.com/watch?v=HvTe_VcUuXE>" fullWidth="true" %}

### ตัวอย่างการใช้ Eclipse ModusToolBox IDE

{% embed url="<https://www.youtube.com/watch?v=G4xLkdk53Ag>" fullWidth="true" %}

### AIC-EEC GPIO API

#### LED Control API

```c
#include "aic-eec/gpio.h"

/* LED Types */
typedef enum {
    AIC_LED_RED = 0,    // LED แดง (P16_7)
    AIC_LED_GREEN,      // LED เขียว (P16_6)
    AIC_LED_BLUE,       // LED น้ำเงิน (P16_5)
    AIC_LED_COUNT
} aic_led_t;

/* Functions */
void aic_gpio_init(void);                           // Initialize GPIO
void aic_gpio_led_set(aic_led_t led, bool state);   // Set LED ON/OFF
void aic_gpio_led_toggle(aic_led_t led);            // Toggle LED
bool aic_gpio_led_get(aic_led_t led);               // Get LED state
void aic_gpio_led_all_on(void);                     // All LEDs ON
void aic_gpio_led_all_off(void);                    // All LEDs OFF
const char* aic_gpio_led_name(aic_led_t led);       // Get LED name string

```

#### Button API

```c
/* Button Types */
typedef enum {
    AIC_BTN_USER = 0,   // USER Button 1 (SW2) - P8_3
    AIC_BTN_USER2,      // USER Button 2 (SW4) - P8_7
    AIC_BTN_COUNT
} aic_button_t;

/* Functions */
bool aic_gpio_button_read(aic_button_t btn);        // Read button state (with debounce)
bool aic_gpio_button_read_raw(aic_button_t btn);    // Read raw state (no debounce)
const char* aic_gpio_button_name(aic_button_t btn); // Get button name

```

#### Button Pin Configuration

| Button        | Macro           | Pin         | Pull-up  | Active     |
| ------------- | --------------- | ----------- | -------- | ---------- |
| USER Button 1 | `AIC_BTN_USER`  | P8\_3 (SW2) | Internal | Active Low |
| USER Button 2 | `AIC_BTN_USER2` | P8\_7 (SW4) | Internal | Active Low |

> **Note**: ปุ่มเป็น Active Low = กดแล้วอ่านค่าเป็น 0 แต่ API จะ return `true` เมื่อกด

#### PWM API

```c
/* PWM for LED brightness */
void aic_gpio_pwm_init(aic_led_t led);              // Initialize PWM
void aic_gpio_pwm_set_brightness(aic_led_t led, uint8_t brightness);
                                                    // brightness: 0-100%

```

#### Hardware Pin Configuration & PWM Limitation

> **สำคัญ**: ไม่ใช่ทุก LED ที่ทำ PWM dimming ได้!

| LED          | Pin    | PWM Support     | หมายเหตุ        |
| ------------ | ------ | --------------- | --------------- |
| LED1 (Red)   | P16\_7 | ❌ GPIO only     | ON/OFF เท่านั้น |
| LED2 (Green) | P16\_6 | ❌ GPIO only     | ON/OFF เท่านั้น |
| LED3 (Blue)  | P16\_5 | ✅ TCPWM0\_LINE5 | Dimming ได้     |

**เหตุผล**: ใน BSP (Board Support Package) เฉพาะ **Blue LED (P16\_5)** ที่ถูก route ไปที่ PWM output (TCPWM0) ผ่าน HSIOM (High Speed I/O Matrix) ส่วน Green และ Red เป็น GPIO ธรรมดา ทำได้แค่ ON/OFF

**ผลต่อตัวอย่าง**:

* **Ex6**: ปุ่ม ON/OFF ควบคุม Green LED, Slider ควบคุม Blue LED (PWM dimming)
* **Ex10**: PWM Brightness ใช้ได้กับ Blue LED เท่านั้น

#### API Summary Table

<table><thead><tr><th width="344.28759765625">Function</th><th width="190.8118896484375">หน้าที่</th><th>Return</th></tr></thead><tbody><tr><td><code>aic_gpio_init()</code></td><td>เริ่มต้น GPIO ทั้งหมด</td><td>void</td></tr><tr><td><code>aic_gpio_led_set(led, state)</code></td><td>ตั้งค่า LED</td><td>void</td></tr><tr><td><code>aic_gpio_led_toggle(led)</code></td><td>สลับสถานะ LED</td><td>void</td></tr><tr><td><code>aic_gpio_led_get(led)</code></td><td>อ่านสถานะ LED</td><td>true/false</td></tr><tr><td><code>aic_gpio_button_read(btn)</code></td><td>อ่านปุ่มกด</td><td>true=pressed</td></tr><tr><td><code>aic_gpio_pwm_set_brightness(led, val)</code></td><td>ตั้งความสว่าง</td><td>void</td></tr></tbody></table>

***
