To get ArduPy running on Wio Terminal we have to flash it with latest ArduPy build. It's described on wiki and in short it's like so:
- Enter bootloader mode by pressing the power button down two times quickly - this will mount Wio Terminal like if it was a flash drive
- Copy the UF2 ArduPy file - the device will reconnect shortly in ArduPy mode
Optionally you can install aip on your local machine:
pip install ardupy-aip
This tool can help managing Wio Terminal, especially for ArduPy.
Scripting LCD via ArduPyThere is a API reference for the LCD and a simple Hello World looks like so:
from machine import LCD
lcd = LCD() # Initialize LCD and turn the backlight
lcd.fillScreen(lcd.color.BLACK) # Fill the LCD screen with color black
lcd.setTextSize(2)
lcd.setTextColor(lcd.color.WHITE)
lcd.drawString("Hello World!", 0, 0)
To use the LCD you just initialize it and then you can draw at specified coordinates (LCD has 320x240 pixels).
When we have that working we can draw text, geometric shapes or images. Depending on end result this can be used to make a control UI for sensor/devices, a simple game or other cool projects.
Displaying bitmap on LCDArduPy lacks Arduino GFX libraries (Adafruit) but still we can draw an image on the display. It's quite tricky. We can set any RGB color to a pixel so we need to convert a 320x240 BMP image to a list of lists containing R, G, B values for each pixel. Pillow on your PC can get such data:
from PIL import Image
import csv
im = Image.open('image.bmp')
pixels = list(im.getdata())
The problem is on microcontrollers there is very little RAM so we can't just read the whole image data at once. I had to make a CSV file and read line by line to fit within the memory limits. This generates 100 lines of pixel data:
import csv
from PIL import Image
im = Image.open('image.bmp')
pixels = list(im.getdata())
res = []
for pixel in pixels:
pixel = f'{pixel[0]}.{pixel[1]}.{pixel[2]}'
res.append(pixel)
block_size = 768
with open('pixels.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
for i in range(100):
start = i * block_size
end = (i + 1) * block_size
writer.writerow(res[start:end])
I copyed pixels.csv to Wio Terminal and updated main.py to:
from machine import LCD
lcd = LCD()
lcd.fillScreen(lcd.color.WHITE)
x = 0
y = 0
file_handle = open('names.csv', 'r')
for i in range(0, 100):
line = file_handle.readline()
image = line.split(',')
for pixel in image:
pixel = pixel.strip()
if pixel:
r, g, b = pixel.split('.')
lcd.drawPixel(x, y, lcd.color565(int(r), int(g), int(b)))
x += 1
if x == 320:
y += 1
x = 0
readline reads a line from the file and will read next line on next call. This allows processing large amount of data due to small chunks.
More details on:
https://rk.edu.pl/en/scripting-lcd-on-wio-terminal-with-ardupy/
Comments
Please log in or sign up to comment.