Skip to content

Instantly share code, notes, and snippets.

@kamilion
Created April 9, 2018 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamilion/ac494c2d2244b277d586a4fc760d2b5a to your computer and use it in GitHub Desktop.
Save kamilion/ac494c2d2244b277d586a4fc760d2b5a to your computer and use it in GitHub Desktop.
"""
Quick Status on TFT
Author: Kamilion (https://github.com/kamilion)
Date: 03/04/2018
"""
import machine, display, time, _thread, math, network
tft = display.TFT()
sta_if = network.WLAN(network.STA_IF)
config_text = "No Data"
if sta_if.isconnected():
ip = sta_if.ifconfig()
config_text = "IP: {}".format(ip[0])
# --- Select correct configuration ---
#tft.init(tft.ILI9341, width=240, height=320, miso=25, clk=26, mosi=27, dc=14, cs=13, rst_pin=12)
tft.init(tft.ILI9341, width=240, height=320, miso=34, clk=32, mosi=33, dc=25, cs=27, rst_pin=26, backl_pin=23, backl_on=1)
maxx = 320
maxy = 240
miny = 12
touch = False
# fonts used in this demo
fontnames = (
tft.FONT_Default,
tft.FONT_Ubuntu,
tft.FONT_Comic,
tft.FONT_Tooney,
tft.FONT_Minya
)
# Check if the display is touched
#-------------
def touched():
if not touch:
return False
else:
tch,_,_ = tft.gettouch()
if tch <= 0:
return False
else:
return True
# print display header
#----------------------
def header(tx, setclip):
# adjust screen dimensions (depends on used display and orientation)
global maxx, maxy, miny
maxx, maxy = tft.screensize()
tft.clear()
if maxx < 240:
tft.font(tft.FONT_Small, rotate=0)
else:
tft.font(tft.FONT_Default, rotate=0)
_,miny = tft.fontSize()
miny += 5
tft.rect(0, 0, maxx-1, miny-1, tft.OLIVE, tft.DARKGREY)
tft.text(tft.CENTER, 2, tx, tft.CYAN, transparent=True)
if setclip:
tft.setwin(0, miny, maxx, maxy)
# Display some config
#-------------------
def dispconfig():
tft.orient(tft.LANDSCAPE)
header("System Configuration", False)
if maxx < 240:
tx = "MicroPython"
else:
tx = config_text
starty = miny + 4
# Set up the font and position.
tft.font(tft.FONT_Tooney) # Set the font
_,fsz = tft.fontSize() # Get the font's size
y = starty # Set the Y position below the header
x = tft.CENTER # Set the X position to the center, or tft.RIGHT
# Print line one
tft.text(x, y, "IP: {}".format(ip[0]), 0x00DD00)
y = y + 1 + fsz; # Go down a line
tft.text(x, y, "GW: {}".format(ip[3]), 0x00DD00)
y = y + 1 + fsz; # Go down a line
tft.text(x, y, "BC: {}".format(ip[2]), 0x00DD00)
y = y + 1 + fsz; # Go down a line
tft.text(x, y, "NM: {}".format(ip[1]), 0x00DD00)
# Display some fonts
#-------------------
def dispFont(sec=5):
header("DISPLAY FONTS", False)
if maxx < 240:
tx = "MicroPython"
else:
tx = config_text
starty = miny + 4
n = time.time() + sec
while time.time() < n:
y = starty
x = 0
i = 0
while y < maxy:
if i == 0:
x = 0
elif i == 1:
x = tft.CENTER
elif i == 2:
x = tft.RIGHT
i = i + 1
if i > 2:
i = 0
for font in fontnames:
tft.font(font)
tft.text(x,y,tx, machine.random(0xFFFFFF))
_,fsz = tft.fontSize()
y = y + 2 + fsz
if y > (maxy-fsz):
y = maxy
if touched():
break
# Display random fonts
#------------------------------
def fontDemo(sec=5, rot=False):
tx = "FONTS"
if rot:
tx = "ROTATED " + tx
header(tx, True)
tx = config_text
n = time.time() + sec
while time.time() < n:
frot = 0
if rot:
frot = math.floor(machine.random(359)/5)*5
for font in fontnames:
if (not rot) or (font != tft.FONT_7seg):
x = machine.random(maxx-8)
tft.font(font, rotate=frot)
_,fsz = tft.fontSize()
y = machine.random(miny, maxy-fsz)
tft.text(x,y,tx, machine.random(0xFFFFFF))
if touched():
break
tft.resetwin()
# Display all demos
#--------------------------------------
def fullDemo(sec=5, rot=tft.LANDSCAPE):
tft.orient(rot)
dispFont(sec)
time.sleep(0.1)
fontDemo(sec, rot=False)
time.sleep(0.1)
fontDemo(sec, rot=True)
time.sleep(0.1)
# Run demo in thread
def dispDemo_th():
while True:
fullDemo(rot=tft.LANDSCAPE)
fullDemo(rot=tft.PORTRAIT)
fullDemo(rot=tft.LANDSCAPE_FLIP)
fullDemo(rot=tft.PORTRAIT_FLIP)
# dispth=_thread.start_new_thread("TFTDemo", dispDemo_th, ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment