Skip to content

Instantly share code, notes, and snippets.

@mipsparc
Created June 3, 2019 04:02
Show Gist options
  • Save mipsparc/b64cfae426103c2b9c9ca82e8ac4dcbc to your computer and use it in GitHub Desktop.
Save mipsparc/b64cfae426103c2b9c9ca82e8ac4dcbc to your computer and use it in GitHub Desktop.
発車メロディ(発車ベル)ながすやつ for Linux/Mac/Windows
#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