mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
scripts/examples: Update examples.
- Add examples index. - Remove RP2040's Bluetooth examples. - Resort examples.
This commit is contained in:
parent
1f0b209750
commit
98a29e0870
@ -1,66 +0,0 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# 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 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)
|
||||
@ -1,103 +0,0 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
# 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)
|
||||
@ -1,67 +0,0 @@
|
||||
# This work is licensed under the MIT license.
|
||||
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
|
||||
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||
#
|
||||
from micropython import const
|
||||
|
||||
import uasyncio as asyncio
|
||||
import aioble
|
||||
import bluetooth
|
||||
|
||||
import random
|
||||
import struct
|
||||
|
||||
# org.bluetooth.service.environmental_sensing
|
||||
_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
|
||||
# org.bluetooth.characteristic.temperature
|
||||
_ENV_SENSE_TEMP_UUID = bluetooth.UUID(0x2A6E)
|
||||
# org.bluetooth.characteristic.gap.appearance.xml
|
||||
_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768)
|
||||
|
||||
# How frequently to send advertising beacons.
|
||||
_ADV_INTERVAL_MS = 250_000
|
||||
|
||||
# Register GATT server.
|
||||
temp_service = aioble.Service(_ENV_SENSE_UUID)
|
||||
temp_characteristic = aioble.Characteristic(
|
||||
temp_service, _ENV_SENSE_TEMP_UUID, read=True, notify=True
|
||||
)
|
||||
aioble.register_services(temp_service)
|
||||
|
||||
|
||||
# Helper to encode the temperature characteristic encoding (sint16, hundredths of a degree).
|
||||
def _encode_temperature(temp_deg_c):
|
||||
return struct.pack("<h", int(temp_deg_c * 100))
|
||||
|
||||
|
||||
# This would be periodically polling a hardware sensor.
|
||||
async def sensor_task():
|
||||
t = 24.5
|
||||
while True:
|
||||
temp_characteristic.write(_encode_temperature(t))
|
||||
t += random.uniform(-0.5, 0.5)
|
||||
await asyncio.sleep_ms(1000)
|
||||
|
||||
|
||||
# Serially wait for connections. Don't advertise while a central is
|
||||
# connected.
|
||||
async def peripheral_task():
|
||||
while True:
|
||||
async with await aioble.advertise(
|
||||
_ADV_INTERVAL_MS,
|
||||
name="mpy-temp",
|
||||
services=[_ENV_SENSE_UUID],
|
||||
appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER,
|
||||
) as connection:
|
||||
print("Connection from", connection.device)
|
||||
await connection.disconnected()
|
||||
|
||||
|
||||
# Run both tasks.
|
||||
async def main():
|
||||
t1 = asyncio.create_task(sensor_task())
|
||||
t2 = asyncio.create_task(peripheral_task())
|
||||
await asyncio.gather(t1, t2)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
34
scripts/examples/index.csv
Normal file
34
scripts/examples/index.csv
Normal file
@ -0,0 +1,34 @@
|
||||
# "Relative Path Regex", "Board Type Regex", "Sensor Type Regex", "Flatten Regex"
|
||||
"HelloWorld/blinky.py", ".+", ".+", ""
|
||||
"HelloWorld/helloworld.py", ".+", "^(?!None).*$", ""
|
||||
"Camera/00-Snapshot", ".+", "^(?!None).*$", ""
|
||||
"Camera/01-Video-Recording", ".+", "^(?!None).*$", ""
|
||||
"Camera/02-Optical-Flow", ".+", "^(?!None).*$", ""
|
||||
"Camera/03-Event-Cameras", ".+", "(FROGEYE2020)", ""
|
||||
"Camera/04-Global-Shutter", ".+", "(MT9V0X2|MT9V0X2-C|MT9V0X4|MT9V0X4-C)", ""
|
||||
"Camera/05-FLIR-Lepton", ".+", "(LEPTON|LEPTON-1.5|LEPTON-1.6|LEPTON-2.0|LEPTON-2.5|LEPTON-3.0|LEPTON-3.5)", ""
|
||||
"Camera/06-Time-of-Flight", ".+", "^(?!None).*$", ""
|
||||
"Camera/07-Sensor-Control", ".+", "^(?!None).*$", ""
|
||||
"Camera/08-Readout-Control", ".+", "^(?!None).*$", ""
|
||||
"Image-Processing", ".+", "^(?!None).*$", ""
|
||||
"Machine-Learning/00-TensorFlow", ".+", "^(?!None).*$", ""
|
||||
"Machine-Learning/01-ST-CubeAI", "(OPENMV2|OPENMV3|OPENMV4|OPENMV4P|OPENMVPT|ARDUINO_GIGA|ARDUINO_PORTENTA_H7|ARDUINO_NICLA_VISION)", "^(?!None).*$", ""
|
||||
"Machine-Learning/02-Haar-Cascade", ".+", "^(?!None).*$", ""
|
||||
"Barcodes", ".+", "^(?!None).*$", ""
|
||||
"Feature-Detection", ".+", "^(?!None).*$", ""
|
||||
"April-Tags", ".+", "^(?!None).*$", ""
|
||||
# STM32 libraries
|
||||
"Interface-Library/00-Arduino", "(OPENMV2|OPENMV3|OPENMV4|OPENMV4P|OPENMVPT|ARDUINO_GIGA|ARDUINO_PORTENTA_H7|ARDUINO_NICLA_VISION)", ".+", ""
|
||||
"Interface-Library/01-Pixy-Emulation", "(OPENMV2|OPENMV3|OPENMV4|OPENMV4P|OPENMVPT|ARDUINO_GIGA|ARDUINO_PORTENTA_H7|ARDUINO_NICLA_VISION)", ".+", ""
|
||||
"Interface-Library/02-MAVLink", ".+", ".+", ""
|
||||
"Interface-Library/03-Modbus", ".+", ".+", ""
|
||||
"RPC-Library", ".+", ".+", ""
|
||||
"WiFi/(?!WINC1500).*$", "^(?!arduino_nano_33_ble_sense).*$", ".+", ""
|
||||
"WiFi/WINC1500", "(OPENMV2|OPENMV3|OPENMV4|OPENMV4P|OPENMVPT)", ".+", ""
|
||||
"Bluetooth", "(OPENMV_RT1060|ARDUINO_GIGA|ARDUINO_PORTENTA_H7|ARDUINO_NICLA_VISION|ARDUINO_NANO_RP2040_CONNECT)", ".+", ""
|
||||
"OpenMV-Boards/(?!main\.py).*$", "(OPENMV.*)", ".+", "OpenMV-Boards$"
|
||||
"Arduino-Boards/Giga-H7", "ARDUINO_GIGA", ".+", "Arduino-Boards(/Giga-H7)?$"
|
||||
"Arduino-Boards/Nano-33-BLE-Sense", "ARDUINO_NANO_33_BLE_SENSE", ".+", "Arduino-Boards(/Nano-33-BLE-Sense)?$"
|
||||
"Arduino-Boards/Nano-RP2040", "ARDUINO_NANO_RP2040_CONNECT", ".+", "Arduino-Boards(/Nano-RP2040)?$"
|
||||
"Arduino-Boards/Nicla-Vision", "ARDUINO_NICLA_VISION", ".+", "Arduino-Boards(/Nicla-Vision)?$"
|
||||
"Arduino-Boards/Portenta-H7", "ARDUINO_PORTENTA_H7", ".+", "Arduino-Boards(/Portenta-H7)?$"
|
||||
|
Can't render this file because it contains an unexpected character in line 1 and column 3.
|
Loading…
Reference in New Issue
Block a user