from machine import Pin, PWM
import time


ir_led = PWM(Pin("GP18"))
ir_led.freq(38000)
ir_led.duty_u16(0)


def emit_pulse(duration_us):
    ir_led.duty_u16(0)      # 0% duty cycle
    time.sleep_us(duration_us)
    ir_led.duty_u16(32768)  # 50% duty cycle

def xmit_bin(msg_data_bin, msg_len):
    ir_led.duty_u16(32768)  # 50% duty cycle
    time.sleep_us(4000)

    for i in range(msg_len):
        if(msg_data_bin & (1<<(msg_len-1))):
            emit_pulse(1000)
        else:
            emit_pulse(2080)

        msg_data_bin <<= 1
        time.sleep_us(500)

    time.sleep_us(2000)
    ir_led.duty_u16(0)  # 50% duty cycle


try:
    while True:
        #xmit_bin(0b010101, 6)
        xmit_bin(7774, 16)
        time.sleep_ms(100)

        xmit_bin(15964, 16)
        time.sleep_ms(100)

except KeyboardInterrupt:
    ir_led.deinit()
