Adsense HTML/JavaScript

Showing posts with label Pyboard. Show all posts
Showing posts with label Pyboard. Show all posts

Tuesday, November 2, 2021

MicroPython/PyBoard exercise: + 1.8" 128x160 TFT ST7735 SPI

It's a MicroPython/PyBoard exercise run on Modified MicroPython pyboard PYBv1.1, to drive 1.8" 128x160 TFT ST7735 SPI using GuyCarver/MicroPython lib.




Prepare library:

Visit https://github.com/GuyCarver/MicroPython/tree/master/lib copy ST7735.py, seriffont.py, sysfont.py and terminalfont.py to your pyboard.


Connection:

Refer to __init__() of class tft in ST7735.py:

  def __init__( self, aLoc, aDC, aReset ) :
    '''aLoc SPI pin location is either 1 for 'X' or 2 for 'Y'.
       aDC is the DC pin and aReset is the reset pin.'''
	.
	.
	.
    cs = "X5" if aLoc == 1 else "Y5"
	.
	.
    self.spi = pyb.SPI(aLoc, ...)
	.
	.

And refer to pyb SPI class document.


That means 
when aloc = 1:
SPI(1) is on the X position: (CS, SCK, MISO, MOSI) = (X5, X6, X7, X8)
when aloc = 2:
SPI(2) is on the Y position: (CS, SCK, MISO, MOSI) = (Y5, Y6, Y7, Y8)

In my exercise code below marked RED, display of ST7735.tft is created with aLoc=1.
display = ST7735.tft(aLoc=1, aDC='Y9', aReset='Y10')
So the connection is:
ST7735	Pyboard
	aLoc=1	(if aLoc=2)
----------------------------
LED	3V3		
SCK	X6	Y6
SDA	X8	Y8
A0	Y9
RESET	Y10
CS	X5	Y5
GND	GND
VCC	3V3

Exercise code:

mpyPyb_tft.py
"""
MicroPython/PyBoard exercise run on Pyb v1.1,
+ 1.8" 128x160 TFT ST7735 SPI

using GuyCarver/MicroPython
https://github.com/GuyCarver/MicroPython
"""
import uos
import usys
import time
import random
import ST7735
from seriffont import seriffont
from sysfont import sysfont
from terminalfont import terminalfont

print("====================================")
print(usys.implementation[0], uos.uname()[3],
      "\nrun on", uos.uname()[4])
print("------------------------------------")

print("====================================")

display = ST7735.tft(aLoc=1, aDC='Y9', aReset='Y10')

displaysize = display.size()
print(displaysize)

display.initg() # or initr()/initb() according to your display

display.rotation(1)
display.fill(display._BLACK)
display.text((0,0),
             usys.implementation[0]+' '+uos.uname()[3],
             display._WHITE,
             terminalfont)
time.sleep(1)
display.text((0,30),
             "run on "+uos.uname()[4],
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont)
time.sleep(3)

#font test
display.fill(display._BLACK)
display.text((0,0),
             "seriffont",
             display._RED,
             seriffont)
display.text((0,10),
             "abcdefghijklmnopqrstuvwxyz",
             display._WHITE,
             seriffont)
display.text((0,30),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display._WHITE,
             seriffont)
display.text((0,50),
             "sysfont",
             display._RED,
             sysfont)
display.text((0,60),
             "abcdefghijklmnopqrstuvwxyz",
             display._WHITE,
             sysfont)
display.text((0,70),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display._WHITE,
             sysfont)
display.text((0,80),
             "terminalfont",
             display._RED,
             terminalfont)
display.text((0,90),
             "abcdefghijklmnopqrstuvwxyz",
             display._WHITE,
             terminalfont)
display.text((0,110),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display._WHITE,
             terminalfont)
time.sleep(5)

