Skip to content

API Reference

BarcodeTTL

Main interface for TTL barcode generation and decoding.

from ttl_barcoder import BarcodeTTL, BarcodeConfig

ttl_barcoder.BarcodeTTL

Main interface for TTL barcode generation and decoding.

Constructs and owns a generator, encoder, and decoder whose parameters are all derived from a single BarcodeConfig so they stay in sync.

info property

Return a summary dict covering config, generator, and encoder parameters.

decode_edges(edge_timestamps, edge_levels)

Decode edge timestamps to (timestamp, barcode_value) or None.

decode_stream(edge_timestamps, edge_levels, *, min_pulse_s=0.0, sample_interval_s=0.0)

Every barcode in an edge stream, found by the configured lengths.

default_config() classmethod

Return the default BarcodeConfig instance.

find_barcodes(edge_timestamps, edge_levels, *, min_pulse_s=0.0, sample_interval_s=0.0)

Start, end and value of every barcode in an edge stream.

get_multiple_sequences(count=1, interval_s=5.0, start_timestamp=None)

Return timing sequences for multiple barcodes spaced interval_s apart.

For timestamp TTL the barcodes encode evenly spaced future times; for random TTL each barcode is independently random.

get_sequence(barcode=None)

Return (level, duration_ms) timing sequence for hardware transmission.

get_sequence_from_timestamp(timestamp)

Get timing sequence from a specific Unix timestamp (timestamp TTL only).

prepare()

Capture wall time and return (barcode_value, wall_time, timing_sequence).

Wall time is captured before generate() so it matches the encoded timestamp.

recover_timestamp(barcode_value, reference_time=None)

Recover original timestamp from barcode value with wraparound handling.


BarcodeConfig

Pydantic v2 configuration model. All fields are validated on construction.

from ttl_barcoder import BarcodeConfig, TTLType, TimestampPrecision

ttl_barcoder.BarcodeConfig

Bases: BaseModel

Barcode configuration with Pydantic v2 validation.

coverage_years property

Coverage in years (timestamp TTL only).

safety_ratio property

Safety ratio (bit_duration vs tolerance window).

total_duration_ms property

Total barcode duration in milliseconds.

default() classmethod

Create default configuration.

from_dict(config_dict) classmethod

Create configuration from dictionary.

info()

Configuration summary as dictionary.


BarcodeDecoder

ttl_barcoder.BarcodeDecoder

Decode edge timestamps back to barcode values.

Must be constructed with parameters that match the encoder that produced the signal. Use BarcodeConfig to keep encoder and decoder in sync.

total_duration_ms property

Duration of one barcode: init and end framing plus all bits.

decode_edges(edge_timestamps, edge_levels)

Decode edge timestamps to (timestamp, barcode_value) or None on failure.

decode_stream(edge_timestamps, edge_levels, *, min_pulse_s=0.0, sample_interval_s=0.0)

(start_s, value) of every barcode in an edge stream.

find_barcodes(edge_timestamps, edge_levels, *, min_pulse_s=0.0, sample_interval_s=0.0)

Locate and decode every barcode in a continuous edge stream.

A barcode is located by its configured lengths, not by silences around it: each rising edge is a candidate start, and the line level is read at the middle of every init, bit and end segment from there. A decoded barcode covers total_duration_ms; the search resumes after it.

Parameters:

Name Type Description Default
edge_timestamps Sequence[float]

Edge times in seconds, ascending.

required
edge_levels Sequence[bool]

Level after each edge.

required
min_pulse_s float

Pulses up to this width are dropped first (see drop_short_pulses); zero-length pulses are always dropped.

0.0
sample_interval_s float

Sampling interval of the source, e.g. the frame interval of a camera line. When the init pulses are shorter than two samples they cannot be seen reliably: framing is not checked, and a barcode starts at the first high sample after the previous one. A sampled edge happened up to one interval before the sample that shows it, so each start is taken half an interval earlier. Bits shorter than about three samples do not decode reliably (at 60 fps, 35 ms bits decode exactly for about a quarter of frame phases, 50 ms bits always).

0.0

Returns:

Type Description
list[FoundBarcode]

One FoundBarcode (start, end, value) per barcode, in time order. Edges

list[FoundBarcode]

outside every [start_s, end_s] are not part of a barcode (trial pulses,

list[FoundBarcode]

other signals).


Generators

ttl_barcoder.TTLGenerator

Bases: ABC

Abstract base for TTL barcode value generators.

max_value property

Maximum possible barcode value.

encode_bits(value)

Encode barcode value as bit array (LSB first).

generate(timestamp=None) abstractmethod

Generate a barcode value.

ttl_barcoder.TimestampGenerator

Bases: TTLGenerator

Generate barcodes from Unix timestamps at configurable precision.

generate(timestamp=None)

Generate barcode from timestamp (defaults to current time).

generate_sequence(count=1, interval_s=5.0, start_timestamp=None)

Generate count barcodes spaced interval_s seconds apart.

recover_timestamp(barcode_value, reference_time=None)

Recover Unix timestamp from a barcode value, resolving modular wraparound.

Checks the previous, current, and next bit-width windows relative to reference_time and returns the candidate closest to the reference.

ttl_barcoder.RandomGenerator

Bases: TTLGenerator

Generate random n-bit barcode values using numpy.

generate(timestamp=None)

Generate a random barcode value.


Utilities

ttl_barcoder.get_preset(name)

Get a preset configuration by name.

ttl_barcoder.create_generator(config)

Create TimestampGenerator or RandomGenerator from config.


Enums

ttl_barcoder.TTLType

Bases: StrEnum

Type of barcode value to generate.

ttl_barcoder.TimestampPrecision

Bases: StrEnum

Timestamp quantization precision.