add more complex examples (#1)

This commit is contained in:
Juraj Michálek 2024-10-22 10:14:59 +02:00 committed by GitHub
commit a01d8580d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1657 additions and 204 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"idf.adapterTargetName": "esp32c2"
}

View file

@ -1,6 +1,8 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp32-c3-lua-test) project(esp32-lua-example)
get_filename_component(configName "${CMAKE_BINARY_DIR}" NAME)
list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_SOURCE_DIR}/components/esp_littlefs")
littlefs_create_partition_image(assets assets FLASH_IN_PROJECT)

View file

@ -1,9 +1,24 @@
# ESP32-C3 Lua evaluation example # ESP32 Lua Evaluation Example
## Build This example demonstrates how to use Lua 5.4 on ESP32, leveraging the LittleFS filesystem to run Lua scripts from external files. It also showcases Wi-Fi scanning and embedded Lua scripts.
```shell ## Prerequisites
idf.py set-target esp32c3
- ESP-IDF version 5.1.0 or higher
- Components:
- [Lua 5.4](https://components.espressif.com/components/georgik/lua/)
- [LittleFS 1.14](https://components.espressif.com/components/joltwallet/littlefs/)
## File Structure
- `main.c`: Main program containing embedded Lua script and file-based Lua execution.
- `assets/`: Directory containing Lua scripts for QR code generation and Fibonacci computation.
- `qr_code.lua`
- `fibonacci.lua`
## Build and Flash
```bash
idf.py set-target esp32 # Replace esp32 with your specific target, e.g., esp32c3
idf.py build flash monitor idf.py build flash monitor
``` ```

11
assets/fibonacci.lua Normal file
View file

@ -0,0 +1,11 @@
-- fibonacci.lua
-- This script calculates the 10th number in the Fibonacci sequence and prints it.
local function fibonacci(n)
if n <= 1 then return n end
return fibonacci(n - 1) + fibonacci(n - 2)
end
local fib_10 = fibonacci(10)
print('Fibonacci of 10 is: ' .. fib_10)
return fib_10

58
assets/qr_code.lua Normal file
View file

