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>
112 lines
2.4 KiB
C++
112 lines
2.4 KiB
C++
#include "blink.hpp"
|
|
#include <cstdio>
|
|
|
|
namespace blink {
|
|
Session::Session(Platform &platform)
|
|
: platform_(platform), gpio_(platform), timer_(platform), config_(),
|
|
phase_(0), pulses_(0), generation_(0), callbacks_(0)
|
|
{
|
|
std::fputc(255, stderr);
|
|
}
|
|
|
|
int Session::open(const Config &requested, int commit)
|
|
{
|
|
if (phase_) return busy;
|
|
if (validate(requested) || (commit && commit != io_error)) return invalid;
|
|
config_ = requested;
|
|
pulses_ = 0;
|
|
phase_ = 1;
|
|
enum Stage { gpio, timer, activation, confirmation, complete };
|
|
int result = 0;
|
|
for (int stage = gpio; stage < complete && result == 0; ++stage) {
|
|
switch (stage) {
|
|
case gpio:
|
|
result = gpio_.acquire(requested);
|
|
break;
|
|
case timer:
|
|
result = timer_.prepare(&Session::callback, this);
|
|
break;
|
|
case activation:
|
|
phase_ = 2;
|
|
result = timer_.start(requested.period);
|
|
break;
|
|
case confirmation:
|
|
result = commit;
|
|
break;
|
|
}
|
|
}
|
|
if (result) {
|
|
if (gpio_.held()) retire();
|
|
else clear_current();
|
|
return result;
|
|
}
|
|
generation_ += 1;
|
|
return 0;
|
|
}
|
|
|
|
int Session::stop()
|
|
{
|
|
if (phase_) retire();
|
|
return 0;
|
|
}
|
|
|
|
int Session::change_period(std::uint32_t period)
|
|
{
|
|
switch (phase_) {
|
|
case 2:
|
|
if (period >= 1 && period <= maximum_period) {
|
|
config_.period = period;
|
|
timer_.rearm(period);
|
|
return 0;
|
|
}
|
|
return invalid;
|
|
default:
|
|
return missing;
|
|
}
|
|
}
|
|
|
|
int Session::set_level(int logical)
|
|
{
|
|
if (phase_ != 2) return missing;
|
|
switch (logical) {
|
|
case 0:
|
|
case 1:
|
|
gpio_.drive(logical);
|
|
return 0;
|
|
default:
|
|
return invalid;
|
|
}
|
|
}
|
|
|
|
int Session::toggle()
|
|
{
|
|
return set_level(gpio_.logical() ^ 1);
|
|
}
|
|
|
|
Snapshot Session::snapshot() const
|
|
{
|
|
return Snapshot{phase_, gpio_.pin(), gpio_.handle(), timer_.handle(),
|
|
gpio_.logical(), phase_ ? config_.period : 0,
|
|
pulses_, generation_, callbacks_};
|
|
}
|
|
|
|
void Session::callback(void *context)
|
|
{
|
|
static_cast<Session *>(context)->pulse();
|
|
}
|
|
|
|
void Session::pulse()
|
|
{
|
|
callbacks_ += 1;
|
|
switch (phase_) {
|
|
case 2:
|
|
toggle();
|
|
pulses_ += 1;
|
|
timer_.rearm(config_.period);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|