Hi David,
I’ve started to use SPI devices and also wanted TRANSFER in addition to READ and WRITE. I didn’t want to change WRITE as you suggested as it would be quite unexpected that an output function suddenly doesn’t return the thing printed anymore but something else.
I just needed TRANSFER-BYTE and decided to just add that. I followed your pattern that you used for put and get and added a TFUN, a TSTREAMFUN, SPITRANSFER, and then fn_transferbyte / transfer-byte.
As transfer is implemented only for an SPI stream, TSTREAMFUN returns an error for all other streams.
So for SPI some output might be:
(with-spi (lora *lora-ss-pin*)
(list (transfer-byte (logand #x42 #x7f) lora)
(transfer-byte 0 lora)))
=> (0 18)
But for other streams one gets:
(with-output-to-string (str) (transfer-byte 65 str))
=> Error: unknown stream type: transfer only supports SPI for now
Or even simpler:
(transfer-byte 65)
=> Error: unknown stream type: transfer only supports SPI for now
You’ll find the code as a diff in my repository at https://github.com/m-g-r/ulisp-esp-m5stack/commit/033051e40ecff188b1b44c3852c2b4cef92dba0c
Or at the bottom of this message.
Feel free to add it to the official uLisp release.
From Berlin,
Max
// transfer-byte for SPI by Max-Gerd Retzlaff <m.retzlaff@gmx.net>, 2023
typedef int (*tfun_t)(char);
// add ENDSPI to enum function
inline int spitransfer (char c) { return SPI.transfer(c); }
tfun_t tstreamfun (object *args) {
int streamtype = SERIALSTREAM;
int address = 0;
tfun_t tfun = spitransfer;
if (args != NULL && first(args) != NULL) {
int stream = isstream(first(args));
streamtype = stream>>8; address = stream & 0xFF;
}
if (streamtype == SPISTREAM) tfun = spitransfer;
else error2(0, PSTR("unknown stream type: transfer only supports SPI for now"));
return tfun;
}
object *fn_transferbyte (object *args, object *env) {
(void) env;
int value = checkinteger(TRANSFERBYTE, first(args));
tfun_t tfun = tstreamfun(cdr(args));
int c = (tfun)(value);
return (c == -1) ? nil : number(c);
}
const char string169b[] PROGMEM = "transfer-byte";
// add to const tbl_entry_t lookup_table[]:
{ string169b, fn_transferbyte, 0x12 },