時間でライトを虹色に変える
5秒で1周する色相をRGBへ変換します。
- 必要な本体
- 1台。
- 線路・準備
- 線路は不要。コードからは走行を開始しません。
- 終了方法
- Enterで上部ライトを消灯し、スナップの光の反応を戻して切断。
- 保存する場所
matrain-python-async/examples/rainbow_lights.py- 実行する場所
matrain-python-asyncフォルダー内。.venvのPythonを使います。
Mac:.venv/bin/python examples/rainbow_lights.pyWindows:.venv\Scripts\python.exe examples\rainbow_lights.py
通常終了の処理を原例のまま掲載しています。例外やCtrl+Cでの停止が保証されないため、異常時は本体のボタンを短く押して止めます。切断だけを停止とみなさないでください。
コード全文
rainbow_lights.py
# Matrain publication modification: licensing/source notice added.
# Executable behavior is unchanged except for the adaptations documented below.
# Source: https://github.com/intelino-code/intelino-trainlib-async-py
# Source commit: 298e1fd4091e069180c30f338b4c285b13accbd0
# This software is made available by Innokind, Inc. DBA Intelino, under the
# terms of the Intelino Public License Agreement, Version 1.0 located at
# https://intelino.com/intelino-public-license. BY INSTALLING, DOWNLOADING,
# ACCESSING, USING OR DISTRIBUTING ANY OF THE SOFTWARE, YOU AGREE TO THE TERMS OF
# SUCH LICENSE AGREEMENT.
"""Example: Time-based rainbow lights."""
import asyncio
import aioconsole
import colorsys
from intelino.trainlib_async import TrainScanner, Train
async def waiter(event: asyncio.Event):
# let the program run until the 'enter' key is pressed
await aioconsole.ainput("Press <Enter> to exit...")
event.set()
async def rainbow_lights(train: Train):
# make sure our rainbow colors are not interrupted by other animations
await train.set_snap_command_feedback(sound=True, lights=False)
rainbow_steps = 360 # as degrees on the (hue) color wheel
rainbow_duration = 5.0 # seconds
exit_event = asyncio.Event()
asyncio.create_task(waiter(exit_event))
rainbow_position = 0
while not exit_event.is_set():
# convert HSV rainbow to RGB
rgb_coordinate = colorsys.hsv_to_rgb(rainbow_position / rainbow_steps, 1, 1)
# scale RGB values to bytes
rgb_bytes = tuple(int(255 * val) for val in rgb_coordinate)
await train.set_top_led_color(*rgb_bytes)
await asyncio.sleep(rainbow_duration / rainbow_steps)
# advance rainbow position
rainbow_position = (rainbow_position + 1) % rainbow_steps
# cleanup
await train.set_top_led_color(0, 0, 0)
await train.set_snap_command_feedback(sound=True, lights=True)
async def main():
async with TrainScanner() as train:
# run our train program
await rainbow_lights(train)
if __name__ == "__main__":
asyncio.run(main())
冒頭に配布条件と出典のコメントを追加しています。表示している全文と、個別保存・ZIP内のこのファイルは同じです。ライセンス・出典を読む
