From e37013c2199c001ca73b133fb8375bac1c54afa0 Mon Sep 17 00:00:00 2001 From: Julien Cabillot Date: Wed, 13 May 2020 19:09:52 -0400 Subject: [PATCH] sync --- README.md | 3 ++ hello/README.md | 3 ++ hello/main.py | 40 +++++++++++++++++++++ hello2/README.md | 4 +++ {color => hello2}/main.py | 31 ++++++++++------ rainbow_buttons/README.md | 1 + rainbow_buttons/main.py | 71 ++++++++++++++++++++++++++++++++++++ scroll_font/README.md | 3 ++ scroll_font/main.py | 66 ++++++++++++++++++++++++++++++++++ wifi/main.py | 75 ++++++++++++++++++++++++++++++++------- 10 files changed, 275 insertions(+), 22 deletions(-) create mode 100644 README.md create mode 100644 hello/README.md create mode 100644 hello/main.py create mode 100644 hello2/README.md rename {color => hello2}/main.py (61%) create mode 100644 rainbow_buttons/README.md create mode 100644 rainbow_buttons/main.py create mode 100644 scroll_font/README.md create mode 100644 scroll_font/main.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..864f924 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +à tester : https://github.com/pimoroni/st7789-python/blob/master/examples/image.py + +comment afficher des images ? https://github.com/devbis/st7789py_mpy/issues/2 diff --git a/hello/README.md b/hello/README.md new file mode 100644 index 0000000..15d1eba --- /dev/null +++ b/hello/README.md @@ -0,0 +1,3 @@ +# README.md + +Source: https://github.com/russhughes/st7789_mpy/blob/master/examples/time_trial.py \ No newline at end of file diff --git a/hello/main.py b/hello/main.py new file mode 100644 index 0000000..f6a5a40 --- /dev/null +++ b/hello/main.py @@ -0,0 +1,40 @@ +""" +Writes "Hello!" at different locations. +""" + +import random +from machine import Pin, SPI +import st7789 as st7789 +import time +import utime + +# Choose a font +#from fonts import vga1_8x8 as font +#from fonts import vga2_8x8 as font +#from fonts import vga1_8x16 as font +from fonts import vga2_8x16 as font +#from fonts import vga1_16x16 as font +#from fonts import vga1_bold_16x16 as font +#from fonts import vga2_16x16 as font +#from fonts import vga2_bold_16x16 as font + +def main(): + tft = st7789.ST7789( + SPI(2, baudrate=30000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19)), + 135, + 240, + reset=Pin(23, Pin.OUT), + cs=Pin(5, Pin.OUT), + dc=Pin(16, Pin.OUT), + backlight=Pin(4, Pin.OUT), + rotation=3 + ) + tft.init() + + tft.fill(st7789.BLACK) + tft.text(font, "MicroPython", 0, 0, st7789.WHITE) + tft.text(font, "MicroPython", 0, 32, st7789.RED) + tft.text(font, "MicroPython", 0, 64, st7789.GREEN) + tft.text(font, "MicroPython", 0, 96, st7789.BLUE) + +main() \ No newline at end of file diff --git a/hello2/README.md b/hello2/README.md new file mode 100644 index 0000000..1fb3c8f --- /dev/null +++ b/hello2/README.md @@ -0,0 +1,4 @@ +# README.md + +Source: https://github.com/russhughes/st7789_mpy/blob/master/examples/ttgo_hello.py +Exemple youtube: https://www.youtube.com/watch?v=-spQgQPzGO0 \ No newline at end of file diff --git a/color/main.py b/hello2/main.py similarity index 61% rename from color/main.py rename to hello2/main.py index 3e4f27b..79736af 100644 --- a/color/main.py +++ b/hello2/main.py @@ -1,20 +1,31 @@ +""" +Writes "Hello!" in random colors at random locations. +""" + import random from machine import Pin, SPI import st7789 + +# Choose a font #from fonts import vga1_8x8 as font -from fonts import vga2_bold_16x32 as font +#from fonts import vga2_8x8 as font +#from fonts import vga1_8x16 as font +#from fonts import vga2_8x16 as font +#from fonts import vga1_16x16 as font +#from fonts import vga1_bold_16x16 as font +#from fonts import vga2_16x16 as font +from fonts import vga2_bold_16x16 as font def main(): - spi = SPI(2, baudrate=30000000, polarity=1, sck=Pin(18), mosi=Pin(19)) tft = st7789.ST7789( - spi, - 135, - 240, - reset=Pin(23, Pin.OUT), - cs=Pin(5, Pin.OUT), - dc=Pin(16, Pin.OUT), - backlight=Pin(4, Pin.OUT), - rotation=3 + SPI(2, baudrate=30000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19)), + 135, + 240, + reset=Pin(23, Pin.OUT), + cs=Pin(5, Pin.OUT), + dc=Pin(16, Pin.OUT), + backlight=Pin(4, Pin.OUT), + rotation=3 ) tft.init() diff --git a/rainbow_buttons/README.md b/rainbow_buttons/README.md new file mode 100644 index 0000000..747b3b3 --- /dev/null +++ b/rainbow_buttons/README.md @@ -0,0 +1 @@ +Source: https://gist.github.com/paulwinex/a65f0a36485ae1dea3de4a82f647949a \ No newline at end of file diff --git a/rainbow_buttons/main.py b/rainbow_buttons/main.py new file mode 100644 index 0000000..918bacd --- /dev/null +++ b/rainbow_buttons/main.py @@ -0,0 +1,71 @@ +import time +from machine import Pin, SPI +import st7789 as st7789 +from random import randrange, choice + + +def hsv_to_rgb(h, s, v): + if s == 0.0: + return v, v, v + i = int(h * 6.0) # XXX assume int() truncates! + f = (h * 6.0) - i + p = v * (1.0 - s) + q = v * (1.0 - s * f) + t = v * (1.0 - s * (1.0 - f)) + i = i % 6 + if i == 0: + return int(v * 255), int(t * 255), int(p * 255) + if i == 1: + return int(q * 255), int(v * 255), int(p * 255) + if i == 2: + return int(p * 255), int(v * 255), int(t * 255) + if i == 3: + return int(p * 255), int(q * 255), int(v * 255) + if i == 4: + return int(t * 255), int(p * 255), int(v * 255) + if i == 5: + return int(v * 255), int(p * 255), int(q * 255) + # Cannot get here + + +def rainbow(): + print('RAINBOW') + for i in range(240): + color = st7789.color565(*hsv_to_rgb(i / 240, 1.0, 1.0)) + tft.hline(0, i, 135, color) + + +def fill(): + print('FILL') + tft.fill_rect(0, 0, 135, 240, st7789.color565( + randrange(0, 255), randrange(0, 255), randrange(0, 255) + )) + + +def main(): + p1 = Pin(0, Pin.IN) + p2 = Pin(35, Pin.IN) + + time.sleep(0.5) + tft = st7789.ST7789( + SPI(2, baudrate=30000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19)), + 135, + 240, + reset=Pin(23, Pin.OUT), + cs=Pin(5, Pin.OUT), + dc=Pin(16, Pin.OUT), + backlight=Pin(4, Pin.OUT), + rotation=0 + ) + tft.init() + + time.sleep(0.5) + fill() + while True: + if not p1.value(): + rainbow() + elif not p2.value(): + fill() + time.sleep(0.1) + +main() \ No newline at end of file diff --git a/scroll_font/README.md b/scroll_font/README.md new file mode 100644 index 0000000..512736b --- /dev/null +++ b/scroll_font/README.md @@ -0,0 +1,3 @@ +# README.md + +Source: https://github.com/russhughes/st7789_mpy/blob/master/examples/scroll_font.py diff --git a/scroll_font/main.py b/scroll_font/main.py new file mode 100644 index 0000000..46619d0 --- /dev/null +++ b/scroll_font/main.py @@ -0,0 +1,66 @@ +""" +Smoothly scrolls all font characters up the screen. +Only works with fonts with heights that are even multiples of the screen height, (i.e. 8 or 16 pixels high) + +Non-functionnal: after some chars the screen is broken +""" + +import utime +import random +from machine import Pin, SPI +import st7789 + +# Choose a font +#from fonts import vga1_8x8 as font +#from fonts import vga2_8x8 as font +#from fonts import vga1_8x16 as font +from fonts import vga2_8x16 as font +#from fonts import vga1_16x16 as font +#from fonts import vga1_bold_16x16 as font +#from fonts import vga2_16x16 as font +#from fonts import vga2_bold_16x16 as font + +def main(): + tft = st7789.ST7789( + SPI(2, baudrate=30000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19)), + 135, + 240, + reset=Pin(23, Pin.OUT), + cs=Pin(5, Pin.OUT), + dc=Pin(16, Pin.OUT), + backlight=Pin(4, Pin.OUT), + rotation=0 + ) + tft.init() + + last_line = tft.height() - font.HEIGHT + tfa = 40 + tfb = 40 + tft.vscrdef(tfa, 240, tfb) + + tft.fill(st7789.BLUE) + scroll = 0 + character = 0 + while True: + tft.fill_rect(0,scroll, tft.width(), 1, st7789.BLUE) + + if scroll % font.HEIGHT == 0: + tft.text( + font, + '\\x{:02x}= {:s} '.format(character, chr(character)), + 0, + (scroll + last_line) % tft.height(), + st7789.WHITE, + st7789.BLUE) + + character = character +1 if character < 256 else 0 + + tft.vscsad(scroll+tfa) + scroll +=1 + + if scroll == tft.height: + scroll = 0 + + utime.sleep(0.01) + +main() \ No newline at end of file diff --git a/wifi/main.py b/wifi/main.py index 8f87a52..c68d376 100644 --- a/wifi/main.py +++ b/wifi/main.py @@ -1,15 +1,66 @@ +""" +Connect to a Wifi Hotspot, creds are stored in 'creds' file. + +""" + import network -file = open('creds', 'r') -wifi_ssid = file.readline().rstrip("\n") -wifi_pass = file.readline().rstrip("\n") -file.close() +# For display +import random +from machine import Pin, SPI +import st7789 as st7789 +import time +import utime +# Choose a font +#from fonts import vga1_8x8 as font +#from fonts import vga2_8x8 as font +#from fonts import vga1_8x16 as font +from fonts import vga2_8x16 as font +#from fonts import vga1_16x16 as font +#from fonts import vga1_bold_16x16 as font +#from fonts import vga2_16x16 as font +#from fonts import vga2_bold_16x16 as font -wlan = network.WLAN(network.STA_IF) -wlan.active(True) -if not wlan.isconnected(): - print('connecting to network...') - wlan.connect(wifi_ssid, wifi_pass) - while not wlan.isconnected(): - pass -print('network config:', wlan.ifconfig()) \ No newline at end of file +def main(): + tft = st7789.ST7789( + SPI(2, baudrate=30000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19)), + 135, + 240, + reset=Pin(23, Pin.OUT), + cs=Pin(5, Pin.OUT), + dc=Pin(16, Pin.OUT), + backlight=Pin(4, Pin.OUT), + rotation=3 + ) + tft.init() + tft.fill(st7789.BLACK) + + file = open('creds', 'r') + wifi_ssid = file.readline().rstrip("\n") + wifi_pass = file.readline().rstrip("\n") + file.close() + + wlan = network.WLAN(network.STA_IF) + wlan.active(True) + if not wlan.isconnected(): + tft.text(font, "Init", 0, 0, st7789.WHITE) + print('Init connection to Wifi...') + wlan.connect(wifi_ssid, wifi_pass) + while not wlan.isconnected(): + tft.text(font, "Connecting ...", 0, 0, st7789.RED) + print('connecting...') + utime.sleep(1) + tft.text(font, "Connecting ...", 0, 0, st7789.BLACK) + pass + + tft.text(font, "Connected", 0, 0, st7789.WHITE) + print('Connected') + + wlan_config = wlan.ifconfig() + print('network config:', wlan_config) + tft.text(font, "IP/network: ", 0, 32, st7789.WHITE) + tft.text(font, wlan_config[0] + "/" + wlan_config[1], 10, 53, st7789.GREEN) + tft.text(font, "Gw/DNS: ", 0, 75, st7789.WHITE) + tft.text(font, wlan_config[2] + "/" + wlan_config[3], 10, 96, st7789.GREEN) + +main()