Add Nano-RP2040 examples.

This commit is contained in:
iabdalkader 2021-08-04 21:51:16 +02:00
parent bd245546b7
commit 816f2fe4c0
11 changed files with 442 additions and 0 deletions

View File

@ -0,0 +1,14 @@
# Blinky example
import time
from machine import Pin
# This is the only LED pin available on the Nano RP2040,
# other than the RGB LED connected to Nina WiFi module.
led = Pin(6, Pin.OUT)
while (True):
led.on()
time.sleep_ms(250)
led.off()
time.sleep_ms(250)

View File

@ -0,0 +1,18 @@
# I2C scanner examples
#
# 7-bit addresses for NANO RP2040 on I2C0 bus:
#
# ATECC608A 0x60
# LSM6DSOX 0x6A
import time
from machine import Pin, I2C
i2c_list = [None, None]
i2c_list[0] = I2C(0, scl=Pin(13), sda=Pin(12), freq=100_000)
i2c_list[1] = I2C(1, scl=Pin(7), sda=Pin(6), freq=100_000)
for bus in range(0, 2):
print("\nScanning bus %d..."%(bus))
for addr in i2c_list[bus].scan():
print("Found device at addres %d:0x%x" %(bus, addr))

View File

@ -0,0 +1,12 @@
# LSM9DS1 Gyro example.
import time
from lsm6dsox import LSM6DSOX
from machine import Pin, I2C
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12)))
while (True):
print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_accel()))
print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_gyro()))
print("")
time.sleep_ms(100)

View File

@ -0,0 +1,59 @@
# Bluetooth Blinky Example
#
# Use nRFConnect app from the App store, connect to the Nano and write 1/0 to control the LED.
import bluetooth
import random
import struct
import time
from ble_advertising import advertising_payload
from machine import Pin
from micropython import const
LED_PIN = 6
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_FLAG_READ = const(0x0002)
_FLAG_WRITE = const(0x0008)
_FLAG_NOTIFY = const(0x0010)
_FLAG_INDICATE = const(0x0020)
_SERVICE_UUID = bluetooth.UUID(0x1523)
_LED_CHAR_UUID = (bluetooth.UUID(0x1525), _FLAG_WRITE)
_LED_SERVICE = (_SERVICE_UUID, (_LED_CHAR_UUID,),)
class BLETemperature:
def __init__(self, ble, name="NANO RP2040"):
self._ble = ble
self._ble.active(True)
self._ble.irq(self._irq)
((self._handle,),) = self._ble.gatts_register_services((_LED_SERVICE,))
self._connections = set()
self._payload = advertising_payload(name=name, services=[_SERVICE_UUID])
self._advertise()
def _irq(self, event, data):
# Track connections so we can send notifications.
if event == _IRQ_CENTRAL_CONNECT:
conn_handle, _, _ = data
self._connections.add(conn_handle)
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle, _, _ = data
self._connections.remove(conn_handle)
# Start advertising again to allow a new connection.
self._advertise()
elif event == _IRQ_GATTS_WRITE:
Pin(LED_PIN, Pin.OUT).value(int(self._ble.gatts_read(data[-1])[0]))
def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)
if __name__ == "__main__":
ble = bluetooth.BLE()
temp = BLETemperature(ble)
while True:
time.sleep_ms(1000)

View File

