1- /* uLisp ESP Release 4.4c - www.ulisp.com
2- David Johnson-Davies - www.technoblogy.com - 21st April 2023
1+ /* uLisp ESP Release 4.4d - www.ulisp.com
2+ David Johnson-Davies - www.technoblogy.com - 30th June 2023
33
44 Licensed under the MIT license: https://opensource.org/licenses/MIT
55*/
@@ -26,7 +26,6 @@ const char LispLibrary[] PROGMEM = "";
2626#include < SPI.h>
2727#include < Wire.h>
2828#include < limits.h>
29- #include < EEPROM.h>
3029#if defined (ESP8266)
3130 #include < ESP8266WiFi.h>
3231#elif defined (ESP32)
@@ -60,38 +59,35 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST);
6059
6160#if defined(ESP8266)
6261 #define WORKSPACESIZE (3928 -SDSIZE) /* Cells (8*bytes) */
62+ #include < EEPROM.h>
6363 #define EEPROMSIZE 4096 /* Bytes available for EEPROM */
6464 #define SDCARD_SS_PIN 10
6565 #define LED_BUILTIN 13
6666
6767#elif defined(ARDUINO_FEATHER_ESP32)
6868 #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
6969 #define LITTLEFS
70- #include " FS.h"
7170 #include < LittleFS.h>
7271 #define analogWrite (x,y ) dacWrite((x),(y))
7372 #define SDCARD_SS_PIN 13
7473
7574#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) || defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT)
76- #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
75+ #define WORKSPACESIZE (8160 -SDSIZE) /* Cells (8*bytes) */
7776 #define LITTLEFS
78- #include " FS.h"
7977 #include < LittleFS.h>
8078 #define analogWrite (x,y ) dacWrite((x),(y))
8179 #define SDCARD_SS_PIN 13
8280
8381#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO)
8482 #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
8583 #define LITTLEFS
86- #include " FS.h"
8784 #include < LittleFS.h>
8885 #define SDCARD_SS_PIN 13
8986 #define LED_BUILTIN 13
9087
9188#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2)
92- #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
89+ #define WORKSPACESIZE (8160 -SDSIZE) /* Cells (8*bytes) */
9390 #define LITTLEFS
94- #include " FS.h"
9591 #include < LittleFS.h>
9692 #define analogWrite (x,y ) dacWrite((x),(y))
9793 #define SDCARD_SS_PIN 13
@@ -100,15 +96,13 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST);
10096#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
10197 #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
10298 #define LITTLEFS
103- #include " FS.h"
10499 #include < LittleFS.h>
105100 #define SDCARD_SS_PIN 13
106101 #define LED_BUILTIN 13
107102
108103#elif defined(ARDUINO_FEATHERS2) /* UM FeatherS2 */
109- #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
104+ #define WORKSPACESIZE (8160 -SDSIZE) /* Cells (8*bytes) */
110105 #define LITTLEFS
111- #include " FS.h"
112106 #include < LittleFS.h>
113107 #define analogWrite (x,y ) dacWrite((x),(y))
114108 #define SDCARD_SS_PIN 13
@@ -117,15 +111,14 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST);
117111#elif defined(ARDUINO_ESP32_DEV) /* For TTGO T-Display */
118112 #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
119113 #define LITTLEFS
120- #include " FS.h"
121114 #include < LittleFS.h>
122115 #define analogWrite (x,y ) dacWrite((x),(y))
123116 #define SDCARD_SS_PIN 13
117+ #define LED_BUILTIN 13
124118
125119#elif defined(ARDUINO_ESP32S2_DEV)
126120 #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
127121 #define LITTLEFS
128- #include " FS.h"
129122 #include < LittleFS.h>
130123 #define analogWrite (x,y ) dacWrite((x),(y))
131124 #define SDCARD_SS_PIN 13
@@ -134,23 +127,20 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST);
134127#elif defined(ARDUINO_ESP32C3_DEV)
135128 #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
136129 #define LITTLEFS
137- #include " FS.h"
138130 #include < LittleFS.h>
139131 #define SDCARD_SS_PIN 13
140132 #define LED_BUILTIN 13
141133
142134#elif defined(ARDUINO_ESP32S3_DEV)
143135 #define WORKSPACESIZE (22000 -SDSIZE) /* Cells (8*bytes) */
144136 #define LITTLEFS
145- #include " FS.h"
146137 #include < LittleFS.h>
147138 #define SDCARD_SS_PIN 13
148139 #define LED_BUILTIN 13
149140
150141#elif defined(ESP32)
151142 #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
152143 #define LITTLEFS
153- #include " FS.h"
154144 #include < LittleFS.h>
155145 #define analogWrite (x,y ) dacWrite((x),(y))
156146 #define SDCARD_SS_PIN 13
@@ -1069,10 +1059,6 @@ object *divide_floats (object *args, float fresult) {
10691059 return makefloat (fresult);
10701060}
10711061
1072- int myround (float number) {
1073- return (number >= 0 ) ? (int )(number + 0.5 ) : (int )(number - 0.5 );
1074- }
1075-
10761062object *compare (object *args, bool lt, bool gt, bool eq) {
10771063 object *arg1 = first (args);
10781064 args = cdr (args);
@@ -1348,13 +1334,13 @@ object *copystring (object *arg) {
13481334 return obj;
13491335}
13501336
1351- object *readstring (uint8_t delim, gfun_t gfun) {
1337+ object *readstring (uint8_t delim, bool esc, gfun_t gfun) {
13521338 object *obj = newstring ();
13531339 object *tail = obj;
13541340 int ch = gfun ();
13551341 if (ch == -1 ) return nil;
13561342 while ((ch != delim) && (ch != -1 )) {
1357- if (ch == ' \\ ' ) ch = gfun ();
1343+ if (esc && ch == ' \\ ' ) ch = gfun ();
13581344 buildstring (ch, &tail);
13591345 ch = gfun ();
13601346 }
@@ -1402,6 +1388,17 @@ void pstr (char c) {
14021388 buildstring (c, &GlobalStringTail);
14031389}
14041390
1391+ object *iptostring (IPAddress ip) {
1392+ union { uint32_t data2; uint8_t u8 [4 ]; };
1393+ object *obj = startstring ();
1394+ data2 = ip;
1395+ for (int i=0 ; i<4 ; i++) {
1396+ if (i) pstr (' .' );
1397+ pintbase (u8 [i], 10 , pstr);
1398+ }
1399+ return obj;
1400+ }
1401+
14051402object *lispstring (char *s) {
14061403 object *obj = newstring ();
14071404 object *tail = obj;
@@ -1824,6 +1821,7 @@ inline int WiFiread () {
18241821 LastChar = 0 ;
18251822 return temp;
18261823 }
1824+ while (!client.available ()) testescape ();
18271825 return client.read ();
18281826}
18291827
@@ -2487,12 +2485,15 @@ object *sp_withspi (object *args, object *env) {
24872485}
24882486
24892487object *sp_withsdcard (object *args, object *env) {
2490- #if defined(sdcardsupport)
2488+ #if defined(sdcardsupport)
24912489 object *params = checkarguments (args, 2 , 3 );
24922490 object *var = first (params);
24932491 params = cdr (params);
24942492 if (params == NULL ) error2 (PSTR (" no filename specified" ));
2493+ builtin_t temp = Context;
24952494 object *filename = eval (first (params), env);
2495+ Context = temp;
2496+ if (!stringp (filename)) error (PSTR (" filename is not a string" ), filename);
24962497 params = cdr (params);
24972498 SD.begin ();
24982499 int mode = 0 ;
@@ -2514,11 +2515,11 @@ object *sp_withsdcard (object *args, object *env) {
25142515 object *result = eval (tf_progn (forms,env), env);
25152516 if (mode >= 1 ) SDpfile.close (); else SDgfile.close ();
25162517 return result;
2517- #else
2518+ #else
25182519 (void ) args, (void ) env;
25192520 error2 (PSTR (" not supported" ));
25202521 return nil;
2521- #endif
2522+ #endif
25222523}
25232524
25242525// Tail-recursive forms
@@ -3328,8 +3329,8 @@ object *fn_round (object *args, object *env) {
33283329 (void ) env;
33293330 object *arg = first (args);
33303331 args = cdr (args);
3331- if (args != NULL ) return number (myround (checkintfloat (arg) / checkintfloat (first (args))));
3332- else return number (myround (checkintfloat (arg)));
3332+ if (args != NULL ) return number (round (checkintfloat (arg) / checkintfloat (first (args))));
3333+ else return number (round (checkintfloat (arg)));
33333334}
33343335
33353336// Characters
@@ -3665,7 +3666,7 @@ object *fn_readbyte (object *args, object *env) {
36653666object *fn_readline (object *args, object *env) {
36663667 (void ) env;
36673668 gfun_t gfun = gstreamfun (args);
3668- return readstring (' \n ' , gfun);
3669+ return readstring (' \n ' , false , gfun);
36693670}
36703671
36713672object *fn_writebyte (object *args, object *env) {
@@ -3701,7 +3702,7 @@ object *fn_writeline (object *args, object *env) {
37013702
37023703object *fn_restarti2c (object *args, object *env) {
37033704 (void ) env;
3704- int stream = first (args)-> integer ;
3705+ int stream = isstream ( first (args)) ;
37053706 args = cdr (args);
37063707 int read = 0 ; // Write
37073708 I2Ccount = 0 ;
@@ -4209,7 +4210,7 @@ object *fn_wifisoftap (object *args, object *env) {
42094210 }
42104211 WiFi.softAP (cstring (first, ssid, 33 ), cstring (second, pass, 65 ), channel, hidden);
42114212 }
4212- return lispstring (( char *) WiFi.softAPIP (). toString (). c_str ());
4213+ return iptostring ( WiFi.softAPIP ());
42134214}
42144215
42154216object *fn_connected (object *args, object *env) {
@@ -4220,7 +4221,7 @@ object *fn_connected (object *args, object *env) {
42204221
42214222object *fn_wifilocalip (object *args, object *env) {
42224223 (void ) args, (void ) env;
4223- return lispstring (( char *) WiFi.localIP (). toString (). c_str ());
4224+ return iptostring ( WiFi.localIP ());
42244225}
42254226
42264227object *fn_wificonnect (object *args, object *env) {
@@ -4230,7 +4231,7 @@ object *fn_wificonnect (object *args, object *env) {
42304231 if (cdr (args) == NULL ) WiFi.begin (cstring (first (args), ssid, 33 ));
42314232 else WiFi.begin (cstring (first (args), ssid, 33 ), cstring (second (args), pass, 65 ));
42324233 int result = WiFi.waitForConnectResult ();
4233- if (result == WL_CONNECTED) return lispstring (( char *) WiFi.localIP (). toString (). c_str ());
4234+ if (result == WL_CONNECTED) return iptostring ( WiFi.localIP ());
42344235 else if (result == WL_NO_SSID_AVAIL) error2 (PSTR (" network not found" ));
42354236 else if (result == WL_CONNECT_FAILED) error2 (PSTR (" connection failed" ));
42364237 else error2 (PSTR (" unable to connect" ));
@@ -5550,7 +5551,7 @@ bool findsubstring (char *part, builtin_t name) {
55505551}
55515552
55525553void testescape () {
5553- if (Serial.available () && Serial.read () == ' ~' ) error2 (PSTR (" escape!" ));
5554+ if (Serial.available () && Serial.read () == ' ~' ) { Context = NIL; error2 (PSTR (" escape!" )); }
55545555}
55555556
55565557bool keywordp (object *obj) {
@@ -6085,7 +6086,7 @@ object *nextitem (gfun_t gfun) {
60856086 if (ch == ' \' ' ) return (object *)QUO;
60866087
60876088 // Parse string
6088- if (ch == ' "' ) return readstring (' "' , gfun);
6089+ if (ch == ' "' ) return readstring (' "' , true , gfun);
60896090
60906091 // Parse symbol, character, or number
60916092 int index = 0 , base = 10 , sign = 1 ;
@@ -6261,7 +6262,7 @@ void setup () {
62616262 initenv ();
62626263 initsleep ();
62636264 initgfx ();
6264- pfstring (PSTR (" uLisp 4.4c " ), pserial); pln (pserial);
6265+ pfstring (PSTR (" uLisp 4.4d " ), pserial); pln (pserial);
62656266}
62666267
62676268// Read/Evaluate/Print loop
0 commit comments