Skip to content

Instantly share code, notes, and snippets.

@mipsparc
Created June 3, 2019 04:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
発車メロディ(発車ベル)ながすやつ 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