amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

85 lines
1.5 KiB
C++

#include "blink.hpp"
namespace blink {
GpioLease::GpioLease(Platform &platform)
: platform_(platform), handle_(-1), pin_(-1), active_low_(0),
logical_(0), configured_(false)
{
}
int GpioLease::acquire(const Config &config)
{
if (held()) {
return busy;
}
int status = validate(config);
if (status != 0) {
return status;
}
int resource = platform_.request(config.pin);
if (resource < 0) {
return resource;
}
handle_ = resource;
pin_ = config.pin;
active_low_ = config.active_low;
logical_ = config.initial;
status = platform_.configure(handle_, logical_ ^ active_low_);
if (status != 0) {
platform_.release(handle_);
handle_ = -1;
pin_ = -1;
active_low_ = 0;
logical_ = 0;
return status;
}
configured_ = true;
return 0;
}
void GpioLease::drive(int logical)
{
if (!held() || !configured_) {
return;
}
logical_ = logical;
platform_.write(handle_, logical_ ^ active_low_);
}
void GpioLease::release()
{
if (!held()) {
return;
}
if (configured_) {
drive(0);
}
platform_.release(handle_);
handle_ = -1;
pin_ = -1;
active_low_ = 0;
logical_ = 0;
configured_ = false;
}
bool GpioLease::held() const
{
return handle_ >= 0;
}
int GpioLease::handle() const
{
return handle_;
}
int GpioLease::pin() const
{
return pin_;
}
int GpioLease::logical() const
{
return logical_;
}
}