Leave out BluetoothSerial and Camera when there's no PSRAM.

This commit is contained in:
Brad Nelson
2021-01-25 11:58:13 -08:00
parent cd4dd0d2c6
commit 3448da192d
2 changed files with 181 additions and 120 deletions

View File

@ -1,15 +1,25 @@
{{opcodes}}
#include "esp_bt_device.h"
#include "esp_camera.h"
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include "SPIFFS.h"
#include "SD_MMC.h"
#include "BluetoothSerial.h"
// For now, default on several options.
#define ENABLE_SPIFFS_SUPPORT
#define ENABLE_WIFI_SUPPORT
#define ENABLE_MDNS_SUPPORT
#define ENABLE_WEBSERVER_SUPPORT
#define ENABLE_SDCARD_SUPPORT
#define ENABLE_I2C_SUPPORT
// For now assume only boards with PSRAM (ESP32-CAM)
// will want SerialBluetooth (very large) and camera support.
// Other boards can support these if they're set to a larger
// parition size. But it's unclear the best way to configure this.
#ifdef BOARD_HAS_PSRAM
# define ENABLE_CAMERA_SUPPORT
# define ENABLE_SERIAL_BLUETOOTH_SUPPORT
#endif
#ifdef ENABLE_WEBSERVER_SUPPORT
# include "WebServer.h"
#endif
#include <errno.h>
#include <unistd.h>
@ -43,31 +53,6 @@
X("Serial.readBytes", SERIAL_READ_BYTES, tos = Serial.readBytes((uint8_t *) *sp, tos); --sp) \
X("Serial.write", SERIAL_WRITE, tos = Serial.write((const uint8_t *) *sp, tos); --sp) \
X("Serial.flush", SERIAL_FLUSH, Serial.flush()) \
/* SerialBT */ \
X("SerialBT.new", SERIALBT_NEW, DUP; tos = (cell_t) new BluetoothSerial()) \
X("SerialBT.delete", SERIALBT_DELETE, delete (BluetoothSerial *) tos; DROP) \
X("SerialBT.begin", SERIALBT_BEGIN, \
tos = ((BluetoothSerial *) tos)->begin((const char *) sp[-1], *sp); sp -= 2) \
X("SerialBT.end", SERIALBT_END, ((BluetoothSerial *) tos)->end(); DROP) \
X("SerialBT.available", SERIALBT_AVAILABLE, tos = ((BluetoothSerial *) tos)->available()) \
X("SerialBT.readBytes", SERIALBT_READ_BYTES, \
tos = ((BluetoothSerial *) tos)->readBytes((uint8_t *) sp[-1], *sp); sp -= 2) \
X("SerialBT.write", SERIALBT_WRITE, \
tos = ((BluetoothSerial *) tos)->write((const uint8_t *) sp[-1], *sp); sp -= 2) \
X("SerialBT.flush", SERIALBT_FLUSH, ((BluetoothSerial *) tos)->flush(); DROP) \
X("SerialBT.hasClient", SERIALBT_HAS_CLIENT, tos = ((BluetoothSerial *) tos)->hasClient()) \
X("SerialBT.enableSSP", SERIALBT_ENABLE_SSP, ((BluetoothSerial *) tos)->enableSSP(); DROP) \
X("SerialBT.setPin", SERIALBT_SET_PIN, tos = ((BluetoothSerial *) tos)->setPin((const char *) *sp); --sp) \
X("SerialBT.unpairDevice", SERIALBT_UNPAIR_DEVICE, \
tos = ((BluetoothSerial *) tos)->unpairDevice((uint8_t *) *sp); --sp) \
X("SerialBT.connect", SERIALBT_CONNECT, tos = ((BluetoothSerial *) tos)->connect((const char *) *sp); --sp) \
X("SerialBT.connectAddr", SERIALBT_CONNECT_ADDR, \
tos = ((BluetoothSerial *) tos)->connect((uint8_t *) *sp); --sp) \
X("SerialBT.disconnect", SERIALBT_DISCONNECT, tos = ((BluetoothSerial *) tos)->disconnect()) \
X("SerialBT.connected", SERIALBT_CONNECTED, tos = ((BluetoothSerial *) tos)->connected(*sp); --sp) \
X("SerialBT.isReady", SERIALBT_IS_READY, tos = ((BluetoothSerial *) tos)->isReady(sp[-1], *sp); sp -= 2) \
/* Bluetooth */ \
X("esp_bt_dev_get_address", ESP_BT_DEV_GET_ADDRESS, DUP; tos = (cell_t) esp_bt_dev_get_address()) \
/* Pins and PWM */ \
X("pinMode", PIN_MODE, pinMode(*sp, tos); --sp; DROP) \
X("digitalWrite", DIGITAL_WRITE, digitalWrite(*sp, tos); --sp; DROP) \
@ -111,6 +96,144 @@
tos = (cell_t) lseek(fd, tos, SEEK_SET); tos = tos < 0 ? errno : 0) \
X("FILE-SIZE", FILE_SIZE, struct stat st; w = fstat(tos, &st); \
tos = (cell_t) st.st_size; PUSH(w < 0 ? errno : 0)) \
OPTIONAL_SPIFFS_SUPPORT \
OPTIONAL_WIFI_SUPPORT \
OPTIONAL_MDNS_SUPPORT \
OPTIONAL_WEBSERVER_SUPPORT \
OPTIONAL_SDCARD_SUPPORT \
OPTIONAL_I2C_SUPPORT \
OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \
OPTIONAL_CAMERA_SUPPORT \
#ifndef ENABLE_SPIFFS_SUPPORT
// Provide a default failing SPIFFS.begin
# define OPTIONAL_SPIFFS_SUPPORT \
X("SPIFFS.begin", SPIFFS_BEGIN, sp -= 2; tos = 0)
#else
# include "SPIFFS.h"
# define OPTIONAL_SPIFFS_SUPPORT \
/* SPIFFS */ \
X("SPIFFS.begin", SPIFFS_BEGIN, \
tos = SPIFFS.begin(sp[-1], (const char *) *sp, tos); sp -=2) \
X("SPIFFS.end", SPIFFS_END, SPIFFS.end()) \
X("SPIFFS.format", SPIFFS_FORMAT, DUP; tos = SPIFFS.format()) \
X("SPIFFS.totalBytes", SPIFFS_TOTAL_BYTES, DUP; tos = SPIFFS.totalBytes()) \
X("SPIFFS.usedBytes", SPIFFS_USED_BYTES, DUP; tos = SPIFFS.usedBytes())
#endif
#ifndef ENABLE_CAMERA_SUPPORT
# define OPTIONAL_CAMERA_SUPPORT
#else
# include "esp_camera.h"
# define OPTIONAL_CAMERA_SUPPORT \
/* Camera */ \
X("esp_camera_init", ESP_CAMERA_INIT, \
tos = esp_camera_init((const camera_config_t *) tos)) \
X("esp_camera_deinit", ESP_CAMERA_DEINIT, \
DUP; tos = esp_camera_deinit()) \
X("esp_camera_fb_get", ESP_CAMERA_FB_GET, \
DUP; tos = (cell_t) esp_camera_fb_get()) \
X("esp_camera_db_return", ESP_CAMERA_FB_RETURN, \
esp_camera_fb_return((camera_fb_t *) tos); DROP) \
X("esp_camera_sensor_get", ESP_CAMERA_SENSOR_GET, \
DUP; tos = (cell_t) esp_camera_sensor_get())
#endif
#ifndef ENABLE_SDCARD_SUPPORT
# define OPTIONAL_SDCARD_SUPPORT
#else
# include "SD_MMC.h"
# define OPTIONAL_SDCARD_SUPPORT \
/* SD_MMC */ \
X("SD_MMC.begin", SD_MMC_BEGIN, \
tos = SD_MMC.begin((const char *) *sp, tos); --sp) \
X("SD_MMC.end", SD_MMC_END, SD_MMC.end()) \
X("SD_MMC.cardType", SD_MMC_CARD_TYPE, DUP; tos = SD_MMC.cardType()) \
X("SD_MMC.totalBytes", SD_MMC_TOTAL_BYTES, DUP; tos = SD_MMC.totalBytes()) \
X("SD_MMC.usedBytes", SD_MMC_USED_BYTES, DUP; tos = SD_MMC.usedBytes())
#endif
#ifndef ENABLE_I2C_SUPPORT
# define OPTIONAL_I2C_SUPPORT
#else
# include <Wire.h>
# define OPTIONAL_I2C_SUPPORT \
/* Wire */ \
X("Wire.begin", WIRE_BEGIN, tos = (cell_t) Wire.begin(*sp, tos); --sp) \
X("Wire.setClock", WIRE_SET_CLOCK, Wire.setClock(tos); DROP) \
X("Wire.getClock", WIRE_GET_CLOCK, DUP; tos = (cell_t) Wire.getClock()) \
X("Wire.setTimeout", WIRE_SET_TIMEOUT, Wire.setTimeout(tos); DROP) \
X("Wire.getTimeout", WIRE_GET_TIMEOUT, DUP; tos = (cell_t) Wire.getTimeout()) \
X("Wire.lastError", WIRE_LAST_ERROR, DUP; tos = (cell_t) Wire.lastError()) \
X("Wire.getErrorText", WIRE_GET_ERROR_TEXT, tos = (cell_t) Wire.getErrorText(tos)) \
X("Wire.beginTransmission", WIRE_BEGIN_TRANSMISSION, Wire.beginTransmission(tos); DROP) \
X("Wire.endTransmission", WIRE_END_TRANSMISSION, tos = (cell_t) Wire.endTransmission(tos)) \
X("Wire.requestFrom", WIRE_REQUEST_FROM, tos = (cell_t) Wire.requestFrom(sp[-1], *sp, tos); sp -= 2) \
X("Wire.writeTransmission", WIRE_WRITE_TRANSMISSION, \
tos = (cell_t) Wire.writeTransmission(sp[-2], (uint8_t *) sp[-1], *sp, tos); sp -=3) \
X("Wire.readTransmission", WIRE_READ_TRANSMISSION, \
tos = (cell_t) Wire.readTransmission(sp[-3], (uint8_t *) sp[-2], sp[-1], *sp, (uint32_t *) tos); sp -=4) \
X("Wire.write", WIRE_WRITE, tos = Wire.write((uint8_t *) *sp, tos); --sp) \
X("Wire.available", WIRE_AVAILABLE, DUP; tos = Wire.available()) \
X("Wire.read", WIRE_READ, DUP; tos = Wire.read()) \
X("Wire.peek", WIRE_PEEK, DUP; tos = Wire.peek()) \
X("Wire.busy", WIRE_BUSY, DUP; tos = Wire.busy()) \
X("Wire.flush", WIRE_FLUSH, Wire.flush())
#endif
#ifndef ENABLE_SERIAL_BLUETOOTH_SUPPORT
# define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT
#else
# include "esp_bt_device.h"
# include "BluetoothSerial.h"
# define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \
/* SerialBT */ \
X("SerialBT.new", SERIALBT_NEW, DUP; tos = (cell_t) new BluetoothSerial()) \
X("SerialBT.delete", SERIALBT_DELETE, delete (BluetoothSerial *) tos; DROP) \
X("SerialBT.begin", SERIALBT_BEGIN, \
tos = ((BluetoothSerial *) tos)->begin((const char *) sp[-1], *sp); sp -= 2) \
X("SerialBT.end", SERIALBT_END, ((BluetoothSerial *) tos)->end(); DROP) \
X("SerialBT.available", SERIALBT_AVAILABLE, tos = ((BluetoothSerial *) tos)->available()) \
X("SerialBT.readBytes", SERIALBT_READ_BYTES, \
tos = ((BluetoothSerial *) tos)->readBytes((uint8_t *) sp[-1], *sp); sp -= 2) \
X("SerialBT.write", SERIALBT_WRITE, \
tos = ((BluetoothSerial *) tos)->write((const uint8_t *) sp[-1], *sp); sp -= 2) \
X("SerialBT.flush", SERIALBT_FLUSH, ((BluetoothSerial *) tos)->flush(); DROP) \
X("SerialBT.hasClient", SERIALBT_HAS_CLIENT, tos = ((BluetoothSerial *) tos)->hasClient()) \
X("SerialBT.enableSSP", SERIALBT_ENABLE_SSP, ((BluetoothSerial *) tos)->enableSSP(); DROP) \
X("SerialBT.setPin", SERIALBT_SET_PIN, tos = ((BluetoothSerial *) tos)->setPin((const char *) *sp); --sp) \
X("SerialBT.unpairDevice", SERIALBT_UNPAIR_DEVICE, \
tos = ((BluetoothSerial *) tos)->unpairDevice((uint8_t *) *sp); --sp) \
X("SerialBT.connect", SERIALBT_CONNECT, tos = ((BluetoothSerial *) tos)->connect((const char *) *sp); --sp) \
X("SerialBT.connectAddr", SERIALBT_CONNECT_ADDR, \
tos = ((BluetoothSerial *) tos)->connect((uint8_t *) *sp); --sp) \
X("SerialBT.disconnect", SERIALBT_DISCONNECT, tos = ((BluetoothSerial *) tos)->disconnect()) \
X("SerialBT.connected", SERIALBT_CONNECTED, tos = ((BluetoothSerial *) tos)->connected(*sp); --sp) \
X("SerialBT.isReady", SERIALBT_IS_READY, tos = ((BluetoothSerial *) tos)->isReady(sp[-1], *sp); sp -= 2) \
/* Bluetooth */ \
X("esp_bt_dev_get_address", ESP_BT_DEV_GET_ADDRESS, DUP; tos = (cell_t) esp_bt_dev_get_address())
#endif
#ifndef ENABLE_WIFI_SUPPORT
# define OPTIONAL_WIFI_SUPPORT
#else
# include <WiFi.h>
# include <WiFiClient.h>
static IPAddress ToIP(cell_t ip) {
return IPAddress(ip & 0xff, ((ip >> 8) & 0xff), ((ip >> 16) & 0xff), ((ip >> 24) & 0xff));
}
static cell_t FromIP(IPAddress ip) {
cell_t ret = 0;
ret = (ret << 8) | ip[3];
ret = (ret << 8) | ip[2];
ret = (ret << 8) | ip[1];
ret = (ret << 8) | ip[0];
return ret;
}
# define OPTIONAL_WIFI_SUPPORT \
/* WiFi */ \
X("WiFi.config", WIFI_CONFIG, \
WiFi.config(ToIP(sp[-2]), ToIP(sp[-1]), ToIP(*sp), ToIP(tos)); sp -= 3; DROP) \
@ -122,16 +245,23 @@
X("WiFi.localIP", WIFI_LOCAL_IPS, DUP; tos = FromIP(WiFi.localIP())) \
X("WiFi.mode", WIFI_MODE, WiFi.mode((wifi_mode_t) tos); DROP) \
X("WiFi.setTxPower", WIFI_SET_TX_POWER, WiFi.setTxPower((wifi_power_t) tos); DROP) \
X("WiFi.getTxPower", WIFI_GET_TX_POWER, DUP; tos = (cell_t) WiFi.getTxPower()) \
X("WiFi.getTxPower", WIFI_GET_TX_POWER, DUP; tos = (cell_t) WiFi.getTxPower())
#endif
#ifndef ENABLE_MDNS_SUPPORT
# define OPTIONAL_MDNS_SUPPORT
#else
# include <ESPmDNS.h>
# define OPTIONAL_MDNS_SUPPORT \
/* mDNS */ \
X("MDNS.begin", MDNS_BEGIN, tos = MDNS.begin((const char *) tos)) \
/* SPIFFS */ \
X("SPIFFS.begin", SPIFFS_BEGIN, \
tos = SPIFFS.begin(sp[-1], (const char *) *sp, tos); sp -=2) \
X("SPIFFS.end", SPIFFS_END, SPIFFS.end()) \
X("SPIFFS.format", SPIFFS_FORMAT, DUP; tos = SPIFFS.format()) \
X("SPIFFS.totalBytes", SPIFFS_TOTAL_BYTES, DUP; tos = SPIFFS.totalBytes()) \
X("SPIFFS.usedBytes", SPIFFS_USED_BYTES, DUP; tos = SPIFFS.usedBytes()) \
X("MDNS.begin", MDNS_BEGIN, tos = MDNS.begin((const char *) tos))
#endif
#ifndef ENABLE_WEBSERVER_SUPPORT
# define OPTIONAL_WEBSERVER_SUPPORT
#else
# include <WebServer.h>
# define OPTIONAL_WEBSERVER_SUPPORT \
/* WebServer */ \
X("WebServer.new", WEBSERVER_NEW, DUP; tos = (cell_t) new WebServer(tos)) \
X("WebServer.delete", WEBSERVER_DELETE, delete (WebServer *) tos; DROP) \
@ -163,55 +293,14 @@
((WebServer *) tos)->send(sp[-2], (const char *) sp[-1], (const char *) *sp); \
sp -= 3; DROP) \
X("WebServer.sendContent", WEBSERVER_SEND_CONTENT, \
WebServerSendContent((WebServer *) tos, (const char *) sp[-1], *sp); \
sp -= 2; DROP) \
((WebServer *) tos)->sendContent((const char *) *sp); --sp; DROP) \
X("WebServer.method", WEBSERVER_METHOD, \
tos = (cell_t) ((WebServer *) tos)->method()) \
X("WebServer.handleClient", WEBSERVER_HANDLE_CLIENT, \
((WebServer *) tos)->handleClient(); DROP) \
/* Wire */ \
X("Wire.begin", WIRE_BEGIN, tos = (cell_t) Wire.begin(*sp, tos); --sp) \
X("Wire.setClock", WIRE_SET_CLOCK, Wire.setClock(tos); DROP) \
X("Wire.getClock", WIRE_GET_CLOCK, DUP; tos = (cell_t) Wire.getClock()) \
X("Wire.setTimeout", WIRE_SET_TIMEOUT, Wire.setTimeout(tos); DROP) \
X("Wire.getTimeout", WIRE_GET_TIMEOUT, DUP; tos = (cell_t) Wire.getTimeout()) \
X("Wire.lastError", WIRE_LAST_ERROR, DUP; tos = (cell_t) Wire.lastError()) \
X("Wire.getErrorText", WIRE_GET_ERROR_TEXT, tos = (cell_t) Wire.getErrorText(tos)) \
X("Wire.beginTransmission", WIRE_BEGIN_TRANSMISSION, Wire.beginTransmission(tos); DROP) \
X("Wire.endTransmission", WIRE_END_TRANSMISSION, tos = (cell_t) Wire.endTransmission(tos)) \
X("Wire.requestFrom", WIRE_REQUEST_FROM, tos = (cell_t) Wire.requestFrom(sp[-1], *sp, tos); sp -= 2) \
X("Wire.writeTransmission", WIRE_WRITE_TRANSMISSION, \
tos = (cell_t) Wire.writeTransmission(sp[-2], (uint8_t *) sp[-1], *sp, tos); sp -=3) \
X("Wire.readTransmission", WIRE_READ_TRANSMISSION, \
tos = (cell_t) Wire.readTransmission(sp[-3], (uint8_t *) sp[-2], sp[-1], *sp, (uint32_t *) tos); sp -=4) \
X("Wire.write", WIRE_WRITE, tos = Wire.write((uint8_t *) *sp, tos); --sp) \
X("Wire.available", WIRE_AVAILABLE, DUP; tos = Wire.available()) \
X("Wire.read", WIRE_READ, DUP; tos = Wire.read()) \
X("Wire.peek", WIRE_PEEK, DUP; tos = Wire.peek()) \
X("Wire.busy", WIRE_BUSY, DUP; tos = Wire.busy()) \
X("Wire.flush", WIRE_FLUSH, Wire.flush()) \
/* Camera */ \
X("esp_camera_init", ESP_CAMERA_INIT, \
tos = esp_camera_init((const camera_config_t *) tos)) \
X("esp_camera_deinit", ESP_CAMERA_DEINIT, \
DUP; tos = esp_camera_deinit()) \
X("esp_camera_fb_get", ESP_CAMERA_FB_GET, \
DUP; tos = (cell_t) esp_camera_fb_get()) \
X("esp_camera_db_return", ESP_CAMERA_FB_RETURN, \
esp_camera_fb_return((camera_fb_t *) tos); DROP) \
X("esp_camera_sensor_get", ESP_CAMERA_SENSOR_GET, \
DUP; tos = (cell_t) esp_camera_sensor_get()) \
/* SD_MMC */ \
X("SD_MMC.begin", SD_MMC_BEGIN, \
tos = SD_MMC.begin((const char *) *sp, tos); --sp) \
X("SD_MMC.end", SD_MMC_END, SD_MMC.end()) \
X("SD_MMC.cardType", SD_MMC_CARD_TYPE, DUP; tos = SD_MMC.cardType()) \
X("SD_MMC.totalBytes", SD_MMC_TOTAL_BYTES, DUP; tos = SD_MMC.totalBytes()) \
X("SD_MMC.usedBytes", SD_MMC_USED_BYTES, DUP; tos = SD_MMC.usedBytes()) \
((WebServer *) tos)->handleClient(); DROP)
#endif
// TODO: Why doesn't ftruncate exist?
// X("RESIZE-FILE", RESIZE_FILE, cell_t fd = tos; DROP; \
// tos = ftruncate(fd, tos); tos = tos < 0 ? errno : 0) \
// TODO: Support RESIZE-FILE (ftruncate is missing?)
static char filename[PATH_MAX];
static String string_value;
@ -220,19 +309,7 @@ static String string_value;
{{interp}}
{{boot}}
static IPAddress ToIP(cell_t ip) {
return IPAddress(ip & 0xff, ((ip >> 8) & 0xff), ((ip >> 16) & 0xff), ((ip >> 24) & 0xff));
}
static cell_t FromIP(IPAddress ip) {
cell_t ret = 0;
ret = (ret << 8) | ip[3];
ret = (ret << 8) | ip[2];
ret = (ret << 8) | ip[1];
ret = (ret << 8) | ip[0];
return ret;
}
#ifdef ENABLE_WEBSERVER_SUPPORT
static void InvokeWebServerOn(WebServer *ws, const char *url, cell_t xt) {
ws->on(url, [xt]() {
cell_t *old_ip = g_sys.ip;
@ -252,23 +329,7 @@ static void InvokeWebServerOn(WebServer *ws, const char *url, cell_t xt) {
g_sys.sp = old_sp;
});
}
static void WebServerSendContent(WebServer *ws, const char *data, cell_t len) {
char buffer[256];
while (len) {
if (len < sizeof(buffer) - 1) {
memcpy(buffer, data, len);
buffer[len] = 0;
ws->sendContent(buffer);
len = 0;
} else {
memcpy(buffer, data, sizeof(buffer) - 1);
buffer[sizeof(buffer)] = 0;
ws->sendContent(buffer);
len -= (sizeof(buffer) - 1);
}
}
}
#endif
void setup() {
cell_t *heap = (cell_t *) malloc(HEAP_SIZE);

View File

@ -299,7 +299,7 @@ WebServer.args ( ws -- n ) Number of args
WebServer.setContentLength ( n ws -- )
WebServer.sendHeader ( name-z value-z fist ws -- )
WebServer.send ( code mimetype data ws -- )
WebServer.sendContent ( a n ws -- )
WebServer.sendContent ( z ws -- )
WebServer.method ( ws -- n ) GET / POST etc.
</pre>