Last active
June 16, 2020 04:16
-
-
Save ak1211/28bec35b5e8cd202e9c4be85b63d42b4 to your computer and use it in GitHub Desktop.
赤外線リモコン信号取得と送信用 circuit python プログラム
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
# https://ak1211.com/7586 (main.py) | |
# Copyright 2020 Akihiro Yamamoto | |
# Licensed under the Apache License, Version 2.0 | |
from digitalio import DigitalInOut, Direction | |
import array | |
import board | |
import pulseio | |
import supervisor | |
import sys | |
import time | |
# IRM | |
irm_pulse = pulseio.PulseIn(board.D0, maxlen=2000, idle_state=True) | |
# PWM | |
# carrier: 38kHz | |
# duty cycle: 1/3 = 65536/3 = 21845 | |
pwm = pulseio.PWMOut(board.D1, duty_cycle=21845, frequency=38000, variable_frequency=False) | |
infrared_led_pulse = pulseio.PulseOut(pwm) | |
led = DigitalInOut(board.D13) | |
led.direction = Direction.OUTPUT | |
toggle = True | |
while True: | |
if (len(irm_pulse) > 10): | |
irm_pulse.pause() | |
output = '{"name":[' | |
for i in range(len(irm_pulse)): | |
output = output + "{},".format(irm_pulse[i]) | |
output = output[:-1] + "]}" | |
print(output) | |
irm_pulse.resume() | |
irm_pulse.clear() | |
# LEDを点滅させる | |
led.value = toggle | |
toggle = not toggle | |
serial_inputs = "" | |
# 時間待ちのついでにシリアル入力をしておく | |
for c in range(6): | |
time.sleep(0.2) | |
while supervisor.runtime.serial_bytes_available: | |
serial_inputs += sys.stdin.read(1) | |
# 入力文字列を配列にする | |
start = serial_inputs.find('[') | |
end = serial_inputs.rfind(']') | |
if (end-start) > 10: | |
contents = serial_inputs[1+start:end] | |
to_output_pulses = array.array( 'H', [int(v.strip()) for v in contents.split(',')] ) | |
# 赤外線リモコン信号送出 | |
infrared_led_pulse.send(to_output_pulses) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
update