1313#include " include/var_map.h"
1414#include " common/var.h"
1515#include " common/device.h"
16+ #include " device.h"
1617#include " module.h"
1718#include " serial.h"
1819#include < Wire.h>
@@ -71,7 +72,6 @@ static TwoWire *getI2C(int interfaceNumber) {
7172 return result;
7273}
7374
74-
7575static void set_pin (var_p_t var, uint8_t pin, uint8_t mode) {
7676 map_init (var);
7777 var->v .m .id = pin;
@@ -359,13 +359,13 @@ static int cmd_i2c_write(var_s *self, int argc, slib_par_t *args, var_s *retval)
359359 break ;
360360 case V_ARRAY: {
361361 var_p_t array = args[1 ].var_p ; // Get array
362- if (array->maxdim > 1 ) {
362+ if (array->maxdim > 1 ) {
363363 v_setstr (retval, " ERROR: I2C: Write requires 1D-array" );
364364 return 0 ;
365365 }
366366 uint32_t bytes = v_ubound (array, 0 ) - v_lbound (array, 0 ) + 1 ;
367367 uint8_t *buffer = new uint8_t [bytes];
368- for (uint32_t ii = 0 ; ii < bytes; ii++) {
368+ for (uint32_t ii = 0 ; ii < bytes; ii++) {
369369 buffer[ii] = get_array_elem_int (array, ii);
370370 }
371371 ptrWire->write (buffer, bytes);
@@ -383,9 +383,9 @@ static int cmd_i2c_write(var_s *self, int argc, slib_par_t *args, var_s *retval)
383383}
384384
385385static int cmd_i2c_read (var_s *self, int argc, slib_par_t *args, var_s *retval) {
386- uint8_t address = get_param_int (argc, args, 0 , 0 );
387- uint32_t bytes = get_param_int (argc, args, 1 , 1 );
388- uint8_t stop = get_param_int (argc, args, 2 , 1 );
386+ uint8_t address = get_param_int (argc, args, 0 , 0 );
387+ uint32_t bytes = get_param_int (argc, args, 1 , 1 );
388+ uint8_t stop = get_param_int (argc, args, 2 , 1 );
389389
390390 if (address == 0 ) {
391391 v_setstr (retval, ERR_PARAM);
@@ -400,9 +400,9 @@ static int cmd_i2c_read(var_s *self, int argc, slib_par_t *args, var_s *retval)
400400 ptrWire->requestFrom (address, bytes, stop);
401401 ptrWire->readBytes (buffer, bytes);
402402
403- if (bytes > 1 ) {
403+ if (bytes > 1 ) {
404404 v_toarray1 (retval, bytes);
405- for (uint32_t ii = 0 ; ii < bytes; ii++) {
405+ for (uint32_t ii = 0 ; ii < bytes; ii++) {
406406 v_setint (v_elem (retval, ii), buffer[ii]);
407407 }
408408 }
@@ -417,7 +417,7 @@ static int cmd_i2c_read(var_s *self, int argc, slib_par_t *args, var_s *retval)
417417static int cmd_i2c_setClock (var_s *self, int argc, slib_par_t *args, var_s *retval) {
418418 uint32_t clockFrequency = get_param_int (argc, args, 0 , 100000 );
419419
420- if (clockFrequency != 100000 && clockFrequency != 400000 && clockFrequency != 1000000 ) {
420+ if (clockFrequency != 100000 && clockFrequency != 400000 && clockFrequency != 1000000 ) {
421421 v_setstr (retval, " ERROR I2C: Clock freuqency not supported" );
422422 return 0 ;
423423 }
@@ -429,9 +429,9 @@ static int cmd_i2c_setClock(var_s *self, int argc, slib_par_t *args, var_s *retv
429429}
430430
431431static int cmd_openi2c (int argc, slib_par_t *args, var_t *retval) {
432- uint8_t interfaceNumber = get_param_int (argc, args, 0 , 0 );
433- uint8_t pinSDA = get_param_int (argc, args, 1 , 0 );
434- uint8_t pinSCL = get_param_int (argc, args, 2 , 0 );
432+ uint8_t interfaceNumber = get_param_int (argc, args, 0 , 0 );
433+ uint8_t pinSDA = get_param_int (argc, args, 1 , 0 );
434+ uint8_t pinSCL = get_param_int (argc, args, 2 , 0 );
435435
436436 if (interfaceNumber > 2 ) {
437437 v_setstr (retval, ERR_PARAM);
@@ -447,7 +447,7 @@ static int cmd_openi2c(int argc, slib_par_t *args, var_t *retval) {
447447 TwoWire *ptrWire;
448448 ptrWire = getI2C (interfaceNumber);
449449
450- if (pinSDA > 0 && pinSCL > 0 ) {
450+ if (pinSDA > 0 && pinSCL > 0 ) {
451451 ptrWire->setSDA (pinSDA);
452452 ptrWire->setSCL (pinSCL);
453453 }
@@ -457,6 +457,18 @@ static int cmd_openi2c(int argc, slib_par_t *args, var_t *retval) {
457457 return 1 ;
458458}
459459
460+ static int cmd_set_interactive (int argc, slib_par_t *args, var_t *retval) {
461+ uint8_t mode = get_param_int (argc, args, 0 , 1 );
462+
463+ if (mode > 0 ) {
464+ setInteractive (1 );
465+ } else {
466+ setInteractive (0 );
467+ }
468+
469+ return 1 ;
470+ }
471+
460472static FuncSpec lib_func[] = {
461473 {0 , 0 , " GETTEMP" , cmd_get_temperature},
462474 {0 , 0 , " GETCPUSPEED" , cmd_get_cpu_speed},
@@ -468,10 +480,18 @@ static FuncSpec lib_func[] = {
468480 {0 , 3 , " OPENI2C" , cmd_openi2c}
469481};
470482
483+ static FuncSpec lib_proc[] = {
484+ {0 , 1 , " SETINTERACTIVE" , cmd_set_interactive}
485+ };
486+
471487static int teensy_func_count (void ) {
472488 return (sizeof (lib_func) / sizeof (lib_func[0 ]));
473489}
474490
491+ static int teensy_proc_count (void ) {
492+ return (sizeof (lib_proc) / sizeof (lib_proc[0 ]));
493+ }
494+
475495static int teensy_func_getname (int index, char *func_name) {
476496 int result;
477497 if (index < teensy_func_count ()) {
@@ -483,6 +503,17 @@ static int teensy_func_getname(int index, char *func_name) {
483503 return result;
484504}
485505
506+ static int teensy_proc_getname (int index, char *proc_name) {
507+ int result;
508+ if (index < teensy_proc_count ()) {
509+ strcpy (proc_name, lib_proc[index]._name );
510+ result = 1 ;
511+ } else {
512+ result = 0 ;
513+ }
514+ return result;
515+ }
516+
486517static int teensy_func_exec (int index, int argc, slib_par_t *args, var_t *retval) {
487518 int result;
488519 if (index >= 0 && index < teensy_func_count ()) {
@@ -503,13 +534,33 @@ static int teensy_func_exec(int index, int argc, slib_par_t *args, var_t *retval
503534 return result;
504535}
505536
537+ static int teensy_proc_exec (int index, int argc, slib_par_t *args, var_t *retval) {
538+ int result;
539+ if (index >= 0 && index < teensy_proc_count ()) {
540+ if (argc < lib_proc[index]._min || argc > lib_proc[index]._max ) {
541+ if (lib_proc[index]._min == lib_proc[index]._max ) {
542+ error (retval, lib_proc[index]._name , lib_proc[index]._min );
543+ } else {
544+ error (retval, lib_proc[index]._name , lib_proc[index]._min , lib_proc[index]._max );
545+ }
546+ result = 0 ;
547+ } else {
548+ result = lib_proc[index]._command (argc, args, retval);
549+ }
550+ } else {
551+ error (retval, " PROC index error" );
552+ result = 0 ;
553+ }
554+ return result;
555+ }
556+
506557static ModuleConfig teensyModule = {
507558 ._func_exec = teensy_func_exec,
508559 ._func_count = teensy_func_count,
509560 ._func_getname = teensy_func_getname,
510- ._proc_exec = nullptr ,
511- ._proc_count = nullptr ,
512- ._proc_getname = nullptr ,
561+ ._proc_exec = teensy_proc_exec ,
562+ ._proc_count = teensy_proc_count ,
563+ ._proc_getname = teensy_proc_getname ,
513564 ._free = nullptr
514565};
515566
0 commit comments