# esp32 - demonstrating RMT module
# Copyright by Anthony Vogelaar, Perth Scotland
# rev .10  2025-09-29  initial release

# see: https://docs.micropython.org/en/latest/library/esp32.html

import	time
from	machine	import	Pin
from	esp32	import	RMT

# ======================== test1 =========================

# longest delay : 80 MHz / 255 / 0x7FFF = 104 mSec, mode #1
def test1():
    rmt = RMT(0, pin=Pin(25), clock_div=255)					# RMT clock is 312.5 kHz = 3.2 uSec
    rmt.write_pulses((0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF), 0)		# two 104 mSec impulses (connect to Led)


test1()

# ======================== test2 =========================

# shortest delay : 80 MHz / 1 / 1 = 12.5 nSec, mode #1
def test2():
    rmt = RMT(0, pin=Pin(25), clock_div=1)						# RMT clock is 80 MHz = 12.5 nSec
    rmt.write_pulses((1, 1, 1, 1, 1, 1, 1, 1), 0)				# four 12.5 nSec impulses
#    while not rmt.wait_done(): pass							# 120 uSed  delay
#    rmt.write_pulses((1, 5, 1, 5, 1, 5, 1, 5), 0)				# note start pulse
#    rmt.write_pulses((7, 1, 5, 1, 5, 1, 5), 1)					# ??
#    rmt.write_pulses((70, 10, 50, 10, 50, 10, 50), 1)			# ok


#test2()

# ======================== test3 =========================

# a 1 mSec and a 500 uSec impulse on pin 25 repeated  5 times, mode #1
def test3():
    rmt = RMT(0, pin=Pin(25), clock_div=80)					# RMT clock is 1 MHz = 1.0 uSec
    rmt.loop(True)
    rmt.write_pulses((1000, 1000, 500, 500), 1)				# a 1 mSEc and a 500 uSec impulse
    time.sleep_ms(10)
    rmt.loop(False)
    
    
#test3()

# ======================== test4 =========================

# a 1 mSec and a 500 uSec impulse on pin 25, mode #2
def test4():
    rmt = RMT(0, pin=Pin(25), clock_div=80)					# RMT clock is 1 MHz = 1.0 uSec
    rmt.write_pulses(500, (1, 1, 0, 0, 1))					# a 1 mSec and a 500 uSec impulse
    
    
#test4()

# ======================== test5 =========================

# a 1 mSec and a 500 uSec impulse on pin 25, mode #3
def test5():
    rmt = RMT(0, pin=Pin(25), clock_div=80)					# RMT clock is 1 MHz = 1.0 uSec
    rmt.write_pulses((1000, 1000, 500, 500),(1, 0, 1, 0))	# a 1 mSec and a 500 uSec impulse
    
    
#test5()

# ======================== test6 =========================

# morse SOS on pin 32  [... --- ...]
def test6():
    rmt = RMT(0, pin=Pin(32), clock_div=160)				# RMT clock is 1 MHz = 2.0 uSec
    S = [1, 0, 1, 0, 1, 0]
    O = [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0]
    sos = S + [0, 0] + O + [0, 0] + S
    rmt.write_pulses(20_000, sos)							# tick = 40 mSec - 20 words per minute
    
    
# test6()

# ========================== end ==========================
