Skip to content

Commit 8aeb070

Browse files
committed
TEENSY: Improve interactive mode
1 parent d1c4971 commit 8aeb070

File tree

6 files changed

+85
-33
lines changed

6 files changed

+85
-33
lines changed

src/platform/teensy/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,9 @@ set(SOURCES
6161
src/ssd1306.cpp
6262
)
6363

64-
option(INTERACTIVE "Whether to run programs via serial" OFF)
65-
6664
# Add executable with the list of source files
6765
add_executable(${TARGET}.elf ${SOURCES})
6866

69-
if (INTERACTIVE)
70-
target_compile_definitions(${TARGET}.elf PRIVATE INTERACTIVE=1)
71-
else()
72-
target_compile_definitions(${TARGET}.elf PRIVATE INTERACTIVE=0)
73-
endif()
74-
7567
# enable all warnings in main.cpp
7668
target_compile_options(${TARGET}.elf PRIVATE -Wall)
7769

src/platform/teensy/run.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# Allows you to edit your SmallBASIC code in your favourite editor.
55
# When you save, the program is transferred to the teensy and run.
66
#
7-
# This works with the INTERACTIVE mode build
8-
# $ cd build && cmake .. -DINTERACTIVE=ON
9-
#
107
# Requires inotify-tools available via:
118
# $ sudo apt install inotify-tools
129
#

src/platform/teensy/src/device.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "serial.h"
1919

2020
uint32_t serialDataTimer = 0;
21+
uint8_t interactive = 1;
2122

2223
//
2324
// setup the Serial device
@@ -33,6 +34,10 @@ void serial_init() {
3334
}
3435
}
3536

37+
void setInteractive(uint8_t mode) {
38+
interactive = mode;
39+
}
40+
3641
//
3742
// delay for the specified number of milliseconds
3843
//
@@ -71,7 +76,7 @@ int dev_events(int wait_flag) {
7176
delay(10);
7277
}
7378

74-
if (Serial) {
79+
if (interactive) {
7580
if (Serial.available()) {
7681
if (serialDataTimer == 0) {
7782
serialDataTimer = millis();

src/platform/teensy/src/device.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 setInteractive(uint8_t mode);

src/platform/teensy/src/main.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,7 @@ void print_error(char *source) {
172172

173173
void interactive_main() {
174174
while (true) {
175-
while (!Serial) {
176-
delay(250);
177-
}
178-
179175
dev_print("\r\n\033[30;47mInteractive mode - waiting for data...\033[0m\r\n");
180-
181176
serial_read();
182177
if (!sbasic_main(SERIAL_SD_BAS)) {
183178
print_error((char *)buffer.c_str());

src/platform/teensy/src/teensy.cpp

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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-
7575
static 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

385385
static 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)
417417
static 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

431431
static 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+
460472
static 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+
471487
static 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+
475495
static 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+
486517
static 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+
506557
static 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

Comments
 (0)