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