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

+25-4
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

+16
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

+1-1
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

+1-1
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

+1-1
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

+8
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

+2-2
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

+35-1
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

+10
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

+48
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+
}

src/platform/teensy/src/module.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#pragma once
11+
12+
void error(var_p_t var, const char *field, int nMin, int nMax);
13+
void error(var_p_t var, const char *field, int n);
14+
void error(var_p_t var, const char *text);
15+
int get_param_int(int argc, slib_par_t *params, int n, int def);
16+
17+
typedef struct API {
18+
const char *name;
19+
int (*command)(int, slib_par_t *, var_t *retval);
20+
} API;
21+
22+
typedef struct FUNC_SIG {
23+
int _min;
24+
int _max;
25+
const char *_name;
26+
int (*_command)(int, slib_par_t *, var_t *retval);
27+
} FUNC_SIG;
28+
29+
typedef int (*sblib_exec_fn)(int, int, slib_par_t *, var_t *);
30+
typedef int (*sblib_getname_fn) (int, char *);
31+
typedef int (*sblib_count_fn) (void);
32+
typedef int (*sblib_init_fn) (const char *);
33+
typedef int (*sblib_free_fn) (int, int);
34+
typedef void (*sblib_close_fn) (void);
35+
36+
typedef struct {
37+
sblib_exec_fn _func_exec;
38+
sblib_count_fn _func_count;
39+
sblib_getname_fn _func_getname;
40+
sblib_exec_fn _proc_exec;
41+
sblib_count_fn _proc_count;
42+
sblib_getname_fn _proc_getname;
43+
sblib_free_fn _free;
44+
} s_module;
45+
46+
s_module *get_teensy_module();
47+

src/platform/teensy/src/ssd1306.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 <Arduino.h>
11+
12+
#include "config.h"
13+
#include "common/sbapp.h"
14+
#include "module.h"
15+
16+
static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
17+
return 0;
18+
}
19+
20+
static int cmd_opendigitalinput(int argc, slib_par_t *params, var_t *retval) {
21+
return 0;
22+
}
23+
24+
static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) {
25+
return 0;
26+
}
27+
28+
FUNC_SIG lib_func[] = {
29+
{1, 1, "OPENANALOGINPUT", cmd_openanaloginput},
30+
{1, 1, "OPENDIGITALINPUT", cmd_opendigitalinput},
31+
{1, 1, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
32+
};
33+
34+
int ssd1306_proc_count(void) {
35+
return 0;
36+
}
37+
38+
int ssd1306_proc_getname(int index, char *proc_name) {
39+
return 0;
40+
}
41+
42+
int ssd1306_proc_exec(int index, int param_count, slib_par_t *params, var_t *retval) {
43+
return 0;
44+
}
45+
46+
int ssd1306_func_count(void) {
47+
return (sizeof(lib_func) / sizeof(lib_func[0]));
48+
}
49+
50+
int ssd1306_func_getname(int index, char *func_name) {
51+
return 0;
52+
}
53+
54+
int ssd1306_func_exec(int index, int param_count, slib_par_t *params, var_t *retval) {
55+
return 0;
56+
}
57+
58+
int ssd1306_free(int cls_id, int id) {
59+
return 0;
60+
}

0 commit comments

Comments
 (0)