From 20dcf41bf417edd1e2063942a2143b7d74aa48bb Mon Sep 17 00:00:00 2001 From: gaoyang3513 Date: Sun, 2 Jul 2023 00:07:40 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BF=AE=E6=94=B9]=20=E5=A2=9E=E5=8A=A0LEd?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pioneer600/Led/LED.py | 20 -------------------- Pioneer600/Oled/SSD1306.py | 14 +++++--------- main.py | 20 +++++++++++++++++++- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/Pioneer600/Led/LED.py b/Pioneer600/Led/LED.py index 71314e3..f4de4e5 100644 --- a/Pioneer600/Led/LED.py +++ b/Pioneer600/Led/LED.py @@ -13,23 +13,3 @@ class LED(object): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(LED,GPIO.OUT) - - try: - while True: - GPIO.output(LED,GPIO.HIGH) - time.sleep(1) - GPIO.output(LED,GPIO.LOW) - time.sleep(1) - except: - print("except") - GPIO.output(LED,GPIO.HIGH) - GPIO.cleanup() - -LED_GPIO_RED = 26 - -def main(): - # 128x64 display with hardware SPI: - led = LED.LED(LED_GPIO_RED) - -if __name__=='__main__': - main() diff --git a/Pioneer600/Oled/SSD1306.py b/Pioneer600/Oled/SSD1306.py index c11ad61..9b0a306 100644 --- a/Pioneer600/Oled/SSD1306.py +++ b/Pioneer600/Oled/SSD1306.py @@ -1,4 +1,4 @@ -import spidev +import spidev import RPi.GPIO as GPIO import time @@ -40,7 +40,7 @@ SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A class SSD1306(object): """class for SSD1306 128*64 0.96inch OLED displays.""" - + def __init__(self,rst,dc,spi): self.width = 128 self.height = 64 @@ -70,7 +70,7 @@ class SSD1306(object): self.command(SSD1306_DISPLAYOFF) # 0xAE self.command(SSD1306_SETDISPLAYCLOCKDIV) # 0xD5 self.command(0x80) # the suggested ra tio 0x80 - + self.command(SSD1306_SETMULTIPLEX) # 0xA8 self.command(0x3F) self.command(SSD1306_SETDISPLAYOFFSET) # 0xD3 @@ -82,7 +82,7 @@ class SSD1306(object): else: self.command(0x14) self.command(SSD1306_MEMORYMODE) # 0x20 - self.command(0x00) # 0x0 act like ks0108 + self.command(0x00) # 0x0 act like ks0108 self.command(SSD1306_SEGREMAP | 0x1) self.command(SSD1306_COMSCANDEC) self.command(SSD1306_SETCOMPINS) # 0xDA @@ -156,7 +156,7 @@ class SSD1306(object): self.command(contrast) def dim(self, dim): - """Adjusts contrast to dim the display if dim is True, + """Adjusts contrast to dim the display if dim is True, otherwise sets the contrast to normal brightness if dim is False.""" # Assume dim display. contrast = 0 @@ -166,7 +166,3 @@ class SSD1306(object): contrast = 0x9F else: contrast = 0xCF - - - - diff --git a/main.py b/main.py index 3d5ecb4..5f96c9f 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ import spidev as SPI import Pioneer600.Led.LED as LED import Pioneer600.Oled.SSD1306 as SSD1306 from PIL import Image,ImageDraw,ImageFont +import threading # Raspberry Pi pin configuration: LED_GPIO_RED = 26 @@ -18,16 +19,33 @@ OLED_GPIO_DC = 16 OLED_SPI_BUS = 0 OLED_SPI_CS = 0 +# 新线程执行的代码: +def blink_loop(*args, **kwargs): + try: + while True: + GPIO.output(args, GPIO.HIGH) + time.sleep(1) + GPIO.output(args, GPIO.LOW) + time.sleep(1) + except: + print("except") + GPIO.output(args, GPIO.HIGH) + GPIO.cleanup() + def main(): # 128x64 display with hardware SPI: led = LED.LED(LED_GPIO_RED) try: + led_blink = threading.Thread(target=blink_loop, name='led_blink', args=(LED_GPIO_RED,)) + led_blink.start() + while True: time.sleep(1) except: - print("except") + print("Except") + led_blink.join() if __name__=='__main__': main()