方案构成框图
本硬件平台可以在Windows下通过PUTTY或Thony来进行MicroPython的编程,在Mac下只能识别到板上的U盘,可以通过将py文件copy进去的方式让程序执行。 以下是部分功能代码的示例:
WHITE = const(0xFFFF) BLACK = const(0x0000) BLUE = const(0x001F) BRED = const(0XF81F) GRED = const(0XFFE0) RED = const(0xF800) MAGENTA=const(0xF81F) GREEN = const(0x07E0) CYAN = const(0x7FFF) YELLOW= const(0xFFE0) BROWN = const(0XBC40) BRRED = const(0XFC07) GRAY = const(0X8430) from display import LCD154 from lpc55 import Pin from lpc55 import SPI import lpc55 lcd_spi = lpc55.SPI(8, SPI.MASTER, bits=8, baudrate=25000000, polarity=1, phase=1) lcd_rst=lpc55.Pin('PIO1_3', Pin.OUT, pull=Pin.PULL_UP, value=1) lcd_cs =Pin('PIO0_20', Pin.OUT, pull=Pin.PULL_UP, value=0) lcd = LCD154(lcd_spi, lcd_rst) lcd.clear(BLUE) # 清除屏幕为棕色 fb0 = lcd.getframe() # 获取基础缓冲区 fb0.string(10,100,"Hello World!", color=WHITE, font=32)
WHITE = const(0xFFFF) BLACK = const(0x0000) BLUE = const(0x001F) BRED = const(0XF81F) GRED = const(0XFFE0) RED = const(0xF800) MAGENTA=const(0xF81F) GREEN = const(0x07E0) CYAN = const(0x7FFF) YELLOW= const(0xFFE0) BROWN = const(0XBC40) BRRED = const(0XFC07) GRAY = const(0X8430) BALL_SIZE = 32 # 圆球区域大小 SCREEN_WIDTH = 240 # 屏幕宽度 SCREEN_HEIGHT = 240 # 屏幕高度 X_BORDER = SCREEN_WIDTH - BALL_SIZE # 圆球活动边界 Y_BORDER = SCREEN_HEIGHT - BALL_SIZE # 圆球活动边界 import math import machine import lpc55 from lpc55 import SPI from lpc55 import Pin from display import LCD154 class path(): # Param: width & height of the moving area def __init__(self, width, height): def rand(): # mpy version self.x0 = lpc55.rng() % width # starting point self.y0 = lpc55.rng() % height # starting point self.a0 = 15 + lpc55.rng() % 60 # starting angle self.a0 = self.a0 + 90 * lpc55.rng() % 4 self.a0 = self.a0 * (2 * math.pi) / 360 # Conver to angle def rand3(): # Version for Python3 self.x0 = random.randrange(width) # starting point self.y0 = random.randrange(height) # starting point self.a0 = 30 + random.randrange(30) # starting angle self.a0 = self.a0 + 90 * random.randrange(4) self.a0 = self.a0 * (2 * math.pi) / 360 self.width = width self.height = height rand() self.quadrant(self.a0) self.prev = [self.x0, self.y0] def quadrant(self, angle): self.tan = math.tan(self.a0) self.step = 1 if angle < (math.pi / 2): self.delta = (1, 1) self.pathx = self.tan <= 1 # print("quadrant-1, pathx=%d" % self.pathx) elif angle < math.pi: self.delta = (-1, 1) self.pathx = -self.tan <= 1 # print("quadrant-2, pathx=%d" % self.pathx) elif angle < math.pi * 1.5: self.delta = (-1, -1) self.pathx = self.tan <= 1 # print("quadrant-3, pathx=%d" % self.pathx) else: self.delta = (1, -1) self.pathx = -self.tan <= 1 # print("quadrant-4, pathx=%d" % self.pathx) def next(self): if self.pathx: x1 = self.x0 + self.step * self.delta[0] y1 = self.y0 + self.step * self.delta[0] * self.tan else: x1 = self.x0 + self.step * self.delta[1] / self.tan y1 = self.y0 + self.step * self.delta[1] if y1 < 0 or y1 >= self.height: angle = math.pi * 2 - self.a0 elif x1 < 0 or x1 >= self.width: angle = math.pi * 3 - self.a0 if angle > math.pi*2: angle -= math.pi*2 else: self.prev[0] = int(x1) self.prev[1] = int(y1) self.step += 1 return self.prev self.a0 = angle self.x0 = self.prev[0] self.y0 = self.prev[1] self.quadrant(angle) return None #self.next() class Panel(): def __init__(self, id, rst, cs = None): lcd_spi = lpc55.SPI(id, SPI.MASTER, bits=8, baudrate=50000000, polarity=1, phase=1) lcd_rst=Pin(rst, Pin.OUT, pull=Pin.PULL_UP, value=1) if cs: lcd_cs =Pin(cs, Pin.OUT, pull=Pin.PULL_UP, value=0) self.lcd = LCD154(lcd_spi, lcd_rst) if not cs: self.lcd.orientation(2) self.fb0 = self.lcd.getframe() # Get the basic framebuf self.track = path(X_BORDER, Y_BORDER) def setup(self, color_table): self.desk = self.lcd.framebuf(0, 0, 240, 240, bits=2, lut=color_table) self.desk.clear(2) self.desk.color(0,1) for x in range(20,240,20): self.desk.line(x, 0, x, SCREEN_HEIGHT) for y in range(20,240,20): self.desk.line(0, y, SCREEN_WIDTH, y) self.desk.string(24, 100, "Hello World!", color=3, font=32, mode=1) self.desk.show() self.ball = self.lcd.framebuf(0, 100, 32, 32, bits=1, lut=(BLACK, RED)) self.ball.clear() self.ball.circle(15,15,14,color=1,style=1) def update(self): pos = self.track.next() if pos: self.ball.move(pos[0], pos[1]) self.ball.showwith(self.desk, mode=1) ############################ def timer_cb(x): lcd1.update() lcd2.update() lcd3.update() lcd1 = Panel(6, 'P6_6', 'P6_7') lcd1.setup((BLACK, MAGENTA, YELLOW, BLUE)) lcd2 = Panel(4, 'P5_6', 'P5_7') lcd2.setup((WHITE, BLUE, CYAN, BLACK)) lcd3 = Panel(8,'PIO1_3', 'PIO0_20') lcd3.setup( (WHITE, BLACK, GREEN, BLUE) ) tick = machine.Timer(-1) tick.init(freq=300, callback=timer_cb)
WHITE = const(0xFFFF) BLACK = const(0x0000) BLUE = const(0x001F) BRED = const(0XF81F) GRED = const(0XFFE0) RED = const(0xF800) MAGENTA=const(0xF81F) GREEN = const(0x07E0) CYAN = const(0x7FFF) YELLOW= const(0xFFE0) BROWN = const(0XBC40) BRRED = const(0XFC07) GRAY = const(0X8430) SCREEN_WIDTH = const(240) # 屏幕宽度 SCREEN_HEIGHT = const(240) # 屏幕高度 SCOPE_HEIGHT = const(200) # 波形区域高度 SCOPE_WIDTH = SCREEN_WIDTH-10 SCOPE_GRID = 30 WAVE_NUM = const(5) # 波形数目 WAVE_HEIGHT = SCOPE_HEIGHT//WAVE_NUM import math import lpc55 from lpc55 import SPI from lpc55 import Pin from display import LCD154 class Panel(): def __init__(self, id, rst, cs): lcd_spi = lpc55.SPI(id, SPI.MASTER, baudrate=50000000, polarity=1, phase=1) lcd_rst=Pin(rst, Pin.OUT, pull=Pin.PULL_UP, value=1) if cs: lcd_cs =Pin(cs, Pin.OUT, pull=Pin.PULL_UP, value=0) self.lcd = LCD154(lcd_spi, lcd_rst) if not cs: self.lcd.orientation(2) # This LCD is placed up side down self.fb0 = self.lcd.getframe() # Get the basic framebuf self.fb0.clear(BROWN) self.fb0.string(18,0,"Oscilloscope DEMO", color=BLACK, font=24,mode=1) # Top banner def setup(self, color_table): BANNER_HEIGHT = 24 Scope_x = (SCREEN_WIDTH-SCOPE_WIDTH)//2 self.ss = self.lcd.scope(Scope_x,BANNER_HEIGHT,SCOPE_WIDTH,SCOPE_HEIGHT, bits=3,lut=color_table) self.ss.clear(0) self.ss.color(5,2) y_low_banner = BANNER_HEIGHT + SCOPE_HEIGHT self.fb0.line(0, y_low_banner, SCREEN_WIDTH, y_low_banner, size=16, color=0xE79C) # 绘制下面的刻度条 for x in range(SCOPE_GRID, SCOPE_WIDTH, SCOPE_GRID): self.ss.line(x, 0, x, SCOPE_HEIGHT, color=6) marker_x = Scope_x + x - len(str(x)) * 4 # 显示刻度字体 self.fb0.string(marker_x, y_low_banner, str(x), font=16, color=0x1C, mode=1) for y in range(SCOPE_GRID, SCOPE_HEIGHT, SCOPE_GRID): self.ss.line(0, y, SCOPE_WIDTH, y, color=6) self.ss.string(48,10,"Hello World!", font=24) # 字体居上 self.ss.show() # Create channels WAVE_COLOR = (RED, GREEN, BLUE, YELLOW, MAGENTA) self.channel = [0,]*WAVE_NUM for ch in range(WAVE_NUM): self.channel[ch] = self.ss.channel(ch + 1) self.channel[ch].init(WAVE_HEIGHT//2 + ch * WAVE_HEIGHT, WAVE_COLOR[ch], offset=0, ratio=WAVE_HEIGHT/2-1) # lcd1 = Panel(4, 'P5_6', 'P5_7') lcd1 = Panel(8,'PIO1_3','PIO0_20') # lcd3 = Panel(6, 'P6_6', 'P6_7') lcd1.setup((BLACK, WHITE, RED, GREEN, BLUE, YELLOW, 0x300, MAGENTA)) # lcd2.setup((BLACK, WHITE, RED, GREEN, BLUE, YELLOW, 0x300, MAGENTA)) # lcd3.setup((BLACK, WHITE, RED, GREEN, BLUE, YELLOW, 0x300, MAGENTA)) Samples = 720 STEP=const(4) # 每次刷新,需要显示的数据数目 wave = [0,]*STEP while True: for x in range(0, Samples, STEP): # start = lpc55.millis() delta = math.pi/120 for i in range(x, x+STEP): wave[i-x] = math.sin(i*delta) for ch in range(WAVE_NUM): lcd1.channel[ch].wave(wave) # lcd2.channel[ch].wave(wave) delta += math.pi/90 for i in range(x, x+STEP): wave[i-x] = wave[i-x] + math.sin(i*delta) # print(lpc55.elapsed_millis(start), end='\t') lcd1.ss.refresh() # print(lpc55.elapsed_millis(start), end='\t') # lcd2.ss.refresh() # print(lpc55.elapsed_millis(start)) # lcd3.ss.refresh()
设计一个控制/菜单显示的界面,实现一款多功能口袋仪器的功能: