Skip to content

Instantly share code, notes, and snippets.

@ak1211
Created Jul 8, 2021
Embed
What would you like to do?
ak1211 / MyFirstApplication 用赤外線リモコン信号取得と送信用 circuit python プログラム
# https://ak1211.com/7707 (main.py)
# Copyright 2021 Akihiro Yamamoto
# Licensed under the Apache License, Version 2.0
from digitalio import DigitalInOut, Direction
import board
import pulseio
import supervisor
import sys
import time
import array
import gc
# 受け付けるリモコンパルス数の最低長
ACCEPT_MIN_IR_PULSES = 40
# 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
# LEDを消灯させる
led.value = 1
def output_pulse_to_console():
# LEDを点灯させる
led.value = 0
# パルス読み込み一時停止
irm_pulse.pause()
#
bytes = array.array('B')
bytes.append(ord('['))
for index in range(len(irm_pulse)):
for ch in str(irm_pulse[index]):
bytes.append(ord(ch))
bytes.append(ord(','))
bytes[len(bytes)-1] = ord(']')
# 返答
for ch in bytes:
sys.stdout.write(chr(ch))
sys.stdout.write("\r\n")
# パルス読み込み再開
irm_pulse.resume()
# LEDを消灯させる
led.value = 1
def serial_input_loop():
inputs = ""
for x in range(1*5000/1):
time.sleep(1/5000)
while supervisor.runtime.serial_bytes_available:
inputs += sys.stdin.read(1)
return inputs
while True:
if (len(irm_pulse) > ACCEPT_MIN_IR_PULSES):
output_pulse_to_console()
# 読み込んだパルスを消去する
irm_pulse.clear()
# ここで時間待ちのついでにシリアル入力をしておく
serial_inputs = serial_input_loop()
# 入力文字列を配列にする
start = serial_inputs.find('[')
end = serial_inputs.rfind(']')
if (end-start) > 1:
contents = serial_inputs[1+start:end]
to_output_pulses = array.array( 'H', [int(v.strip()) for v in contents.split(',')] )
if len(to_output_pulses) > ACCEPT_MIN_IR_PULSES:
# LEDを点灯させる
led.value = 0
# パルス読み込み一時停止
irm_pulse.pause()
# 読み込んだパルスを消去する
irm_pulse.clear()
# 赤外線リモコン信号送出
infrared_led_pulse.send(to_output_pulses)
# パルス読み込み再開
irm_pulse.resume()
# 返答
sys.stdout.write("ok")
sys.stdout.write("\r\n")
# LEDを消灯させる
led.value = 1
del to_output_pulses
del contents
#
del end
del start
del serial_inputs
gc.collect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment