Created
June 3, 2019 04:02
-
-
Save mipsparc/b64cfae426103c2b9c9ca82e8ac4dcbc to your computer and use it in GitHub Desktop.
発車メロディ(発車ベル)ながすやつ for Linux/Mac/Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
# Requirements: Python3, PySerial, Pygame | |
# 同じディレクトリに bell.wav と announce.wav を用意する | |
# 「どこでも発車ベル」と同じボタンをつなぐ | |
# シリアルポートを設定する | |
import serial | |
import time | |
import pygame | |
# シリアルポートのデバイスファイル名に差し替える | |
port = '/dev/ttyUSB0' | |
pygame.mixer.init(44100, -16, 1, 256) | |
bell = pygame.mixer.Sound('bell.wav') | |
announce = pygame.mixer.Sound('announce.wav') | |
s = serial.Serial(port, dsrdtr=True) | |
s.dtr = True | |
last_state = False | |
while True: | |
state = s.dsr | |
if last_state == False and state == True: | |
bell.play(loops=-1) | |
last_state = True | |
elif last_state == True and state == False: | |
bell.stop() | |
last_state = False | |
announce.play() | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment