@@ -133,23 +133,6 @@ struct IOTask {
133133 return result;
134134 }
135135
136- // int foo(int)
137- int invokeIntInt (const char *name, int value, var_s *retval) {
138- int result = 0 ;
139- if (_instance != nullptr ) {
140- jmethodID method = env->GetMethodID (_clazz, name, " (I)I" );
141- var_num_t value = 0 ;
142- if (method != nullptr ) {
143- value = env->CallIntMethod (_instance, method, value);
144- }
145- if (!checkException (retval)) {
146- v_setint (retval, value);
147- result = 1 ;
148- }
149- }
150- return result;
151- }
152-
153136 // void foo(boolean)
154137 int invokeVoidBool (const char *name, int value, var_s *retval) {
155138 int result = 0 ;
@@ -238,6 +221,61 @@ struct IOTask {
238221 return result;
239222 }
240223
224+ // int readWrite(int address, byte[] write) {
225+ int invokeReadWrite (int argc, slib_par_t *arg, var_s *retval) {
226+ int result = 0 ;
227+ if (_instance != nullptr ) {
228+ jmethodID method = env->GetMethodID (_clazz, " readWrite" , " (I[B)I" );
229+ var_num_t value = 0 ;
230+ if (method != nullptr ) {
231+ jbyteArray array = env->NewByteArray (argc - 1 );
232+ jbyte *elements = env->GetByteArrayElements (array, nullptr );
233+ populateByteArray (argc, arg, elements);
234+ auto address = get_param_int (argc, arg, 0 , 0 );
235+
236+ value = env->CallIntMethod (_instance, method, address, array);
237+ releaseArray (array, elements);
238+ }
239+ if (!checkException (retval)) {
240+ v_setint (retval, value);
241+ result = 1 ;
242+ }
243+ }
244+ return result;
245+ }
246+
247+ // int readWrite(int address, byte[] write) {
248+ int invokeWrite (int argc, slib_par_t *arg, var_s *retval) {
249+ int result = 0 ;
250+ if (_instance != nullptr ) {
251+ jmethodID method = env->GetMethodID (_clazz, " write" , " (I[B)V" );
252+ if (method != nullptr ) {
253+ jbyteArray array = env->NewByteArray (argc - 1 );
254+ jbyte *elements = env->GetByteArrayElements (array, nullptr );
255+ populateByteArray (argc, arg, elements);
256+ auto address = get_param_int (argc, arg, 0 , 0 );
257+
258+ env->CallVoidMethod (_instance, method, address, array);
259+ releaseArray (array, elements);
260+ }
261+ if (!checkException (retval)) {
262+ result = 1 ;
263+ }
264+ }
265+ return result;
266+ }
267+
268+ void populateByteArray (int argc, slib_par_t *arg, jbyte *elements) {
269+ for (int i = 1 ; i < argc; i++) {
270+ elements[i] = get_param_int (argc, arg, i, 0 );
271+ }
272+ }
273+
274+ void releaseArray (jbyteArray array, jbyte *elements) {
275+ env->ReleaseByteArrayElements (array, elements, 0 );
276+ env->DeleteLocalRef (array);
277+ }
278+
241279 int open (int pin, var_s *retval) {
242280 return invokeVoidInt (" open" , pin, retval);
243281 }
@@ -271,30 +309,27 @@ static int get_io_class_id(var_s *map, var_s *retval) {
271309 return result;
272310}
273311
274- static int cmd_twimaster_write (var_s *self, int argc, slib_par_t *arg, var_s *retval) {
312+ static int cmd_twimaster_readwrite (var_s *self, int argc, slib_par_t *arg, var_s *retval) {
275313 int result = 0 ;
276- if (argc != 2 ) {
277- error (retval, " TwiMaster.write " , 2 );
314+ if (argc < 2 ) {
315+ error (retval, " TwiMaster.readWrite " , 2 , 10 );
278316 } else {
279317 int id = get_io_class_id (self, retval);
280318 if (id != -1 ) {
281- auto address = get_param_int (argc, arg, 0 , 0 );
282- auto data = get_param_int (argc, arg, 1 , 0 );
283- result = _ioTaskMap.at (id).invokeVoidInt2 (" write" , address, data, retval);
319+ result = _ioTaskMap.at (id).invokeReadWrite (argc, arg, retval);
284320 }
285321 }
286322 return result;
287323}
288324
289- static int cmd_twimaster_read (var_s *self, int argc, slib_par_t *arg, var_s *retval) {
325+ static int cmd_twimaster_write (var_s *self, int argc, slib_par_t *arg, var_s *retval) {
290326 int result = 0 ;
291- if (argc != 1 ) {
292- error (retval, " TwiMaster.read " , 1 );
327+ if (argc < 2 ) {
328+ error (retval, " TwiMaster.write " , 2 , 10 );
293329 } else {
294330 int id = get_io_class_id (self, retval);
295331 if (id != -1 ) {
296- auto address = get_param_int (argc, arg, 0 , 0 );
297- result = _ioTaskMap.at (id).invokeIntInt (" read" , address, retval);
332+ result = _ioTaskMap.at (id).invokeWrite (argc, arg, retval);
298333 }
299334 }
300335 return result;
@@ -317,7 +352,7 @@ static int cmd_spimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *re
317352
318353static void create_twimaster (var_t *map) {
319354 v_create_callback (map, " write" , cmd_twimaster_write);
320- v_create_callback (map, " read " , cmd_twimaster_read );
355+ v_create_callback (map, " readWrite " , cmd_twimaster_readwrite );
321356}
322357
323358static void create_spimaster (var_t *map) {
0 commit comments