pikasTech / PikaPython

An ultra-lightweight Python interpreter that runs with only 4KB of RAM, zero dependencies. It is ready to use out of the box without any configuration required and easy to extend with C. Similar project: MicroPython, JerryScript.
http://pikapython.com/
MIT License
1.47k stars 134 forks source link

PikaScript, ESP32C3,LittleFS, Compile and write to FS every boot? #279

Closed laldana73 closed 1 year ago

laldana73 commented 1 year ago

Hello. I am running pikascript on the esp32C3, i am using LittleFS. when i run the python code with pikaVM_runSingleFile(), no Problem. When i run python code that import other python file, using pikaVM_runFile(), the code run ok, but seem like pikascript is compiling the code and saving in the filesystem in every boot, this is not so good for the flash memory. There are some way to solve this? i see in the docs, that we can use the py.o files or the pikaModules.py.a file, but i don't figure out how to use it.

Thanks in advance if you can point me in the right direction.

my code ...

#test.py
import app
import loop

print("test")
#loop.py
for i in range(1,10):
    print("Hola",i)
#app.py
print("Hola MassLink")
print(6+5)
//main.c
/*
 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_log.h"
#include "esp_littlefs.h"
#include "dirent.h"

#include "driver/uart.h"

#include "pikaScript.h"
#include "PikaVM.h"

static const char *TAG = "MassLink";

FILE* __platform_fopen(const char* filename, const char* modes){
    return fopen(filename, modes);
}

int __platform_fclose(FILE* stream){
    return fclose(stream);
}

size_t __platform_fwrite(const void* ptr, size_t size, size_t n, FILE* stream){
    return fwrite(ptr, size, n, stream);
}

size_t __platform_fread(void* ptr, size_t size, size_t n, FILE* stream){
    return fread(ptr, size, n, stream);
}

char __platform_getchar()
{
    char data = 0xff;
    do
    {
        vTaskDelay(1);
        data = getchar();
    } while (data == 0xff);
    return data;
}

void app_main(void)
{
    ESP_LOGI(TAG, "Initializing LITTLEFS");

    esp_vfs_littlefs_conf_t conf = {
            .base_path = "/fs",
            .partition_label = "storage",
            .format_if_mount_failed = true,
            .dont_mount = false,
    };

    // Use settings defined above to initialize and mount LittleFS filesystem.
    // Note: esp_vfs_littlefs_register is an all-in-one convenience function.
    esp_err_t ret = esp_vfs_littlefs_register(&conf);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount or format filesystem");
        } else if (ret == ESP_ERR_NOT_FOUND) {
            ESP_LOGE(TAG, "Failed to find SPIFFS partition");
        } else {
            ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
        }
        return;
    }

    size_t total = 0, used = 0;
    ret = esp_littlefs_info(conf.partition_label, &total, &used);
    if (ret != ESP_OK)
    {
            ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret));
    }
    else
    {
            ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
    }

#

    // Use POSIX and C standard library functions to work with files.

    PikaObj *pikaMain;
    pikaMain = newRootObj("pikaMain", New_PikaMain);

    pikaVM_runSingleFile(pikaMain, "loop.py");
    pikaVM_runSingleFile(pikaMain, "app.py");

    pikaVM_runFile(pikaMain, "test.py");

    // All done, unmount partition and disable LITTLEFS
    esp_vfs_littlefs_unregister(conf.partition_label);
    ESP_LOGI(TAG, "LITTLEFS unmounted");

    pikaScriptShell(pikaScriptInit());
}

// this is the output of the monitor

I (304) MassLink: Initializing LITTLEFS
I (321) MassLink: Partition size: total: 1048576, used: 20480
Hola 1
Hola 2
Hola 3
Hola 4
Hola 5
Hola 6
Hola 7
Hola 8
Hola 9
Hola MassLink
11
(pikascript) pika compiler:
  compiling test.py...
  compiling loop.py...
  compiling app.py...
  linking /fs/pikascript-api/pikaModules_cache.py.a...
  loading pikaModules_cache_py_a[]...
(pikascript) all succeed.

Hola MassLink
11
Hola 1
Hola 2
Hola 3
Hola 4
Hola 5
Hola 6
Hola 7
Hola 8
Hola 9
test
I (948) MassLink: LITTLEFS unmounted
======[pikascript packages installed]======
pikascript-core==v1.11.6 (2022/10/28 14:38:46)
ESP32==v0.1.0
PikaStdDevice==v1.11.0
PikaStdLib==v1.11.6
===========================================
hello PikaScript!
mem used max:
3.18 kB
>>> 
pikasTech commented 1 year ago

You can try the following code to boot from pikaModules.py.a

FILE* f = fopen("pikascript-api/pikaModules.py.a", "rb");
uint8_t buff[4096] = {0};
fread(f, buff, 4096);
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
obj_linkLibrary(pikaMain, buff);
obj_runModule(pikaMain, "main");
laldana73 commented 1 year ago

This Works, Thanks a lot