class Display: def __init__(self): self.Led = Pixel() self.tem = [[0, 0, 0]] * 25 # 显示缓存区m= def Disrupt_Col(color): a = color[-1] while True: if color_num != 1: import random random.shuffle(color) if a != color[0]: break def scroll(self, val, color=Red, delay=150): pixel_col = [[0, 0, 0]] * 25 # 显示缓存区m= val = str(val) + ' ' color_num = 1 if color != Red: if isinstance(color[0], list): color_num = len(color) # self.Disrupt_Col(color) #打乱颜色顺序 col_cnt = 0 it = iter(color) for val1 in val: val2 = CharData[val1] if color_num == 1: now_col = color else: if col_cnt < color_num: now_col = next(it) # 确定当前字符的颜色 else: col_cnt = 0 it = iter(color) now_col = next(it) col_cnt += 1 for i in range(25): # 为字符的像素点添加颜色 if val2[i] == 1: pixel_col[i] = now_col else: pixel_col[i] = [0, 0, 0] for i in range(6): # 开始滚动显示 for t in range(4): self.tem[20 - (t * 5):20 - (t * 5) + 5] = self.tem[20 - ((t + 1) * 5):20 - ((t + 1) * 5) + 5] # 数据向前移动5位 if i == 5: self.tem[0:5] = Zero[0:5] # 每个字符之间间隔一行 else: self.tem[0:5] = pixel_col[20 - (i * 5):20 - (i * 5) + 5] for r in range(25): self.Led.LoadPos(r, self.tem[r]) # 亮度为0 self.Led.Show() sleep_ms(delay) def clear(self): self.Led.fill((0, 0, 0)) self.Led.Show() def __show(self, it, color): it = iter(it) for r in range(25): col = next(it) self.Led.LoadPos(r, color if col else black) self.Led.Show() def show(self, images, color=Red, *, loop=False, delay=500): if isinstance(images, str): images = CharData[images] if isinstance(images, list) and (isinstance(images[0], Image) or isinstance(images[0], list)): for i in images: self.__show(i, color) sleep_ms(delay) try: while loop: for i in images: self.__show(i, color) sleep_ms(delay) except Exception as e: self.Led.fill((0, 0, 0)) self.Led.Show() else: it = iter(images) self.__show(it, color) try: while loop: self.__show(it, color) except Exception as e: self.Led.fill((0, 0, 0)) self.Led.Show()
class Display: lock = _thread.allocate_lock() def __init__(self): self.alive = False self.Led = Pixel() self.tem = [[0, 0, 0]] * 25 # 显示缓存区m= def stop(self): self.alive = False if Display.lock.acquire(): # print("stop") Display.lock.release() def scroll(self, val, color=Red, delay=150): self.stop() self.alive = True if Display.lock.acquire(): # print("start") _thread.start_new_thread(self._scroll, ([val, color, delay])) Display.lock.release() def Disrupt_Col(color): a = color[-1] while True: if color_num != 1: import random random.shuffle(color) if a != color[0]: break def _scroll(self, val, color=Red, delay=150): pixel_col = [[0, 0, 0]] * 25 # 显示缓存区m= val = str(val) + ' ' color_num = 1 if color != Red: if isinstance(color[0], list): color_num = len(color) # self.Disrupt_Col(color) #打乱颜色顺序 col_cnt = 0 it = iter(color) for val1 in val: val2 = CharData[val1] if color_num == 1: now_col = color else: if col_cnt < color_num: now_col = next(it) # 确定当前字符的颜色 else: col_cnt = 0 it = iter(color) now_col = next(it) col_cnt += 1 for i in range(25): # 为字符的像素点添加颜色 if val2[i] == 1: pixel_col[i] = now_col else: pixel_col[i] = [0, 0, 0] if Display.lock.acquire(): for i in range(6): # 开始滚动显示 if self.alive == False: Display.lock.release() self.clear() _thread.exit() else: for t in range(4): self.tem[20 - (t * 5):20 - (t * 5) + 5] = self.tem[20 - ((t + 1) * 5):20 - ((t + 1) * 5) + 5] # 数据向前移动5位 if i == 5: self.tem[0:5] = Zero[0:5] # 每个字符之间间隔一行 else: self.tem[0:5] = pixel_col[20 - (i * 5):20 - (i * 5) + 5] for r in range(25): self.Led.LoadPos(r, self.tem[r]) # 亮度为0 self.Led.Show() sleep_ms(delay) Display.lock.release() _thread.exit() def clear(self): self.stop() self.Led.fill((0, 0, 0)) self.Led.Show() def __show(self, it, color): it = iter(it) for r in range(25): col = next(it) self.Led.LoadPos(r, color if col else black) self.Led.Show() def show(self, images, wait=True, color=Red, *, loop=False, delay=500, clear=False): if isinstance(images, str): images = CharData[images] if isinstance(images, list) and (isinstance(images[0], Image) or isinstance(images[0], list)): for i in images: self.__show(i, color) sleep_ms(delay) try: while loop: for i in images: self.__show(i, color) sleep_ms(delay) except Exception as e: self.Led.fill((0, 0, 0)) self.Led.Show() else: it = iter(images) self.__show(it, color) try: while loop: self.__show(it, color) except Exception as e: self.Led.fill((0, 0, 0)) self.Led.Show() def get_pixel(self, x=0, y=0): print("get_pixel will be supported in the future.") def set_pixel(self, x=0, y=0, value=9): print("set_pixel will be supported in the future.") def on(self): self.clear() def off(self): self.clear() def is_on(self): return self.Led != None
def loop(): digital_outputs = [False, False, False] while True: data = uart.readline() sleep(8) if data: cmd = str(data, 'utf-8').rstrip() if not len(cmd): continue # noinspection PyUnresolvedReferences cmd_list = cmd.split(",") # get command id try: cmd_id = cmd_list[0] except IndexError: cmd_id = 'z' continue # display image command if cmd_id == 'd': image_dict = {"HAPPY": Image.HAPPY, "SAD": Image.SAD, "ANGRY": Image.ANGRY, "SMILE": Image.SMILE, "CONFUSED": Image.CONFUSED, "ASLEEP": Image.ASLEEP, "SURPRISED": Image.SURPRISED, "SILLY": Image.SILLY, "FABULOUS": Image.FABULOUS, "MEH": Image.MEH, "YES": Image.YES, "NO": Image.NO, "RABBIT": Image.RABBIT, "COW": Image.COW, "ROLLERSKATE": Image.ROLLERSKATE, "HOUSE": Image.HOUSE, "SNAKE": Image.SNAKE, "HEART": Image.HEART, "DIAMOND": Image.DIAMOND, "DIAMOND_SMALL": Image.DIAMOND_SMALL, "SQUARE": Image.SQUARE, "SQUARE_SMALL": Image.SQUARE_SMALL, "TRIANGLE": Image.TRIANGLE, "TARGET": Image.TARGET, "STICKFIGURE": Image.STICKFIGURE, "ARROW_N": Image.ARROW_N, "ARROW_NE": Image.ARROW_NE, "ARROW_E": Image.ARROW_E, "ARROW_SE": Image.ARROW_SE, "ARROW_S": Image.ARROW_S, "ARROW_SW": Image.ARROW_SW, "ARROW_W": Image.ARROW_W, "ARROW_NW": Image.ARROW_NW} # get image key try: image_key = cmd_list[1] except IndexError: continue try: r_value = int(cmd_list[2]) except ValueError: continue except IndexError: continue if r_value < 0: r_value = 0 if r_value > 255: r_value = 255 try: g_value = int(cmd_list[3]) except ValueError: continue except IndexError: continue if g_value < 0: g_value = 0 if g_value > 255: g_value = 255 try: b_value = int(cmd_list[4]) except ValueError: continue except IndexError: continue if b_value < 0: b_value = 0 if b_value > 255: b_value = 255 #if image_key in image_dict: display.show(image_dict.get(image_key),[r_value,g_value,b_value]) # scroll text command elif cmd_id == 's': try: r_value = int(cmd_list[2]) except ValueError: continue except IndexError: continue if r_value < 0: r_value = 0 if r_value > 255: r_value = 255 try: g_value = int(cmd_list[3]) except ValueError: continue except IndexError: continue if g_value < 0: g_value = 0 if g_value > 255: g_value = 255 try: b_value = int(cmd_list[4]) except ValueError: continue except IndexError: continue if b_value < 0: b_value = 0 if b_value > 255: b_value = 255 display.scroll(str(cmd_list[1]),[r_value,g_value,b_value]) # write pixel command elif cmd_id == 'p': # get row, column and intensity value # make sure values are within valid range # print(cmd) try: x = int(cmd_list[1]) except ValueError: continue except IndexError: continue if x < 0: x = 0 if x > 4: x = 4 try: y = int(cmd_list[2]) except ValueError: continue except IndexError: continue if y < 0: y = 0 if y > 4: y = 4 try: r_value = int(cmd_list[3]) except ValueError: continue except IndexError: continue if r_value < 0: r_value = 0 if r_value > 255: r_value = 255 try: g_value = int(cmd_list[4]) except ValueError: continue except IndexError: continue if g_value < 0: g_value = 0 if g_value > 255: g_value = 255 try: b_value = int(cmd_list[5]) except ValueError: continue except IndexError: continue if b_value < 0: b_value = 0 if b_value > 255: b_value = 255 #display.set_pixel(x, y, value) from pixel import Pixel View = Pixel() View.LoadXY(x, y, [r_value,g_value,b_value]) View.Show() # clear display command elif cmd_id == 'c': display.clear() # analog write command # if values are out of range, command is ignored elif cmd_id == 'a': # check pin and value ranges try: pin = int(cmd_list[1]) value = int(cmd_list[2]) digital_outputs[pin] = True except IndexError: continue except ValueError: continue if 0 <= pin <= 2: if not 0 <= value <= 1023: value = 256 if pin == 0: pin0.write_analog(value) elif pin == 1: pin1.write_analog(value) elif pin == 2: pin2.write_analog(value) # digital write command elif cmd_id == 't': # check pin and value ranges # if values are out of range, command is ignored try: pin = int(cmd_list[1]) value = int(cmd_list[2]) digital_outputs[pin] = True except IndexError: continue except ValueError: continue if 0 <= pin <= 2: if 0 <= value <= 1: if pin == 0: pin0.write_digital(value) elif pin == 1: pin1.write_digital(value) elif pin == 2: pin2.write_digital(value) else: pass elif cmd == 'g': # This string will contain the sensor values and will # be "printed" to the serial port. # Fields are comma delimited sensor_string = "" # accelerometer sensor_string += str(int(accelerometer.get_x()*20)) + ',' sensor_string += str(int(accelerometer.get_y()*20)) + ',' sensor_string += str(int(accelerometer.get_z()*20)) + ',' # buttons sensor_string += str(button_a.is_pressed()) + ',' sensor_string += str(button_b.is_pressed()) + ',' # get digital input pin values if not digital_outputs[0]: sensor_string += str(pin0.read_digital()) + ',' else: sensor_string += '0' + ',' # if not digital_outputs[1]: sensor_string += str(pin1.read_digital()) + ',' else: sensor_string += '0' + ',' # if not digital_outputs[2]: sensor_string += str(pin2.read_digital()) + ',' else: sensor_string += '0' + ',' # get analog input pin values if not digital_outputs[0]: sensor_string += '0' + ',' else: sensor_string += '0' + ',' if not digital_outputs[1]: sensor_string += str(pin1.read_analog()) + ',' else: sensor_string += '0' + ',' if not digital_outputs[2]: sensor_string += str(pin2.read_analog())+',' else: sensor_string += '0' + ',' ###temperature sensor_string += str(temperature()) +',' #L R light import light R = light.Intensity(39) L = light.Intensity(36) sensor_string += str(int(L.read()))+',' sensor_string += str(int(R.read())) print(sensor_string) sleep(10) elif cmd == 'v': print('s2mb.py Version 1.10 14 April 2018') else: continue sleep(8)