Skip to content

Commit

Permalink
deploy: 11614d3
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikUmble committed Apr 25, 2024
0 parents commit 41b2a67
Show file tree
Hide file tree
Showing 38 changed files with 4,339 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 2a63537479357844bf1192f560225979
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file added .doctrees/bluetooth.doctree
Binary file not shown.
Binary file added .doctrees/environment.pickle
Binary file not shown.
Binary file added .doctrees/index.doctree
Binary file not shown.
Binary file added .doctrees/movement.doctree
Binary file not shown.
Binary file added .doctrees/usage.doctree
Binary file not shown.
Empty file added .nojekyll
Empty file.
108 changes: 108 additions & 0 deletions _downloads/e6ffb4613247059f19b76a508c48412c/nanonav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

from ble_advertising import advertising_payload
import bluetooth
from machine import Pin, PWM, ADC, freq
import machine
from micropython import const
import rp2

import time

# Define BLE constants (these are not packaged in bluetooth for space efficiency)
_IO_CAPABILITY_DISPLAY_ONLY = const(0)
_FLAG_READ = const(0x0002)
_FLAG_WRITE = const(0x0008)
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)

class BLE:
"""
A helpful wraper around the BLE service functions needed for the Wumpus World project
"""
def __init__(self, ble=bluetooth.BLE(), name="NANO RP2040"):
# Setup bluetooth low energy communication service
_SERVICE_UUID = bluetooth.UUID(0x1523) # unique service id for the communication
_NanoBot_CHAR_UUID = (bluetooth.UUID(0x1525), _FLAG_WRITE | _FLAG_READ) # characteristic
_NanoBot_SERVICE = (_SERVICE_UUID, (_NanoBot_CHAR_UUID,),) # service to provide the characteristic

self._ble = ble
self._ble.active(True)
self._ble.config(
bond=True,
mitm=True,
le_secure=True,
io=_IO_CAPABILITY_DISPLAY_ONLY
)
self._ble.irq(self._irq)
((self._handle,),) = self._ble.gatts_register_services((_NanoBot_SERVICE,))
self._connections = set()
self._payload = advertising_payload(name=name, services=[_SERVICE_UUID])
self._advertise()
self.value = b'a'

def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)

def _irq(self, event, data):
# handle bluetooth event
if event == _IRQ_CENTRAL_CONNECT:
# handle succesfull connection
conn_handle, addr_type, addr = data
self._connections.add(conn_handle)

elif event == _IRQ_CENTRAL_DISCONNECT:
# handle disconnect
conn_handle, _, _ = data
self._connections.remove(conn_handle)
self._advertise()

elif event == _IRQ_GATTS_WRITE:
conn_handle, value_handle = data
if conn_handle in self._connections:
# Value has been written to the characteristic
self.value = self._ble.gatts_read(value_handle)


def send(self, value):
"""
Send value to the bluetooth characteristic.
:param value: The value to send.
:type value: bytes, int, str
:raise ValueError: If the value is not bytes, int, or str.
"""
if not isinstance(value, bytes):
if isinstance(value, int):
value = value.to_bytes(1, "big")
elif isinstance(value, str):
value = value.encode('utf-8')
else:
raise ValueError("send value should be type bytes, int, or string")
self.value = value
self._ble.gatts_write(self._handle, value)

def read(self, as_type="bytes"):
"""
Return the current value of the bluetooth characteristic, or None if an error occurred.
:param as_type: The type to return the value as. Must be one of 'bytes', 'str', or 'int'.
:type as_type: str
:return: The value of the characteristic.
:rtype: bytes, str, int, None
:raise ValueError: If as_type is not 'bytes', 'str', or 'int'.
"""
value = self.value # try using the last value written to characteristic
try:
if as_type == "bytes":
return value
elif as_type == "str":
return value.decode("utf-8")
elif as_type == "int":
print(f'read {value}')
return int.from_bytes(value, "big")
except Exception as e:
return None

raise ValueError("as_type must be one of 'bytes', 'str', or 'int'")
30 changes: 30 additions & 0 deletions _sources/bluetooth.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Bluetooth
=========
Using Bluetooth Low Energy (BLE) to communicate with the NanoNav.

Quick Example
-------------

.. code-block:: python
from nanonav import BLE
# Create a Bluetooth object
ble = BLE(name="NanoNav")
ble.send(43)
response = ble.read()
Usage
-----
.. autoclass:: nanonav.BLE
:members:
.. automethod:: __init__


Connecting from Mobile
----------------------

Various mobile apps are available for communicating with Bluetooth Low Energy. We recommend LightBlue which is available for both iOS and Android.
TODO: Add screenshots and instructions for connecting to the NanoNav using LightBlue.

19 changes: 19 additions & 0 deletions _sources/index.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.. NanoNav documentation master file, created by
sphinx-quickstart on Mon Apr 15 12:52:38 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to NanoNav's documentation.
===================================

Check out the :doc:`usage` section to get started.

Contents
--------

.. toctree::

usage
bluetooth
movement

12 changes: 12 additions & 0 deletions _sources/movement.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Movement
=========
Working with NanoNav's motors.

Quick Example
-------------

.. code-block:: python
from nanonav import ...
# TODO
22 changes: 22 additions & 0 deletions _sources/usage.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Quick Start
===========

Installation
------------

To use the NanoNav supplementary code, either download :download:`nanonav.py </../../nanonav.py>` to your project directory

Or copy the code below into a file called nanonav.py

.. raw:: html

<div style="max-height: 50vh; overflow-y: scroll;">

.. literalinclude:: /../../nanonav.py
:language: python
:linenos:

.. raw:: html

</div>

Loading

0 comments on commit 41b2a67

Please sign in to comment.