类型错误:函数采用 1 个位置参数,但给出了 2 个

问题描述 投票:0回答:1

我正在尝试将多个按钮与 Esp8266 连接并将其显示在 16x2 LCD 上。我正在使用 micropython。这是我的代码

import time
from machine import I2C, Pin, Timer
from i2c_lcd import I2cLcd

DEFAULT_I2C_ADDR = 0x27


class Button:
    def __init__(self, pin, callback):
        self.pin = Pin(pin, Pin.IN, Pin.PULL_UP)
        self.callback = callback
        self.debounce_timer = Timer(-1)
        self.state = False

    def init(self):
        self.pin.irq(trigger=Pin.IRQ_FALLING, handler=self.interrupt_handler)
        print('Button initialized')

    def interrupt_handler(self):
        if not self.state:
            self.debounce_timer.init(period=50, mode=Timer.ONE_SHOT, callback=self.debounce_callback)       

    def debounce_callback(self):
        if not self.state and self.pin.value() == 0:
            self.callback()
        self.state = False


class Counter:
    def __init__(self, lcd):
        self.lcd = lcd
        self.count = 0

    def increment(self):
        print('Increament start')
        self.count += 1
        print('About to call update display')
        self.update_display()
        

    def decrement(self):
        self.count -= 1
        self.update_display()

    def update_display(self):
        print('Update display')
        self.lcd.move_to(11, 1)
        self.lcd.putstr("     ")
        self.lcd.move_to(11, 1)
        self.lcd.putstr(str(self.count))

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)

# Initialize counter
counter = Counter(lcd)

# Create a list of buttons
buttons = []


    
# Initialize buttons with their pins and callback functions (if any)
buttons.append(Button(13, counter.increment))  # Increment button
buttons.append(Button(14, counter.decrement))  # Decrement button


# Start all buttons
for button in buttons:
    button.init()
    
lcd.clear()
lcd.move_to(11, 0)
lcd.putstr("COUNT")

当我按下中断按钮时,代码编译正常,但出现错误

TypeError: function takes 1 positional arguments but 2 were given

我尝试调试了大约一个小时,但尚未成功。我不确定出了什么问题。

esp8266 i2c micropython
1个回答
0
投票

根据 micropython 文档,中断处理程序需要 Pin 对象。这是我修改后的代码,效果很好。

import time
from machine import I2C, Pin, Timer
from i2c_lcd import I2cLcd

DEFAULT_I2C_ADDR = 0x27


class Button:
    def __init__(self, pin, callback):
        self.pin = Pin(pin, Pin.IN, Pin.PULL_UP)
        self.callback = callback
        self.debounce_timer = Timer(-1)
        self.state = False

    def init(self):
        self.pin.irq(trigger=Pin.IRQ_FALLING, handler=self.interrupt_handler)

    def interrupt_handler(self, pin):
        if not self.state:
            self.debounce_timer.init(period=50, mode=Timer.ONE_SHOT, callback=self.debounce_callback)       

    def debounce_callback(self, pin):
        if not self.state and self.pin.value() == 0:            
            self.callback()
            self.state = False        


class Counter:
    def __init__(self, lcd):
        self.lcd = lcd
        self.count = 0

    def increment(self):
        print("Incrementing counter")
        self.count += 1
        self.update_display()
        

    def decrement(self):
        print("Decrementing counter")
        self.count -= 1
        self.update_display()

    def update_display(self):
        self.lcd.move_to(11, 1)
        self.lcd.putstr("     ")
        self.lcd.move_to(11, 1)
        self.lcd.putstr(str(self.count))

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)

# Initialize counter
counter = Counter(lcd)

# Create a list of buttons
buttons = []


    
# Initialize buttons with their pins and callback functions (if any)
buttons.append(Button(13, counter.increment))  # Increment button
#buttons.append(Button(14, counter.decrement))  # Decrement button


# Start all buttons
for button in buttons:
    button.init()
    
lcd.clear()
lcd.move_to(11, 0)
lcd.putstr("COUNT")
© www.soinside.com 2019 - 2024. All rights reserved.