@ -0,0 +1,58 @@
-- qr_code.lua
-- Generates and prints a QR code for a given text using luaqrcode
-- Include the QR Code library
local qrencode = dofile("/assets/qrencode.lua")
-- Text to encode (using lowercase as per your request)
local text = 'https://developer.espressif.com/tags/lua'
-- Generate the QR code
local ok, tab_or_message = qrencode.qrcode(text)
if not ok then
print("Error generating QR code: " .. tab_or_message)
return
end
local function matrix_to_string(tab, padding, padding_char, white_pixel, black_pixel)
padding = padding or 1
white_pixel = white_pixel or ' '
black_pixel = black_pixel or '██'
padding_char = padding_char or white_pixel
local str_tab = {} -- Hold each row of the QR code in a table
-- Add padding rows at the top
for i = 1, padding do
table.insert(str_tab, string.rep(padding_char, #tab + 2 * padding))
end
for y = 1, #tab do
local line = string.rep(padding_char, padding)
for x = 1, #tab do
if tab[x][y] > 0 then
line = line .. black_pixel
else
line = line .. white_pixel
end
end
line = line .. string.rep(padding_char, padding)
table.insert(str_tab, line)
end
-- Add padding rows at the bottom
for i = 1, padding do
table.insert(str_tab, string.rep(padding_char, #tab + 2 * padding))
end
return str_tab
end
-- Convert the QR code matrix to a string
local qr_lines = matrix_to_string(tab_or_message)
-- Print the QR code
print("QR Code for: " .. text)
for _, line in ipairs(qr_lines) do
print(line)
end

1353
assets/qrencode.lua Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,2 +1,2 @@
idf_component_register(SRCS "esp32-c3-lua-test.c" idf_component_register(SRCS "esp32-lua-example.c"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

View file

@ -1,182 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_heap_caps.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_log.h"
#define WIFI_SCAN_LIST_SIZE 10
static const char *TAG = "WIFI_SCAN";
// Function to log memory usage with the message at the end
void log_memory_usage(const char *message)
{
printf("Free heap: %d, Min free heap: %d, Largest free block: %d, %s\n",
heap_caps_get_free_size(MALLOC_CAP_DEFAULT),
heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT),
heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT),
message);
}
// Function to run a Lua test
void run_lua_test(const char *lua_script, const char *test_name)
{
printf("Starting Lua test: %s\n", test_name);
log_memory_usage("Start of test");
lua_State *L = luaL_newstate();
log_memory_usage("After luaL_newstate");
luaL_openlibs(L);
log_memory_usage("After luaL_openlibs");
if (luaL_dostring(L, lua_script) == LUA_OK) {
lua_pop(L, lua_gettop(L));
}
log_memory_usage("After executing Lua script");
lua_close(L);
log_memory_usage("After lua_close");
printf("End of Lua test: %s\n", test_name);
}
// Function to run a Lua test and print returned QR code
void run_lua_qr_code_test(const char *lua_script, const char *test_name)
{
printf("Starting Lua QR Code test: %s\n", test_name);
log_memory_usage("Start of test");
lua_State *L = luaL_newstate();
log_memory_usage("After luaL_newstate");
luaL_openlibs(L);
log_memory_usage("After luaL_openlibs");
if (luaL_dostring(L, lua_script) == LUA_OK) {
// Retrieve the QR code string from the Lua stack
const char *qr_code_string = lua_tostring(L, -1);
if (qr_code_string) {
printf("QR Code:\n%s\n", qr_code_string);
}
lua_pop(L, lua_gettop(L));
}
log_memory_usage("After executing Lua script");
lua_close(L);
log_memory_usage("After lua_close");
printf("End of Lua QR Code test: %s\n", test_name);
}
// Function to initialize and perform Wi-Fi scan
void scan_wifi_networks(void)
{
printf("Starting Wi-Fi scan...\n");
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// Initialize Wi-Fi
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// Start Wi-Fi in STA mode
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
// Set the Wi-Fi scan parameters
wifi_scan_config_t scan_config = {
.ssid = NULL,
.bssid = NULL,
.channel = 0,
.show_hidden = true
};
// Perform Wi-Fi scan
ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, true));
// Get the scan results
uint16_t ap_count = WIFI_SCAN_LIST_SIZE;
wifi_ap_record_t ap_info[WIFI_SCAN_LIST_SIZE];
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info));
// Print the scan results
printf("Found %d access points:\n", ap_count);
for (int i = 0; i < ap_count; i++) {
printf("SSID: %s, RSSI: %d\n", ap_info[i].ssid, ap_info[i].rssi);
}
ESP_ERROR_CHECK(esp_wifi_stop());
ESP_ERROR_CHECK(esp_wifi_deinit());
printf("Wi-Fi scan completed.\n");
}
void app_main(void)
{
// Test 1: Simple Lua script
const char *simple_script = "answer = 42; print('The answer is: '..answer)";
run_lua_test(simple_script, "Simple Script");
// Test 2: Simple Lua script to create a mock QR code representation
const char *mock_qr_code_script =
"local function generate_mock_qr(text)\n"
" local result = ''\n"
" for i = 1, #text do\n"
" local char = text:byte(i)\n"
" local line = ''\n"
" for j = 1, 10 do\n" // 10x10 mock representation
" if ((char + j) % 2 == 0) then\n"
" line = line .. '##'\n"
" else\n"
" line = line .. ' '\n"
" end\n"
" end\n"
" result = result .. line .. '\\n'\n"
" end\n"
" return result\n"
"end\n"
"local qr_string = generate_mock_qr('Hello, ESP32-C3!')\n"
"return qr_string";
run_lua_qr_code_test(mock_qr_code_script, "Mock QR Code Script");
// Perform Wi-Fi scan
scan_wifi_networks();
// Test 3: Lua script to compute Fibonacci sequence
const char *fibonacci_script =
"local function fibonacci(n)\n"
" if n <= 1 then return n end\n"
" return fibonacci(n - 1) + fibonacci(n - 2)\n"
"end\n"
"local fib_10 = fibonacci(10)\n"
"print('Fibonacci of 10 is: ' .. fib_10)\n"
"return fib_10";
run_lua_test(fibonacci_script, "Fibonacci Script");
printf("End of testing application.\n");
// Prevent the task from ending
while(1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

190
main/esp32-lua-example.c Normal file
View file

@ -0,0 +1,190 @@
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_heap_caps.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "esp_vfs.h"
#include "esp_littlefs.h"
#include <dirent.h>
#define WIFI_SCAN_LIST_SIZE 10
#define LUA_FILE_PATH "/assets"
static const char *TAG = "lua_example";
// Function to log memory usage with the message at the end
void log_memory_usage(const char *message) {
ESP_LOGI(TAG, "Free heap: %d, Min free heap: %d, Largest free block: %d, %s",
heap_caps_get_free_size(MALLOC_CAP_DEFAULT),
heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT),
heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT),
message);
}
// Initialize and mount the filesystem
void init_filesystem() {
ESP_LOGI(TAG, "Initializing File System");
esp_vfs_littlefs_conf_t conf = {
.base_path = LUA_FILE_PATH,
.partition_label = "assets",
.format_if_mount_failed = false,
.dont_mount = false,
};
esp_err_t err = esp_vfs_littlefs_register(&conf);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount or format filesystem");
} else {
ESP_LOGI(TAG, "Filesystem mounted at %s", LUA_FILE_PATH);
}
}
// Function to run a Lua script from file
void run_lua_file(const char *file_name, const char *test_name) {
ESP_LOGI(TAG, "Starting Lua test from file: %s", test_name);
log_memory_usage("Start of test");
lua_State *L = luaL_newstate();
if (L == NULL) {
ESP_LOGE(TAG, "Failed to create new Lua state");
return;
}
log_memory_usage("After luaL_newstate");
luaL_openlibs(L);
// Set the Lua module search path to include the assets directory
if (luaL_dostring(L, "package.path = package.path .. ';./?.lua;/assets/?.lua'")) {
ESP_LOGE(TAG, "Failed to set package.path: %s", lua_tostring(L, -1));
lua_pop(L, 1); // Remove error message from the stack
}
log_memory_usage("After luaL_openlibs");
// Construct the full file path
char full_path[128];
snprintf(full_path, sizeof(full_path), LUA_FILE_PATH"/%s", file_name);
if (luaL_dofile(L, full_path) == LUA_OK) {
lua_pop(L, lua_gettop(L));
} else {
ESP_LOGE(TAG, "Error running Lua script from file '%s': %s", full_path, lua_tostring(L, -1));
lua_pop(L, 1); // Remove error message from the stack
}
log_memory_usage("After executing Lua script from file");
lua_close(L);
log_memory_usage("After lua_close");
ESP_LOGI(TAG, "End of Lua test from file: %s", test_name);
}
// Function to run an embedded Lua script
void run_embedded_lua_test(const char *lua_script, const char *test_name) {
ESP_LOGI(TAG, "Starting Lua test: %s", test_name);
log_memory_usage("Start of test");
lua_State *L = luaL_newstate();
if (L == NULL) {
ESP_LOGE(TAG, "Failed to create new Lua state");
return;
}
log_memory_usage("After luaL_newstate");
luaL_openlibs(L);
log_memory_usage("After luaL_openlibs");
if (luaL_dostring(L, lua_script) == LUA_OK) {
lua_pop(L, lua_gettop(L));
} else {
ESP_LOGE(TAG, "Error running embedded Lua script: %s", lua_tostring(L, -1));
lua_pop(L, 1); // Remove error message from the stack
}
log_memory_usage("After executing Lua script");
lua_close(L);
log_memory_usage("After lua_close");
ESP_LOGI(TAG, "End of Lua test: %s", test_name);
}
// Function to scan Wi-Fi networks
void scan_wifi_networks(void) {
ESP_LOGI(TAG, "Starting Wi-Fi scan...");
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGW(TAG, "NVS Flash init error (%s), erasing...", esp_err_to_name(ret));
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
wifi_scan_config_t scan_config = {
.ssid = NULL,
.bssid = NULL,
.channel = 0,
.show_hidden = true
};
ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, true));
uint16_t ap_count = WIFI_SCAN_LIST_SIZE;
wifi_ap_record_t ap_info[WIFI_SCAN_LIST_SIZE];
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info));
ESP_LOGI(TAG, "Found %d access points:", ap_count);
for (int i = 0; i < ap_count; i++) {
ESP_LOGI(TAG, "SSID: %s, RSSI: %d", ap_info[i].ssid, ap_info[i].rssi);
}
ESP_ERROR_CHECK(esp_wifi_stop());
ESP_ERROR_CHECK(esp_wifi_deinit());
ESP_LOGI(TAG, "Wi-Fi scan completed.");
}
void app_main(void) {
// Initialize and mount the filesystem
init_filesystem();
// Test 1: Simple embedded Lua script
const char *simple_script = "answer = 42; print('The answer is: '..answer)";
run_embedded_lua_test(simple_script, "Simple Embedded Script");
// Test 2: Run Lua script from a file (e.g., fibonacci.lua)
run_lua_file("fibonacci.lua", "Fibonacci Script from File");
// Test 3: Run Lua script to generate QR code (e.g., qr_code.lua)
run_lua_file("qr_code.lua", "QR Code Script from File");
// Perform Wi-Fi scan
scan_wifi_networks();
ESP_LOGI(TAG, "End of testing application.");
// Prevent the task from ending
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

View file

@ -1,17 +1,8 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
georgik/lua: "^5.5.0" georgik/lua: "==5.4.7"
joltwallet/littlefs: "==1.14.8"
## Required IDF version ## Required IDF version
idf: idf:
version: ">=4.1.0" version: ">=4.1.0"
# # Put list of dependencies here
# # For components maintained by Espressif:
# component: "~1.0.0"
# # For 3rd party components:
# username/component: ">=1.0.0,<2.0.0"
# username2/component2:
# version: "~1.0.0"
# # For transient dependencies `public` flag can be set.
# # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default.
# public: true

6
partitions.csv Normal file
View file

@ -0,0 +1,6 @@
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 24K,
phy_init, data, phy, 0xf000, 4K,
factory, app, factory, 0x10000, 2M,
assets, data, spiffs,0x210000, 1896K,
1 # ESP-IDF Partition Table
2 # Name, Type, SubType, Offset, Size, Flags
3 nvs, data, nvs, 0x9000, 24K,
4 phy_init, data, phy, 0xf000, 4K,
5 factory, app, factory, 0x10000, 2M,
6 assets, data, spiffs,0x210000, 1896K,

6
sdkconfig.defaults Normal file
View file

@ -0,0 +1,6 @@
# This file was generated using idf.py save-defconfig. It can be edited manually.
# Espressif IoT Development Framework (ESP-IDF) 5.4.0 Project Minimal Configuration
#
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_ESP_MAIN_TASK_STACK_SIZE=32000