Hardware Abstraction Layer (HAL) និង Dependency Injection
សរសេរ Logic ដែលមិនស្គាល់ Pin ឬ millis() ដោយផ្ទាល់ ដូច្នេះ Logic ដដែលរត់បានទាំងលើ Arduino Uno និងលើ PC ជាមួយ Fake hardware។
- រៀបចំ PlatformIO Project (src/, lib/, test/, platformio.ini) ដែលមានច្រើន Environment
- រចនា HAL Interface (Abstract class) និង Dependency Injection តាម Constructor
- សរសេរ Unit test ដោយ Unity (pio test -e native) និង pytest សម្រាប់ Python
ក្នុងវគ្គ C/C++ for Robotics អ្នកបានសរសេរ Driver class ដែលហៅ digitalWrite() ដោយផ្ទាល់ ហើយក្នុងវគ្គ Algorithms អ្នកបានបំបែក Logic Python ចេញពី GPIO។ ឥឡូវយើងធ្វើដូចគ្នាក្នុង C++ Embedded តាមរយៈ Hardware Abstraction Layer (HAL)៖ Logic មិនស្គាល់ Pin ឬ millis() ទេ វាស្គាល់តែ Interface ដូចជា IDigitalOut::write(bool) និង IClock::nowMs()។ នៅលើ Arduino យើងផ្ដល់ ArduinoPin ដែលហៅ digitalWrite(); នៅលើកុំព្យូទ័រ យើងផ្ដល់ FakePin ដែលគ្រាន់តែកត់ត្រាតម្លៃ។
ក្នុង C++ Interface គឺជា Abstract class ដែលមាន Pure virtual function (virtual void write(bool high) = 0;)។ Logic class ទទួល Interface តាម Constructor (Dependency Injection) ជា Reference — Blinker(IDigitalOut& led, const IClock& clock, uint32_t periodMs) — ជំនួសការបង្កើតហាដវែរដោយខ្លួនឯង។ ប្រយោជន៍៖ អ្នកអាចប្ដូរ Arduino Uno ទៅ ESP32 ឬ LED ទៅ Relay ដោយមិនកែ Logic មួយបន្ទាត់ ហើយ Test អាចគ្រប់គ្រង ពេលវេលា (fakeClock.t = 500) ដោយមិនចាំបាច់រង់ចាំ 500 ms ពិតប្រាកដ។
តម្លៃនៃ Virtual function លើ ATmega328P គឺតូច៖ Pointer ទៅ vtable 2 bytes ក្នុង Object នីមួយៗ (avr-gcc ដាក់ vtable ខ្លួនឯងក្នុង SRAM ផងដែរ) និង Indirect call ពីរបី Cycle — ធ្វេសប្រហែសបានបើប្រៀបនឹង digitalWrite() ដែលចំណាយប្រហែល 3–5 µs។ ប៉ុន្តែយើងជៀសវាង new/delete និង STL (std::vector, std::function មិនមានក្នុង AVR toolchain) ដោយបង្កើត Object ជា Global ឬ Member។ ហេតុនេះ Interface ប្រកាស Destructor ជា protected non-virtual (ច្បាប់ C.35 ក្នុង C++ Core Guidelines)៖ មិនអាច delete តាម Pointer របស់ Interface បានទេ ហើយមិនទាមទារ operator delete លើ AVR។
បច្ចេកទេស #ifdef ARDUINO ក្នុងឧទាហរណ៍ខាងក្រោមអនុញ្ញាតឱ្យ File តែមួយ Build បានទាំងពីរ៖ PlatformIO កំណត់ Macro ARDUINO ពេលប្រើ framework = arduino ដូច្នេះ pio run -e uno Compile ផ្នែក Adapter ពិត ចំណែក pio run -e native Compile ផ្នែក Fake និង main()។ ក្នុង Project ធំ យើងបំបែក Interface និង Logic ទៅ lib/ ហើយ Adapter ទៅ src/ ជំនួសការប្រើ #ifdef (ដូចដែលនឹងធ្វើក្នុងគម្រោងបញ្ចប់)។
- HAL = Interface (Abstract class) រវាង Logic និងហាដវែរ — Logic គ្មាន
Arduino.h - Dependency Injection តាម Constructor ធ្វើឱ្យប្ដូរ Board ឬ Fake បានដោយមិនកែ Logic
- Fake clock គ្រប់គ្រងពេលវេលាក្នុង Test — គ្មាន
delay()គ្មានការរង់ចាំ - លើ AVR៖ ជៀសវាង
newនិង STL; Interface ប្រើ Protected non-virtual destructor
// src/main.cpp — one file, two targets:
// pio run -e uno -t upload -> real LED on Arduino Uno pin D13
// pio run -e native && .pio/build/native/program -> runs on your PC
#include <stdint.h>
// ---------------- HAL interfaces (what the logic needs) ----------------
class IDigitalOut {
public:
virtual void write(bool high) = 0;
protected:
~IDigitalOut() = default; // never deleted through the interface (no heap)
};
class IClock {
public:
virtual uint32_t nowMs() const = 0;
protected:
~IClock() = default;
};
// ---------------- Pure logic: no Arduino.h, no pin numbers ----------------
class Blinker {
public:
Blinker(IDigitalOut& led, const IClock& clock, uint32_t periodMs)
: led_(led), clock_(clock), periodMs_(periodMs) {}
void update() {
const uint32_t now = clock_.nowMs();
if (now - lastToggleMs_ >= periodMs_) { // rollover-safe subtraction
lastToggleMs_ = now;
on_ = !on_;
led_.write(on_);
}
}
bool isOn() const { return on_; }
private:
IDigitalOut& led_; // injected dependency
const IClock& clock_; // injected dependency
uint32_t periodMs_;
uint32_t lastToggleMs_ = 0;
bool on_ = false;
};
#ifdef ARDUINO
// ---------------- Real hardware adapters ----------------
#include <Arduino.h>
class ArduinoPin : public IDigitalOut {
public:
explicit ArduinoPin(uint8_t pin) : pin_(pin) {}
void begin() { pinMode(pin_, OUTPUT); }
void write(bool high) override { digitalWrite(pin_, high ? HIGH : LOW); }
private:
uint8_t pin_;
};
class ArduinoClock : public IClock {
public:
uint32_t nowMs() const override { return millis(); }
};
constexpr uint8_t PIN_LED = 13;
constexpr uint32_t BLINK_MS = 500;
ArduinoPin led(PIN_LED);
ArduinoClock systemClock;
Blinker blinker(led, systemClock, BLINK_MS);
void setup() { led.begin(); }
void loop() { blinker.update(); }
#else
// ---------------- Fakes for the PC ----------------
#include <stdio.h>
class FakePin : public IDigitalOut {
public:
void write(bool high) override {
level = high;
writes++;
}
bool level = false;
int writes = 0;
};
class FakeClock : public IClock {
public:
uint32_t nowMs() const override { return t; }
uint32_t t = 0;
};
int main() {
FakePin pin;
FakeClock fakeClock;
Blinker blinker(pin, fakeClock, 500);
const uint32_t times[] = {499, 500, 999, 1000};
for (uint32_t t : times) {
fakeClock.t = t; // we control time: no waiting, fully repeatable
blinker.update();
printf("t=%4u ms led=%d writes=%d\n", (unsigned)t, pin.level ? 1 : 0, pin.writes);
}
const bool ok = (!pin.level && pin.writes == 2);
puts(ok ? "PASS" : "FAIL");
return ok ? 0 : 1;
}
#endifការពន្យល់៖ Logic Blinker ដូចគ្នា ១០០% រត់លើ Arduino Uno និងលើ PC។ រត់ pio run -e native រួច .pio/build/native/program អ្នកនឹងឃើញ LED ប្ដូរស្ថានភាពនៅ 500 ms និង 1000 ms ហើយ Program បញ្ចប់ដោយ PASS (Exit code 0) — CI អាចប្រើ Exit code នេះដើម្បីដឹងថាជោគជ័យ។
លំហាត់ C++ HAL៖ បង្កើត Interface សេនស័រដែលអាច Unit Test បាន
បង្កើត pure virtual method readDistanceCm() = 0 ក្នុង class IDistanceSensor រួចបង្កើត FakeSensor ដែល override វាសម្រាប់តេស្តលើ PC។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- C++ Core Guidelines (C.35: Base class destructors) isocpp.github.ioគោលការណ៍រចនា Interface, Destructor និង Ownership ក្នុង C++ ទំនើប។
- PlatformIO Documentation docs.platformio.orgរបៀបដែល Framework arduino និង Platform native Build កូដដូចគ្នាតាម Environment។