adafruit / Adafruit_CircuitPython_PN532

CircuitPython driver for the PN532 NFC/RFID Breakout and PN532 NFC/RFID Shield
MIT License
89 stars 47 forks source link

MemoryError: memory allocation failed #3

Closed andreykats closed 5 years ago

andreykats commented 5 years ago

main.py output: Traceback (most recent call last): File "main.py", line 15, in File "adafruit_pn532.py", line 53, in MemoryError: memory allocation failed, allocating 80 bytes

Is this library very memory heavy? I can't seem to get it running on my Trinket M0 along with the Adafruit.hid Keyboard library. The simple sample code does seem to work just fine.

ladyada commented 5 years ago

yeah this is a very complex chip. we only tested it on an M4 and raspi - maybe try without HID library and see if towkrs then

ladyada commented 5 years ago

(you're using the .mpy right?)

andreykats commented 5 years ago

Thanks for creating this library by the way. Yea, I'm using the mpy. Its working just fine without the HID library which is good but what I was really hoping to make was a NFC to USB device using your little Trinket M0.

ladyada commented 5 years ago

if you're feeling up for it you might be able to split the pn532.py into 4 pices - the main pn532, one for i2c, one for spi and one for uart...then you would only import one!

caternuson commented 5 years ago

just to verify, *.mpy version of all:

Adafruit CircuitPython 3.0.1 on 2018-08-21; Adafruit Trinket M0 with samd21e18
>>> from adafruit_hid.keyboard import Keyboard
>>> import adafruit_pn532
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_pn532.py", line 468, in <module>
MemoryError: memory allocation failed, allocating 584 bytes
>>> 
caternuson commented 5 years ago

just documenting that this also doesn't work:

Adafruit CircuitPython 3.0.1 on 2018-08-21; Adafruit Trinket M0 with samd21e18
>>> from adafruit_hid.keyboard import Keyboard
>>> from adafruit_pn532 import PN532_I2C
>>> import board, busio
>>> i2c = busio.I2C(board.SCL, board.SDA)
MemoryError: memory allocation failed, allocating 384 bytes
>>> 

was worth a try though.

ladyada commented 5 years ago

chekcin, you split up the files?

caternuson commented 5 years ago

yep. here you go - #6

here's a quick I2C test which includes the HID import to verify it now works:

import board, busio
from adafruit_hid.keyboard import Keyboard
from adafruit_pn532.i2c import PN532_I2C

i2c = busio.I2C(board.SCL, board.SDA)

pn532 = PN532_I2C(i2c)

pn532.SAM_configuration()

print('Waiting for RFID/NFC card...')
while True:
    # Check if a card is available to read
    uid = pn532.read_passive_target(timeout=0.5)
    print('.', end="", flush=True)
    # Try again if no card is available.
    if uid is None:
        continue
    print('Found card with UID:', [hex(i) for i in uid])

I mpy-cross'd everything before copying over to CP board. Tested on Trinket M0:

Adafruit CircuitPython 3.0.1 on 2018-08-21; Adafruit Trinket M0 with samd21e18
>>> import pn532_i2c_test
Waiting for RFID/NFC card...
....Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']
.Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']
.Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']
.Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']
ladyada commented 5 years ago

@andreykats wanna try it?

caternuson commented 5 years ago

@andreykats Yes! Please do. If you need help figuring out how to mpy-cross the *.py files, let us know.

andreykats commented 5 years ago

Yes! @caternuson What do I need to do? I tried splitting it up myself but had some errors during my build.

caternuson commented 5 years ago

To get the files, clone the repo from here: https://github.com/caternuson/Adafruit_CircuitPython_PN532 and then switch to the branch with the changes: git checkout iss3_refactor

Then, get a precompiled exectuable of mpy-cross from here: https://github.com/adafruit/circuitpython/releases/ Get the one for your operating system.

Generate a .mpy for each of the .py files by running mpy-cross. For example: mpy-cross adafruit_pn532.py Do the same thing for the other .py files.

Delete the existing library file from your CP board - CIRCUITPYTHON/lib/adafruit_pn532.mpy

Create a new folder on your CP board - CIRCUITPYTHON/lib/adafruit_pn532

Put all of the .mpy files in there. EDIT - also add a file called __init__.py in that same folder. It can be an empty file.

Then you should be ready to test!

andreykats commented 5 years ago

@caternuson Sorry, this is a little unfamiliar to me. I download the mpy-cross for my os and tried run ./mpy-cross adafruit_pn532.py and mpy-cross adafruit_pn532.py but getting "command not found". Am I missing a step?

caternuson commented 5 years ago

It sounds like it's not finding the mpy-cross program. What OS are you on?

andreykats commented 5 years ago

I'm on the latest macOS. I'm kind of surprised that ./mpy-cross adafruit_pn532.py since that file is sitting alongside adafruit_pn532.py.

caternuson commented 5 years ago

Did you rename the file? It looks like it downloads as mpy-cross-3.x-macos-high-sierra. You may also need to change it to be executable:

chmod +x mpy-cross-3.x-macos-high-sierra
andreykats commented 5 years ago

@caternuson Yes! that worked. Good old chmod. So I was able to replicate your results in REPL using your code snippet from above but adding the same code to main.py still gives me memory allocation failed.

main.py output: Traceback (most recent call last): File "main.py", line 3, in File "i2c.py", line 44, in File "adafruit_pn532.py", line 141, in MemoryError: memory allocation failed, allocating 376 bytes

Can you please check on your end?

caternuson commented 5 years ago

Hmmm. It worked for me saving the same code to main.py and running it that way:

Adafruit CircuitPython 3.0.1 on 2018-08-21; Adafruit Trinket M0 with samd21e18
>>>
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
Waiting for RFID/NFC card...
.Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']
.Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']
.Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']
.Found card with UID: ['0xf1', '0x4a', '0xb5', '0xfb']

So you typed in all the commands in the REPL and it worked. But then tried saving it to main.py and got the error?

EDIT - the contents of my main.py are the same as the code listing above for the "quick I2C test".

caternuson commented 5 years ago

@ladyada Finally getting back to this. Do you still want to go this refactor route? If so I'll work on changing the examples to match and make Travis happy so #6 can be merged.

ladyada commented 5 years ago

yep im into it! want me to test?

caternuson commented 5 years ago

that'd be super!

ladyada commented 5 years ago

should be resolved in latest commit, zoom!

ladyada commented 5 years ago

& released! check v2.0.0 and reopen if still an issue