|
| 1 | +# Hardware Integration |
| 2 | + |
| 3 | +## Bpod |
| 4 | + |
| 5 | +The Bpod integration injects barcode timing states into a pybpodapi `StateMachine`. |
| 6 | +The barcode is transmitted as a series of BNC output states; the last injected |
| 7 | +state chains to whatever state you specify as `last_state_name`. |
| 8 | + |
| 9 | +### Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +pip install ttl-barcoder[bpod] |
| 13 | +``` |
| 14 | + |
| 15 | +### Usage |
| 16 | + |
| 17 | +```python |
| 18 | +from ttl_barcoder.core import BarcodeTTL |
| 19 | +from ttl_barcoder.hardware.bpod import inject_barcode_states |
| 20 | + |
| 21 | +barcoder = BarcodeTTL() |
| 22 | +barcoder.prepare() |
| 23 | + |
| 24 | +inject_barcode_states( |
| 25 | + sma, # pybpodapi StateMachine |
| 26 | + barcoder.get_sequence(), |
| 27 | + bnc_channel="BNC1", # Bpod BNC output channel |
| 28 | + first_state_name="barcode_start", |
| 29 | + last_state_name="next_trial", # state to enter after barcode finishes |
| 30 | +) |
| 31 | +``` |
| 32 | + |
| 33 | +### State naming |
| 34 | + |
| 35 | +`inject_barcode_states` adds one state per barcode bit plus preamble and |
| 36 | +trailer states. The `first_state_name` is the entry point — set this as the |
| 37 | +`state_change_conditions` target from your preceding state. The sequence |
| 38 | +chains internally and exits to `last_state_name` when complete. |
| 39 | + |
| 40 | +### Channel |
| 41 | + |
| 42 | +Pass any BNC output channel supported by your Bpod model, e.g. `"BNC1"` or |
| 43 | +`"BNC2"`. The same channel must be wired to the recording system input. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Raspberry Pi GPIO (pigpio) |
| 48 | + |
| 49 | +!!! note "lgpio migration planned" |
| 50 | + The current implementation uses `pigpio`. Support for `lgpio` (compatible |
| 51 | + with Raspberry Pi 5 and newer kernels) is planned — see the |
| 52 | + [GitHub issue tracker](https://github.com/murineshiftwork/ttl-barcoder/issues). |
| 53 | + |
| 54 | +### Installation |
| 55 | + |
| 56 | +```bash |
| 57 | +pip install ttl-barcoder[pigpio] |
| 58 | +``` |
| 59 | + |
| 60 | +`pigpio` requires the pigpio daemon to be running on the Pi: |
| 61 | + |
| 62 | +```bash |
| 63 | +sudo pigpiod |
| 64 | +``` |
| 65 | + |
| 66 | +### Usage |
| 67 | + |
| 68 | +```python |
| 69 | +from ttl_barcoder.core import BarcodeTTL |
| 70 | +from ttl_barcoder.hardware.pigpio import send_barcode_sequence |
| 71 | + |
| 72 | +barcoder = BarcodeTTL() |
| 73 | +barcoder.prepare() |
| 74 | +send_barcode_sequence(barcoder.get_sequence(), pin=18) |
| 75 | +``` |
| 76 | + |
| 77 | +`pin` is the BCM GPIO pin number. The function is blocking — it returns after |
| 78 | +the full sequence has been transmitted. |
| 79 | + |
| 80 | +### Timing accuracy |
| 81 | + |
| 82 | +`pigpio` provides hardware-timed waveforms with ~1 µs jitter, well within the |
| 83 | +default 25 % tolerance. Ensure the pigpio daemon is started with sufficient |
| 84 | +priority (`sudo pigpiod -t 0`) on loaded systems. |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## Custom hardware |
| 89 | + |
| 90 | +For any hardware not listed above, consume the timing sequence directly: |
| 91 | + |
| 92 | +```python |
| 93 | +sequence = barcoder.get_sequence() |
| 94 | +# sequence: list of (level: bool, duration_ms: float) |
| 95 | + |
| 96 | +for level, duration_ms in sequence: |
| 97 | + your_output.set(level) |
| 98 | + time.sleep(duration_ms / 1000.0) |
| 99 | +``` |
| 100 | + |
| 101 | +The sequence is hardware-agnostic — any output that can switch a digital line |
| 102 | +and hold it for a specified duration can transmit a barcode. |
0 commit comments