Skip to content

Commit e4a3116

Browse files
committed
Implemented digital output write function
1 parent 493a217 commit e4a3116

5 files changed

Lines changed: 41 additions & 28 deletions

File tree

include/module.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@
1616
extern "C" {
1717
#endif
1818

19-
typedef struct {
20-
// the parameter
21-
var_t *var_p;
22-
23-
// whether the parameter can be used by reference
24-
uint8_t byref;
25-
} slib_par_t;
26-
2719
/**
2820
* @ingroup modstd
2921
*

include/param.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,11 @@ const char *format_text(int argc, slib_par_t *params, int param) {
581581
return buffer;
582582
}
583583

584-
void v_create_func(var_p_t map, const char *name, method cb) {
584+
void v_create_func(var_p_t map, const char *name, callback mcb) {
585585
var_p_t v_func = map_add_var(map, name, 0);
586586
v_func->type = V_FUNC;
587-
v_func->v.fn.cb = cb;
587+
v_func->v.fn.cb = nullptr;
588+
v_func->v.fn.mcb = mcb;
588589
v_func->v.fn.id = map->v.m.id;
589590
}
590591

include/var.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ extern "C" {
4747
#endif
4848

4949
struct var_s;
50+
51+
typedef struct {
52+
// the parameter
53+
struct var_s *var_p;
54+
55+
// whether the parameter can be used by reference
56+
uint8_t byref;
57+
} slib_par_t;
58+
59+
// signature for module callback/virtual functions
60+
typedef int (*callback) (struct var_s *self, int param_count, slib_par_t *params, struct var_s *retval);
61+
62+
// signature for internal v_funcs
5063
typedef void (*method) (struct var_s *self, struct var_s *retval);
5164

5265
typedef struct var_s {
@@ -83,6 +96,7 @@ typedef struct var_s {
8396
// object method
8497
struct {
8598
method cb;
99+
callback mcb;
86100
uint32_t id;
87101
} fn;
88102

@@ -487,7 +501,7 @@ int v_strlen(const var_t *v);
487501
*
488502
* setup a method on the map using the given name
489503
*/
490-
void v_create_func(var_p_t map, const char *name, method cb);
504+
void v_create_func(var_p_t map, const char *name, callback cb);
491505

492506
#if defined(__cplusplus)
493507
}

ioio/main.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,43 +81,49 @@ struct IOClass {
8181
};
8282

8383
#define CLS_IOCLASS 1
84-
robin_hood::unordered_map<int, IOClass> _ioClassMap;
84+
robin_hood::unordered_map<int, IOClass> _classMap;
8585
int _nextId = 1;
8686

8787
static int get_io_class_id(var_s *map) {
8888
int result = -1;
8989
if (is_map(map)) {
9090
int id = map->v.m.id;
91-
if (id != -1 && _ioClassMap.find(id) != _ioClassMap.end()) {
91+
if (id != -1 && _classMap.find(id) != _classMap.end()) {
9292
result = id;
9393
}
9494
}
9595
return result;
9696
}
9797

98-
int value = 0;
99-
static void cmd_digital_output_write(var_s *map, var_s *retval) {
100-
value = !value;
101-
int id = get_io_class_id(map);
102-
if (id != -1) {
103-
_ioClassMap.at(id).write(value);
98+
static int cmd_digital_output_write(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
99+
int result = 0;
100+
if (param_count == 1) {
101+
int id = get_io_class_id(self);
102+
if (id != -1) {
103+
int value = get_param_int(param_count, params, 0, 0);
104+
_classMap.at(id).write(value);
105+
result = 1;
106+
} else {
107+
error(retval, "IOClass not found");
108+
}
104109
} else {
105-
error(retval, "IOClass not found");
110+
error(retval, "Missing value argument");
106111
}
112+
return result;
107113
}
108114

109115
static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
110116
int result;
111117
int pin = get_param_int(argc, params, 0, 0);
112118
int id = ++_nextId;
113-
IOClass &input = _ioClassMap[id];
119+
IOClass &input = _classMap[id];
114120
if (input.create("net/sourceforge/smallbasic/ioio/AnalogInput") &&
115121
input.open(pin)) {
116122
map_init_id(retval, id, CLS_IOCLASS);
117123
//v_create_func(retval, "write", cmd_digital_output_write);
118124
result = 1;
119125
} else {
120-
_ioClassMap.erase(id);
126+
_classMap.erase(id);
121127
error(retval, "openAnalogInput() failed");
122128
result = 0;
123129
}
@@ -128,14 +134,14 @@ static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) {
128134
int result;
129135
int pin = get_param_int(argc, params, 0, 0);
130136
int id = ++_nextId;
131-
IOClass &output = _ioClassMap[id];
137+
IOClass &output = _classMap[id];
132138
if (output.create("net/sourceforge/smallbasic/ioio/DigitalOutput") &&
133139
output.open(pin)) {
134140
map_init_id(retval, id, CLS_IOCLASS);
135141
v_create_func(retval, "write", cmd_digital_output_write);
136142
result = 1;
137143
} else {
138-
_ioClassMap.erase(id);
144+
_classMap.erase(id);
139145
error(retval, "openDigitalOutput() failed");
140146
result = 0;
141147
}
@@ -182,16 +188,16 @@ SBLIB_API void sblib_free(int cls_id, int id) {
182188
if (id != -1) {
183189
switch (cls_id) {
184190
case CLS_IOCLASS:
185-
_ioClassMap.erase(id);
191+
_classMap.erase(id);
186192
break;
187193
}
188194
}
189195
}
190196

191197
void sblib_close(void) {
192-
if (!_ioClassMap.empty()) {
198+
if (!_classMap.empty()) {
193199
fprintf(stderr, "IOClass leak detected\n");
194-
_ioClassMap.clear();
200+
_classMap.clear();
195201
}
196202
jvm->DetachCurrentThread();
197203
// hangs

ioio/samples/led.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ out = ioio.openDigitalOutput(0)
55
value = false
66
for i = 0 to 5
77
print "setting LED "+ value
8-
out.write()
8+
out.write(value)
99
value = !value
1010
delay 1000
1111
next

0 commit comments

Comments
 (0)