JanLunge / pog

A Kmk firmware flashing and configuration tool
https://pog.heaper.de/
MIT License
323 stars 20 forks source link

RGB LED - MemoryError: memory allocation failed #24

Open MangoPG opened 3 months ago

MangoPG commented 3 months ago

Howdy,

Stumbled on this looking into getting a keyboard that is ergo to me etc. Fell into the rabbit hole of the wonderous world of custom keyboards, anyways enough of my life story - just know I have very little coding knowledge and enough knowledge with a soldering iron to be dangerous. So off to prototype.

I am wondering if I misunderstood - this should work on the Pico W, correct?

I have the following testing setup:

On the Cytron, there is a neopixel on Pin GP28, I went to the RGB section of POG - enabled and changed to 1 led and the pin information to "board.GP28".

I added the neopixel.mpy file to the lib as requested in POG, hit save on POG and got the following error (shown in Mu Serial).

Traceback (most recent call last): File "code.py", line 17, in <module> File "kb.py", line 97, in __init__ MemoryError: memory allocation failed, allocating 464 bytes

I am fully aware, I may have misunderstood steps or the whole thing. Do let me know if you want me to do anything different or test anything?

Thanks,

Mango

JanLunge commented 3 months ago

Yes all you have done is correct so far, maybe check if the circuit python version matches the one of the neopixel.mpy v9 is not backwards compatible.

MangoPG commented 3 months ago

Hi Jan,

Thanks for getting back to me, good to know I had not got confused ๐Ÿ˜„

Ok so the neopixel.mpy is the latest one, if I am reading Github right - for clarity so you can see if I am correct, the folder name is : \adafruit-circuitpython-neopixel-9.x-mpy-6.3.11\lib

I can confirm in the serial / REPL the following is shown: Adafruit CircuitPython 9.0.5 on 2024-05-22; Raspberry Pi Pico W with rp2040

Any ideas what I can do next?

Would love to have some RGB to indicate layers on my finished board.

Thanks for all your hard work!

Mango

JanLunge commented 3 months ago

That looks alright, i just heard today that there are pi picos with less memory. For those you can try installing only the compiled version of kmk to save on memory but that is something i have not tried before and am also not sure if it actually solves the issue

MangoPG commented 3 months ago

Oh wow, really? Do you have link to this information (I did try googling, but could not find any information on this), I am more than happy to purchase a new controller for this project - just want to make sure I don't accidently purchase the wrong one in the future.

I have a spare seeed thingy that uses the rp2040 chip, so I can try that later.

JanLunge commented 3 months ago

I just looked up a few things and it seem only the flash size is variable, all controllers come with the same 256kb memory, so it should work fine on your existing controller. there could be a bug somehow where the json file could be too large and when reading it into memory it is overallocating. could you check your pog.json file if there is excessive data in it? otherwise it would be some other bug that is hard to diagnose like this

MangoPG commented 3 months ago

Thank you so much Jan for continuing to try and help me! ๐Ÿ˜„

So I am unsure what is too large for a JSON file, but it is 130 lines and 3KB in size according to win10 explorer.

I had a thought which I have not tested, I used the CoordMap section then "FLASH COORDMAP FINDER TO KEYBOARD", pressed my buttons and then after testing they worked in keymap turned on the RGB.

I am wondering should I take note of the files that are created by the "FLASH COORDMAP FINDER TO KEYBOARD" button, and delete them after creating the CoordMap? Could they be taking up space at all?

Sorry for my ignorance, happy to testing or supply info as needed!

Mango

JanLunge commented 3 months ago

All files can be left on there, this step is just there to write the pog files with a flag in the json to start in coordmap testing mode. After you save again this flag is removed and it starts normally. Does the keyboard work if you disable rgb?

MangoPG commented 3 months ago

Ah cool, I won't worry about doing that test.

Yes works fine! Just enabling the RGB that causes the memory problem.

MangoPG commented 3 months ago

Howdy,

I used the SEEED board I happened to have for another project and it worked brilliantly. 144 LED's connected and no ram issues at all.

I will see if I can use another PI Pico board from another project and see if there is an error on the board I was using and update you.

Thanks Mango

MangoPG commented 3 months ago

Sad news I am afraid, I actually used a Pico W that was/will be again the controller for my Desk RGB's.

As you can see I get the same error:

  File "code.py", line 17, in <module>
  File "kb.py", line 97, in __init__
MemoryError: memory allocation failed, allocating 296 bytes

So I can not get this to work on a Raspberry Pi Pico W, I feel that it is unlikely to be hardware at this point, but obviously can't rule that out for 100%.

Let me know if there is anything I can do to help troubleshoot this in the future.

I do have a Pi Pico that is not a W variant somewhere, I could test that if you think that would be helpful? Maybe the wifi chip is causing issues?

parallacks commented 3 months ago

If you have the exact same setup on a SEEED RP2040 and it works, it seems much more likely that its an issue specifically with the Pico W (aka hardware). Either way, this isn't really a POG issue but rather something you should search for or raise in KMK's github or Zulip community.

MangoPG commented 3 months ago

Hi @parallacks sorry for the delay in doing a test to confirm what you said. Unfortunately I can't close this issue with your suggestion just yet, as I did the following:

I wiped the Pi Pico W, installed circuit python and then installed KMK and manually implemented RGB, through the main.py file. - it worked and did not have any memory error.

The code I used is incredibly basic, but it does in my understanding prove that KMK works on a Pi Pico W, but not using POG.


from kmk.kmk_keyboard import KMKKeyboard
from kmk.keys import KC
from kmk.scanners import DiodeOrientation
import busio as io
from kmk.extensions.rgb import RGB, AnimationModes

keyboard = KMKKeyboard()

keyboard.col_pins = (board.GP0,)
keyboard.row_pins = (board.GP1,)
keyboard.diode_orientation = DiodeOrientation.COL2ROW

rgb = RGB(pixel_pin=board.GP28,
        num_pixels=1,
        #pixels=pixels,
        hue_default=0,
        sat_default=255,
        val_default=255,
        hue_step=1,
        sat_step=0,
        val_step=0,
        animation_speed=0.5,
        animation_mode=AnimationModes.SWIRL,
        )

keyboard.extensions.append(rgb)

keyboard.keymap = [
    [KC.A,]
]

if __name__ == '__main__':
    keyboard.go()

Not sure what next to do, would love more suggestions ๐Ÿ‘

JanLunge commented 3 months ago

Its very possible that the bare bones config you have just saves a lot on resources and will fit on your controller, i really don't know how it can be different to the pi pico but you can try enabling the extensions used in the kb.py file one by one, in the code.py there are feature flags calling the constructor that are set in the pog.json (at least in the new version) you can remove one feature from the list at a time and check at which point it starts to work.

Oh I just googled a bit and it turns out the pico w has less memory available because of the software for the wifi chip somewhere about 20-30kb (of the 256kb) so it is possible that you just need to remove some feature(s) that you dont use to save this amount. You could check how much memory is free with you simple code just to see how much needs to be saved. You could likely also use the kmk firmware precompiled which reduces its memory footprint as well (the mpy files)

awaytome commented 2 months ago

I can confirm that I am running into the same issue when trying to enable the RGB extension in POG. I've included the trace back below. I am running an Adafruit KB2040 on a Reviung41 keyboard.

Traceback (most recent call last): File "code.py", line 17, in <module> File "kb.py", line 108, in __init__ MemoryError: memory allocation failed, allocating 224 bytes

I also confirmed that line 108 in kb.py is the RGB extension being loaded.