カメラ心拍アプリの難しいバグは、いつも数式の中にあるわけではありません。ユーザーの動きとアルゴリズムのタイミングの隙間にあります。指を半秒だけ離す。カメラが温まる。結果が出て消える。

解決策は if を増やすことではありませんでした。測定をボタン操作ではなく、状態機械として考えることです。
This diagram could not be rendered. Its source is still available below.
View diagram source
stateDiagram-v2
[*] --> initial
initial --> preparing: startDetection
preparing --> detectingFinger: camera ready
detectingFinger --> measuring: good coverage stays stable
measuring --> calculating: duration and quality pass
calculating --> completed: result accepted
calculating --> failed: result rejected
measuring --> detectingFinger: finger lost beyond grace period
failed --> initial: reset
completed --> initial: stop or new measurement
preparing は本物の状態
カメラ権限、セッション構成、トーチのウォームアップ、露出の安定化は瞬時ではありません。すぐ測定に入ると、最初の数秒は準備動作で汚れます。
指検出には待ち時間が必要
良い 1 フレームだけでは、指が正しく置かれたとは言えません。MonoPump は良好な覆いが一定時間続くまで測定を始めません。
This diagram could not be rendered. Its source is still available below.
View diagram source
flowchart TD
A[覆いが良好になる] --> B[安定タイマー開始]
B --> C{遅延後も良好か}
C -- no --> D[ガイドを続ける]
C -- yes --> E[解析器をリセットして測定開始]
猶予時間は急なリセットより自然
初期版は、指が動くとすぐリセットしていました。技術的には正しくても、使う側には厳しすぎます。人は握り方を調整します。短い猶予時間があると、短い悪化を許容できます。
This diagram could not be rendered. Its source is still available below.
View diagram source
sequenceDiagram
participant F as Frame analyzer
participant D as Detector
participant UI as UI
F-->>D: coverage becomes bad
D->>D: start grace timer
D-->>UI: keep measurement context
F-->>D: coverage recovers
D->>D: cancel grace timer
D-->>UI: continue measuring
延長測定は早すぎる失敗より良い
予定時間が終わっても品質や信頼度が弱い場合、MonoPump は最大時間まで測定を延長できます。すぐやり直しにせず、しかし無限にも続けないための折り合いです。
This diagram could not be rendered. Its source is still available below.
View diagram source
flowchart LR
A[予定測定時間に到達] --> B{品質と信頼度は十分か}
B -- yes --> C[完了]
B -- no --> D{最大時間未満か}
D -- yes --> E[収集を延長]
D -- no --> F[理由付きで失敗]
状態機械は内部整理だけではありません。表示、触覚、トーチ停止、結果保存、失敗の伝え方を決めます。生フレームが直接 UX を動かさない。フレームは状態に入り、状態がアプリを動かします。