Skip to content
This repository was archived by the owner on Nov 16, 2022. It is now read-only.

Commit b85d81d

Browse files
add spi
1 parent 6a131ea commit b85d81d

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ continue
3939
```tcl
4040
open stream ?details?
4141
# 2 serial ports are setup as /dev/serial0 and /dev/serial1 -- take baud rate
42+
# SPI port is setup as /dev/spi -- takes nothing
4243
# other files on SD card -- take "r" for read and "w" for write
43-
puts ?stream? string
44+
puts ?-nonewline? ?stream? string
4445
# if no stream specified uses /dev/serial (which is default serial port)
4546
close stream
4647
# closing a serial port is a noop

tcl_io.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/* File Identifier format:
77
SDCard File: 0x1C + 4 bytes pointer to file object
88
Serial port: 0x11 + port number
9+
SPI port: 0x12
910
1011
*/
1112

@@ -45,31 +46,44 @@ static tcl_result_t tcl_cmd_puts(struct tcl *tcl, tcl_value_t *args, void *arg)
4546
else Serial.print(tcl_string(text));
4647
}
4748
}
49+
else if (text[0] == 0x12) { // ASCII Device Control 2 ==> SPI port
50+
tcl_free(text);
51+
text = tcl_list_at(args, i + 1);
52+
SPI.transfer(tcl_string(text), tcl_length(text));
53+
}
4854
else {
4955
// default to serial
5056
if (newline) Serial.println(tcl_string(text));
5157
else Serial.print(tcl_string(text));
5258
}
59+
tcl_free(text);
5360
return tcl_result(tcl, TCL_OK, text);
5461
}
5562

5663
static tcl_result_t tcl_cmd_open(struct tcl *tcl, tcl_value_t *args, void *arg) {
5764
tcl_value_t *filename = tcl_list_at(args, 1);
5865
if (strcmp(filename, "/dev/serial") == 0 || strcmp(filename, "/dev/serial0") == 0) {
59-
int baud = (int)tcl_num(tcl_list_at(args, 2));
66+
tcl_value_t *bauds = tcl_list_at(args, 2);
67+
int baud = (int)tcl_num(bauds);
6068
if (baud == 0) baud = 9600;
6169
Serial.begin(baud);
62-
tcl_free(args);
70+
tcl_free(bauds);
6371
tcl_free(filename);
6472
return tcl_result(tcl, TCL_OK, tcl_alloc("\x11\x30", 2)); // \x30 is ASCII '0'
6573
}
6674
if (strcmp(filename, "/dev/serial1") == 0) {
67-
int baud = (int)tcl_num(tcl_list_at(args, 2));
75+
tcl_value_t *bauds = tcl_list_at(args, 2);
76+
int baud = (int)tcl_num(bauds);
6877
if (baud == 0) baud = 9600;
6978
Serial1.begin(baud);
70-
tcl_free(args);
79+
tcl_free(bauds);
7180
tcl_free(filename);
72-
return tcl_result(tcl, TCL_OK, tcl_alloc("\x11\x31", 2)); // \x30 is ASCII '1'
81+
return tcl_result(tcl, TCL_OK, tcl_alloc("\x11\x31", 2)); // \x31 is ASCII '1'
82+
}
83+
if (strcmp(filename, "/dev/spi") == 0) {
84+
SPI.begin();
85+
tcl_free(filename);
86+
return tcl_result(tcl, TCL_OK, tcl_alloc("\x12", 1));
7387
}
7488
// it's a filename
7589
int mode = FILE_READ;
@@ -92,12 +106,19 @@ static tcl_result_t tcl_cmd_open(struct tcl *tcl, tcl_value_t *args, void *arg)
92106

93107
static tcl_result_t tcl_cmd_close(struct tcl *tcl, tcl_value_t *args, void *arg) {
94108
tcl_value_t fd = tcl_list_at(args, 0);
95-
if (fd[0] != 0x11) { // Serial ports do not need to be closed
96-
uintptr_t fp = (uintptr_t)*(fd + 1);
97-
File f = (File)*fp;
98-
f.close();
99-
free(f);
109+
if (fd[0] == 0x11) { // Serial ports do not need to be closed
110+
tcl_free(fd);
111+
return tcl_result(tcl, TCL_OK, tcl_alloc("", 0));
112+
}
113+
if (fd[0] == 0x12) {
114+
SPI.end();
115+
tcl_free(fd);
116+
return tcl_result(tcl, TCL_OK, tcl_alloc("", 0));
100117
}
118+
uintptr_t fp = (uintptr_t)*(fd + 1);
119+
File f = (File)*fp;
120+
f.close();
121+
free(f);
101122
tcl_free(fd);
102123
return tcl_result(tcl, TCL_OK, tcl_alloc("", 0));
103124
}

0 commit comments

Comments
 (0)