.. note::

    こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ！Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。

    **参加する理由は？**

    - **エキスパートサポート**：コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
    - **学び＆共有**：ヒントやチュートリアルを交換してスキルを向上させましょう。
    - **独占的なプレビュー**：新製品の発表や先行プレビューに早期アクセスしましょう。
    - **特別割引**：最新製品の独占割引をお楽しみください。
    - **祭りのプロモーションとギフト**：ギフトや祝日のプロモーションに参加しましょう。

    👉 私たちと一緒に探索し、創造する準備はできていますか？[|link_sf_facebook|]をクリックして今すぐ参加しましょう！

.. _4.1.18_py_pi5:

4.1.15 GAME - 10秒間
=======================

はじめに
-------------------

次に、集中力を試すゲームデバイスを作ってみましょう。
傾きスイッチを棒に結び付けて、魔法の杖を作ります。杖を振ると、
4桁のセグメント表示がカウントを開始し、もう一度振るとカウントが停止します。表示されたカウントを
**10.00** で停止させることができれば、あなたの勝利です。友達とこのゲームをして、
誰が時間の魔法使いかを競い合うことができます。

必要な部品
------------------------------

このプロジェクトには、次のコンポーネントが必要です。

.. image:: ../python_pi5/img/4.1.18_game_10_second_list.png
    :width: 800
    :align: center

一式を購入するのが便利です、こちらがリンクです： 

.. list-table::
    :widths: 20 20 20
    :header-rows: 1

    *   - 名前	
        - このキットのアイテム
        - リンク
    *   - Raphael Kit
        - 337
        - |link_Raphael_kit|

以下のリンクから別々に購入することもできます。

.. list-table::
    :widths: 30 20
    :header-rows: 1

    *   - コンポーネントの紹介
        - 購入リンク

    *   - :ref:`cpn_gpio_board`
        - |link_gpio_board_buy|
    *   - :ref:`cpn_breadboard`
        - |link_breadboard_buy|
    *   - :ref:`cpn_wires`
        - |link_wires_buy|
    *   - :ref:`cpn_resistor`
        - |link_resistor_buy|
    *   - :ref:`cpn_4_digit`
        - \-
    *   - :ref:`cpn_74hc595`
        - |link_74hc595_buy|
    *   - :ref:`cpn_tilt_switch`
        - \-

回路図
------------------------

============ ======== ======== ===
T-Board Name physical wiringPi BCM
GPIO17       Pin 11   0        17
GPIO27       Pin 13   2        27
GPIO22       Pin 15   3        22
SPIMOSI      Pin 19   12       10
GPIO18       Pin 12   1        18
GPIO23       Pin 16   4        23
GPIO24       Pin 18   5        24
GPIO26       Pin 37   25       26
============ ======== ======== ===

.. image:: ../python_pi5/img/4.1.18_game_10_second_schematic.png
   :align: center



実験手順
---------------------------------

**ステップ1**: 回路を組み立てる。

.. image:: ../python_pi5/img/4.1.18_game_10_second_circuit.png

**ステップ2**: コードのフォルダに移動する。

.. raw:: html

   <run></run>

.. code-block::

    cd ~/raphael-kit/python-pi5

**ステップ3**: 実行可能ファイルを実行する。

.. raw:: html

   <run></run>

.. code-block::

    sudo python3 3.1.13_GAME_10Second_zero.py

杖を振ると、4桁のセグメント表示がカウントを開始し、もう一度振るとカウントが停止します。表示されたカウントを **10.00** で停止させることができれば、あなたの勝利です。もう一度振ると、ゲームの次のラウンドが始まります。

.. warning::

    エラー メッセージ ``RuntimeError: Cannot determine SOC peripheral base address`` が表示された場合は、 :ref:`faq_soc` を参照してください。

**コード**

.. note::
    下記のコードは **変更/リセット/コピー/実行/停止** が可能です。しかし、それを行う前に、 ``raphael-kit/python-pi5`` のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を確認することができます。

.. raw:: html

    <run></run>

