0

Memory Allocation Error - M0 Express, HID, RTC
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Memory Allocation Error - M0 Express, HID, RTC

by JBurns on Tue Dec 04, 2018 12:15 pm

Hello-

I am trying to create an HID keyboard using a Feather M0 Express and an Adalogger FeatherWing. The RTC is being used to date stamp a number of custom text strings.

I would like to confirm that the error I am seeing is a limitation of the M0 Express. It reads: "MemoryError: memory allocation failed, allocating 776 bytes." The code errors when attempting to import the Keyboard module.

The following libraries are being imported:

Code: Select all | TOGGLE FULL SIZE
import time
import busio
import adfruit_pcf8523
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
import board


Is there any way to make this code more efficient so that it can run on a M0? If not, would an M4 express be able to handle these libraries?

For reference, here is the full code as it sits, have not implemented date stamps into text string yet:

Code: Select all | TOGGLE FULL SIZE
mport time
import busio
import adafruit_pcf8523
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
import board

#Initiate RTC
myI2C = busio.I2C(board.SCL, board.SCA)
rtc = adafruit_pcf8523.PCF8523(myI2C)

days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

if True: #Change to True if you want to write the time!
    #                   (year, mon, date, hour, min, sec, wday, yday, isdst)
    t = time.struct_time((2018, 12, 4, 8, 50, 0, 3, -1, -1))
    # you must set year, mon, date, hour, min, sec and weekday
    #yearday is not supported, isdst can be set but we dont do anything with it at this time
    print("Setting time to:", t)
    rtc.datetime = t
    print()
   
while True:
    t = rtc.datetime
    #print(t) #uncomment for debugging
   
    print ("The date is %s %d/%d/%d" % (days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year))
    print ("The time is %d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec))
   
    time.sleep(1)



# The button pins we'll use, each will have an internal pullup
buttonpins = [board.A0, board.A1, board.A2, board.A3, board.A4, board.A5, board.D5, board.D6]
# The keycode sent for each button, will be paired with a control key
buttonkeys = ["DATE.", "JB/MQ DATE.", "The DHR has been reviewed...", "No DHR review has been conducted as no adverse event was reported. JB/MQ DATE.", "The device arrived in BNP on DATE. JB/MQ DATE.", "Product not returned...", "The investigation activity has been completed. Please review and process complaint accordingly. Thank you. JB/MQ DATE.", Keycode.L]
#controlkey = Keycode.COMMAND
kbd = Keyboard()
layout = KeyboardLayoutUS(kbd)
# our array of button objects
buttons = []

# make all pin objects, make them inputs w/pullups
for pin in buttonpins:
    button = digitalio.DigitalInOut(pin)
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP
    buttons.append(button)

print("Waiting for button presses")
     
while True:
    # check each button
    for button in buttons:
        if not button.value:  # pressed?
            i = buttons.index(button)
     
            print("Button #%d Pressed" % i)
     
     
            while not button.value:
                pass  # wait for it to be released!
            # type the keycode!
            k = buttonkeys[i]  # get the corresp. keycode
            if isinstance(k, str):
                layout.write(k)
               
            else:
                kbd.press(controlkey, k)  # press...
                kbd.release_all()  # release!
 
    time.sleep(0.01)

JBurns
 
Posts: 18
Joined: Thu May 26, 2016 2:19 pm

Re: Memory Allocation Error - M0 Express, HID, RTC

by kenidowa on Wed Dec 05, 2018 10:28 pm

Noticed your first line is incorrect at least as shown. You have "mport" instead of import. Maybe just a copy error? As far as memory out problems I have had similar problems with ItsyBitsyM0. Haven't found good solutions.

kenidowa
 
Posts: 23
Joined: Wed Nov 07, 2012 7:50 pm

Re: Memory Allocation Error - M0 Express, HID, RTC

by jerryn on Wed Dec 05, 2018 10:56 pm

Are you using the .mpy versions of the libraries? Not the .py versions. They use a lot less memory when loading. I would expect that an M4 would have no problem, but I'm surprised that the M0 express can't handle this if the .mpy versions are used.
the .mpy versions are in the 3.x or 4.x bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases

jerryn
 
Posts: 820
Joined: Sat Sep 14, 2013 9:05 am

Re: Memory Allocation Error - M0 Express, HID, RTC

by keith204 on Wed Dec 05, 2018 11:03 pm

I received a similar error as well. Just got my two Adafruit ItsyBitsy M0's in the mail this week. Ran the test script: worked. Ran the update to 3x. Now the main.py test script breaks.

keith204
 
Posts: 52
Joined: Tue Mar 26, 2013 1:27 pm

Re: Memory Allocation Error - M0 Express, HID, RTC

by JBurns on Wed Dec 05, 2018 11:25 pm

jerryn wrote:Are you using the .mpy versions of the libraries? Not the .py versions. They use a lot less memory when loading. I would expect that an M4 would have no problem, but I'm surprised that the M0 express can't handle this if the .mpy versions are used.
the .mpy versions are in the 3.x or 4.x bundle https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases


Yes, I updated to latest CircuitPython 3.x release. Also tried 2.x with no luck. Appears they are all mpy versions.

JBurns
 
Posts: 18
Joined: Thu May 26, 2016 2:19 pm

Re: Memory Allocation Error - M0 Express, HID, RTC

by drborst on Fri Dec 07, 2018 8:10 pm

I had the same problem using a M0 express board. I also own an M4 express board so I transferred my M0 problem code to the M4. The error was the almost the same, and oddly, it seems to stay after going back to the original M4 code that worked before.

I was using a flaky USB cable for this test. It kept dropping connection and freezing the Mu editor (I'm getting a new one this weekend), but feels like I corrupted something.

drborst
 
Posts: 3
Joined: Wed Nov 12, 2014 2:43 pm

Re: Memory Allocation Error - M0 Express, HID, RTC

by adafruit2 on Fri Dec 14, 2018 12:22 am

keyboard and RTC are both really 'heavy' libraries - use an M4 for a roomy spacious memory experience!

adafruit2
Site Admin
 
Posts: 17772
Joined: Fri Mar 11, 2005 7:36 pm

Re: Memory Allocation Error - M0 Express, HID, RTC

by JBurns on Fri Dec 14, 2018 12:48 am

adafruit2 wrote:keyboard and RTC are both really 'heavy' libraries - use an M4 for a roomy spacious memory experience!

Switched to an M4 express and now works like a charm!

JBurns
 
Posts: 18
Joined: Thu May 26, 2016 2:19 pm

Please be positive and constructive with your questions and comments.