@ -0,0 +1,97 @@
# This example demonstrates a simple temperature sensor peripheral.
#
# The sensor's local value updates every second, and it will notify
# any connected central every 10 seconds.
import bluetooth
import random
import struct
import time
from ble_advertising import advertising_payload
from machine import Pin
from micropython import const
LED_PIN = 6
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_INDICATE_DONE = const(20)
_FLAG_READ = const(0x0002)
_FLAG_NOTIFY = const(0x0010)
_FLAG_INDICATE = const(0x0020)
# org.bluetooth.service.environmental_sensing
_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
# org.bluetooth.characteristic.temperature
_TEMP_CHAR = (
bluetooth.UUID(0x2A6E),
_FLAG_READ | _FLAG_NOTIFY | _FLAG_INDICATE,
)
_ENV_SENSE_SERVICE = (
_ENV_SENSE_UUID,
(_TEMP_CHAR,),
)
# org.bluetooth.characteristic.gap.appearance.xml
_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768)
class BLETemperature:
def __init__(self, ble, name="NANO RP2040"):
self._ble = ble
self._ble.active(True)
self._ble.irq(self._irq)
((self._handle,),) = self._ble.gatts_register_services((_ENV_SENSE_SERVICE,))
self._connections = set()
self._payload = advertising_payload(
name=name, services=[_ENV_SENSE_UUID], appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER
)
self._advertise()
def _irq(self, event, data):
# Track connections so we can send notifications.
if event == _IRQ_CENTRAL_CONNECT:
conn_handle, _, _ = data
self._connections.add(conn_handle)
Pin(LED_PIN, Pin.OUT).high()
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle, _, _ = data
self._connections.remove(conn_handle)
# Start advertising again to allow a new connection.
self._advertise()
Pin(LED_PIN, Pin.OUT).low()
elif event == _IRQ_GATTS_INDICATE_DONE:
conn_handle, value_handle, status = data
def set_temperature(self, temp_deg_c, notify=False, indicate=False):
# Data is sint16 in degrees Celsius with a resolution of 0.01 degrees Celsius.
# Write the local value, ready for a central to read.
self._ble.gatts_write(self._handle, struct.pack("<h", int(temp_deg_c * 100)))
if notify or indicate:
for conn_handle in self._connections:
if notify:
# Notify connected centrals.
self._ble.gatts_notify(conn_handle, self._handle)
if indicate:
# Indicate connected centrals.
self._ble.gatts_indicate(conn_handle, self._handle)
def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)
if __name__ == "__main__":
ble = bluetooth.BLE()
temp = BLETemperature(ble)
t = 25
i = 0
while True:
# Write every second, notify every 10 seconds.
i = (i + 1) % 10
temp.set_temperature(t, notify=i == 0, indicate=False)
# Random walk the temperature.
t += random.uniform(-0.5, 0.5)
time.sleep_ms(1000)

View File

@ -0,0 +1,61 @@
# WiFi AP Mode Example
#
# This example shows how to use WiFi in Access Point mode.
import network, socket, sys, time, gc
SSID ='OPENMV_AP' # Network SSID
KEY ='1234567890' # Network key (must be 10 chars)
HOST = '' # Use first available interface
PORT = 8080 # Arbitrary non-privileged port
# Init wlan module and connect to network
wlan = network.WLAN(network.AP_IF)
wlan.active(True)
wlan.config(essid=SSID, key=KEY, security=wlan.WEP, channel=2)
print("AP mode started. SSID: {} IP: {}".format(SSID, wlan.ifconfig()[0]))
def recvall(sock, n):
# Helper function to recv n bytes or return None if EOF is hit
data = bytearray()
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
raise OSError("Timeout")
data.extend(packet)
return data
def start_streaming(server):
print ('Waiting for connections..')
client, addr = server.accept()
# set client socket timeout to 5s
client.settimeout(5.0)
print ('Connected to ' + addr[0] + ':' + str(addr[1]))
# FPS clock
clock = time.clock()
while (True):
try:
# Read data from client
data = recvall(client, 1024)
# Send it back
client.send(data)
except OSError as e:
print("start_streaming(): socket error: ", e)
client.close()
break
while (True):
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind and listen
server.bind([HOST, PORT])
server.listen(1)
# Set server socket to blocking
server.setblocking(True)
while (True):
start_streaming(server)
except OSError as e:
server.close()
print("Server socket error: ", e)

View File

@ -0,0 +1,38 @@
# Simple HTTP client example.
import network, socket
# AP info
SSID='' # Network SSID
KEY='' # Network key
PORT = 80
HOST = "www.google.com"
# Init wlan module and connect to network
print("Trying to connect. Note this may take a while...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY)
# We should have a valid IP now via DHCP
print("WiFi Connected ", wlan.ifconfig())
# Get addr info via DNS
addr = socket.getaddrinfo(HOST, PORT)[0][4]
print(addr)
# Create a new socket and connect to addr
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(addr)
# Set timeout
client.settimeout(3.0)
# Send HTTP request and recv response
client.send("GET / HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST))
print(client.recv(1024))
# Close socket
client.close()

View File

@ -0,0 +1,37 @@
# NTP Example
#
# This example shows how to get the current time using NTP with the WiFi shield.
import network, usocket, ustruct, utime
# AP info
SSID='' # Network SSID
KEY='' # Network key
TIMESTAMP = 2208988800
# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WLAN()
wlan.active(True)
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
# We should have a valid IP now via DHCP
print(wlan.ifconfig())
# Create new socket
client = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
client.bind(("", 8080))
#client.settimeout(3.0)
# Get addr info via DNS
addr = usocket.getaddrinfo("pool.ntp.org", 123)[0][4]
# Send query
client.sendto('\x1b' + 47 * '\0', addr)
data, address = client.recvfrom(1024)
# Print time
t = ustruct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP
print ("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (utime.localtime(t)[0:6]))

View File

@ -0,0 +1,16 @@
# Scan Example
#
# This example shows how to scan for WiFi networks.
import time, network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Scanning...")
while (True):
scan_result = wlan.scan()
for ap in scan_result:
print("Channel:%d RSSI:%d Auth:%d BSSID:%s SSID:%s"%(ap))
print()
time.sleep_ms(1000)

View File

@ -0,0 +1,60 @@
import image, audio, time
from ulab import numpy as np
from ulab import scipy as sp
CHANNELS = 1
FREQUENCY = 32000
N_SAMPLES = 32 if FREQUENCY == 16000 else 64
SCALE = 2
SIZE = (N_SAMPLES * SCALE) // CHANNELS
raw_buf = None
fb = image.Image(SIZE+(50*SCALE), SIZE, image.RGB565, copy_to_fb=True)
audio.init(channels=CHANNELS, frequency=FREQUENCY, gain_db=16)
def audio_callback(buf):
# NOTE: do Not call any function that allocates memory.
global raw_buf
if (raw_buf == None):
raw_buf = buf
# Start audio streaming
audio.start_streaming(audio_callback)
def draw_fft(img, fft_buf):
fft_buf = (fft_buf / max(fft_buf)) * SIZE
fft_buf = np.log10(fft_buf + 1) * 20
color = (0xFF, 0x0F, 0x00)
for i in range(0, len(fft_buf)):
img.draw_line(i*SCALE, SIZE, i*SCALE, SIZE-int(fft_buf[i]) * SCALE, color, SCALE)
def draw_audio_bar(img, level, offset):
blk_size = (SIZE//10)
color = (0xFF, 0x00, 0xF0)
blk_space = (blk_size//4)
for i in range(0, int(round(level/10))):
fb.draw_rectangle(SIZE+offset, SIZE - ((i+1)*blk_size) + blk_space, 20 * SCALE, blk_size - blk_space, color, 1, True)
while (True):
if (raw_buf != None):
pcm_buf = np.frombuffer(raw_buf, dtype=np.int16)
raw_buf = None
if CHANNELS == 1:
fft_buf = sp.signal.spectrogram(pcm_buf)
l_lvl = int((np.mean(abs(pcm_buf[1::2])) / 32768)*100)
else:
fft_buf = sp.signal.spectrogram(pcm_buf[0::2])
l_lvl = int((np.mean(abs(pcm_buf[1::2])) / 32768)*100)
r_lvl = int((np.mean(abs(pcm_buf[0::2])) / 32768)*100)
fb.clear()
draw_fft(fb, fft_buf)
draw_audio_bar(fb, l_lvl, 0)
draw_audio_bar(fb, l_lvl, 25*SCALE)
if CHANNELS == 2:
draw_audio_bar(fb, r_lvl, 25 * SCALE)
fb.flush()
# Stop streaming
audio.stop_streaming()

View File

@ -0,0 +1,30 @@
# Thermal Camera Demo
#
# This example shows how to use common low-res FIR sensors (like MLX or AMG).
# NOTE: Only the AMG8833 is currently enabled for NANO RP2040.
import image, time, fir
IMAGE_SCALE = 5 # Higher scaling uses more memory.
drawing_hint = image.BICUBIC # or image.BILINEAR or 0 (nearest neighbor)
# Initialize the thermal sensor
fir.init() #Auto-detects the connected sensor.
w = fir.width() * IMAGE_SCALE
h = fir.height() * IMAGE_SCALE
# FPS clock
clock = time.clock()
while (True):
clock.tick()
try:
img = fir.snapshot(x_size=w, y_size=h,
color_palette=fir.PALETTE_IRONBOW, hint=drawing_hint,
copy_to_fb=True)
except OSError:
continue
# Print FPS.
print(clock.fps())