단편 : 루아의 콜백 함수에 매개 변수를 전달하려면 어떻게합니까?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()
:이므로 모든
어떻게'buttonPinPressed'가 1로 설정되어 있습니까? – hjpotter92
죄송합니다. 이것은 게시의 실수였습니다. 코드를 변경했습니다. var는 buttonPinPressed가 아니라 buttonPin입니다. 문제는 여전히 지속됩니다. 서식을 잘못 작성한 경우 죄송합니다. 기능'에 – Jens
변경 '기능 (buttonPin) notifyButtonPressed (buttonPin) end'() notifyButtonPressed (buttonPin가) – hjpotter92