necir - necir NEC protocol infrared receiving#

Adaptation status unknown

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Example

--Attention:
--1、This library passed the Air101 and Air103 tests. Air105 is temporarily not supported due to the gap between SPI transmission frames and frames.
--2、This library implements the NEC infrared data reception, send please use LuatOS underlying firmware comes with ir.sendNEC() function.
--3、Since this library is based on the standard four-wire SPI interface implementation, although only the MISO pin is used, the other three SPI-related pins are used
--  It cannot be used for other purposes unless you execute necir.close() and wait for necir.isClosed() to be true, the SPI will be completely released before it can be used for other purposes.
--Hardware module: VS1838 and its compatible integrated receiver
--Wiring Diagram:
--1、Single IO mode is supported, that is, only one SPI_MISO pin is used. At this time, the irq_pin parameter of necir.init must be the pin where the SPI_MISO is located.
--2、Multi-slave is supported. If other slave devices need to be connected to SPI bus, in order to prevent VS1838 from interfering with MISO of bus during non-infrared communication, you can use
--  An NMOS (such as AO3400) controls the power supply ground of VS1838. As shown below, GPIO acts as the chip select signal of VS1838. When GPIO output is low
--  VS1838 is disabled. VS1838 does not work at this time. Its OUT pin is high impedance and will not interfere with MISO. When GPIO output is high, it is enabled VS1838,
--  At this time its OUT pin will output infrared communication signal to the microcontroller. Since the general SPI slave chip selection logic is low enable, this can be used.
--  The same chip selects GPIO to control VS1838 and another SPI slave because the chip select logic is reversed. with necir library necir.close()
--  And necir.isClosed() can maximize the reuse of SPI interface, avoiding SPI exclusive and waste.
--  ____________________                ____________________
-- |                    |    Single IO      |                    |
-- |           SPI_MISO |--------------| OUT                |
-- | Air10x             |              |       VS1838       |
-- |                    |              |     Integrated receiver head    |
-- |               GPIO |----      ----| GND                |
-- |____________________|   |      |   |____________________| 
--                          |      |
--                          |  ____|________ 
--                          | |    D        |
--                          --| G      NMOS | 
--                            |____S________|
--                                 |
--                                GND
--Usage Example: Demo drives VS1838 and VS1838 with the same SPI interface W25QXX
--Usage Examples:
local necir = require("necir")

--Defining User Callback Functions
local function my_ir_cb(frameTab)
    log.info('get ir msg','addr=',frameTab[1],frameTab[2],'data=',frameTab[3])
end

sys.taskInit(function()
    local CS = gpio.setup(pin.PA07,0)  --VS1838(NMOS control its GND) chip select pin shared with W25QXX
    necir.init(spi.SPI_0,pin.PB03,my_ir_cb)

    while 1 do
        --===============================
        log.info('------necir start------')
        CS(1)     --enable VS1838
        necir.start()  --Open necir data receiving process
        sys.wait(10000)
        log.info('necir request to close')
        necir.close()   --Request Close necir
        while not (necir.isClosed()) do
            sys.wait(200)
        end
        CS(0)    --In addition to energy VS1838
        log.info('necir closed')
        sys.wait(1000)

        --===============================
        log.info('------setup to read w25qxx chip id------')
        spi.setup(spi.SPI_0,nil,
            0,--CPHA
            0,--CPOL
            8,--Data Width
            100000,--Frequency
            spi.MSB,--High and low bit sequence  
            spi.master,--Main Mode
            spi.full--Full Duplex
        )
        --Read W25QXX chi id,0XEF15, indicating that the chip model is W25Q32,0XEF16, indicating that the chip model is W25Q64
        CS(0)   --Film selection W25QXX
        spi.send(spi.SPI_0,string.char(0x90)..string.char(0x00)..string.char(0x00)..string.char(0x00))
        local chip_id = spi.recv(spi.SPI_0,2)
        log.info('w25qxx id=',chip_id:toHex())
        CS(1)   --Cancel film selection W25QXX
        sys.wait(1000)
    end
end)

necir.init(spi_id,irq_pin,recv_cb)#

necir Initialization

Parameters

Incoming Value Type

Explanation

number

spi_id,The SPI interface used by ID

number

irq_pin,The interrupt pin used, which must be the MISO pin of SPI in single IO mode

function

recv_cb,After the infrared data is received, the callback function has a table type parameter, which stores the address code, address code inversion, data code, and data code inversion respectively.

Return Value

None

Examples

local function my_ir_cb(frameTab)
    log.info('get ir msg','addr=',frameTab[1],frameTab[2],'data=',frameTab[3])
end

necir.init(spi.SPI_0,pin.PB03,my_ir_cb)

necir.start()#

Open necir data receiving process

Parameters

None

Return Value

None

Examples

necir.start()

necir.close()#

Request to close the necir data receiving process. After this function is executed, it is not guaranteed to close immediately. Necir. isClosed() is required to query whether it has been closed.

Parameters

None

Return Value

None

Examples

necir.close()

necir.isClosed()#

Determine whether the necir has been completely closed. After closing, the SPI interface used will be released and can be reused for other functions. To open again, you need to call again necir.start()

Parameters

None

Return Value

return value type

explanation

bool

Close successfully returned true

Examples

necir.isClosed()