Skip to content

Commit 1fd9fc7

Browse files
committed
TEENSY: adding text editing (wip)
1 parent d8f0459 commit 1fd9fc7

File tree

12 files changed

+188
-21
lines changed

12 files changed

+188
-21
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2025-02-02 (12.28)
2+
TEENSY: added experimental runtime platform for teensy mcu
3+
4+
2025-01-27 (12.28)
5+
ANDROID: Updated app icon, thanks to bplus, j7m, round157, qube, baggey
6+
Final design by j7m, rendered in blender by Orlano W-S
7+
18
2024-10-09 (12.28)
29
COMMON: Fix #232: TICKS stops working after 50 days
310
ANDROID: Fix delay accuracy

src/lib/str.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,32 @@ size_t strlcat(char *dst, const char *src, size_t siz) {
6161
return (dlen + (s - src));
6262
}
6363
#endif
64+
65+
#if defined(_MCU)
66+
#include <ctype.h>
67+
char* strcasestr(const char *haystack, const char *needle) {
68+
if (!*needle) {
69+
// If needle is empty, return the haystack
70+
return (char *)haystack;
71+
}
72+
73+
for (; *haystack; haystack++) {
74+
const char *h = haystack;
75+
const char *n = needle;
76+
77+
// Compare characters case-insensitively
78+
while (*h && *n && tolower((unsigned char)*h) == tolower((unsigned char)*n)) {
79+
h++;
80+
n++;
81+
}
82+
83+
// If we have matched the entire needle
84+
if (!*n) {
85+
return (char *)haystack;
86+
}
87+
}
88+
// Not found
89+
return NULL;
90+
}
91+
92+
#endif