display.rotation(0)
display.fill(display._RED)
display.text((10,10),
             "RED",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

display.fill(display._GREEN)
display.text((10,10),
             "GREEN",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

display.fill(display._BLUE)
display.text((10,10),
             "BLUE",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

#rotation test
display.rotation(0)
display.fill(display._BLACK)
display.fillrect((1,1),
                 (display.size()[0]-2, display.size()[1]-2),
                 display._WHITE)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._BLACK,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

display.rotation(1)
display.fillrect((2,2),
                 (display.size()[0]-4, display.size()[1]-4),
                 display._BLACK)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._WHITE,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

display.rotation(2)
display.fillrect((3,3),
                 (display.size()[0]-6, display.size()[1]-6),
                 display._WHITE)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._BLACK,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

display.rotation(3)
display.fillrect((4,4),
                 (display.size()[0]-8, display.size()[1]-8),
                 display._BLACK)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._WHITE,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

#Random pixel
for p in range(1000):
    x = random.randint(5, display.size()[0]-10)
    y = random.randint(5, display.size()[1]-10)
    c = ST7735.TFTColor(random.randint(0, 0xFF),
                        random.randint(0, 0xFF),
                        random.randint(0, 0xFF))
    display.pixel((x, y), c)
    
#Random line
for l in range(100):
    display.line((random.randint(5, display.size()[0]-10),
                  random.randint(5, display.size()[1]-10)),
                 (random.randint(5, display.size()[0]-10),
                  random.randint(5, display.size()[1]-10)),
                 ST7735.TFTColor(random.randint(0, 0xFF),
                        random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))

#Random circle
for l in range(20):
    display.circle((random.randint(5, display.size()[0]-10),
                    random.randint(5, display.size()[1]-10)),
                   random.randint(1, 50),
                   ST7735.TFTColor(random.randint(0, 0xFF),
                                   random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))
    
#Random fillcircle
for l in range(20):
    display.fillcircle((random.randint(5, display.size()[0]-10),
                    random.randint(5, display.size()[1]-10)),
                   random.randint(1, 50),
                   ST7735.TFTColor(random.randint(0, 0xFF),
                                   random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))

print("~ bye ~")
When you run the exercise code, it will fail with:
NameError: name 'BLACK' isn't defined

To fix it, edit ST7735.py in pyboard.
  def fill( self, aColor = BLACK ) :
to
  def fill( self, aColor = _BLACK ) :
Re-run again, it should work as expected.




Related:
MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library. (in my another new blogspot)

Monday, March 8, 2021

Update PyBoard MicroPython firmware, run on Ubuntu 20.10.

Install dfu-util: 

Run in Terminal -

$ sudo apt install dfu-util

Download firmware:

To update MicroPython firmware, visit http://micropython.org/download/, select your board and download firmware for your board. ex. pybv11-20210308-unstable-v1.14-83-g680ce4532.dfu, latest firmware for PYBv1.1 boards.

Flash .dfu to pyboard:

Switch to the downloaded file folder.

Run the command:

$ sudo dfu-util --alt 0 -D <downloaded firmware .dfu> 




Friday, September 11, 2020

MicroPython on pyboard to display text on OLED (with I2C interface ssd1306)

It's a 0.96 inch 128x64  OLED displays with ssd1306 driver using I2C interface. This example show how to display text on it with pyboard using MicroPython ssd1306 library.

Connection between pyboard and OLED:
pyboard 3V3 connected to ssd1306 VCC
pyboard GND connected to ssd1306 GND
pyboard X12 connected to ssd1306 SCL
pyboard X11 connected to ssd1306 SDA


Download the MicroPython ssd1306 library HERE, ans save it to pyboard flash.

MicroPython script, pyb_i2c_ssd1306.py:

# Download ssd1306
# https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py
# and save to pyboard flash

import ssd1306
import machine
import time

WIDTH = const(128)
HEIGHT = const(64)

ssd1306_scl = machine.Pin('X12', machine.Pin.OUT_PP)
ssd1306_sda = machine.Pin('X11', machine.Pin.OUT_PP)
i2c_ssd1306 = machine.I2C(scl=ssd1306_scl, sda=ssd1306_sda)

print(i2c_ssd1306.scan())
oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c_ssd1306)
oled.fill(0)

oled.text("MicroPython", 0, 0)
oled.text("OLED(ssd1306)", 0, 10)
oled.text("pyboard", 0, 20)
oled.show()

while True:
    time.sleep(1)
    oled.invert(1)
    time.sleep(1)
    oled.invert(0)


pyboard MicroPython exercise: scan I2C address

 It's a MicroPython exercise running on pyboard, to scan I2C address of attached I2C device. Actually, the I2C device used is a OLED with I2C interface ssd1306 in next exercise.

i2c_scan.py

#I2C SCL connected to pyboard X12
#I2C SDA connected to pyboard X11

import machine
i2c_scl = machine.Pin('X12', machine.Pin.OUT_PP)
i2c_sda = machine.Pin('X11', machine.Pin.OUT_PP)

i2c = machine.I2C(scl=i2c_scl, sda=i2c_sda)
print(i2c.scan())


Monday, September 7, 2020

Modified MicroPython pyboard PYBv1.1, and more...



It's a modified version of MicroPython pyboard, PYBv1.1, with extra modification:
- BOOT0 button for enter DFU mode, such that you no need to use a wire to connect P1 and 3V3.
- SWD connetor
- PWR LED


Getting pyboard MicroPython REPL using Thonny, on Ubuntu 20.04


To install Thonny on Ubuntu 20.04 and add USB permission to user, read the post "Install esptool/thonny on Ubuntu 20.04". (No need to install esptool if you target not for ESP devices such as ESP8266/ESP32)

Run Thonny
> Run > Select interpreter...
> Select MicroPython (generic) and connected USB Port

Run MicroPython script using Thonny IDE




Update MicroPython firmware on pyboard

A list of all available firmware is here.
In my case, it's pybv11-20200902-v1.13.dfu.

To update pyboard MicroPython firmware on Ubuntu, have to install dfu-util:
$ sudo apt update
$ sudo apt install dfu-util

First, disconnect everything from your pyboard. Then connect the DFU pin with the 3.3V pin (they're right next to each other) to put the board into DFU (Device Firmware Update) mode, or press and hold the BOOT0 button for my Modified MicroPython pyboard PYBv1.1. Now connect the pyboard to your computer via USB.

Update firmware using dfu-util:
$ sudo dfu-util --alt 0 -D <downloaded dfu file>

After the the program finished, disconnect the pyboard from USB and remove the jumper between the DFU and the 3.3v ports.


MicroPython firmware updated


Related:

Sunday, February 10, 2019

MicroPython introduce new Pyboard D Series Board with STM32F7xx

The upcoming Pyboard D-series (aka Pyboard D, or just PyBD) board was introduced during the FOSDEM 2019 Micropython presentation with a faster STM32F7 Cortex-M7 MCU, as well as built-in WiFi and Bluetooth connectivity.

(FOSDEM – stands for Free and Open Source Software Developers’ European Meeting)

know more:
CNXSOFT – Pyboard D Series MicroPython Board Features STM32F7 MCU, WiFi and Bluetooth