# 設定ファイル（v1）

v1では、DAQ・スキャン・テストの設定を1つのファイルで統一的に管理できます。

## クイックスタート

`haniwers-v1 config init` コマンドで設定テンプレートを自動生成

```bash
$ haniwers-v1 config init
# → hnw.toml が生成されます
```

または、
`src/haniwers/v1/assets/config.toml`
をコピーして使用してください。

## 設定の優先順位

設定は以下の順序で適用されます（上ほど優先度が高い）

1. **CLIオプション**：`haniwers-v1 daq --port /dev/ttyUSB0`
2. **環境変数**：`HANIWERS_DEVICE_PORT`
3. **設定ファイル**：`hnw.toml`
4. **デフォルト値**：コード内の設定

## 主要セクション

### `[device]` - 実際のOSECHI検出器

```toml
[device]
label = "device"
port = "/dev/ttyUSB0"        # シリアルポート
baudrate = 115200            # ボーレート
timeout = 1.0                # タイムアウト（秒）
```

### `[mocker]` - テスト用（ハードウェア不要）

```toml
[mocker]
label = "mocker"
port = "/dev/null"
csv_path = ""                # CSVからデータを読み込む（空=ランダムデータ）
shuffle = false              # イベント順序をランダム化
speed = 1.0                  # 再生速度（1.0=等速、10.0=10倍速）
jitter = 0.0                 # タイミング揺らぎ（秒）
loop = true                  # CSVの終端に達したら最初に戻る
```

### `[sensors.ch1]`, `[sensors.ch2]`, `[sensors.ch3]` - センサー設定

```toml
[sensors.ch1]
id = 1
name = "ch1"
label = "top"
center = 300                 # 中心閾値
nsteps = 10                  # 中心から上下各10ステップ
step_size = 1                # 1ステップあたり1単位
threshold = "none"           # スキャン前は "none"、決定後は "280" など
```

:::note

**重要**

すべてのセンサーは同じ `nsteps` と `step_size` を使用する必要があります。センサーごとに異なるのは `center`（中心値）だけです。

:::

### `[sampler]` - データ処理（推奨）

```toml
[sampler]
label = "sampler"
mode = "count_based"         # または "time_based"
stream_mode = true           # true=長期測定、false=スキャン用
workspace = "."              # 出力ディレクトリ
filename_prefix = "osechi_data"
filename_suffix = ".csv"
events_per_file = 1000       # ファイルあたりイベント数
number_of_files = 10000      # ファイル数上限
max_retry = 3                # リトライ回数（スキャン用）
duration = 10.0              # スキャン時の測定時間（秒）
suppress_threshold = 1000    # ノイズ抑制閾値
```

### `[daq]` - DAQ設定（非推奨）

```toml
[daq]
label = "daq"
workspace = "."
filename_prefix = "osechi_data"
filename_suffix = ".csv"
events_per_file = 1000
number_of_files = 10000
stream_mode = true
```

### `[scan]` - スキャン設定（非推奨）

```toml
[scan]
label = "scan"
workspace = "."
filename_prefix = "scan_data"
filename_suffix = ".csv"
events_per_file = 1000
number_of_files = 10000
duration = 10.0
stream_mode = false
suppress_threshold = 1000
max_retry = 3
```

## 使用例

### 例1：実際のOSECHI検出器でデータ取得

```toml
[device]
port = "/dev/ttyUSB0"

[sensors.ch1]
center = 300

[sensors.ch2]
center = 310

[sensors.ch3]
center = 320

[sampler]
mode = "count_based"
stream_mode = true
events_per_file = 1000
```

### 例2：モック機能でテスト（ハードウェア不要）

```toml
[mocker]
csv_path = "data/sample.csv"  # サンプルCSVから再生
speed = 10.0                   # 10倍速で再生

[sampler]
mode = "count_based"
stream_mode = true
```

### 例3：閾値スキャン用

```toml
[sampler]
mode = "count_based"
stream_mode = false            # スキャンモード
duration = 10.0                # 各ステップで10秒測定
max_retry = 3
suppress_threshold = 1000
```

## 注意点

- **`[daq]` と `[scan]` は非推奨**：将来のリリースで削除される予定です。新しい設定には `[sampler]` を使用してください。
- **センサー設定の統一**：すべてのセンサーで `nsteps` と `step_size` は同じにする必要があります。

## 参照

- [サンプル設定](../../src/haniwers/v1/assets/config.toml)
- [設定ジェネレーター](../../src/haniwers/v1/config/generator.py)
