-
Notifications
You must be signed in to change notification settings - Fork 0
Example Notebook from UnitV2 Distribution
Example notebook. OPen http://unitv2.py and switch to notebook mode, then reload page
UnitV2 along with this Jupyter Notebook is running the Python 3.8 which supports all cool features of the python!
import sys
print("This is Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
You can try the fancy prints like this:
hello = 'hello'
print(f'{hello} %s' % 'world')
or some new gramma candy like this:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
For all the new features, please refer to https://docs.python.org/3/whatsnew/3.8.html and check out the python tutorial https://docs.python.org/3.8/tutorial/index.html !
Inside the M5Stack UnitV2 there is two LED.
A white led on the GPIO0 and a red led on the GPIO1. You can control them by export the GPIO and set the value.
def control_white_led(value):
open('/sys/class/gpio/export', 'w').write('0') # Export the GPIO0
open('/sys/class/gpio/gpio0/direction', 'w').write('out') # Set the direction of the GPIO
open('/sys/class/gpio/gpio0/value', 'w').write(str(value)) # Set the calute, '1' or '0'
def control_red_led(value):
open('/sys/class/gpio/export', 'w').write('1') # Export the GPIO0
open('/sys/class/gpio/gpio1/direction', 'w').write('out') # Set the direction of the GPIO
open('/sys/class/gpio/gpio1/value', 'w').write(str(value)) # Set the calute, '1' or '0'
The LED are connected between the VCC and the GPIO,so we need to set it to zero to let it shine!
control_white_led(0)
We can also let blink the led!
import time
for i in range(10):
control_white_led(0)
time.sleep(0.5)
control_white_led(1)
time.sleep(0.5)
The grove port is connect to the device only through the on-board UART. The UART device is located at /dev/ttyS1.
You can operate the port like this:
import serial
# Open the serial port, and set the baudrate to 115200
uart_grove = serial.Serial('/dev/ttyS1', 115200, timeout=0.5)
# Send Something
uart_grove.write('hello'.encode('utf-8'))
# encode is for converting the string to bytes, or you can directly send bytes
uart_grove.write(b'hello')
# Receive soemthing
x = uart_grove.read() # read one byte
s = uart_grove.read(10) # read up to ten bytes (timeout)
line = uart_grove.readline() # read a '\n' terminated line
You can read more on the serial package at here: https://pyserial.readthedocs.io/en/latest/index.html
M5Stack UnitV2 has a built-in camera located at /dev/video0.
You can try to capture a still image like this:
import cv2
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
if ret:
print('Capture Successful!')
else:
print('OOps, we get some trouble!')
If you see 'Capture Successful', that means we captured a frame, congratulations! Let try to show it here!
The first thing to do is import a libiary called matplotlib. this can help us show image and plots!
from matplotlib import pyplot as plt
# Magic to tell matplotlib display the image in jupyter notebook
%matplotlib inline
# Let show the image!
plt.imshow(frame)
Hmmmm, the color looks weired, doesn't it? What is going wrong?
OpenCV , for historical reason,is using BGR instead of RGB, but matplotlib do not know it. We need to help matplotlib show the correct image!
# Convert from BGR to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imshow(frame_rgb)
Wow, it looks cool! We can also convert it to grey.
# Convert from BGR to RGB
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# We need to tell matplotlib it is a grey image. You can try to change to other cmap, refer here: https://matplotlib.org/stable/tutorials/colors/colormaps.html
plt.imshow(frame_gray, cmap='gray')