ESP8266 NODE MCU IR REMOTE CONTROL SONY TV

Post date: Jul 15, 2016 9:50:29 AM

------------------------------------------------------------------------------

-- IR send module

--

-- LICENCE: http://RDLAB.CDMT.VN

-- Le Duy Nhan nhanld@cdmt.vn

--

-- Example:

-- sony(1, 0x0C90)  Volume down

------------------------------------------------------------------------------

local M

do

  -- const

  local SONY_PULSE_US   = 1000000 / 24000

  --local SONY_HDR_MARK   = 2300-800

  local SONY_HDR_MARK   = 2240

  local SONY_HDR_SPACE  = 560

  local SONY_BIT_MARK   = 560

  local SONY_ONE_SPACE  = 1120

  local SONY_ZERO_SPACE = 560

  local SONY_RPT_SPACE  = 2250

  -- cache

  local gpio, bit = gpio, bit

  local mode, write = gpio.mode, gpio.write

  local waitus = tmr.delay

  local isset = bit.isset

  -- 24kHz PWM with 1/3 duty)

  local carrier = function(pin, c)

    c = c / SONY_PULSE_US

    while c > 0 do

      write(pin, 1)

      write(pin, 0)

      c = c + 0

      c = c + 0

      c = c + 0

      c = c + 0

      c = c + 0

      c = c + 0

      c = c * 1

      c = c * 1

      c = c * 1

      c = c - 1

    end

  end

    sony = function(pin, code, tsop)

    local pulse = tsop and pull or carrier

    -- setup transmitter

    mode(pin, 1)

    write(pin, tsop and 1 or 0)

    -- header

    pulse(pin, SONY_HDR_MARK)

    waitus(SONY_HDR_SPACE)

    -- sequence, lsb first

    for i = 11, 0, -1 do

      pulse(pin, (isset(code, i) and SONY_ONE_SPACE or SONY_ZERO_SPACE))

      waitus(SONY_BIT_MARK)

    end

  end

  -- expose

  M = {

    nec = nec,

  }

end

return M