/ README.rst
README.rst
1 Introduction 2 ============ 3 4 .. image:: https://readthedocs.org/projects/adafruit-circuitpython-bluefruitspi/badge/?version=latest 5 :target: https://circuitpython.readthedocs.io/projects/bluefruitspi/en/latest/ 6 :alt: Documentation Status 7 8 .. image:: https://img.shields.io/discord/327254708534116352.svg 9 :target: https://adafru.it/discord 10 :alt: Discord 11 12 .. image:: https://github.com/adafruit/Adafruit_CircuitPython_BluefruitSPI/workflows/Build%20CI/badge.svg 13 :target: https://github.com/adafruit/Adafruit_CircuitPython_BluefruitSPI/actions/ 14 :alt: Build Status 15 16 Helper class to work with the Adafruit Bluefruit LE SPI Friend. 17 18 Dependencies 19 ============= 20 This driver depends on: 21 22 * `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_ 23 * `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_ 24 25 Please ensure all dependencies are available on the CircuitPython filesystem. 26 This is easily achieved by downloading 27 `the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_. 28 29 Installing from PyPI 30 ==================== 31 32 On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from 33 PyPI <https://pypi.org/project/adafruit-circuitpython-bluefruitspi/>`_. To install for current user: 34 35 .. code-block:: shell 36 37 pip3 install adafruit-circuitpython-bluefruitspi 38 39 To install system-wide (this may be required in some cases): 40 41 .. code-block:: shell 42 43 sudo pip3 install adafruit-circuitpython-bluefruitspi 44 45 To install in a virtual environment in your current project: 46 47 .. code-block:: shell 48 49 mkdir project-name && cd project-name 50 python3 -m venv .env 51 source .env/bin/activate 52 pip3 install adafruit-circuitpython-bluefruitspi 53 54 Usage Example 55 ============= 56 57 .. code-block:: python 58 59 # A simple echo test for the Feather M0 Bluefruit 60 # Sets the name, then echo's all RX'd data with a reversed packet 61 62 import time 63 import busio 64 import board 65 from digitalio import DigitalInOut 66 from adafruit_bluefruitspi import BluefruitSPI 67 68 spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) 69 cs = DigitalInOut(board.D8) 70 irq = DigitalInOut(board.D7) 71 rst = DigitalInOut(board.D4) 72 bluefruit = BluefruitSPI(spi_bus, cs, irq, rst, debug=False) 73 74 # Initialize the device and perform a factory reset 75 print("Initializing the Bluefruit LE SPI Friend module") 76 bluefruit.init() 77 bluefruit.command_check_OK(b'AT+FACTORYRESET', delay=1) 78 79 # Print the response to 'ATI' (info request) as a string 80 print(str(bluefruit.command_check_OK(b'ATI'), 'utf-8')) 81 82 # Change advertised name 83 bluefruit.command_check_OK(b'AT+GAPDEVNAME=BlinkaBLE') 84 85 while True: 86 print("Waiting for a connection to Bluefruit LE Connect ...") 87 # Wait for a connection ... 88 dotcount = 0 89 while not bluefruit.connected: 90 print(".", end="") 91 dotcount = (dotcount + 1) % 80 92 if dotcount == 79: 93 print("") 94 time.sleep(0.5) 95 96 # Once connected, check for incoming BLE UART data 97 print("\n *Connected!*") 98 connection_timestamp = time.monotonic() 99 while True: 100 # Check our connection status every 3 seconds 101 if time.monotonic() - connection_timestamp > 3: 102 connection_timestamp = time.monotonic() 103 if not bluefruit.connected: 104 break 105 106 # OK we're still connected, see if we have any data waiting 107 resp = bluefruit.uart_rx() 108 if not resp: 109 continue # nothin' 110 print("Read %d bytes: %s" % (len(resp), resp)) 111 # Now write it! 112 print("Writing reverse...") 113 send = [] 114 for i in range(len(resp), 0, -1): 115 send.append(resp[i-1]) 116 print(bytes(send)) 117 bluefruit.uart_tx(bytes(send)) 118 119 print("Connection lost.") 120 121 Contributing 122 ============ 123 124 Contributions are welcome! Please read our `Code of Conduct 125 <https://github.com/adafruit/Adafruit_CircuitPython_BluefruitSPI/blob/master/CODE_OF_CONDUCT.md>`_ 126 before contributing to help this project stay welcoming. 127 128 Documentation 129 ============= 130 131 For information on building library documentation, please check out `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.