0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でIoTプログラミング on ESP32 #7 - ボタンスイッチ3

Last updated at Posted at 2025-02-22

Buttonプログラムの3つ目になります。
今回のプログラムではButtonパッケージを導入して高度なイベントを検出できるようにしてみました。
回路は今まで同じです。

ハードウェア

・Freenove ESP32-S3-WROOM Board or Espressif ESP32-S3-DevKitC
・ブレッドボード
・タクトスイッチ
・ジャンパーワイヤ

配線

button.PNG

導入パッケージ

・nanoFramework.Iot.Device.Button
(nanoFramework.System.Device.Gpioも同時に追加されます)

プログラミング

・新規プロジェクトを作成します。プロジェクト名は「ESP32S3_Button_Nuget」としました。

・コードを入力する前にパッケージは導入してください。

・コードは次のようになります。

Program.cs
using System;
using System.Threading;
using System.Diagnostics;

using System.Device.Gpio;
using Iot.Device.Button;

namespace ESP32S3_Button_Nuget
{
    public class Program
    {
        private static GpioPin led;
        private static Boolean sw;

        public static void Main()
        {
            GpioController controller = new GpioController();

            led = controller.OpenPin(2, PinMode.Output);

            sw = false;

            //Buttonの設定
            GpioButton button = new GpioButton(35, controller, true, PinMode.InputPullDown);
            //ダブルクリックを有効にする
            button.IsDoublePressEnabled = true;
            //長押しを有効にする
            button.IsHoldingEnabled = true;

            //event
            button.ButtonDown += Button_ButtonDown;         //ボタンを押した時にイベント
            button.ButtonUp += Button_ButtonUp;             //ボタンを離した時にイベント
            button.Press += Button_Press;                   //ボタンを押してから離した時にイベント
            button.DoublePress += Button_DoublePress;      //ボタンをダブルクリック下時にイベント
            button.Holding += Button_Holding;              //ボタンを長押しした時にイベント
            Debug.WriteLine("Start!");

            Thread.Sleep(Timeout.Infinite);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Button_Holding(object sender, ButtonHoldingEventArgs e)
        {
            switch (e.HoldingState)
            {
                case ButtonHoldingState.Started:
                    Debug.WriteLine("Holding Start.");
                        break;
                case ButtonHoldingState.Completed:
                    Debug.WriteLine("Holding Completed.");
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Button_DoublePress(object sender, EventArgs e)
        {
            Debug.WriteLine("Button DoublePress");
            if (sw == false)
            {
                led.Write(PinValue.High);
                sw = true;
            }
            else
            {
                led.Write(PinValue.Low);
                sw = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Button_Press(object sender, EventArgs e)
        {
            Debug.WriteLine("Button Press");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Button_ButtonUp(object sender, EventArgs e)
        {
            Debug.WriteLine("Button Up");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Button_ButtonDown(object sender, EventArgs e)
        {
            Debug.WriteLine("Button Down");
        }
    }
}

実行するとボタンを操作した状態をデバッグウィンドウに出力します。
ダブルクリックをすることでオンボードLEDの点灯状態を変えることができます。
一定時間ボタンを押したままにすると長押し状態が検出され、そのあとボタンを離すと長押し状態が完了(解除)した状態が検出されます。

Buttonパッケージを使うと高度なスイッチの状態を検出できるようになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?