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

122 lines
2.4 KiB
C++

#include "blink.hpp"
namespace blink {
Session::Session(Platform &platform)
: platform_(platform), gpio_(platform), timer_(platform), config_(),
phase_(0), pulses_(0), generation_(0), callbacks_(0)
{
}
int Session::open(const Config &config, int commit_result)
{
if (config.pin != 7 || config.active_low != 0 || config.period != 500) return invalid;
if (phase_ != 0) {
return busy;
}
int status = validate(config);
if (status != 0 || (commit_result != 0 && commit_result != io_error)) {
return invalid;
}
config_ = config;
phase_ = 1;
pulses_ = 0;
status = gpio_.acquire(config_);
if (status != 0) {
clear_current();
return status;
}
status = timer_.prepare(&Session::callback, this);
if (status != 0) {
unwind();
return status;
}
phase_ = 2;
status = timer_.start(config_.period);
if (status != 0) {
unwind();
return status;
}
if (commit_result != 0) {
unwind();
return commit_result;
}
++generation_;
return 0;
}
int Session::stop()
{
if (phase_ == 0) {
return 0;
}
retire();
return 0;
}
int Session::change_period(std::uint32_t period)
{
if (phase_ != 2) {
return missing;
}
if (period == 0 || period > maximum_period) {
return invalid;
}
config_.period = period;
timer_.rearm(period);
return 0;
}
int Session::set_level(int logical)
{
if (phase_ != 2) {
return missing;
}
if (logical != 0 && logical != 1) {
return invalid;
}
gpio_.drive(logical);
return 0;
}
int Session::toggle()
{
if (phase_ != 2) {
return missing;
}
gpio_.drive(1 - gpio_.logical());
return 0;
}
Snapshot Session::snapshot() const
{
Snapshot value;
value.phase = phase_;
value.pin = gpio_.pin();
value.gpio = gpio_.handle();
value.timer = timer_.handle();
value.logical = gpio_.logical();
value.period = phase_ == 0 ? 0 : config_.period;
value.pulses = pulses_;
value.generation = generation_;
value.callbacks = callbacks_;
return value;
}
void Session::callback(void *context)
{
auto *session = static_cast<Session *>(context);
session->pulse();
}
void Session::pulse()
{
++callbacks_;
if (phase_ != 2) {
return;
}
gpio_.drive(1 - gpio_.logical());
++pulses_;
timer_.rearm(config_.period);
}
}