src/lib/str.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C" {
3333
#if !defined(HAVE_STRLCPY)
3434
size_t strlcpy(char *dst, const char *src, size_t siz);
3535
#endif
36-
36+
3737
/**
3838
* Appends src to string dst of size siz (unlike strncat, siz is the
3939
* full size of dst, not space left). At most siz-1 characters
@@ -45,6 +45,10 @@ size_t strlcpy(char *dst, const char *src, size_t siz);
4545
size_t strlcat(char *dst, const char *src, size_t siz);
4646
#endif
4747

48+
#if defined(_MCU)
49+
char *strcasestr(const char *haystack, const char *needle);
50+
#endif
51+
4852
#ifdef __cplusplus
4953
}
5054
#endif

src/platform/teensy/CMakeLists.txt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ include_directories(${MODULES}/Wire)
4949
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../..)
5050
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../..)
5151

52-
# Add executable with the list of source files
53-
add_executable(${TARGET}.elf
52+
set(SOURCES
5453
src/noop.c
5554
src/device.cpp
5655
src/main.cpp
@@ -59,6 +58,26 @@ add_executable(${TARGET}.elf
5958
src/ssd1306.cpp
6059
)
6160

61+
option(WITH_EDITOR "Whether to include the program editor" OFF)
62+
if (WITH_EDITOR)
63+
set(SOURCES
64+
${SOURCES}
65+
src/editor.cpp
66+
../../ui/strlib.cpp
67+
../../ui/inputs.cpp
68+
../../ui/textedit.cpp
69+
)
70+
endif()
71+
72+
# Add executable with the list of source files
73+
add_executable(${TARGET}.elf ${SOURCES})
74+
75+
if (WITH_EDITOR)
76+
target_compile_definitions(${TARGET}.elf PRIVATE WITH_EDITOR=1)
77+
else()
78+
target_compile_definitions(${TARGET}.elf PRIVATE WITH_EDITOR=0)
79+
endif()
80+
6281
# enable all warnings in main.cpp
6382
target_compile_options(${TARGET}.elf PRIVATE -Wall)
6483

src/platform/teensy/Makefile.am

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# Download the GNU Public License (GPL) from www.gnu.org
66
#
77

8+
tidy:
9+
@cd build && make tidy
10+
811
build/modules/cores/teensy4/Makefile:
912
./setup.sh
1013

@@ -22,7 +25,11 @@ build/smallbasic.elf : \
2225
src/device.cpp \
2326
src/teensy.cpp \
2427
src/ssd1306.cpp \
25-
src/main_bas.h
28+
src/editor.cpp \
29+
src/main_bas.h \
30+
../../ui/strlib.cpp \
31+
../../ui/inputs.cpp \
32+
../../ui/textedit.cpp
2633
@cd build && make -j 32
2734

2835
all: build/smallbasic.elf

src/platform/teensy/src/device.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "config.h"
1414
#include "common/device.h"
1515
#include "common/smbas.h"
16+
#include "common/keymap.h"
17+
#include "lib/maapi.h"
1618

1719
void dev_delay(uint32_t timeout) {
1820
delay(timeout);
@@ -22,6 +24,10 @@ uint64_t dev_get_millisecond_count() {
2224
return millis();
2325
}
2426

27+
int maGetMilliSecondCount() {
28+
return millis();
29+
}
30+
2531
void dev_trace_line(int lineNo) {
2632
dev_printf("<%d>", lineNo);
2733
}
@@ -144,3 +150,85 @@ void panic(const char *fmt, ...) {
144150
Serial.println("Fatal error");
145151
for (;;);
146152
}
153+
154+
//
155+
// read the next set of characters following escape
156+
//
157+
int getEscape() {
158+
int result = -1;
159+
if (Serial.available()) {
160+
char secondByte = Serial.read();
161+
if (secondByte == '[') {
162+
if (Serial.available()) {
163+
int key = Serial.read();
164+
switch (key) {
165+
case 0x32:
166+
result = SB_KEY_INSERT;
167+
break;
168+
case 0x33:
169+
result = SB_KEY_DELETE;
170+
break;
171+
case 0x35:
172+
result = SB_KEY_PGUP;
173+
break;
174+
case 0x36:
175+
result = SB_KEY_PGDN;
176+
break;
177+
case 0x41:
178+
result = SB_KEY_UP;
179+
break;
180+
case 0x42:
181+
result = SB_KEY_DOWN;
182+
break;
183+
case 0x43:
184+
result = SB_KEY_RIGHT;
185+
break;
186+
case 0x44:
187+
result = SB_KEY_LEFT;
188+
break;
189+
case 0x46:
190+
result = SB_KEY_END;
191+
break;
192+
case 0x48:
193+
result = SB_KEY_HOME;
194+
break;
195+
default:
196+
dev_printf("Unknown esc[ key [%x]\n", key);
197+
break;
198+
}
199+
}
200+
} else {
201+
result = SB_KEY_ESCAPE;
202+
}
203+
}
204+
return result;
205+
}
206+
207+
//
208+
// read the next key from the serial device
209+
//
210+
int getKey() {
211+
int result = -1;
212+
if (Serial.available()) {
213+
result = Serial.read();
214+
switch (result) {
215+
case 0x09:
216+
result = SB_KEY_TAB;
217+
break;
218+
case 0x0d:
219+
result = SB_KEY_ENTER;
220+
break;
221+
case 0x1b:
222+
result = getEscape();
223+
break;
224+
case 0x7f:
225+
result = SB_KEY_BACKSPACE;
226+
break;
227+
}
228+
dev_printf("got key [%d]\n", result);
229+
} else {
230+
delay(500);
231+
yield();
232+
}
233+
return result;
234+
}

src/platform/teensy/src/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "common/sbapp.h"
1414
#include "main_bas.h"
1515
#include "module.h"
16+
#include "editor.h"
1617

1718
#define MAIN_BAS "__main_bas__"
1819

@@ -90,6 +91,11 @@ void setup() {
9091

9192
extern "C" int main(void) {
9293
setup();
94+
95+
#if WITH_EDITOR
96+
editSource();
97+
#endif
98+
9399
if (!sbasic_main(MAIN_BAS)) {
94100
dev_print("Error: run failed\n");
95101
opt_quiet = 0;

src/platform/teensy/src/ssd1306.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ static int cmd_init(int argc, slib_par_t *params, var_t *retval) {
3131
dev_print("SSD1306 initialization failed");
3232
for (;;);
3333
}
34-
display.display();
34+
35+
display.clearDisplay();
36+
display.setTextColor(SSD1306_WHITE);
37+
display.fillScreen(SSD1306_BLACK);
38+
display.setTextSize(1);
3539
return 1;
3640
}
3741

@@ -195,7 +199,6 @@ static int cmd_invertdisplay(int argc, slib_par_t *params, var_t *retval) {
195199
// void print(uint8_t);
196200
static int cmd_print(int argc, slib_par_t *params, var_t *retval) {
197201
auto str = get_param_str(argc, params, 0, 0);
198-
dev_print(str);
199202
display.print(str);
200203
return 1;
201204
}
@@ -207,14 +210,6 @@ static int cmd_setrotation(int argc, slib_par_t *params, var_t *retval) {
207210
return 1;
208211
}
209212

210-
// void setTextColor(uint16_t c, uint16_t bg) {
211-
static int cmd_settextcolor(int argc, slib_par_t *params, var_t *retval) {
212-
auto c = get_param_int(argc, params, 0, 0);
213-
auto bg = get_param_int(argc, params, 1, 0);
214-
display.setTextSize(c, bg);
215-
return 1;
216-
}
217-
218213
// void setCursor(int16_t x, int16_t y) {
219214
static int cmd_setcursor(int argc, slib_par_t *params, var_t *retval) {
220215
auto x = get_param_int(argc, params, 0, 0);
@@ -252,12 +247,11 @@ FuncSpec lib_proc[] = {
252247
{4, 4, "FILLCIRCLE", cmd_fillcircle},
253248
{5, 5, "FILLRECT", cmd_fillrect},
254249
{6, 6, "FILLROUNDRECT", cmd_fillroundrect},
255-
{2, 2, "FILLSCREEN", cmd_fillscreen},
250+
{1, 1, "FILLSCREEN", cmd_fillscreen},
256251
{7, 7, "FILLTRIANGLE", cmd_filltriangle},
257252
{1, 1, "INVERTDISPLAY", cmd_invertdisplay},
258253
{1, 1, "PRINT", cmd_print},
259-
{2, 2, "SETROTATION", cmd_setrotation},
260-
{2, 2, "SETTEXTCOLOR", cmd_settextcolor},
254+
{1, 1, "SETROTATION", cmd_setrotation},
261255
{2, 2, "SETCURSOR", cmd_setcursor},
262256
{1, 1, "SETTEXTSIZE", cmd_settextsize},
263257
{1, 1, "SETTEXTWRAP", cmd_settextwrap},

src/ui/inputs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,12 @@ void FormEditInput::setFocus(bool focus) {
482482
if (!_noFocus && focus == (focusInput != this)) {
483483
focusInput = focus ? this : nullptr;
484484
focusEdit = focus ? this : nullptr;
485-
g_system->getOutput()->setDirty();
485+
system_output_set_dirty();
486486
}
487487
}
488488

489489
void FormEditInput::clicked(int x, int y, bool pressed) {
490-
if (pressed && g_system->isRunning()) {
490+
if (pressed && system_is_running()) {
491491
dev_clrkb();
492492
setFocus(true);
493493
focusEdit = this;

src/ui/system.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@
9999

100100
System *g_system;
101101

102+
bool system_is_running() {
103+
return g_system->isRunning();
104+
}
105+
106+
void system_output_set_dirty() {
107+
g_system->getOutput()->setDirty();
108+
}
109+
102110
void Cache::add(const char *key, const char *value) {
103111
if (_size == _count) {
104112
// overwrite at next index position

0 commit comments

Comments
 (0)