"""
main
Pi 側アプリケーションのエントリーポイント
カメラ画像の送信と操舵量の受信・モーター制御を行う
"""

import time

from pi.camera.capture import CameraCapture
from pi.comm.zmq_client import PiZmqClient


def main() -> None:
    """Pi 側のメインループを実行する"""
    camera = CameraCapture()
    zmq_client = PiZmqClient()

    try:
        camera.start()
        zmq_client.start()
        print("Pi: カメラ・通信を開始")

        while True:
            # カメラ画像を取得して送信
            frame = camera.capture()
            zmq_client.send_image(frame)

            # 操舵量を受信（現時点ではログ出力のみ）
            control = zmq_client.receive_control()
            if control is not None:
                throttle, steer = control
                print(
                    f"受信: throttle={throttle:.2f},"
                    f" steer={steer:.2f}"
                )

            # タイムアウト判定
            if zmq_client.is_timeout():
                # TODO(rinto): モーター停止処理を追加する
                pass

            time.sleep(0.01)

    except KeyboardInterrupt:
        print("\nPi: 終了")
    finally:
        camera.stop()
        zmq_client.stop()


if __name__ == "__main__":
    main()
