Giter Site home page Giter Site logo

Comments (2)

phrogger avatar phrogger commented on June 12, 2024

------------------------------------ CLIP --------------------------------------
#!/usr/bin/python

Demonstration of bug in Adafruit_I2C.reverseByteOrder()

from Adafruit_I2C import *

Initialize the class; address and bus number must be that of a valid I2C device

interface = Adafruit_I2C(0x18 , busnum=2)

for i in range( 0x03FD, 0x0404):
y = ((i << 8 & 0xFF00) | (i >> 8 & 0X00FF)) # Reverse 16 bit byte order
z = interface.reverseByteOrder(y)
print 'Value = %04X, Input = %04X, Result = %04X' % (i, y, z)

---------------------------------- END CLIP -------------------------------------

Resulting demonstration

Result should equal Value.

Input is the byte reversal of Value, and is correct by inspection

root@BBB1:~# python bugdemo.py
Value = 03FD, Input = FD03, Result = 03FD
Value = 03FE, Input = FE03, Result = 03FE
Value = 03FF, Input = FF03, Result = 03FF
Value = 0400, Input = 0004, Result = 0004 # <<<<<<<<<< FAIL
Value = 0401, Input = 0104, Result = 0401
Value = 0402, Input = 0204, Result = 0402
Value = 0403, Input = 0304, Result = 0403


Suggested fix:
I think it is impossible to make Adafruit_I2C.reverseByteOrder() work as written.
It is trying to determine the size of a value by detecting the presence of "ones"
to mark the first valid byte.
This fails when a leading valid byte is 0x00.

I suggest that Adafruit_I2C.reverseByteOrder() be replaced by two separate methods:

def reverseByteOrder16(self, data):
    "Reverses the byte order of a 16-bit value"
    val = ((data << 8 & 0xFF00) | (data >> 8 & 0X00FF))
    return val

def reverseByteOrder32(self, data):
    "Reverses the byte order of a 32-bit value"
    val = ((data << 24 & 0xFF000000) | (data << 8 & 0xFF0000) | (data >> 8 & 0xFF00) | (data >> 24 & 0xFF))
    return val

from adafruit-raspberry-pi-python-code.

ladyada avatar ladyada commented on June 12, 2024

Thank you for the Issue!
This library has been deprecated in favor of our python3 Blinka library. We have replaced all of the libraries that use this repo with CircuitPython libraries that are Python3 compatible, and support a wide variety of single board/linux computers!

Visit https://circuitpython.org/blinka for more information

CircuitPython has support for almost 200 different drivers, and a as well as FT232H support for Mac/Win/Linux!

from adafruit-raspberry-pi-python-code.

Related Issues (20)

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.