您现在的位置是: 网站首页 > 程序设计  > 树莓派 

点亮LED

2020年1月22日 08:00 1007人围观

简介树莓派点亮LED

    封装LED类,对外提供on和off两个接口

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    #!/usr/bin/python3 
    #encoding=utf-8
    
    from GPIO import * 
    import time
    
    class Led(object): 
        def __init__(self, index): 
            self.__led = GPIO(index) 
            self.__status = False
    
        def on (self): 
            self.__led.high() 
            self.__status = True
    
        def off(self): 
            self.__led.low() 
            self.__status = False
    
        def get_status(self): 
            return self.__status;
    
    def main(): 
        GPIO.init(GPIO.BCM)
    
        io = [12, 16, 20, 21] 
        leds = list()
    
        for i in io: 
            leds.append(Led(i))
    
        index = 0 
        while index < 20000: 
            for led in leds: 
                led.on() 
                time.sleep(1) 
                led.off()
    
            index += 1
    
        GPIO.cleanup()
    
    if '__main__' == __name__: 
    main()
    

    上一篇: GPIO-封装

    下一篇: GPIO-引脚定义