2017-01-06 1 views
0

단편 : 루아의 콜백 함수에 매개 변수를 전달하려면 어떻게합니까?Lua : 콜백 함수에 대한 매개 변수 추가

긴 이야기 :

은 내가 NodeMCU 펌웨어를 가진 ESP8266에서 일하고 있어요. 기본적으로 노드 당 여러 개의 버튼이있는 대시 버튼을 만들려고합니다. GPIO 핀의 인터럽트 가능성으로이 작업을 수행하고 있습니다.

그러나 매개 변수를 콜백 함수에 전달하는 방법에 대해서는 잘 설명되어 있지 않습니다. 제 경우에는 어느 핀이 인터럽트인지를 알고 싶습니다. 이것이 내가 생각해내는 것입니다. 핀의 값을 제외하고는 동작하고 있습니다. 트리거되었을 때 초기화 값 1로 리셋 된 것처럼 보입니다.

-- Have an area to hold all pins to query (in testing only one) 

    buttonPins = { 5 } 
    direction="up" 

    armAllButtons() 

    function armAllButtons() 
    for i,v in ipairs(buttonPins) 
    do 
     armButton(v) 
    end 
    end 


    function armButton(buttonPin) 
    print("Arming pin "..buttonPin.." for button presses.") 

    gpio.mode(buttonPin,gpio.INT,gpio.FLOAT) 
    gpio.trig(buttonPin, direction, function (buttonPin) notifyButtonPressed(buttonPin) end) 

    print("Waiting for button press on "..buttonPin.."...") 
    end 

    function notifyButtonPressed(buttonPin) 
    print("Button at pin "..buttonPin.." pressed.") 

    --rearm the pins for interrupts 
    armButton(buttonPin) 
    end 

는 그러나 notifyButtonPressed() 함수 내에서, buttonPin의 값을 누르면 내가 기대하는 것처럼, 아니 5가 될 한 항상. 아마도 숫자 변수의 초기화 값이라고 가정합니다. 이 출력

buttonPins = { 5 } 
    direction="up" 

    function armButton(buttonPin) 
    print("Arming pin "..buttonPin.." for button presses.") 

    gpio.mode(buttonPin,gpio.INT,gpio.FLOAT) 
    gpio.trig(buttonPin, direction, function (buttonPin) --notifyButtonPressed(buttonPin) end) 

    print("Waiting for button press on "..buttonPin.."...") 
    end 

    function notifyButtonPressed(buttonPin) 
    print("Button at pin "..buttonPin.." pressed.") 

    --rearm the pins for interrupts 
    armButton(buttonPin) 
    end 

    function armAllButtons() 
    for i,v in ipairs(buttonPins) 
    do 
     armButton(v) 
    end 
    end 

armAllButtons() 

:이므로 모든

+0

어떻게'buttonPinPressed'가 1로 설정되어 있습니까? – hjpotter92

+0

죄송합니다. 이것은 게시의 실수였습니다. 코드를 변경했습니다. var는 buttonPinPressed가 아니라 buttonPin입니다. 문제는 여전히 지속됩니다. 서식을 잘못 작성한 경우 죄송합니다. 기능'에 – Jens

+1

변경 '기능 (buttonPin) notifyButtonPressed (buttonPin) end'() notifyButtonPressed (buttonPin가) – hjpotter92

답변

1

먼저, 코드 내가이 같은 조각을 재 배열 후에는

input:6: attempt to call a nil value (global 'armAllButtons') 

가 발생합니다 ... 전혀 실행되지 않습니다 :

콜백 작업을 완벽하게 수행하려면 각 단추마다 다른 함수를 전달하고 arg 함수에 대한 함수 ... 시도해보십시오 :

buttonPins = { 5 } 
    direction="up" 

    function armButton(buttonPin) 
    print("Arming pin "..buttonPin.." for button presses.") 

    gpio.mode(buttonPin,gpio.INT,gpio.FLOAT) 
    gpio.trig(
     buttonPin, 
     direction, 
     function() 
     notifyButtonPressed(buttonPin) 
     end 
    ) -- this should create a function for each button, and each function shall pass a different argument to notifyButtonPressed 

    print("Waiting for button press on "..buttonPin.."...") 
    end 

    function notifyButtonPressed(buttonPin) 
    print("Button at pin "..buttonPin.." pressed.") 

    --rearm the pins for interrupts 
    armButton(buttonPin) 
    end 

    function armAllButtons() 
    for i,v in ipairs(buttonPins) 
    do 
     armButton(v) 
    end 
    end 

armAllButtons() 
관련 문제