Skip to content

Commit 9edff2d

Browse files
committed
TEENSY: module handling - wip
1 parent 78c2e89 commit 9edff2d

File tree

13 files changed

+412
-10
lines changed

13 files changed

+412
-10
lines changed

src/common/plugins.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
#define WIN_EXTLIB
2121
#define LIB_EXT ".dll"
2222
#elif defined(_MCU)
23-
23+
#define LIB_EXT ""
2424
#elif defined(_UnixOS)
2525
#include <dlfcn.h>
2626
#define LNX_EXTLIB
2727
#define LIB_EXT ".so"
2828
#endif
2929

30-
#if defined(LNX_EXTLIB) || defined(WIN_EXTLIB)
30+
#if defined(LNX_EXTLIB) || defined(WIN_EXTLIB) || defined(_MCU)
3131
#include "common/plugins.h"
32-
#include <dirent.h>
3332

3433
#define MAX_SLIBS 64
3534
#define MAX_PARAM 16
@@ -64,7 +63,29 @@ typedef struct {
6463

6564
static slib_t *plugins[MAX_SLIBS];
6665

67-
#if defined(LNX_EXTLIB)
66+
#if defined(_MCU)
67+
int slib_llopen(slib_t *lib) {
68+
lib->_handle = plugin_lib_open(lib->_fullname);
69+
if (lib->_handle == NULL) {
70+
sc_raise("LIB: error on loading %s\n", lib->_name);
71+
}
72+
return (lib->_handle != NULL);
73+
}
74+
75+
void *slib_getoptptr(slib_t *lib, const char *name) {
76+
return plugin_lib_address(lib->_handle, name);
77+
}
78+
79+
static int slib_llclose(slib_t *lib) {
80+
if (!lib->_handle) {
81+
return 0;
82+
}
83+
plugin_lib_close(lib->_handle);
84+
lib->_handle = NULL;
85+
return 1;
86+
}
87+
88+
#elif defined(LNX_EXTLIB)
6889
int slib_llopen(slib_t *lib) {
6990
lib->_handle = dlopen(lib->_fullname, RTLD_NOW);
7091
if (lib->_handle == NULL) {

src/common/plugins.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@
1919
extern "C" {
2020
#endif
2121

22+
//
23+
// lowlevel - open the named library
24+
//
25+
void *plugin_lib_open(const char *name);
26+
27+
//
28+
// lowlevel -return a pointer to the give library function name
29+
// name could be one of the method names in module.h
30+
//
31+
void *plugin_lib_address(void *handle, const char *name);
32+
33+
//
34+
// lowlevel -close the named library
35+
//
36+
void plugin_lib_close(void *handle);
37+
2238
//
2339
// initialise the plugin system
2440
//

src/common/scan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3937,7 +3937,7 @@ char *comp_load(const char *file_name) {
39373937
buf = NULL;
39383938
panic(MSG_CANT_OPEN_FILE, comp_file_name);
39393939
} else {
3940-
int size;
3940+
size_t size;
39413941

39423942
size = lseek(h, 0, SEEK_END);
39433943
lseek(h, 0, SEEK_SET);

src/include/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int sblib_func_exec(int index, int param_count, slib_par_t *params, var_t *retva
116116
* @param cls_id the variable class identifier
117117
* @param id the variable instance identifier
118118
*/
119-
void sblib_free(int cls_id, int id);
119+
int sblib_free(int cls_id, int id);
120120

121121
/**
122122
* @ingroup modlib

src/platform/teensy/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../..)
4343
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../..)
4444

4545
# Add executable with the list of source files
46-
add_executable(${TARGET}.elf src/noop.c src/device.cpp src/main.cpp)
46+
add_executable(${TARGET}.elf src/noop.c src/device.cpp src/main.cpp src/module.cpp src/teensy.cpp)
4747

4848
# enable all warnings in main.cpp
4949
target_compile_options(${TARGET}.elf PRIVATE -Wall)

src/platform/teensy/main.bas

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
for i = 0 to 100
2+
print "hello " + i
3+
delay 100
4+
next
5+
print "done !"
6+
7+
8+

src/platform/teensy/src/device.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void dev_printf(const char *format, ...) {
9090
static char _buffer[1024];
9191
va_list args;
9292
va_start(args, format);
93-
unsigned size = vsnprintf(NULL, 0, format, args);
93+
unsigned size = vsnprintf(nullptr, 0, format, args);
9494
va_end(args);
9595

9696
if (size < sizeof(_buffer)) {
@@ -107,7 +107,7 @@ void log_printf(const char *format, ...) {
107107
if (opt_verbose) {
108108
va_list args;
109109
va_start(args, format);
110-
unsigned size = vsnprintf(NULL, 0, format, args);
110+
unsigned size = vsnprintf(nullptr, 0, format, args);
111111
va_end(args);
112112

113113
if (size) {

src/platform/teensy/src/main.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,51 @@
1212
#include "config.h"
1313
#include "common/sbapp.h"
1414
#include "main_bas.h"
15+
#include "module.h"
1516

1617
#define MAIN_BAS "__main_bas__"
1718

19+
void *plugin_lib_open(const char *name) {
20+
void *result = nullptr;
21+
if (strcmp(name, "teensy") == 0) {
22+
result = get_teensy_module();
23+
}
24+
return result;
25+
}
26+
27+
void *plugin_lib_address(void *handle, const char *name) {
28+
auto *pModule = (s_module *)handle;
29+
void *result = nullptr;
30+
if (strcmp(name, "sblib_func_exec") == 0) {
31+
result = (void *)pModule->_func_exec;
32+
} else if (strcmp(name, "sblib_proc_exec") == 0) {
33+
result = (void *)pModule->_proc_exec;
34+
} else if (strcmp(name, "sblib_free") == 0) {
35+
result = (void *)pModule->_free;
36+
} else if (strcmp(name, "sblib_proc_count") == 0){
37+
result = (void *)pModule->_proc_count;
38+
} else if (strcmp(name, "sblib_proc_getname") == 0) {
39+
result = (void *)pModule->_proc_getname;
40+
} else if (strcmp(name, "sblib_func_count") == 0) {
41+
result = (void *)pModule->_func_count;
42+
} else if (strcmp(name, "sblib_func_getname") == 0) {
43+
result = (void *)pModule->_func_getname;
44+
}
45+
return result;
46+
}
47+
48+
void plugin_lib_close(void *handle) {
49+
// unused
50+
}
51+
1852
char *dev_read(const char *fileName) {
1953
char *buffer;
2054
if (strcmp(fileName, MAIN_BAS) == 0) {
2155
buffer = (char *)malloc(main_bas_len + 1);
2256
memcpy(buffer, main_bas, main_bas_len);
2357
buffer[main_bas_len] = '\0';
2458
} else {
25-
buffer = NULL;
59+
buffer = nullptr;
2660
}
2761
return buffer;
2862
}

src/platform/teensy/src/main_bas.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
unsigned char main_bas[] = {
2+
0x66, 0x6f, 0x72, 0x20, 0x69, 0x20, 0x3d, 0x20, 0x30, 0x20, 0x74, 0x6f,
3+
0x20, 0x31, 0x30, 0x30, 0x0a, 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74,
4+
0x20, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x22, 0x20, 0x2b, 0x20,
5+
0x69, 0x0a, 0x20, 0x20, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x20, 0x31, 0x30,
6+
0x30, 0x0a, 0x6e, 0x65, 0x78, 0x74, 0x0a, 0x70, 0x72, 0x69, 0x6e, 0x74,
7+
0x20, 0x22, 0x64, 0x6f, 0x6e, 0x65, 0x20, 0x21, 0x22, 0x0a, 0x0a, 0x0a,
8+
0x0a
9+
};
10+
unsigned int main_bas_len = 73;

src/platform/teensy/src/module.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This file is part of SmallBASIC
2+
//
3+
// Copyright(C) 2001-2025 Chris Warren-Smith.
4+
// Copyright(C) 2000 Nicholas Christopoulos
5+
//
6+
// This program is distributed under the terms of the GPL v2.0 or later
7+
// Download the GNU Public License (GPL) from www.gnu.org
8+
//
9+
10+
#include "config.h"
11+
#include "common/sbapp.h"
12+
13+
void error(var_p_t var, const char *field, int nMin, int nMax) {
14+
char message[256];
15+
snprintf(message, sizeof(message), "Invalid Input: [%s] - expected [%d - %d] arguments", field, nMin, nMax);
16+
v_setstr(var, message);
17+
}
18+
19+
void error(var_p_t var, const char *field, int n) {
20+
char message[256];
21+
snprintf(message, sizeof(message), "Invalid Input: [%s] - expected [%d] arguments", field, n);
22+
v_setstr(var, message);
23+
}
24+
25+
void error(var_p_t var, const char *text) {
26+
char message[256];
27+
snprintf(message, sizeof(message), "Error: [%s]", text);
28+
v_setstr(var, message);
29+
}
30+
31+
int get_param_int(int argc, slib_par_t *params, int n, int def) {
32+
int result;
33+
if (n >= 0 && n < argc) {
34+
switch (params[n].var_p->type) {
35+
case V_INT:
36+
result = params[n].var_p->v.i;
37+
break;
38+
case V_NUM:
39+
result = params[n].var_p->v.n;
40+
break;
41+
default:
42+
result = def;
43+
}
44+
} else {
45+
result = def;
46+
}
47+
return result;
48+
}

0 commit comments

Comments
 (0)