#author("2024-08-19T16:19:44+09:00","default:nobuoki","nobuoki") #author("2024-08-22T10:15:18+09:00","default:nobuoki","nobuoki") * ラズパイでe-paperのボタン押下を取得する [#d172e256] ** 使ったもの [#k2a6bd76] - Raspberry Pi 3B+ with "Debian GNU/Linux 12 (bookworm)" - [[264x176, 2.7inch E-Ink display HAT for Raspberry Pi, SPI interface | WF10190CZ22>https://www.waveshare.com/2.7inch-e-paper-hat.htm]] #ref(https://www.waveshare.com/img/devkit/LCD/2.7inch-e-Paper-HAT/2.7inch-e-Paper-HAT-details-5.jpg,480x300) e-paper は、秋葉原 千石通商で買いました([[WaveShare 13354 2.7インチ 264×176 E-Ink 電子ペーパーモジュール for RaspberryPi>https://www.sengoku.co.jp/mod/sgk_cart/detail.php?code=EEHD-58WL]]) ** 4つのボタン(key1 .. key4)のGPIOピンアサイン [#zbca4565] - [[2.7inch e-Paper HAT Manual - Waveshare Wiki>https://www.waveshare.com/wiki/2.7inch_e-Paper_HAT_Manual#accordion31]] #ref(https://www.waveshare.com/w/upload/c/ce/2.7inch_e-Paper_HAT_Manual02.png) ** お手本(サンプルコード) [#z4701b06] - [[Key example · waveshareteam/e-Paper@22032b7 · GitHub>https://github.com/waveshareteam/e-Paper/commit/22032b7cc1ed1669a914c6177017c718ce9d1218]] - 残念なことに、bookwormだと動かない(RPi.GPIO.add_event_detect()がエラーを吐く) ** gpiozero で書き換え [#q6c1ea1e] - [[button-13354-stdout.py>https://gist.github.com/kemasoft-oss/0527438f4da33ee8a1249c94d19b7d9f]] #gist(0527438f4da33ee8a1249c94d19b7d9f) 実行すると、key1..4の押下と同時に標準出力へ 1..4 を出力します(Ctrl-Cで終了) #prism(bash){{{ $ python3 button-13354-stdout.py 4 3 2 2 4 ^C Program interrupted. Cleaning up GPIO... GPIO cleanup complete. }}} ** 活用例 [#g14f255d] #prism(bash){{{ python3 button-13354-stdout.py | while read -r b; do case "$b" in 1) # key1を押した時の処理 curl -sSL 'https://example.com/api/v1/someapi?id=1¶m=a' & ;; 2) # key2を押した時の処理 ;; 3) # key3を押した時の処理 sudo reboot ;; 4) # key1を押した時の処理 ;; esac done }}}