How to save file in flash memory ?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
P@T
Posts: 33
Joined: Tue Nov 06, 2018 2:37 pm

How to save file in flash memory ?

Post by P@T » Tue Apr 07, 2020 10:43 am

Hi all,

In my appication modbus slave, i have a dictionnary in a file : registre.py

Code: Select all

import registre as Reg
In my principal program, i have a function (doc string in french sorry) :

Code: Select all

def mise_a_jour_des_donnees(adresse_voulue: int, quantite_voulue: int ,donnee: list) -> bool:
    """
    Cette fonction permet d'ecrire une liste de donnee à l'adresse_voulue 
    seulement si la taille de la liste de donnee correspond a la quantite de 
    donnee que l'on veut ecrire. Sinon la fonction renvoie False.
    zip permet d'avoir deux range differents mais simultanement.
    """
    if len(donnee) == (adresse_voulue+quantite_voulue - adresse_voulue):
        for adresse_registre, adresse_data in zip(range(adresse_voulue,adresse_voulue+quantite_voulue), range(len(donnee))):
            Reg.registre[adresse_registre] = donnee[adresse_data]
            
        return True
    
    else:
        return False
the key = address register
the value = value in the register
donnee = writeable data list

My function is OK, how to save my dictionnary in flash before the "retrun True" ?

Thank you

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to save file in flash memory ?

Post by pythoncoder » Tue Apr 07, 2020 11:42 am

See the ujson library http://docs.micropython.org/en/latest/l ... ujson.html

Code: Select all

import ujson
d = {1:'one', 2:'two'}
with open('my_file', 'w') as f:
    ujson.dump(d, f)
Loading is similar.
Peter Hinch
Index to my micropython libraries.

P@T
Posts: 33
Joined: Tue Nov 06, 2018 2:37 pm

Re: How to save file in flash memory ?

Post by P@T » Tue Apr 07, 2020 6:42 pm

Ok thank you peter.

I will to try

Post Reply