dbisu / pico-ducky

Create a USB Rubber Ducky like device using a Raspberry PI Pico
GNU General Public License v2.0
2.42k stars 440 forks source link

Saving some memory #55

Closed pvhil closed 2 years ago

pvhil commented 2 years ago

Hi! First of all, I love this project.

But I had some memory problems with some smaller circuitpython boards and biig payload files.

Then i saw this code below:

f = open(duckyScriptPath,"r",encoding='utf-8')
    previousLine = ""
    duckyScript = f.readlines()
    for line in duckyScript:
        line = line.rstrip()
        if(line[0:6] == "REPEAT"):
            for i in range(int(line[7:])):
                #repeat the last command
                parseLine(previousLine)
                time.sleep(float(defaultDelay)/1000)
        else:
            parseLine(line)
            previousLine = line
        time.sleep(float(defaultDelay)/1000)

It is opening the file and it is saving it as the variable f, then its making a list from f into duckyScript variable. I thought because of creating the list, it is using double the memory, so i thought about a method without creating a list that should reduce memory use.

f = open(duckyScriptPath,"r",encoding='utf-8')
previousLine = ""
while 1:
    line = f.readline().replace("\n","").replace("\r","")
    if line == "":
        break
    print(line)
    if(line[0:6] == "REPEAT"):
        for i in range(int(line[7:])):
            #repeat the last command
            parseLine(previousLine)
            time.sleep(float(defaultDelay)/1000)
    else:
        parseLine(line)
        previousLine = line
    time.sleep(float(defaultDelay)/1000)

It is directly reading from the file (f)

dbisu commented 2 years ago

Thanks for the PR. I'm curious to which other boards you have this code. I did some digging and I think a better way to handle the parsing would be to do something like this:

f = open(duckyScriptPath,"r",encoding='utf-8')  
previousLine = ""
for line in f:
    line = line.rstrip()
    print(line)`

or if that is too much memory:

line = f.readline()
    while line:
        line = f.readline().rstrip()
        print(line)

If you could update the PR with this change, I should be able to get it merged in soon. Thanks.

pvhil commented 2 years ago

I love to use the trinket M0 because it fits perfectly in a usb drive.

Will have a look later. Thanks :)

pvhil commented 2 years ago

ok that should work

dbisu commented 2 years ago

Thanks for the changes. I'm giving a talk at BSidesIowa next month. Would you mind if I mentioned your trinket port/fork? Thanks.

pvhil commented 2 years ago

Wow Thats cool! Yeah I do not mind you can mention it.

pvhil commented 2 years ago

Hi again, Im just wondering if theres a video of your talk at bsidesiowa? Would love to watch it :)

dbisu commented 2 years ago

It was recorded. The BSidesIowa people said the videos should be up on their YouTube channel in the next few weeks.