.. code-block:: python

    #!/usr/bin/env python3

    from gpiozero import OutputDevice, Button
    import time
    import threading

    # Initialize the button connected to GPIO 26
    sensorPin = Button(26)

    # Define GPIO pins connected to the 74HC595 shift register
    SDI = OutputDevice(24)   # Serial Data Input
    RCLK = OutputDevice(23)  # Register Clock
    SRCLK = OutputDevice(18) # Shift Register Clock

    # Define GPIO pins for digit selection on the 7-segment display
    placePin = [OutputDevice(pin) for pin in (10, 22, 27, 17)]

    # Define segment codes for numbers 0 to 9 on the 7-segment display
    number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)

    # Counter and timer variables
    counter = 0
    timer1 = None
    gameState = 0

    def clearDisplay():
        """ Clear all segments on the 7-segment display. """
        for _ in range(8):
            SDI.on()
            SRCLK.on()
            SRCLK.off()
        RCLK.on()
        RCLK.off()

    def hc595_shift(data):
        """ Shift data to the 74HC595 shift register to display a digit. """
        for i in range(8):
            SDI.value = 0x80 & (data << i)
            SRCLK.on()
            SRCLK.off()
        RCLK.on()
        RCLK.off()

    def pickDigit(digit):
        """ Select which digit to display on the 7-segment display. """
        for pin in placePin:
            pin.off()
        placePin[digit].on()

    def display():
        """ Display the current counter value on the 7-segment display. """
        global counter
        clearDisplay()
        pickDigit(0)
        hc595_shift(number[counter % 10])

        clearDisplay()
        pickDigit(1)
        hc595_shift(number[counter % 100 // 10])

        clearDisplay()
        pickDigit(2)
        hc595_shift(number[counter % 1000 // 100] - 0x80)

        clearDisplay()
        pickDigit(3)
        hc595_shift(number[counter % 10000 // 1000])

    def stateChange():
        """ Handle state changes for the counter based on button presses. """
        global gameState, counter, timer1
        if gameState == 0:
            counter = 0
            time.sleep(1)
            timer()
        elif gameState == 1 and timer1 is not None:
            timer1.cancel()
            time.sleep(1)
        gameState = (gameState + 1) % 2

    def loop():
        """ Main loop to check for button presses and update the display. """
        global counter
        currentState = 0
        lastState = 0
        while True:
            display()
            currentState = sensorPin.value
            if (currentState == 0) and (lastState == 1):
                stateChange()
            lastState = currentState

    def timer():
        """ Timer function that increments the counter every 0.01 second. """
        global counter, timer1
        timer1 = threading.Timer(0.01, timer)
        timer1.start()
        counter += 1

    try:
        loop()
    except KeyboardInterrupt:
        if timer1:
            timer1.cancel()


**コードの説明**

#. 必要なモジュールをインポートしてスクリプトを開始します。 ``gpiozero`` ライブラリはボタンなどのGPIOデバイスとのインターフェースに使用され、 ``time`` および ``threading`` モジュールはタイミング関連のタスクや並行操作を扱うために使用されるかもしれません。

   .. code-block:: python

       #!/usr/bin/env python3

       from gpiozero import OutputDevice, Button
       import time
       import threading

#. GPIOピン26に接続された ``Button`` オブジェクトを初期化します。このセットアップにより、ボタン押下を検出することができます。

   .. code-block:: python

       # Initialize the button connected to GPIO 26
       sensorPin = Button(26)

#. シフトレジスタのシリアルデータ入力（SDI）、レジスタクロック入力（RCLK）、シフトレジスタクロック入力（SRCLK）に接続されたGPIOピンを初期化します。

   .. code-block:: python

       # Define GPIO pins connected to the 74HC595 shift register
       SDI = OutputDevice(24)   # Serial Data Input
       RCLK = OutputDevice(23)  # Register Clock
       SRCLK = OutputDevice(18) # Shift Register Clock

#. 7セグメント表示の各桁の選択用のピンを初期化し、0〜9の数字を表示するためのバイナリコードを定義します。

   .. code-block:: python

       # Define GPIO pins for digit selection on the 7-segment display
       placePin = [OutputDevice(pin) for pin in (10, 22, 27, 17)]

       # Define segment codes for numbers 0 to 9 on the 7-segment display
       number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)

#. 7セグメント表示を制御するための関数です。 ``clearDisplay`` はすべてのセグメントをオフにし、 ``hc595_shift`` はシフトレジスタにデータをシフトし、 ``pickDigit`` は表示上の特定の桁をアクティブにします。

   .. code-block:: python

       def clearDisplay():
           """ Clear all segments on the 7-segment display. """
           for _ in range(8):
               SDI.on()
               SRCLK.on()
               SRCLK.off()
           RCLK.on()
           RCLK.off()

       def hc595_shift(data):
           """ Shift data to the 74HC595 shift register to display a digit. """
           for i in range(8):
               SDI.value = 0x80 & (data << i)
               SRCLK.on()
               SRCLK.off()
           RCLK.on()
           RCLK.off()

       def pickDigit(digit):
           """ Select which digit to display on the 7-segment display. """
           for pin in placePin:
               pin.off()
           placePin[digit].on()

#. 現在のカウンター値を7セグメント表示に表示する関数です。

   .. code-block:: python

       def display():
           """ Display the current counter value on the 7-segment display. """
           global counter
           clearDisplay()
           pickDigit(0)
           hc595_shift(number[counter % 10])

           clearDisplay()
           pickDigit(1)
           hc595_shift(number[counter % 100 // 10])

           clearDisplay()
           pickDigit(2)
           hc595_shift(number[counter % 1000 // 100] - 0x80)

           clearDisplay()
           pickDigit(3)
           hc595_shift(number[counter % 10000 // 1000])

#. ボタン押下に基づいてカウンターの状態を変更する関数です。

   .. code-block:: python

       def stateChange():
           """ Handle state changes for the counter based on button presses. """
           global gameState, counter, timer1
           if gameState == 0:
               counter = 0
               time.sleep(1)
               timer()
           elif gameState == 1 and timer1 is not None:
               timer1.cancel()
               time.sleep(1)
           gameState = (gameState + 1) % 2

#. ボタンの状態を継続的にチェックし、表示を更新するメインループです。ボタンの状態が変わると、 ``stateChange`` を呼び出します。

   .. code-block:: python

       def loop():
           """ Main loop to check for button presses and update the display. """
           global counter
           currentState = 0
           lastState = 0
           while True:
               display()
               currentState = sensorPin.value
               if (currentState == 0) and (lastState == 1):
                   stateChange()
               lastState = currentState

#. 0.01秒ごとにカウンターを増加させるタイマー関数です。

   .. code-block:: python

       def timer():
           """ Timer function that increments the counter every 0.01 second. """
           global counter, timer1
           timer1 = threading.Timer(0.01, timer)
           timer1.start()
           counter += 1

#. メインループを実行し、キーボード割り込み（Ctrl+C）を使用してプログラムからクリーンに終了することができます。

   .. code-block:: python

       try:
           loop()
       except KeyboardInterrupt:
           if timer1:
               timer1.cancel()