Tutorial: 13.56 MHz NFC/NDEF (hal.nfc), Hardware RMT IR (hal.ir) & GNSS (hal.gnss)

This guide covers PylotOS's short-range badge/tag, infrared, and satellite positioning subsystems — all backed by zero-ISR-allocation C drivers.


1. 13.56 MHz HF RFID / NFC (hal.nfc + decode_ndef_record)

Supported Readers

import hal
from pylotos import wireless

uid_buf = bytearray(10)
blk_buf = bytearray(16)

hal.nfc.open()
uid_len = await wireless.poll_nfc_into(uid_buf, poll_ms=40, timeout_ms=5000)
if uid_len > 0:
    print("Tag UID:", ":".join("{:02X}".format(b) for b in uid_buf[:uid_len]))
    # Read NTAG213/215/216 user page 4 (16 bytes) and decode NDEF record:
    if hal.nfc.read_block(4, blk_buf) > 0:
        rec = wireless.decode_ndef_record(blk_buf)
        print("Decoded NDEF:", rec)  # {'type': 'uri', 'value': 'https://sdk.pylotos.org'}
hal.nfc.sleep()

2. Hardware ESP-IDF RMT v2 Infrared (hal.ir)

Unlike bit-banged Python loops that jitter whenever FreeRTOS or Wi-Fi interrupts fire, hal.ir drives ESP-IDF v5.5's hardware RMT v2 DMA peripheral with an exact 38 kHz (30..60 kHz configurable) hardware carrier:

import array
import hal

hal.ir.open(carrier_hz=38000)

# 1. Standard NEC protocol (8-bit address, 8-bit command):
hal.ir.tx_nec(0x04, 0x08)

# 2. Custom raw microsecond mark/space burst buffer (uint16 array):
pulses = array.array("H", [9000, 4500, 560, 560, 560, 1690, 560, 560])
hal.ir.tx_pulses(pulses, carrier_hz=38000)

# 3. Hardware RMT RX learning (on Grove IR Unit):
rx_raw = bytearray(128)
count = hal.ir.rx_into(rx_raw)
hal.ir.close()

3. Zero-Alloc C Streaming GNSS Parser (hal.gnss)

Parsing 9600/115200 baud NMEA sentences in Python every second creates hundreds of temporary strings and heap churn. hal_gnss_parser.h parses NMEA-0183 ($GPGGA/$GNGGA, $GPRMC/$GNRMC) and binary u-blox UBX-NAV-PVT in pure C using 32-bit fixed-point integers (lat_e7, lon_e7, alt_mm):

import hal

hal.gnss.open(uart_port=1, baudrate=9600)
fix = hal.gnss.poll()
if fix["valid"]:
    lat = fix["lat_e7"] / 1e7
    lon = fix["lon_e7"] / 1e7
    print("Fix ({} sats): {:.6f}, {:.6f}, {} m".format(fix["sats"], lat, lon, fix["alt_mm"] // 1000))
hal.gnss.close()

Preview both in pylotos-sim:

pylotos-sim examples/nfc_badge_scanner.py --board m5stack_dial --port 8080
pylotos-sim examples/ir_universal_remote.py --board m5stack_cardputer_adv --port 8080