ラズパイでe-paperのボタン押下を取得する

使ったもの

4つのボタン(key1 .. key4)のGPIOピンアサイン

ちょっと古いサンプルコード

gpiozero で書き換え

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# vim:fenc=utf-8 ff=unix ft=python ts=4 sw=4 sts=4 si et ar :

# Python: Python RuntimeError GPIO
# https://copyprogramming.com/howto/python-runtimeerror-gpio

# Question:Do you have any sample about how to use those 4 keys on the side of display
# (labeled key1 key2 key3 key4)?
# https://www.waveshare.com/wiki/2.7inch_e-Paper_HAT_Manual#accordion31

from gpiozero import Button
from signal import pause
import sys


def handle(button_number):
    print(button_number, file=sys.stdout, flush=True)


key1 = 5
key2 = 6
key3 = 13
key4 = 19

try:
    button1 = Button(key1, pull_up=True)
    button2 = Button(key2, pull_up=True)
    button3 = Button(key3, pull_up=True)
    button4 = Button(key4, pull_up=True)

    button1.when_pressed = lambda: handle(1)
    button2.when_pressed = lambda: handle(2)
    button3.when_pressed = lambda: handle(3)
    button4.when_pressed = lambda: handle(4)

    pause()  # ボタンイベント待機
except KeyboardInterrupt:
    print("\nProgram interrupted. Cleaning up GPIO...", file=sys.stderr)
finally:
    print("GPIO cleanup complete.", file=sys.stderr)


# Key example · waveshareteam/e-Paper@22032b7 · GitHub
# https://github.com/waveshareteam/e-Paper/commit/22032b7cc1ed1669a914c6177017c718ce9d1218

# 2.7inch e-Paper: How to read the Keys on Raspberry PI?: · Issue #89
# https://github.com/waveshareteam/e-Paper/issues/89

利用例

"$SCRIPT_DIR/button-13354-stdout" | while read -r b; do
  case "$b" in
    1 | 4)
      curl -sSL 'https://radi.gost.kkhm.tokyo/cgi-bin/ffp-cron?option=-T' &
      ;;
    3)
      sudo reboot &
      ;;
  esac
done

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS