Move Arduino examples to separate dirs.

This commit is contained in:
iabdalkader 2021-01-07 00:44:59 +02:00
parent ffcd796a7b
commit 66dc9a7c86
206 changed files with 301 additions and 0 deletions

View File

@ -0,0 +1,19 @@
# I2C scanner examples
# 7-bit addresses for NANO33 BLE SENSE
# Sensors on I2C 1 bus:
# LBS22HB 0x5C
# HTS221 0x5F
# LSM9DS1 0x1E
# LSM9DS1 0x6B
# APDS9960 0x39
import time
from machine import Pin, I2C
i2c_list = [None, None]
i2c_list[0] = I2C(0, scl=Pin(2), sda=Pin(31))
i2c_list[1] = I2C(1, scl=Pin(15), sda=Pin(14))
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,16 @@
from time import sleep_ms
from machine import Pin, I2C
from apds9960.const import *
from apds9960 import uAPDS9960 as APDS9960
bus = I2C(1, sda=Pin(13), scl=Pin(14))
apds = APDS9960(bus)
print("Light Sensor Test")
print("=================")
apds.enableLightSensor()
while True:
sleep_ms(250)
val = apds.readAmbientLight()
print("AmbientLight={}".format(val))

View File

@ -0,0 +1,31 @@
from time import sleep_ms
from machine import Pin, I2C
from apds9960.const import *
from apds9960 import uAPDS9960 as APDS9960
bus = I2C(1, sda=Pin(13), scl=Pin(14))
apds = APDS9960(bus)
dirs = {
APDS9960_DIR_NONE: "none",
APDS9960_DIR_LEFT: "left",
APDS9960_DIR_RIGHT: "right",
APDS9960_DIR_UP: "up",
APDS9960_DIR_DOWN: "down",
APDS9960_DIR_NEAR: "near",
APDS9960_DIR_FAR: "far",
}
apds.setProximityIntLowThreshold(50)
print("Gesture Test")
print("============")
apds.enableGestureSensor()
while True:
sleep_ms(500)
if apds.isGestureAvailable():
motion = apds.readGesture()
print("Gesture={}".format(dirs.get(motion, "unknown")))

View File

@ -0,0 +1,19 @@
from time import sleep_ms
from machine import Pin, I2C
from apds9960.const import *
from apds9960 import uAPDS9960 as APDS9960
bus = I2C(1, sda=Pin(13), scl=Pin(14))
apds = APDS9960(bus)
apds.setProximityIntLowThreshold(50)
print("Proximity Sensor Test")
print("=====================")
apds.enableProximitySensor()
while True:
sleep_ms(250)
val = apds.readProximity()
print("proximity={}".format(val))

View File

@ -0,0 +1,12 @@
import time
import hts221
from machine import Pin, I2C
bus = I2C(1, scl=Pin(15), sda=Pin(14))
hts = hts221.HTS221(bus)
while (True):
rH = hts.humidity()
temp = hts.temperature()
print ("rH: %.2f%% T: %.2fC" %(rH, temp))
time.sleep_ms(100)

View File

@ -0,0 +1,12 @@
import time
import lps22h
from machine import Pin, I2C
bus = I2C(1, scl=Pin(15), sda=Pin(14))
lps = lps22h.LPS22H(bus)
while (True):
pressure = lps.pressure()
temperature = lps.temperature()
print("Pressure: %.2f hPa Temperature: %.2f C"%(pressure, temperature))
time.sleep_ms(100)

View File

@ -0,0 +1,14 @@
import time
import lsm9ds1
from machine import Pin, I2C
bus = I2C(1, scl=Pin(15), sda=Pin(14))
lsm = lsm9ds1.LSM9DS1(bus)
while (True):
#for g,a in lsm.iter_accel_gyro(): print(g,a) # using fifo
print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_accel()))
print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_magnet()))
print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_gyro()))
print("")
time.sleep_ms(500)

View File

@ -0,0 +1,35 @@
# Use nRF Connect from App store, connect to the Nano and write 1/0 to control the LED.
import time
from board import LED
from ubluepy import Service, Characteristic, UUID, Peripheral, constants
def event_handler(id, handle, data):
global periph
global service
if id == constants.EVT_GAP_CONNECTED:
pass
elif id == constants.EVT_GAP_DISCONNECTED:
# restart advertisment
periph.advertise(device_name="Nano Blinky", services=[service])
elif id == constants.EVT_GATTS_WRITE:
LED(1).on() if int(data[0]) else LED(1).off()
# start off with LED(1) off
LED(1).off()
notif_enabled = False
uuid_service = UUID("0x1523")
uuid_led = UUID("0x1525")
service = Service(uuid_service)
char_led = Characteristic(uuid_led, props=Characteristic.PROP_WRITE)
service.addCharacteristic(char_led)
periph = Peripheral()
periph.addService(service)
periph.setConnectionHandler(event_handler)
periph.advertise(device_name="Nano Blinky", services=[service])
while (True):
time.sleep_ms(500)

View File

@ -0,0 +1,35 @@
import time
from ubluepy import Scanner, constants
def bytes_to_str(bytes):
string = ""
for b in bytes:
string += chr(b)
return string
def get_device_names(scan_entries):
dev_names = []
print(len(scan_entries))
for e in scan_entries:
scan = e.getScanData()
for s in scan:
print(s)
if s[0] == constants.ad_types.AD_TYPE_COMPLETE_LOCAL_NAME:
dev_names.append((e, bytes_to_str(s[2])))
return dev_names
def find_device_by_name(name):
s = Scanner()
scan_res = s.scan(1000)
device_names = get_device_names(scan_res)
for dev in device_names:
if name == dev[1]:
return dev[0]
while (True):
res = find_device_by_name("micr")
if res:
print("address:", res.addr())
print("address type:", res.addr_type())
print("rssi:", res.rssi())
time.sleep_ms(500)

View File

@ -0,0 +1,52 @@
# HTS221 + BLE example.
import time
import hts221
from board import LED
from machine import Pin, I2C
from ubluepy import Service, Characteristic, UUID, Peripheral, constants
def event_handler(id, handle, data):
global periph, service, notif_enabled
if id == constants.EVT_GAP_CONNECTED:
# indicated 'connected'
LED(1).on()
elif id == constants.EVT_GAP_DISCONNECTED:
# indicate 'disconnected'
LED(1).off()
# restart advertisment
periph.advertise(device_name="Temperature Sensor", services=[service])
elif id == constants.EVT_GATTS_WRITE:
# write to this Characteristic is to CCCD
if int(data[0]) == 1:
notif_enabled = True
else:
notif_enabled = False
# start off with LED(1) off
LED(1).off()
notif_enabled = False
uuid_service = UUID("0x181A") # Environmental Sensing service
uuid_temp = UUID("0x2A6E") # Temperature characteristic
service = Service(uuid_service)
temp_props = Characteristic.PROP_READ|Characteristic.PROP_NOTIFY
temp_attrs = Characteristic.ATTR_CCCD
temp_char = Characteristic(uuid_temp, props=temp_props, attrs=temp_attrs)
service.addCharacteristic(temp_char)
periph = Peripheral()
periph.addService(service)
periph.setConnectionHandler(event_handler)
periph.advertise(device_name="Temperature Sensor", services=[service])
bus = I2C(1, scl=Pin(15), sda=Pin(14))
hts = hts221.HTS221(bus)
while (True):
if notif_enabled:
temp = int(hts.temperature()*100)
temp_char.write(bytearray([temp & 0xFF, temp >> 8]))
time.sleep_ms(100)

View File

@ -0,0 +1,56 @@
import image, audio, time, array, math, ulab as np
from ulab import extras, numerical
CHANNELS = 1
SIZE = 256//(2*CHANNELS)
raw_buf = None
fb = image.Image(SIZE+50, SIZE, image.RGB565, copy_to_fb=True)
audio.init(channels=CHANNELS, frequency=16000, gain_db=80, highpass=0.9883)
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.vector.log10(fft_buf + 1) * 20
color = (0xFF, 0x0F, 0x00)
for i in range(0, SIZE):
img.draw_line(i, SIZE, i, SIZE-int(fft_buf[i]), color, 1)
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, blk_size - blk_space, color, 1, True)
while (True):
if (raw_buf != None):
pcm_buf = np.array(array.array('h', raw_buf))
raw_buf = None
if CHANNELS == 1:
fft_buf = extras.spectrogram(pcm_buf)
l_lvl = int((numerical.mean(abs(pcm_buf)) / 32768)*100)
else:
fft_buf = extras.spectrogram(pcm_buf[0::2])
l_lvl = int((numerical.mean(abs(pcm_buf[1::2])) / 32768)*100)
r_lvl = int((numerical.mean(abs(pcm_buf[0::2])) / 32768)*100)
fb.clear()
draw_fft(fb, fft_buf)
draw_audio_bar(fb, l_lvl, 0)
if CHANNELS == 2:
draw_audio_bar(fb, r_lvl, 25)
fb.flush()
# Stop streaming
audio.stop_streaming()

Some files were not shown because too many files have changed in this diff Show More