from ST7735 import TFT from sysfont import sysfont from machine import SPI,Pin import time import math #hardware SPI, HSPI spi = SPI(1, baudrate=8000000, polarity=0, phase=0) # dc, rst, cs tft=TFT(spi,16) tft.init_7735(tft.GREENTAB80x160) def testlines(color): tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((0,0),(x, tft.size()[1] - 1), color) for y in range(0, tft.size()[1], 6): tft.line((0,0),(tft.size()[0] - 1, y), color) tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((tft.size()[0] - 1, 0), (x, tft.size()[1] - 1), color) for y in range(0, tft.size()[1], 6): tft.line((tft.size()[0] - 1, 0), (0, y), color) tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((0, tft.size()[1] - 1), (x, 0), color) for y in range(0, tft.size()[1], 6): tft.line((0, tft.size()[1] - 1), (tft.size()[0] - 1,y), color) tft.fill(TFT.BLACK)
from ST7735 import TFT from sysfont import sysfont from machine import SPI, Pin import time import math #hardware SPI, HSPI spi = SPI(1, baudrate=8000000, polarity=0, phase=0) # dc, rst, cs # tft=TFT(spi,2,16,0) tft = TFT(spi, 2, 16) tft.init_7735(tft.REDTAB) ''' def testlines(color): tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((0,0),(x, tft.size()[1] - 1), color) for y in range(0, tft.size()[1], 6): tft.line((0,0),(tft.size()[0] - 1, y), color) tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((tft.size()[0] - 1, 0), (x, tft.size()[1] - 1), color) for y in range(0, tft.size()[1], 6): tft.line((tft.size()[0] - 1, 0), (0, y), color) tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((0, tft.size()[1] - 1), (x, 0), color) for y in range(0, tft.size()[1], 6): tft.line((0, tft.size()[1] - 1), (tft.size()[0] - 1,y), color)
class main(): x_min = 15 x_max = 120 y_top = 50 y_bottom = 120 temp = [] pres = [] water = [] humi = [] date = [] ntp_url = 'ntp4.aliyun.com' dat_url = 'put your own address here' last_time_update = None def __init__(self, wlan, SSID, PASSWD): #init wlan self.wlan = wlan self.SSID = SSID self.PASSWD = PASSWD #init time print('ntp_url:', self.ntp_url) ntptime.host = self.ntp_url print('ntp_host:', ntptime.host) ntptime.time() ntptime.settime() self.last_time_update = time.time() print('init_time:', time.localtime(self.last_time_update + 3600 * 8)) #init data print('dat_url:', self.dat_url) #init display mod ''' mode: 0:cycle 1:temp only 2:pres only 3:water only 4:humi only ''' self.mode = 0 ''' last: 0:temp 1:pres 2:water 3:humi ''' self.last = 0 #init tft print('init TFT...') spi = SPI(1, baudrate=32000000, polarity=0, phase=0) self.tft = TFT(spi, 2, 16, 0) self.tft.init_7735(self.tft.GREENTAB128x128) self.tft.fill(TFT.WHITE) self.tft.text((5, 10), "initlizing", TFT.YELLOW, 2, nowrap=True) self.tft.text((5, 50), "please wait", TFT.YELLOW, 2, nowrap=True) self.run() def run(self): while True: self.display_time() self.get_dat() self.display_data() time.sleep(5) def get_time(self): time_now = time.time() if time_now - self.last_time_update >= 3600: ntptime.time() ntptime.settime() time_now = time.time() self.last_time_update = time_now return time.localtime(time_now + 3600 * 8) def display_time(self): time_now = self.get_time() print("p time", time_now) self.tft.fill(TFT.BLACK) self.tft.text( (5, 5), str(time_now[0]) + ' ' + str(time_now[1]) + ' ' + str(time_now[2]), TFT.GRAY, 1.8, nowrap=True) self.tft.text( (35, 20), str(time_now[3]) + ':' + str(time_now[4]) + ':' + str(time_now[5]), TFT.GRAY, 1.8, nowrap=True) def get_dat(self): req = requests.get(self.dat_url) data = req.json() if len(self.date) > 0 and self.date[len(self.date) - 1] == data['date']: return if len(self.temp) >= 10: self.temp.pop(0) self.water.pop(0) self.humi.pop(0) self.pres.pop(0) self.date.pop(0) self.temp.append(data['tmp2']) self.water.append(data['tmp0']) self.humi.append(data['humi']) self.pres.append(data['pres']) self.date.append(data['date']) return def display_data(self): def draw_axis(self): self.tft.line((self.x_min, self.y_top), (self.x_min, self.y_bottom), TFT.WHITE) self.tft.line((self.x_min, self.y_bottom), (self.x_max, self.y_bottom), TFT.WHITE) self.tft.line((self.x_min, self.y_top), (self.x_min - 5, self.y_top + 5), TFT.WHITE) self.tft.line((self.x_min, self.y_top), (self.x_min + 5, self.y_top + 5), TFT.WHITE) self.tft.line((self.x_max, self.y_bottom), (self.x_max - 5, self.y_bottom + 5), TFT.WHITE) self.tft.line((self.x_max, self.y_bottom), (self.x_max - 5, self.y_bottom - 5), TFT.WHITE) self.tft.text((self.x_min, self.y_top - 10), "y", TFT.WHITE, 1, nowrap=True) self.tft.text((self.x_max, self.y_bottom - 10), "x", TFT.WHITE, 1, nowrap=True) for i in range(self.x_min, self.x_max, floor((self.x_max - self.x_min) / 10)): self.tft.line((i, self.y_bottom - 2), (i, self.y_bottom + 3), TFT.WHITE) def draw_points(self, data): self.y_top = self.y_top + 10 x_dis = floor((self.x_max - self.x_min) / 10) x_now = self.x_min dat_min = 100000 dat_max = -100000 for i in data: if i < dat_min: dat_min = i if i > dat_max: dat_max = i if dat_min > dat_max: return points = [] which = 0 if dat_min == dat_max: for i in data: points.append((floor(x_now), floor(self.y_bottom + (self.y_top - self.y_bottom) / 2))) self.tft.circle(points[which], 2, TFT.YELLOW) self.tft.text((points[which][0] - 5, points[which][1] + 5), str(i), TFT.CYAN, 0.5, nowrap=True) self.tft.text( (points[which][0] - 5, points[which][1] - 10), self.date[which][11:16], TFT.CYAN, 0.6, nowrap=True) if which > 0: self.tft.line(points[which - 1], points[which], TFT.YELLOW) which = which + 1 x_now = x_now + x_dis else: for i in data: points.append( (floor(x_now), floor(self.y_bottom + (self.y_top - self.y_bottom) * ((i - dat_min) / (dat_max - dat_min))))) self.tft.circle(points[which], 2, TFT.YELLOW) self.tft.text((points[which][0] - 5, points[which][1] + 5), str(i), TFT.CYAN, 0.5, nowrap=True) self.tft.text( (points[which][0] - 5, points[which][1] - 10), self.date[which][11:16], TFT.CYAN, 0.6, nowrap=True) if which > 0: self.tft.line(points[which - 1], points[which], TFT.YELLOW) which = which + 1 x_now = x_now + x_dis self.y_top = self.y_top - 10 draw_axis(self) if self.mode == 0: if self.last == 0: self.last = 1 self.tft.text((50, 45), "pres", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.pres) elif self.last == 1: self.last = 2 self.tft.text((50, 45), "water", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.water) elif self.last == 2: self.last = 3 self.tft.text((50, 45), "humi", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.humi) elif self.last == 3: self.last = 0 self.tft.text((50, 45), "temp", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.temp) elif self.mode == 1: self.last = 0 self.tft.text((50, 45), "temp", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.temp) elif self.mode == 2: self.last = 1 self.tft.text((50, 45), "pres", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.pres) elif self.mode == 3: self.last = 2 self.tft.text((50, 45), "water", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.water) elif self.mode == 4: self.last = 3 self.tft.text((50, 45), "humi", TFT.YELLOW, 1, nowrap=True) draw_points(self, self.humi)
from ST7735 import TFT from sysfont import sysfont from machine import SPI, Pin import time import math #hardware SPI, HSPI spi = SPI(1, baudrate=8000000, polarity=0, phase=0) tft = TFT(spi, 16) tft.init_7735(tft.GREENTAB128x128) def testlines(color): tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((0, 0), (x, tft.size()[1] - 1), color) for y in range(0, tft.size()[1], 6): tft.line((0, 0), (tft.size()[0] - 1, y), color) tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((tft.size()[0] - 1, 0), (x, tft.size()[1] - 1), color) for y in range(0, tft.size()[1], 6): tft.line((tft.size()[0] - 1, 0), (0, y), color) tft.fill(TFT.BLACK) for x in range(0, tft.size()[0], 6): tft.line((0, tft.size()[1] - 1), (x, 0), color) for y in range(0, tft.size()[1], 6): tft.line((0, tft.size()[1] - 1), (tft.size()[0] - 1, y), color)
def LCD_LEDCtrl(timer): global Lcd_LEDCount, LCD_LEDSet Lcd_LEDCount = Lcd_LEDCount + 1 if Lcd_LEDCount == 10: Lcd_LEDCount = 0 if LCD_LEDSet > Lcd_LEDCount: lcd_led.value(0) else: lcd_led.value(1) lcd_led_tim = pyb.Timer(2) lcd_led_tim.init(freq=4000) # Freq: 4KHz lcd_led_tim.callback(LCD_LEDCtrl) tft = TFT(spi2, dc, rst, cs) tft.init_7735(TFT.REDTAB) tft.rotation(2) tft.rgb(True) def display_weact_logo(): tft.fill(TFT.BLACK) f = open('WeAct_logo_128_160.bmp', 'rb') if f.read(2) == b'BM': # header dummy = f.read(8) # file size(4), creator bytes(4) offset = int.from_bytes(f.read(4), 'little') hdrsize = int.from_bytes(f.read(4), 'little') width = int.from_bytes(f.read(4), 'little') height = int.from_bytes(f.read(4), 'little') buf = bytearray(width * 2) # init buf if int.from_bytes(f.read(2), 'little') == 1: # planes must be 1
for row in range(h): if flip: pos = offset + (height - 1 - row) * rowsize else: pos = offset + row * rowsize if f.tell() != pos: dummy = f.seek(pos) for col in range(w): bgr = f.read(3) _tft._pushcolor(TFTColor(bgr[2], bgr[1], bgr[0])) else: print(FileName + 'is not 24bit pic') tft = TFT(spi, dc, rst, cs) tft.init_7735(TFT.GREENTAB80x160) tft.rotation(3) LCD_ShowBmp(tft, 'WeActStudiologo.bmp') time.sleep(50) LCD_LEDSet = 1 time.sleep(1000) tft.fill(TFT.BLACK) tft.text((0, 30), 'Hello WeAct!', TFT.WHITE, sysfont, 2, nowrap=False) sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat( sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) if sensor.get_id() == sensor.OV7725: sensor.set_hmirror(True)