Git Product home page Git Product logo

adafruit_circuitpython_bitbangio's Introduction

Introduction

Documentation Status Discord Build Status Code Style: Black

A library for adding bitbang I2C and SPI to CircuitPython without the built-in bitbangio module. The interface is intended to be the same as bitbangio and therefore there is no bit order or chip select functionality. If your board supports bitbangio, it is recommended to use that instead as the timing should be more reliable.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-bitbangio

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-bitbangio

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-bitbangio

Usage Example

"""
This example is for demonstrating how to retrieving the
board ID from a BME280, which is stored in register 0xD0.
It should return a result of [96]
"""

import board
import digitalio
import adafruit_bitbangio as bitbangio

# Change these to the actual connections
SCLK_PIN = board.D6
MOSI_PIN = board.D17
MISO_PIN = board.D18
CS_PIN = board.D5

cs = digitalio.DigitalInOut(CS_PIN)
cs.switch_to_output(value=True)

spi = bitbangio.SPI(SCLK_PIN, MOSI=MOSI_PIN, MISO=MISO_PIN)
cs.value = 0
while not spi.try_lock():
    pass
spi.write([0xD0])
data = [0x00]
spi.readinto(data)
spi.unlock()
cs.value = 1
print("Result is {}".format(data))

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

adafruit_circuitpython_bitbangio's People

Contributors

alustig3 avatar dhalbert avatar evaherrada avatar foamyguy avatar kattni avatar ladyada avatar linkeor avatar makermelissa avatar sommersoft avatar tannewt avatar tcfranks avatar tekktrik avatar tinue avatar wolfmanjm avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_circuitpython_bitbangio's Issues

Add I2C to Library

I2C would be a great addition to this library, so just creating an issue as a "To Do".

Missing Type Annotations

There are missing type annotations for some functions in this library.

The typing module does not exist on CircuitPython devices so the import needs to be wrapped in try/except to catch the error for missing import. There is an example of how that is done here:

try:
    from typing import List, Tuple
except ImportError:
    pass

Once imported the typing annotations for the argument type(s), and return type(s) can be added to the function signature. Here is an example of a function that has had this done already:

def wrap_text_to_pixels(
    string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
) -> List[str]:

If you are new to Git or Github we have a guide about contributing to our projects here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

There is also a guide that covers our CI utilities and how to run them locally to ensure they will pass in Github Actions here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code In particular the pages: Sharing docs on ReadTheDocs and Check your code with pre-commit contain the tools to install and commands to run locally to run the checks.

If you are attempting to resolve this issue and need help, you can post a comment on this issue and tag both @FoamyGuy and @kattni or reach out to us on Discord: https://adafru.it/discord in the #circuitpython-dev channel.

The following locations are reported by mypy to be missing type annotations:

  • adafruit_bitbangio.py:65
  • adafruit_bitbangio.py:79
  • adafruit_bitbangio.py:117
  • adafruit_bitbangio.py:124
  • adafruit_bitbangio.py:134
  • adafruit_bitbangio.py:199
  • adafruit_bitbangio.py:225
  • adafruit_bitbangio.py:249
  • adafruit_bitbangio.py:255
  • adafruit_bitbangio.py:264
  • adafruit_bitbangio.py:278
  • adafruit_bitbangio.py:315
  • adafruit_bitbangio.py:334
  • adafruit_bitbangio.py:340
  • adafruit_bitbangio.py:372
  • adafruit_bitbangio.py:418

Fix docs link

This needs to be added. It's been long enough that I forgot how to do it, so I'll create an issue for now.

Clock Stretching not currently supported

While testing another issue came across that this library will not work if your I2C is created with bitbangio.I2C. Tested on the feather M4 express and Unexpected Maker FeatherS2

Pylint pinned to 1.9.2

Pylint is still pinned to 1.9.2. This needs to be updated to latest (not pinned) and any necessary changes made to the code.

@makermelissa I'm guessing you cookiecuttered this before we made the Pylint change. If you get a chance, please update it. Thanks!

Bitbang SPI phase=1 lowest bit is wrong

CircuitPython version

Adafruit CircuitPython 6.1.0 on 2021-01-21; Adafruit QT Py M0 with samd21e18 also
Adafruit CircuitPython 7.0.0-beta.0-161-g65753a1c2 on 2021-09-01; with samd51j19

Code/REPL

# 09/03/2021
# looses both least significant bits
# tested on QT Py and Metro_M4_Express

import board
import digitalio
import adafruit_bitbangio as bitbangio


nEN = digitalio.DigitalInOut(board.A1)    
nEN.direction = digitalio.Direction.OUTPUT

spi = bitbangio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

def TestSPI():  
    select = nEN
    select.value = True
    while not spi.try_lock():
        pass
    spi.configure(baudrate=100000, polarity=0, phase=1, bits=8 )
    bytes_read = bytearray(4)
    select.value = False
    bytes_read = bytearray([0x33])  # changes 0x33 to 0x32 
    spi.write(bytes_read)
    bytes_read = bytearray([0x0b])  # changes 0x0b to 0x0a
    spi.write(bytes_read)
    select.value = True
    spi.unlock()

Behavior

image

Description

No response

Additional information

This works correctly:
from adafruit_bus_device.spi_device import SPIDevice

Leaves pins in use after deinit()

Tested on a Kaluga with CircuitPython 6.1 beta version, built locally with DEBUG=1,

>>> import adafruit_bitbangio as busio
>>> import board
>>> bus = busio.I2C(board.IO10, board.IO11); bus.try_lock(); bus.scan(); bus.deinit()
True
[0, 11, 112]
>>> bus = busio.I2C(board.IO10, board.IO11); bus.try_lock(); bus.scan(); bus.deinit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_bitbangio.py", line 103, in __init__
ValueError: IO10 in use

Expected behavior: The bus object can be recreated

Actual behavior: The bus object cannot be recreated

Add OneWire Support

Will Blinka ever support OneWire like the main fork of Circuit Python? I have a DS18B20 temp sensor and many hours have gone into trying to get it to work. I'm down to writing lots of code (bitbang?) from scratch at this point. It would be nice if busio or bitbingio modules had the OneWire code in them.

When running the Usage Example, I get "RuntimeError: First call try_lock()"

On a Raspberry Pi Zero W Rev 1.1, running Raspbian Buster with kernel Linux 4.19.97+, I get the error "RuntimeError: First call try_lock()" when I run the "Usage Example" of the readme file. This is with Python 3.7.3.

While I can do this as suggested, I am wondering if there is an error somewhere. Raspbian was freshly installed and no other libraries are present, so the pins that the sample uses should not be in use. I also tried with the pins that I really need bitbangio.SPI(clock=board.D23, MOSI=board.D24) but I get the same result.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.