PlatformIO Project Layout៖ ជំនួស .ino តែមួយ
ផ្លាស់ពី Sketch តែមួយ ទៅជា Project ដែលមាន src/, lib/, test/ និង platformio.ini — Build ដូចគ្នានៅគ្រប់កុំព្យូទ័រ និងក្នុង CI។
- រៀបចំ PlatformIO Project (src/, lib/, test/, platformio.ini) ដែលមានច្រើន Environment
- រចនា HAL Interface (Abstract class) និង Dependency Injection តាម Constructor
- សរសេរ Unit test ដោយ Unity (pio test -e native) និង pytest សម្រាប់ Python
Arduino IDE ល្អសម្រាប់ចាប់ផ្ដើម ប៉ុន្តែ Sketch .ino តែមួយដែលមាន 800 បន្ទាត់ពិបាក Test ពិបាក Review ហើយមិនកំណត់កំណែ Library ច្បាស់ — Laptop ម្នាក់ Compile បាន ម្នាក់ទៀតមិនបាន។ PlatformIO (Extension ក្នុង VS Code ឬ CLI pio) ប្រើ C++ ស្តង់ដារ (.cpp/.h) ហើយកំណត់ Platform, Board និង Library ក្នុង File platformio.ini តែមួយ ដូច្នេះគ្រប់គ្នា និង CI Build បានលទ្ធផលដូចគ្នា។
រចនាសម្ព័ន្ធស្តង់ដារ៖ src/ សម្រាប់ main.cpp (setup()/loop() និងកូដភ្ជាប់ហាដវែរ), lib/ សម្រាប់ Module ផ្ទាល់ខ្លួនដែលនីមួយៗនៅក្នុង Folder ផ្ទាល់ (ឧ. lib/TrafficCore/), include/ សម្រាប់ Header រួម និង test/ សម្រាប់ Unit test ដែលនីមួយៗនៅក្នុង Folder ចាប់ផ្ដើមដោយ test_ (ឧ. test/test_traffic/)។ ចំណាំសំខាន់៖ តាមលំនាំដើម pio test មិន Compile `src/` ទេ — ដូច្នេះ Logic ដែលចង់ Test ត្រូវដាក់ក្នុង lib/ ហើយ src/main.cpp គ្រាន់តែជាស្រទាប់ស្ដើងដែលភ្ជាប់ Logic ទៅ Pin ពិត។
File platformio.ini អាចមាន Environment ច្រើន៖ [env:uno] (Platform atmelavr), [env:esp32] (Platform espressif32) និង [env:native] ដែលមានន័យថា Compile ដោយ gcc/clang សម្រាប់កុំព្យូទ័ររបស់អ្នកផ្ទាល់។ Environment native គ្មាន Arduino.h, digitalWrite() ឬ millis() ទេ — នេះហើយជាហេតុផលដែលយើងត្រូវការ HAL ក្នុងមេរៀនបន្ទាប់។ ផ្នែក [env] (គ្មានឈ្មោះ) ផ្ទុកការកំណត់រួមសម្រាប់គ្រប់ Environment ដូចជា test_framework = unity។
Command ប្រចាំថ្ងៃ៖ pio run -e uno (Compile), pio run -e uno -t upload (Upload), pio device monitor (Serial Monitor តាម monitor_speed) និង pio test -e native (Unit test លើកុំព្យូទ័រ)។ Library ខាងក្រៅប្រកាសក្នុង lib_deps ជាមួយ SemVer range ឧ. lib_deps = bblanchon/ArduinoJson@^7.0.0 មានន័យថាទទួលយកកំណែ 7.x ណាមួយ (≥ 7.0.0) ប៉ុន្តែមិនឡើងដល់ 8.0.0 ដែលអាចបំបែក API ឡើយ — នេះជាការអនុវត្ត Semantic Versioning ពី Module 1 ផ្ទាល់។
src/= Glue code ហាដវែរ,lib/= Logic ដែលអាច Test,test/test_*/= Unit testspio testមិន Compilesrc/តាមលំនាំដើម — ដាក់ Logic ក្នុងlib/- Environment
nativeរត់លើ PC ហើយគ្មានArduino.h— ត្រូវការ HAL - ចាក់សោកំណែ Library ក្នុង
lib_deps(ឧ.@^7.0.0) ដើម្បីឱ្យ Build ដូចគ្នាគ្រប់ម៉ាស៊ីន
; platformio.ini — one codebase, several targets ("environments")
;
; traffic-light/
; ├── platformio.ini
; ├── src/main.cpp <- setup()/loop() + Arduino adapters only
; ├── lib/TrafficCore/TrafficCore.h <- pure logic, no Arduino.h (testable on PC)
; └── test/test_traffic/test_main.cpp
;
; pio run -e uno -t upload build + flash the Arduino Uno
; pio test -e native run unit tests on your computer
; pio device monitor -e uno open the serial monitor at monitor_speed
[platformio]
default_envs = uno
; settings shared by every environment
[env]
test_framework = unity
[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 115200
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
; host computer: needs a C/C++ compiler (gcc/clang) installed
[env:native]
platform = native
build_flags = -Wallការពន្យល់៖ Environment uno និង esp32 Build Firmware ពិត ចំណែក native សម្រាប់ Test លើកុំព្យូទ័រ។ default_envs = uno ធ្វើឱ្យ pio run ដោយគ្មាន -e Build តែ Uno។ Environment native ទាមទារ Compiler លើកុំព្យូទ័ររបស់អ្នក (Linux៖ build-essential, macOS៖ Xcode Command Line Tools, Windows៖ MinGW-w64)។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- PlatformIO — platformio.ini Project Configuration docs.platformio.orgជម្រើសទាំងអស់របស់ platformio.ini៖ env, lib_deps, build_flags, test options។
- PlatformIO — Native Development Platform docs.platformio.orgPlatform native សម្រាប់ Build និង Test កូដលើកុំព្យូទ័រ Host។