micro_mpc_kmk/ir_test.py

72 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from machine import Pin
from time import ticks_ms, ticks_diff
from ir_pair import IRRxTxPollPair
# --- Настройки пинов ---
# RX_PIN: выход ИК-приёмника (обычно TTL сигнал с модуля VS1838/TSOP*)
# TX_PIN: пин ИК-светодиода/ключа (на него подаётся 38кГц PWM во время окна опроса)
RX_PIN = 16
TX_PIN = 6
# --- Тайминги опроса ---
POLL_PERIOD_MS = 500 # период опроса (как часто проверяем)
TX_ON_MS = 50 # сколько держим 38кГц включённым в каждом цикле
STATUS_PERIOD_MS = 1000
# --- Условия теста ---
# N влияет сразу на:
# - сколько раз "мигнуть" ИК-передатчиком за 1 цикл опроса
# - сколько фронтов (edges) нужно увидеть, чтобы считать луч НЕ перекрытым
N = 5
LED = Pin("LED", Pin.OUT)
def main():
pair = IRRxTxPollPair(
rx_pin=RX_PIN,
tx_pin=TX_PIN,
poll_period_ms=POLL_PERIOD_MS,
tx_on_ms=TX_ON_MS,
blinks_per_poll=N,
blink_off_ms=5,
freq_hz=38_000,
duty_percent=33,
min_edges=N,
# Считаем только FALLING: обычно на каждом включении carrier RX даёт 1 спад.
# Тогда 5 миганий ~= 5 edges (а при подсчёте обоих фронтов было бы ~10).
count_rising=False,
count_falling=True,
)
last_status = ticks_ms()
print("IR poll test started")
print("RX pin:", RX_PIN, "TX pin:", TX_PIN)
print("poll_period_ms:", POLL_PERIOD_MS, "tx_on_ms:", TX_ON_MS)
print("N:", N, "(blinks_per_poll and min_edges)")
try:
while True:
pair.poll_once() # результат и счетчики сохраняются внутри pair
LED.toggle()
now = ticks_ms()
if ticks_diff(now, last_status) >= STATUS_PERIOD_MS:
seen = bool(pair.last_seen)
print(
("BEAM NOT BLOCKED" if seen else "BEAM BLOCKED")
+ " | edges=%d" % pair.last_edges
)
last_status = now
finally:
pair.deinit()
main()