Skip to content

Commit c759707

Browse files
committed
Add spiffs to locate mruby sourcefile.
1 parent 83a80e4 commit c759707

File tree

7 files changed

+45
-22
lines changed

7 files changed

+45
-22
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
build/
22
sdkconfig.old
3-
main/example_mrb.h
3+
main/main_mrb.h
44
components/mruby_component/esp32_build_config.rb.lock

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ are at least somewhat familiar with the building steps. With that in mind you
2626
can do something like the following and see the example running:
2727

2828
```
29-
idf.py build -D MRUBY_EXAMPLE=simplest_mrb.rb
29+
cp main/examples/$(YOU_WISH_TO_TRY_FILE) main/spiffs/main.rb
30+
idf.py build
3031
idf.py -p $(YOUR_SERIAL_PORT) flash monitor
3132
```
3233

33-
The flag `MRUBY_EXAMPLE` can be replaced with one of the following:
34+
The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following:
3435

3536
* _simplest_mrb.rb_ - Simply prints two strings
3637
* _wifi_example_mrb.rb_ - An example of connecting to WiFi, you will need to

main/CMakeLists.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
set(MRBC_COMMAND "${COMPONENT_DIR}/../components/mruby_component/mruby/bin/mrbc")
2-
31
idf_component_register(
42
SRCS mruby_main.c
53
INCLUDE_DIRS .
64
REQUIRES mruby_component
7-
PRIV_REQUIRES nvs_flash
8-
)
9-
10-
add_custom_command(
11-
OUTPUT example_mrb.h
12-
COMMAND ${MRBC_COMMAND} -B example_mrb -o example_mrb.h ${COMPONENT_DIR}/examples/${MRUBY_EXAMPLE}
13-
WORKING_DIRECTORY ${COMPONENT_DIR}
14-
VERBATIM
5+
PRIV_REQUIRES nvs_flash spiffs
156
)
167

17-
add_custom_target(main DEPENDS example_mrb.h)
18-
add_dependencies(${COMPONENT_LIB} main)
8+
spiffs_create_partition_image(storage ./spiffs FLASH_IN_PROJECT)

main/mruby_main.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,38 @@
44
#include "freertos/task.h"
55
#include "esp_system.h"
66
#include "esp_log.h"
7+
#include "esp_spiffs.h"
78
#include "nvs_flash.h"
89

910
#include "mruby.h"
1011
#include "mruby/irep.h"
1112
#include "mruby/compile.h"
1213
#include "mruby/error.h"
1314
#include "mruby/string.h"
14-
15-
#include "example_mrb.h"
15+
#include "mruby/dump.h"
1616

1717
#define TAG "mruby_task"
1818

19+
typedef mrb_value (*mrb_load_func)(mrb_state*, FILE*, mrbc_context*);
20+
1921
void mruby_task(void *pvParameter)
2022
{
2123
mrb_state *mrb = mrb_open();
2224
mrbc_context *context = mrbc_context_new(mrb);
2325
int ai = mrb_gc_arena_save(mrb);
24-
ESP_LOGI(TAG, "%s", "Loading binary...");
25-
mrb_load_irep_cxt(mrb, example_mrb, context);
26+
ESP_LOGI(TAG, "%s", "Loading...");
27+
28+
mrb_load_func load = mrb_load_detect_file_cxt;
29+
FILE *fp = fopen("/spiffs/main.rb", "r");
30+
if (fp == NULL) {
31+
load = mrb_load_irep_file_cxt;
32+
fp = fopen("/spiffs/main.mrb", "r");
33+
if (fp == NULL) {
34+
ESP_LOGI(TAG, "File is none.");
35+
goto exit;
36+
}
37+
}
38+
load(mrb, fp, context);
2639
if (mrb->exc) {
2740
ESP_LOGE(TAG, "Exception occurred: %s", mrb_str_to_cstr(mrb, mrb_inspect(mrb, mrb_obj_value(mrb->exc))));
2841
mrb->exc = 0;
@@ -32,9 +45,11 @@ void mruby_task(void *pvParameter)
3245
mrb_gc_arena_restore(mrb, ai);
3346
mrbc_context_free(mrb, context);
3447
mrb_close(mrb);
48+
fclose(fp);
3549

3650
// This task should never end, even if the
3751
// script ends.
52+
exit:
3853
while (1) {
3954
vTaskDelay(1);
4055
}
@@ -43,5 +58,14 @@ void mruby_task(void *pvParameter)
4358
void app_main()
4459
{
4560
nvs_flash_init();
61+
62+
esp_vfs_spiffs_conf_t conf = {
63+
.base_path = "/spiffs",
64+
.partition_label = NULL,
65+
.max_files = 10,
66+
.format_if_mount_failed = false
67+
};
68+
ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
69+
4670
xTaskCreate(&mruby_task, "mruby_task", 16384, NULL, 5, NULL);
4771
}

main/spiffs/main.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
puts "1234"
2+
puts "4321"

partitions.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Name, Type, SubType, Offset, Size, Flags
2+
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3+
nvs, data, nvs, 0x9000, 0x6000,
4+
phy_init, data, phy, 0xf000, 0x1000,
5+
factory, app, factory, 0x10000, 1500K,
6+
storage, data, spiffs, 0x190000,200K,

sdkconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
297297
# Partition Table
298298
#
299299
# CONFIG_PARTITION_TABLE_SINGLE_APP is not set
300-
CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y
300+
# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set
301301
# CONFIG_PARTITION_TABLE_TWO_OTA is not set
302-
# CONFIG_PARTITION_TABLE_CUSTOM is not set
302+
CONFIG_PARTITION_TABLE_CUSTOM=y
303303
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
304-
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp_large.csv"
304+
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
305305
CONFIG_PARTITION_TABLE_OFFSET=0x8000
306306
CONFIG_PARTITION_TABLE_MD5=y
307307
# end of Partition Table

0 commit comments

Comments
 (0)