Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eating up the memory #5

Closed
n0n3nt1ty opened this issue Apr 6, 2021 · 3 comments
Closed

Eating up the memory #5

n0n3nt1ty opened this issue Apr 6, 2021 · 3 comments

Comments

@n0n3nt1ty
Copy link
Contributor

n0n3nt1ty commented Apr 6, 2021

Hello! I used your library with my Pico to my alarm project. I used the LCD to type in the PIN needed to arm and disarm the alarm. My code utilises a while loop to await input from the keypad and then print a typed number on the LCD. I noticed that with every character printed a lot of memory of my Pico is being consumed. Apart from printstr() function I also noticed that lcd.clear() consumes a lot of memory too. When it's executed in a loop it takes all the memory from my Pico and the microcontroller freezes. I got rid of lcd.clear() function in my loop, but eveytime I printed a character on the LCD it still took memory. What I did was adding GarbageCollector library and adding a line with gc.collect() at the end of every function in pico_i2c_lcd.py. This really helped! Now any function from your library barely takes any memory off my microcontroller. I think you should add those lines to your code.

`import utime

from lcd_api import LcdApi
from machine import I2C
import gc

MASK_RS = 0x01
MASK_RW = 0x02
MASK_E = 0x04

SHIFT_BACKLIGHT = 3
SHIFT_DATA = 4

class I2cLcd(LcdApi):

def __init__(self, i2c, i2c_addr, num_lines, num_columns):
    self.i2c = i2c
    self.i2c_addr = i2c_addr
    self.i2c.writeto(self.i2c_addr, bytes([0]))
    utime.sleep_ms(20)   # Allow LCD time to powerup
    self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
    utime.sleep_ms(5)    # Need to delay at least 4.1 msec
    self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
    utime.sleep_ms(1)
    self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
    utime.sleep_ms(1)
    self.hal_write_init_nibble(self.LCD_FUNCTION)
    utime.sleep_ms(1)
    LcdApi.__init__(self, num_lines, num_columns)
    cmd = self.LCD_FUNCTION
    if num_lines > 1:
        cmd |= self.LCD_FUNCTION_2LINES
    self.hal_write_command(cmd)
    gc.collect()

def hal_write_init_nibble(self, nibble):
    byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
    self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
    self.i2c.writeto(self.i2c_addr, bytes([byte]))
    gc.collect()
    
def hal_backlight_on(self):
    self.i2c.writeto(self.i2c_addr, bytes([1 << SHIFT_BACKLIGHT]))
    gc.collect()
    
def hal_backlight_off(self):
    self.i2c.writeto(self.i2c_addr, bytes([0]))
    gc.collect()
    
def hal_write_command(self, cmd):
    byte = ((self.backlight << SHIFT_BACKLIGHT) |
            (((cmd >> 4) & 0x0f) << SHIFT_DATA))
    self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
    self.i2c.writeto(self.i2c_addr, bytes([byte]))
    byte = ((self.backlight << SHIFT_BACKLIGHT) |
            ((cmd & 0x0f) << SHIFT_DATA))
    self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
    self.i2c.writeto(self.i2c_addr, bytes([byte]))
    if cmd <= 3:
        utime.sleep_ms(5)
    gc.collect()

def hal_write_data(self, data):
    byte = (MASK_RS |
            (self.backlight << SHIFT_BACKLIGHT) |
            (((data >> 4) & 0x0f) << SHIFT_DATA))
    self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
    self.i2c.writeto(self.i2c_addr, bytes([byte]))
    byte = (MASK_RS |
            (self.backlight << SHIFT_BACKLIGHT) |
            ((data & 0x0f) << SHIFT_DATA))      
    self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
    self.i2c.writeto(self.i2c_addr, bytes([byte]))
    gc.collect()

`

@T-622
Copy link
Owner

T-622 commented Apr 6, 2021

Please submit this as a pull request so I can merge and keep track of code changes

@T-622 T-622 closed this as completed Apr 6, 2021
@T-622
Copy link
Owner

T-622 commented Apr 6, 2021

Very good work by the way!

@n0n3nt1ty
Copy link
Contributor Author

Very good work by the way!

You're welcome! I'm sorry but I'm new to GitHub and don't know how to create that Pull request. Could you help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants