[測試] Raspberry Pi 第二個彩色TFT LCD

本帖最後由 mkchai 於 2012-11-14 10:32 編輯

Raspberry Pi真系幾好玩,又細,做SERVER一流。但是小弟又想MON住佢,又唔想刻意買個HDMI MON,所以用了一個專系為佢而設的UART TFT LCD,解像度320x240,65k色,用來做系統資信顯示。LCD已經有FONTS在Flash裏面,Pi只要透過佢onboard的UARTsend 指令去LCD道,LCD就會顯示,基本上唔需要什麽programming,用很簡單的python script,通過Pyserial就可以控制LCD。

LCD裡面有FLASH,可以通過WINDWOS軟件去upload JPEG上去,直接通過Pi UART send指令就可以顯示icon/image. LCD有touchpad,可以作為一個簡單的input system.LCD會用過UART send已touched 的coordinates去Pi, Pi 就可以看座標去判斷LCD哪裡被按。

Pi去LCD基本上兩條線就OK(唔計電源),唔用touchpad,一條Pi的Tx就OK。亦都可以通過Wireless BLUETOOTH UART去將佢地相連。即使PI放一道,Wireless UART TFT LCD放另外一道,相隔不超過10米就一定收的到。

去片 http://www.youtube.com/watch?v=D4A7AymU7ys
當然,只是有UART的裝置都可以控制LCD(包括PC,arduino等等)。下一個目標:加入support 顯示中文字。 歡迎任何的意見。
以下是用來顯示Pi狀況的Python script,用了Pyserial 和Psutil


  1. import serial
  2. import random
  3. import time
  4. import struct
  5. import psutil
  6. from datetime import datetime, timedelta

  7. class BackgroundColours(object):
  8.     """Palette of 16-bit (highcolor) background colours"""
  9.     BLACK = 0x0000
  10.     BLUE = 0x001f
  11.     GREEN = 0x07e0
  12.     CYAN = 0x07ff
  13.     BROWN = 0x79e0
  14.     DARK_GREY = 0x7bef
  15.     LIGHT_GREY = 0xbdf7
  16.     RED = 0xf800
  17.     PURPLE = 0xf81f
  18.     ORANGE = 0xfbe0
  19.     GOLD = 0xfd20
  20.     YELLOW = 0xffe0
  21.     WHITE = 0xffff

  22. def _send_text(X0,Y0,X1,Y1,B_color,F_color,fontno,textcontent):
  23.        
  24.         ser.write(bytes(chr(36)))
  25.         ser.write(bytes(chr(33)))
  26.         ser.write(bytes(chr(B_color/256)))
  27.         ser.write(bytes(chr(B_color%256)))       
  28.         ser.write(bytes(chr(F_color/256)))
  29.         ser.write(bytes(chr(F_color%256)))       
  30.         ser.write(bytes(chr(X0/256)))
  31.         ser.write(bytes(chr(X0%256)))       
  32.         ser.write(bytes(chr(Y0/256)))
  33.         ser.write(bytes(chr(Y0%256)))       
  34.         ser.write(bytes(chr(X1/256)))
  35.         ser.write(bytes(chr(X1%256)))       
  36.         ser.write(bytes(chr(Y1/256)))
  37.         ser.write(bytes(chr(Y1%256)))       
  38.         ser.write(bytes(chr(0)))
  39.         ser.write(bytes(chr(fontno)))       
  40.         ser.write(str(textcontent))       
  41.         ser.write('\n')
  42.         ser.write(bytes(chr(0)))
  43.        
  44.         ser.write(bytes(chr(160)))
  45.         ser.write(bytes(chr(157)))
  46.        
  47. def _send_bar(X0,Y0,X1,Y1,F_color,percentage,gradvalue):
  48.        
  49.         ser.write(bytes(chr(36)))
  50.         ser.write(bytes(chr(22)))
  51.         ser.write(bytes(chr(0)))
  52.         ser.write(bytes(chr(133)))
  53.         ser.write(bytes(chr(F_color/256)))
  54.         ser.write(bytes(chr(F_color%256)))       
  55.         ser.write(bytes(chr(X0/256)))
  56.         ser.write(bytes(chr(X0%256)))       
  57.         ser.write(bytes(chr(Y0/256)))
  58.         ser.write(bytes(chr(Y0%256)))       
  59.         ser.write(bytes(chr(X1/256)))
  60.         ser.write(bytes(chr(X1%256)))       
  61.         ser.write(bytes(chr(Y1/256)))
  62.         ser.write(bytes(chr(Y1%256)))       
  63.         ser.write(bytes(chr(0)))
  64.         ser.write(bytes(chr(percentage)))       
  65.         ser.write(bytes(chr(0)))
  66.         ser.write(bytes(chr(gradvalue)))       
  67.        
  68.         ser.write(bytes(chr(160)))
  69.         ser.write(bytes(chr(157)))
  70.        
  71.        
  72. def _clr_screen(B_color):
  73.        
  74.         ser.write(bytes(chr(36)))
  75.         ser.write(bytes(chr(16)))
  76.         ser.write(bytes(chr(B_color/256)))
  77.         ser.write(bytes(chr(B_color%256)))       
  78.         ser.write(bytes(chr(160)))
  79.         ser.write(bytes(chr(157)))
  80.        
  81.        
  82. def _show_image(X0,Y0,Img_location):
  83.        
  84.         ser.write(bytes(chr(36)))
  85.         ser.write(bytes(chr(32)))
  86.         ser.write(bytes(chr(Img_location/256)))
  87.         ser.write(bytes(chr(Img_location%256)))       
  88.         ser.write(bytes(chr(X0/256)))
  89.         ser.write(bytes(chr(X0%256)))       
  90.         ser.write(bytes(chr(Y0/256)))
  91.         ser.write(bytes(chr(Y0%256)))
  92.         ser.write(bytes(chr(160)))
  93.         ser.write(bytes(chr(157)))

  94. def _startup_loadimg():
  95.         _clr_screen(0)
  96.         time.sleep(0.28)
  97.         _show_image(0,0,400)
  98.         time.sleep(0.1)
  99.         _show_image(0,54,409)
  100.         time.sleep(0.03)
  101.         _show_image(0,76,408)
  102.         time.sleep(0.03)
  103.         _show_image(0,210,410)
  104.         time.sleep(0.03)
  105.         _show_image(210,215,411)
  106.         time.sleep(0.03)
  107.        
  108. def _read_TP():
  109.         ser.flushInput()
  110.         ser.write(bytes(chr(36)))
  111.         ser.write(bytes(chr(54)))
  112.         ser.write(bytes(chr(160)))
  113.         ser.write(bytes(chr(157)))
  114.         time.sleep(0.05)
  115.         a = ser.read(size=6)
  116.         print "Bytes received"
  117.         print ser.inWaiting()
  118.         #print struct.unpack('>B',a[1]) + struct.unpack('>B',a[2])+ struct.unpack('>B',a[3])
  119.         try:
  120.                 return  struct.unpack('>B',a[1]) + struct.unpack('>B',a[2])+ struct.unpack('>B',a[3])
  121.         except:
  122.                 print "No data Received"

  123. #main
  124.         
  125. ser = serial.Serial(port='/dev/ttyAMA0')
  126. ser.baudrate=115200
  127. ser.timeout=0.3
  128. _startup_loadimg()

  129. while 1:
  130.        
  131.         (bb)=_read_TP()
  132.         _send_text(260,203,319,215, 0,BackgroundColours.WHITE,1,bb)
  133.        
  134.         network1 = psutil.network_io_counters()
  135.         time.sleep(0.5)
  136.         network2 = psutil.network_io_counters()
  137.         ran = random.randint(0,256)
  138.         CPU = psutil.cpu_percent(interval=0)
  139.         mem = psutil.virtual_memory()
  140.         mem1 = mem.percent
  141.         mem2 = mem.total - mem.available
  142.         sent = network2.bytes_sent - network1.bytes_sent
  143.         recv = network2.bytes_recv - network1.bytes_recv
  144.        
  145.         sent_total = network2.bytes_sent
  146.         recv_total = network2.bytes_recv
  147.         uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)
  148.         timenow = datetime.now()
  149.         timenow = timenow.strftime("%y/%m/%d %H:%M:%S")
  150.         log = psutil.phymem_usage()
  151.         disk2=psutil.disk_io_counters(perdisk=False);
  152.         diskreadcount = disk2.read_count
  153.         diskwritecount = disk2.write_count
  154.         diskusage= psutil.disk_usage('/')
  155.         disk_percent = diskusage.percent
  156.         disk_total = diskusage.total
  157.         disk_used = diskusage.used
  158.         disk_free = diskusage.free
  159.        
  160.         (bb)=_read_TP()
  161.         _send_text(260,203,319,215, 0,BackgroundColours.WHITE,1,bb)
  162.        
  163.         time.sleep(0.08)
  164.         _send_text(201,54,319,70, 0,BackgroundColours.WHITE,4,"CPU "+str(CPU)+"%")
  165.         time.sleep(0.08)
  166.         _send_bar(35,58,190,70,BackgroundColours.WHITE,int(CPU),1)
  167.         time.sleep(0.08)
  168.        
  169.         _send_text(201,78,319,90, 0,BackgroundColours.WHITE,4,"Mem "+str(mem1)+"%")
  170.         time.sleep(0.08)
  171.         _send_bar(35,82,190,94,0xf8e0,int(mem1),1)
  172.         time.sleep(0.08)
  173.        
  174.         time.sleep(0.08)       
  175.         _send_text(65,102,319,110, 0,BackgroundColours.CYAN ,2,"Memory Usage: "+str(int(mem2/1024/1024))+"Mb/"+str(int(mem.total/1024/1024))+"Mb" )       
  176.         time.sleep(0.08)
  177.         _send_text(14,120,319,135, 0,0xEbe0,3,"Network Sent speed: "+str(int(sent/512))+"kb/s  Total:"+str(int(sent_total/1024/1024))+"Mb" )       
  178.         time.sleep(0.08)
  179.         _send_text(10,140,319,155, 0,0xEbe0,3,"Network Receive speed: "+str(int(recv/512))+"kb/s  Total:"+str(int(recv_total/1024/1024))+"Mb" )       
  180.         time.sleep(0.08)
  181.         _send_text(40,165,319,175, 0,0xBCe0,3,"Disk, Read: "+str(int(diskreadcount))+"      Write:" + str(int(diskwritecount)) )       
  182.         time.sleep(0.08)       

  183.        
  184.         _send_text(0,185,319,200, 0,0xBCe0,3,"Disk: "+str(int(disk_percent))+"% used, Free: " + str(int(disk_free/1024/1024))+" Mb, Total: " + str(int(disk_total/1024/1024))+" Mb")       
  185.         time.sleep(0.08)       
  186.                
  187.         _send_text(30,210,210,239, 0,0x07e0,5,""+str(timenow))
  188.         time.sleep(0.08)
  189.         _send_text(235,215,319,239, 0,0xE0e0,4, str(uptime).split('.')[0])
  190.         time.sleep(0.08)
  191.        
  192.        
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

one SIMPLE question. 個lcd幾銀? 有無taobao link睇睇?! 謝謝....

TOP

做返個case放塊LCD上去RPi上面,都幾型,不過要諗諗點散熱

TOP

回復 2# augustoyeung

taobao 無的,
個LCD成本唔會太貴,一百幾十倒

TOP

回復 3# lazyfai


系啊,入CASE會熱,要加風扇。。

TOP

Nice to see your new work MKChai, any chance our older version USB LCD from you would be able to "upgrade" by firmware to use with RPi? XD

TOP

係邊到買ka 我有興趣買返個URTA TFT LCD
來玩下

TOP

回復 6# domeso


    haha , thanks,
cannot la, different PCB..and this LCD have touchscreen,...

TOP

回復 7# shal

可以啊。PM你

TOP

回復  shal

可以啊。PM你
mkchai 發表於 2012-11-15 08:53


Plesae also PM to me.

Thanks

TOP