RPI pico code breaks when I delete a comment

May 10, 2024, 22:49

lordchunky3

I have written code for my pico that allows a multiplexing 4x7 segment display i made to run on its own thread. Initially the code was not working so i wrote some test code which i then commented out. I got the main program to work but when I delete the comment the code stops working. After some troubleshooting ive determined thread is likely dying from some sort of memory issue and running collect() in the loop fixes this. Here is the code in its current state. I would love some help with why this happens and ideally a good method of managing memory import machine, utime, _thread from gc import collect, mem_free class Display: def __init__(self, seg_pins, cathode_pins): self.leds = [machine.Pin(pin, machine.Pin.OUT) for pin in seg_pins] self.cathodes = [machine.Pin(pin, machine.Pin.OUT) for pin in cathode_pins] def display_digit(self, digit): self.outputs = { '0' : [1,1,1,1,1,1,0], '1' : [0,1,1,0,0,0,0], '2' : [1,1,0,1,1,0,1], '3' : [1,1,1,1,0,0,1], '4' : [0,1,1,0,0,1,1], '5' : [1,0,1,1,0,1,1], '6' : [1,0,1,1,1,1,1], '7' : [1,1,1,0,0,0,0], '8' : [1,1,1,1,1,1,1], '9' : [1,1,1,1,0,1,1], 'none': [0,0,0,0,0,0,0] } for i in range(len(self.leds)): self.leds[i].value(self.outputs[digit][i]) def display_number(self, number): digits = list(str(number)) utime.sleep(0.00) for i in range(len(digits)): self.display_digit(digits[i]) self.cathodes[i].value(1) utime.sleep(0.002) self.cathodes[i].value(0) self.display_digit("none") display_output = 1234 seg_pins = [20, 19, 26, 27, 28, 22, 21] cathode_pins = [15, 14, 12, 13] display1 = Display(seg_pins, cathode_pins) def persistant_display(): utime.sleep(0.2) global display_output while True: display1.display_number(display_output) collect() _thread.start_new_thread(persistant_display, ()) utime.sleep(0.2) while True: utime.sleep(1) display_output+=1

lordchunky3

This is the code in a working state but using a large docstring to manage the memory (no idea why it works) import machine, utime, _thread ''' ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ''' class Display: def __init__(self, seg_pins, cathode_pins): self.leds = [machine.Pin(pin, machine.Pin.OUT) for pin in seg_pins] self.cathodes = [machine.Pin(pin, machine.Pin.OUT) for pin in cathode_pins] def display_digit(self, digit): self.outputs = { '0' : [1,1,1,1,1,1,0], '1' : [0,1,1,0,0,0,0], '2' : [1,1,0,1,1,0,1], '3' : [1,1,1,1,0,0,1], '4' : [0,1,1,0,0,1,1], '5' : [1,0,1,1,0,1,1], '6' : [1,0,1,1,1,1,1], '7' : [1,1,1,0,0,0,0], '8' : [1,1,1,1,1,1,1], '9' : [1,1,1,1,0,1,1], 'none': [0,0,0,0,0,0,0] } for i in range(len(self.leds)): self.leds[i].value(self.outputs[digit][i]) def display_number(self, number): digits = list(str(number)) utime.sleep(0.00) for i in range(len(digits)): self.display_digit(digits[i]) self.cathodes[i].value(1) utime.sleep(0.002) self.cathodes[i].value(0) self.display_digit("none") display_output = 1234 seg_pins = [20, 19, 26, 27, 28, 22, 21] cathode_pins = [15, 14, 12, 13] display1 = Display(seg_pins, cathode_pins) def persistant_display(): utime.sleep(0.2) global display_output while True: display1.display_number(display_output) _thread.start_new_thread(persistant_display, ()) utime.sleep(0.2) while True: utime.sleep(1) display_output+=1

lordchunky3

here is it working, and not working

lordchunky3

seems to be each call to display is around 5k bytes, and the memory doesn’t clear until less then that

lordchunky3

i tried implementing some code to trigger the collect after a certain thresh hold, it works but the thread still stops, this is what the memory looks like when it is run 181584 176368 171168 165968 160768 155568 150368 145168 139968 134768 129568 124368 119168 113968 108768 103568 98368 93168 87968 82768 77568 72368 67168 61968 56768 51568 46368 41168 185200 180000

lordchunky3

with the end being when the thread stops

lordchunky3

bump <:aaaaa:955586249182298182>