Merge pull request #9368 from gemu2015/scripter-update

Scripter update
This commit is contained in:
Theo Arends 2020-09-23 17:21:37 +02:00 committed by GitHub
commit c1fd24efc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 484 additions and 102 deletions

View File

@ -654,7 +654,7 @@ bool ESP32_MailClient::readMail(IMAPData &imapData)
{
_sdOk = sdTest();
if (_sdOk)
if (!SD.exists(imapData._savePath.c_str()))
if (!imapData.fsp->exists(imapData._savePath.c_str()))
createDirs(imapData._savePath);
}
else if (imapData._storageType == MailClientStorageType::SPIFFS)
@ -1788,6 +1788,7 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData)
else
{
/*
if (!_sdOk)
{
if (smtpData._storageType == MailClientStorageType::SD)
@ -1798,13 +1799,18 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData)
if (!_sdOk)
continue;
*/
bool file_existed = false;
/*
if (smtpData._storageType == MailClientStorageType::SD)
file_existed = SD.exists(smtpData._attach._filename[i].c_str());
else if (smtpData._storageType == MailClientStorageType::SPIFFS)
file_existed = SPIFFS.exists(smtpData._attach._filename[i].c_str());
else if (smtpData._storageType == MailClientStorageType::FFat)
file_existed = FFat.exists(smtpData._attach._filename[i].c_str());
*/
file_existed = smtpData.fsp->exists(smtpData._attach._filename[i].c_str());
if (file_existed)
{
smtpData._cbData._info = smtpData._attach._filename[i];
@ -1820,10 +1826,15 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData)
smtpData._net->getStreamPtr()->print(buf.c_str());
File file;
/*
if (smtpData._storageType == MailClientStorageType::SD)
file = SD.open(smtpData._attach._filename[i].c_str(), FILE_READ);
else if (smtpData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(smtpData._attach._filename[i].c_str(), FILE_READ);
else if (smtpData._storageType == MailClientStorageType::FFat)
file = FFat.open(smtpData._attach._filename[i].c_str(), FILE_READ);
*/
file = smtpData.fsp->open(smtpData._attach._filename[i].c_str(), FILE_READ);
send_base64_encode_mime_file(smtpData._net->getStreamPtr(), file);
smtpData._net->getStreamPtr()->print(ESP32_MAIL_STR_34);
@ -2036,6 +2047,7 @@ bool ESP32_MailClient::sdBegin(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t
_mosi = mosi;
_ss = ss;
_sdConfigSet = true;
SPI.begin(_sck, _miso, _mosi, _ss);
return SD.begin(_ss, SPI);
}
@ -2331,7 +2343,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
delete[] midx;
if (imapData._storageType == MailClientStorageType::SD)
if (!SD.exists(filepath.c_str()))
if (!imapData.fsp->exists(filepath.c_str()))
createDirs(filepath);
if (!imapData._headerSaved)
@ -2353,7 +2365,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
}
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(filepath.c_str(), FILE_WRITE);
file = imapData.fsp->open(filepath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(filepath.c_str(), FILE_WRITE);
}
@ -2922,7 +2934,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
delete[] midx;
if (imapData._storageType == MailClientStorageType::SD)
if (!SD.exists(filepath.c_str()))
if (!imapData.fsp->exists(filepath.c_str()))
createDirs(filepath);
if (!imapData._headerSaved)
@ -2944,7 +2956,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
}
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(filepath.c_str(), FILE_WRITE);
file = imapData.fsp->open(filepath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(filepath.c_str(), FILE_WRITE);
}
@ -3003,7 +3015,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
delete[] midx;
if (imapData._storageType == MailClientStorageType::SD)
if (!SD.exists(filepath.c_str()))
if (!imapData.fsp->exists(filepath.c_str()))
createDirs(filepath);
filepath += ESP32_MAIL_STR_202;
@ -3011,7 +3023,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
filepath += imapData._messageDataInfo[mailIndex][messageDataIndex]._filename;
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(filepath.c_str(), FILE_WRITE);
file = imapData.fsp->open(filepath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(filepath.c_str(), FILE_WRITE);
}
@ -3167,7 +3179,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT
{
if (imapData._storageType == MailClientStorageType::SD)
file = SD.open(hpath.c_str(), FILE_WRITE);
file = imapData.fsp->open(hpath.c_str(), FILE_WRITE);
else if (imapData._storageType == MailClientStorageType::SPIFFS)
file = SPIFFS.open(hpath.c_str(), FILE_WRITE);
@ -3714,6 +3726,17 @@ void IMAPData::setFetchUID(const String &fetchUID)
void IMAPData::setFileStorageType(uint8_t storageType)
{
_storageType = storageType;
switch (storageType) {
case MailClientStorageType::SPIFFS:
fsp = &SPIFFS;
break;
case MailClientStorageType::SD:
fsp = &SD;
break;
case MailClientStorageType::FFat:
fsp = &FFat;
break;
}
}
void IMAPData::setDownloadAttachment(bool download)
@ -4733,6 +4756,17 @@ void SMTPData::removeAttachFile(uint8_t index)
void SMTPData::setFileStorageType(uint8_t storageType)
{
_storageType = storageType;
switch (storageType) {
case MailClientStorageType::SPIFFS:
fsp = &SPIFFS;
break;
case MailClientStorageType::SD:
fsp = &SD;
break;
case MailClientStorageType::FFat:
fsp = &FFat;
break;
}
}
void SMTPData::clearAttachData()

View File

@ -48,6 +48,7 @@
#include "RFC2047.h"
#include "ESP32MailHTTPClient.h"
#include "ESP32TimeHelper.h"
#include <FFat.h>
#define FORMAT_SPIFFS_IF_FAILED true
@ -90,6 +91,7 @@ struct MailClientStorageType
{
static const uint8_t SPIFFS = 0;
static const uint8_t SD = 1;
static const uint8_t FFat = 2;
};
static const char ESP32_MAIL_STR_1[] PROGMEM = "Content-Type: multipart/mixed; boundary=\"";
@ -1302,6 +1304,7 @@ private:
std::string _host = "";
uint16_t _port = 993;
uint8_t _storageType = 1;
FS *fsp = &SD;
bool _unseen = false;
std::string _loginEmail = "";
std::string _loginPassword = "";
@ -1859,7 +1862,7 @@ protected:
string _host = "";
uint16_t _port = 0;
uint8_t _storageType = 1;
FS *fsp = &SD;
string _fromName = "";
string _senderEmail = "";
string _subject = "";

View File

@ -330,6 +330,12 @@ String buffer;
if (!buffer.startsWith(F("354"))) {
goto exit;
}
buffer = F("MIME-Version: 1.0\r\n");
client->print(buffer);
buffer = F("Content-Type: Multipart/mixed; boundary=frontier\r\n");
client->print(buffer);
buffer = F("From: ");
buffer += from;
client->println(buffer);
@ -350,6 +356,7 @@ String buffer;
AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),buffer.c_str());
#endif
#ifdef USE_SCRIPT
if (*msg=='*' && *(msg+1)==0) {
g_client=client;
@ -378,9 +385,98 @@ exit:
}
void xsend_message_txt(char *msg) {
g_client->println(msg);
}
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),msg);
#endif
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
if (*msg=='@') {
msg++;
attach_File(msg);
} else if (*msg=='&') {
msg++;
attach_Array(msg);
} else {
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n\r\n"));
g_client->println(msg);
g_client->print(F("\r\n--frontier\r\n"));
}
#else
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n\r\n"));
g_client->println(msg);
g_client->print(F("\r\n--frontier\r\n"));
#endif
}
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
#include <LittleFS.h>
extern FS *fsp;
void attach_File(char *path) {
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n"));
char buff[64];
char *cp = path;
while (*cp=='/') cp++;
File file = fsp->open(path, "r");
if (file) {
sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s\"\r\n\r\n"), cp);
g_client->write(buff);
uint16_t flen = file.size();
uint8_t fbuff[64];
uint16_t blen = sizeof(fbuff);
while (flen>0) {
file.read(fbuff, blen);
flen -= blen;
g_client->write(fbuff, blen);
if (flen<blen) blen = flen;
}
file.close();
} else {
g_client->print(F("\r\n\r\nfile not found!\r\n"));
}
g_client->print(F("\r\n--frontier\r\n"));
}
float *get_array_by_name(char *name, uint16_t *alen);
void flt2char(float num, char *nbuff);
void attach_Array(char *aname) {
float *array = 0;
uint16_t alen;
array = get_array_by_name(aname, &alen);
g_client->print(F("--frontier\r\n"));
g_client->print(F("Content-Type: text/plain\r\n"));
if (array && alen) {
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("array found %d"),alen);
#endif
char buff[64];
sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s.txt\"\r\n\r\n"), aname);
g_client->write(buff);
float *fp=array;
for (uint32_t cnt = 0; cnt<alen; cnt++) {
// export array as tab gelimited text
char nbuff[16];
flt2char(*fp++, nbuff);
if (cnt < (alen - 1)) {
strcat(nbuff,"\t");
} else {
strcat(nbuff,"\n");
}
g_client->write(nbuff, strlen(nbuff));
}
} else {
g_client->print(F("\r\n\r\narray not found!\r\n"));
}
g_client->print(F("\r\n--frontier\r\n"));
}
#endif // defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
#else
/*
* Created by K. Suwatchai (Mobizt)
@ -413,6 +509,9 @@ void script_send_email_body(void(*func)(char *));
#define xPSTR(a) a
//The Email Sending data object contains config and data to send
SMTPData smtpData;
#define MAX_ATTCHMENTS 8
char *attachments[MAX_ATTCHMENTS];
uint8_t num_attachments;
//Callback function to get the Email sending status
//void sendCallback(SendStatus info);
@ -435,7 +534,7 @@ uint16_t SendMail(char *buffer) {
// return if not enough memory
uint32_t mem=ESP.getFreeHeap();
AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem);
//AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem);
if (mem<SEND_MAIL32_MINRAM) {
return 4;
}
@ -463,6 +562,13 @@ uint16_t SendMail(char *buffer) {
cmd=endcmd+1;
for (uint32_t cnt=0; cnt<MAX_ATTCHMENTS; cnt++) {
attachments[cnt]=0;
}
num_attachments=0;
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("mailsize: %d"),blen);
#endif
@ -540,9 +646,10 @@ uint16_t SendMail(char *buffer) {
}
#endif
#ifdef DEBUG_EMAIL_PORT
#ifdef DEBUG_EMAIL_PORT
AddLog_P2(LOG_LEVEL_INFO, PSTR("%s - %s - %s - %s"),from,to,subject,cmd);
#endif
#endif
smtpData.setDebug(true);
@ -598,9 +705,16 @@ uint16_t SendMail(char *buffer) {
//Set the storage types to read the attach files (SD is default)
//smtpData.setFileStorageType(MailClientStorageType::SPIFFS);
#if defined (USE_SCRIPT) && defined(USE_SCRIPT_FATFS)
#ifdef USE_SCRIPT_FATFS
#if USE_SCRIPT_FATFS<0
smtpData.setFileStorageType(MailClientStorageType::FFat);
#else
smtpData.setFileStorageType(MailClientStorageType::SD);
#endif
#endif
//smtpData.setSendCallback(sendCallback);
//Start sending Email, can be set callback function to track the status
@ -613,17 +727,59 @@ uint16_t SendMail(char *buffer) {
}
//Clear all data from Email object to free memory
smtpData.empty();
for (uint32_t cnt=0; cnt<MAX_ATTCHMENTS; cnt++) {
if (attachments[cnt]) {
free(attachments[cnt]);
attachments[cnt]=0;
}
}
exit:
if (oparams) free(oparams);
return status;
}
float *get_array_by_name(char *name, uint16_t *alen);
void flt2char(float num, char *nbuff);
void attach_Array(char *aname) {
float *array = 0;
uint16_t alen;
String ttstr = "";
array = get_array_by_name(aname, &alen);
if (array && alen) {
float *fp=array;
for (uint32_t cnt = 0; cnt<alen; cnt++) {
// export array as tab gelimited text
char nbuff[16];
flt2char(*fp++, nbuff);
if (cnt < (alen - 1)) {
strcat(nbuff,"\t");
} else {
strcat(nbuff,"\n");
}
ttstr += nbuff;
}
if (num_attachments<MAX_ATTCHMENTS) {
attachments[num_attachments] = (char*)malloc(ttstr.length()+1);
strcpy(attachments[num_attachments],ttstr.c_str());
char name[32];
sprintf(name,"%s.txt",aname);
smtpData.addAttachData(name, "text/plain",(uint8_t*)attachments[num_attachments],ttstr.length());
num_attachments++;
}
} else {
//g_client->print(F("\r\n\r\narray not found!\r\n"));
}
}
void send_message_txt(char *txt) {
if (*txt=='&') {
if (*txt=='@') {
txt++;
smtpData.addAttachFile(txt);
} else if (*txt=='&') {
txt++;
attach_Array(txt);
} else if (*txt=='$') {
txt++;
#if defined(ESP32) && defined(USE_WEBCAM)

View File

@ -65,6 +65,7 @@ keywords if then else endif, or, and are better readable for beginners (others m
#define SCRIPT_MAXPERM (PMEM_SIZE)-4/sizeof(float)
#define MAX_SCRIPT_SIZE MAX_RULE_SIZE*MAX_RULE_SETS
#define MAX_SARRAY_NUM 32
uint32_t EncodeLightId(uint8_t relay_id);
uint32_t DecodeLightId(uint32_t hue_id);
@ -395,14 +396,16 @@ struct SCRIPT_MEM {
void *script_mem;
uint16_t script_mem_size;
uint8_t script_dprec;
uint8_t script_lzero;
uint8_t var_not_found;
uint8_t glob_error;
uint8_t max_ssize;
uint8_t script_loglevel;
uint8_t flags;
uint8_t si_num;
uint8_t siro_num;
char *last_index_string;
uint8_t si_num[3];
uint8_t siro_num[3];
uint8_t sind_num;
char *last_index_string[3];
#ifdef USE_SCRIPT_FATFS
File files[SFS_MAX];
@ -438,6 +441,30 @@ uint8_t fast_script=0;
uint8_t glob_script=0;
uint32_t script_lastmillis;
void flt2char(float num, char *nbuff) {
dtostrfd(num, glob_script_mem.script_dprec, nbuff);
}
// convert float to char with leading zeros
void f2char(float num, uint32_t dprec, uint32_t lzeros, char *nbuff) {
dtostrfd(num, dprec, nbuff);
if (lzeros>1) {
// check leading zeros
uint32_t nd = num;
nd/=10;
nd+=1;
if (lzeros>nd) {
// insert zeros
char cpbuf[24];
char *cp = cpbuf;
for (uint32_t cnt = 0; cnt < lzeros - nd; cnt++) {
*cp++='0';
}
*cp=0;
strcat(cpbuf,nbuff);
strcpy(nbuff,cpbuf);
}
}
}
#ifdef USE_BUTTON_EVENT
int8_t script_button[MAX_KEYS];
@ -801,6 +828,7 @@ char *script;
glob_script_mem.numvars = vars;
glob_script_mem.script_dprec = SCRIPT_FLOAT_PRECISION;
glob_script_mem.script_lzero = 0;
glob_script_mem.script_loglevel = LOG_LEVEL_INFO;
@ -811,7 +839,7 @@ char *script;
} else {
char string[32];
dtostrfd(glob_script_mem.fvars[dvtp[count].index], glob_script_mem.script_dprec, string);
f2char(glob_script_mem.fvars[dvtp[count].index], glob_script_mem.script_dprec, glob_script_mem.script_lzero, string);
toLog(string);
}
}
@ -1127,6 +1155,23 @@ float *Get_MFAddr(uint8_t index, uint16_t *len, uint16_t *ipos) {
return 0;
}
char *isvar(char *lp, uint8_t *vtype, struct T_INDEX *tind, float *fp, char *sp, JsonObject *jo);
float *get_array_by_name(char *name, uint16_t *alen) {
struct T_INDEX ind;
uint8_t vtype;
isvar(name, &vtype, &ind, 0, 0, 0);
if (vtype==VAR_NV) return 0;
if (vtype&STYPE) return 0;
uint16_t index = glob_script_mem.type[ind.index].index;
if (glob_script_mem.type[ind.index].bits.is_filter) {
float *fa = Get_MFAddr(index, alen, 0);
return fa;
}
return 0;
}
float Get_MFVal(uint8_t index, int16_t bind) {
uint8_t *mp = (uint8_t*)glob_script_mem.mfilt;
@ -1392,6 +1437,81 @@ uint32_t match_vars(char *dvnam, float **fp, char **sp, uint32_t *ind) {
}
#endif //USE_SCRIPT_GLOBVARS
char *isargs(char *lp, uint32_t isind) {
float fvar;
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
SCRIPT_SKIP_SPACES
if (*lp!='"') {
return lp;
}
lp++;
if (glob_script_mem.si_num[isind]>0 && glob_script_mem.last_index_string[isind]) {
free(glob_script_mem.last_index_string[isind]);
}
char *sstart = lp;
uint8_t slen = 0;
for (uint32_t cnt = 0; cnt<256; cnt++) {
if (*lp=='\n' || *lp=='"' || *lp==0) {
lp++;
if (cnt>0 && !slen) {
slen++;
}
glob_script_mem.siro_num[isind] = slen;
break;
}
if (*lp=='|') {
slen++;
}
lp++;
}
glob_script_mem.si_num[isind] = fvar;
if (glob_script_mem.si_num[isind]>0) {
if (glob_script_mem.si_num[isind]>MAX_SARRAY_NUM) {
glob_script_mem.si_num[isind] = MAX_SARRAY_NUM;
}
glob_script_mem.last_index_string[isind] = (char*)calloc(glob_script_mem.max_ssize*glob_script_mem.si_num[isind], 1);
for (uint32_t cnt = 0; cnt<glob_script_mem.siro_num[isind]; cnt++) {
char str[SCRIPT_MAXSSIZE];
GetTextIndexed(str, sizeof(str), cnt, sstart);
strlcpy(glob_script_mem.last_index_string[isind] + (cnt*glob_script_mem.max_ssize), str,glob_script_mem.max_ssize);
}
} else {
glob_script_mem.last_index_string[isind] = sstart;
}
lp++;
return lp;
}
char *isget(char *lp, char *sp, uint32_t isind) {
float fvar;
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
SCRIPT_SKIP_SPACES
char str[SCRIPT_MAXSSIZE];
str[0] = 0;
uint8_t index = fvar;
if (index<1) index = 1;
index--;
last_sindex = index;
glob_script_mem.sind_num = isind;
if (glob_script_mem.last_index_string[isind]) {
if (!glob_script_mem.si_num[isind]) {
if (index<=glob_script_mem.siro_num[isind]) {
GetTextIndexed(str, sizeof(str), index , glob_script_mem.last_index_string[isind]);
}
} else {
if (index>glob_script_mem.si_num[isind]) {
index = glob_script_mem.si_num[isind];
}
strlcpy(str,glob_script_mem.last_index_string[isind] + (index * glob_script_mem.max_ssize), glob_script_mem.max_ssize);
}
}
lp++;
if (sp) strlcpy(sp, str, glob_script_mem.max_ssize);
return lp;
}
// vtype => ff=nothing found, fe=constant number,fd = constant string else bit 7 => 80 = string, 0 = number
// no flash strings here for performance reasons!!!
@ -2085,6 +2205,93 @@ chknext:
len = 0;
goto exit;
}
if (!strncmp(vname, "fwa(", 4)) {
struct T_INDEX ind;
uint8_t vtype;
lp = isvar(lp + 4, &vtype, &ind, 0, 0, 0);
if (vtype!=VAR_NV && (vtype&STYPE)==0 && glob_script_mem.type[ind.index].bits.is_filter) {
// found array as result
} else {
// error
fvar = 0;
goto exit;
}
while (*lp==' ') lp++;
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
uint8_t index = fvar;
if (index>=SFS_MAX) index = SFS_MAX - 1;
if (glob_script_mem.file_flags[index].is_open) {
uint16_t len = 0;
float *fa = Get_MFAddr(glob_script_mem.type[ind.index].index, &len, 0);
char dstr[24];
for (uint32_t cnt = 0; cnt<len; cnt++) {
dtostrfd(*fa, glob_script_mem.script_dprec, dstr);
fa++;
if (cnt < (len - 1)) {
strcat(dstr,"\t");
} else {
strcat(dstr,"\n");
}
glob_script_mem.files[index].print(dstr);
}
} else {
fvar = 0;
}
lp++;
len = 0;
goto exit;
}
if (!strncmp(vname, "fra(", 4)) {
struct T_INDEX ind;
uint8_t vtype;
lp = isvar(lp + 4, &vtype, &ind, 0, 0, 0);
if (vtype!=VAR_NV && (vtype&STYPE)==0 && glob_script_mem.type[ind.index].bits.is_filter) {
// found array as result
} else {
// error
fvar = 0;
goto exit;
}
while (*lp==' ') lp++;
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
uint8_t find = fvar;
if (find>=SFS_MAX) find = SFS_MAX - 1;
char str[glob_script_mem.max_ssize + 1];
if (glob_script_mem.file_flags[find].is_open) {
uint16_t len = 0;
float *fa = Get_MFAddr(glob_script_mem.type[ind.index].index, &len, 0);
char dstr[24];
for (uint32_t cnt = 0; cnt<len; cnt++) {
uint8_t index = 0;
char *cp = str;
while (glob_script_mem.files[find].available()) {
uint8_t buf[1];
glob_script_mem.files[find].read(buf,1);
if (buf[0]=='\t' || buf[0]==',' || buf[0]=='\n' || buf[0]=='\r') {
break;
} else {
*cp++ = buf[0];
index++;
if (index>=glob_script_mem.max_ssize - 1) break;
}
}
*cp = 0;
*fa++=CharToFloat(str);
}
} else {
fvar = 0;
}
lp++;
len = 0;
goto exit;
}
#endif // USE_SCRIPT_FATFS_EXT
if (!strncmp(vname, "fl1(", 4) || !strncmp(vname, "fl2(", 4) ) {
uint8_t lknum = *(lp+2)&3;
@ -2202,7 +2409,6 @@ chknext:
}
#endif //USE_LIGHT
break;
#define MAX_SARRAY_NUM 32
case 'i':
if (!strncmp(vname, "int(", 4)) {
lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0);
@ -2212,77 +2418,35 @@ chknext:
goto exit;
}
if (!strncmp(vname, "is(", 3)) {
lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0);
SCRIPT_SKIP_SPACES
if (*lp!='"') {
break;
}
lp++;
if (glob_script_mem.si_num>0 && glob_script_mem.last_index_string) {
free(glob_script_mem.last_index_string);
}
char *sstart = lp;
uint8_t slen = 0;
for (uint32_t cnt = 0; cnt<256; cnt++) {
if (*lp=='\n' || *lp=='"' || *lp==0) {
lp++;
if (cnt>0 && !slen) {
slen++;
}
glob_script_mem.siro_num = slen;
break;
}
if (*lp=='|') {
slen++;
}
lp++;
}
glob_script_mem.si_num = fvar;
if (glob_script_mem.si_num>0) {
if (glob_script_mem.si_num>MAX_SARRAY_NUM) {
glob_script_mem.si_num = MAX_SARRAY_NUM;
}
glob_script_mem.last_index_string = (char*)calloc(glob_script_mem.max_ssize*glob_script_mem.si_num, 1);
for (uint32_t cnt = 0; cnt<glob_script_mem.siro_num; cnt++) {
char str[SCRIPT_MAXSSIZE];
GetTextIndexed(str, sizeof(str), cnt, sstart);
strlcpy(glob_script_mem.last_index_string + (cnt*glob_script_mem.max_ssize), str,glob_script_mem.max_ssize);
}
} else {
glob_script_mem.last_index_string = sstart;
}
lp++;
lp = isargs(lp + 3, 0);
fvar = 0;
len = 0;
goto exit;
}
if (!strncmp(vname, "is1(", 4)) {
lp = isargs(lp + 4, 1);
fvar = 0;
len = 0;
goto exit;
}
if (!strncmp(vname, "is2(", 4)) {
lp = isargs(lp + 4, 2);
fvar = 0;
len = 0;
goto exit;
}
if (!strncmp(vname, "is[", 3)) {
lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0);
SCRIPT_SKIP_SPACES
char str[SCRIPT_MAXSSIZE];
str[0] = 0;
uint8_t index = fvar;
if (index<1) index = 1;
index--;
last_sindex = index;
if (glob_script_mem.last_index_string) {
if (!glob_script_mem.si_num) {
if (index<=glob_script_mem.siro_num) {
GetTextIndexed(str, sizeof(str), index , glob_script_mem.last_index_string);
}
} else {
if (index>glob_script_mem.si_num) {
index = glob_script_mem.si_num;
}
strlcpy(str,glob_script_mem.last_index_string + (index * glob_script_mem.max_ssize), glob_script_mem.max_ssize);
}
}
lp++;
if (sp) strlcpy(sp, str, glob_script_mem.max_ssize);
lp = isget(lp + 3, sp, 0);
len = 0;
goto strexit;
}
if (!strncmp(vname, "is1[", 4)) {
lp = isget(lp + 4, sp, 1);
len = 0;
goto strexit;
}
if (!strncmp(vname, "is2[", 4)) {
lp = isget(lp + 4, sp, 2);
len = 0;
goto strexit;
}
@ -2614,7 +2778,7 @@ chknext:
if (!strncmp(vname, "s(", 2)) {
lp = GetNumericArgument(lp + 2, OPER_EQU, &fvar, 0);
char str[glob_script_mem.max_ssize + 1];
dtostrfd(fvar, glob_script_mem.script_dprec, str);
f2char(fvar, glob_script_mem.script_dprec, glob_script_mem.script_lzero, str);
if (sp) strlcpy(sp, str, glob_script_mem.max_ssize);
lp++;
len = 0;
@ -3260,6 +3424,7 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
uint16_t count;
uint8_t vtype;
uint8_t dprec = glob_script_mem.script_dprec;
uint8_t lzero = glob_script_mem.script_lzero;
float fvar;
cp = srcbuf;
struct T_INDEX ind;
@ -3273,16 +3438,21 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
dstbuf[count] = *cp++;
} else {
if (isdigit(*cp)) {
if (*(cp+1)=='.') {
lzero = *cp & 0xf;
cp+=2;
}
dprec = *cp & 0xf;
cp++;
} else {
dprec = glob_script_mem.script_dprec;
lzero = glob_script_mem.script_lzero;
}
if (*cp=='(') {
// math expression
cp++;
cp = GetNumericArgument(cp, OPER_EQU, &fvar, 0);
dtostrfd(fvar, dprec, string);
f2char(fvar, dprec, lzero, string);
uint8_t slen = strlen(string);
if (count + slen<dstsize - 1) {
strcpy(&dstbuf[count], string);
@ -3295,7 +3465,7 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
// found variable as result
if (vtype==NUM_RES || (vtype&STYPE)==0) {
// numeric result
dtostrfd(fvar, dprec, string);
f2char(fvar, dprec, lzero, string);
} else {
// string result
}
@ -3806,6 +3976,10 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) {
} else if (!strncmp(lp, "dp", 2) && isdigit(*(lp + 2))) {
lp += 2;
// number precision
if (*(lp + 1)== '.') {
glob_script_mem.script_lzero = atoi(lp);
lp+=2;
}
glob_script_mem.script_dprec = atoi(lp);
goto next_line;
}
@ -4153,9 +4327,9 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) {
#endif //USE_SCRIPT_GLOBVARS
if (saindex>=0) {
if (lastop==OPER_EQU) {
strlcpy(glob_script_mem.last_index_string + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
strlcpy(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
} else if (lastop==OPER_PLSEQU) {
strncat(glob_script_mem.last_index_string + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
strncat(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
}
last_sindex = -1;
} else {
@ -6182,6 +6356,7 @@ const char SCRIPT_MSG_GTE1[] PROGMEM = "'%s'";
#define MAX_GARRAY 4
char *gc_get_arrays(char *lp, float **arrays, uint8_t *ranum, uint16_t *rentries, uint16_t *ipos) {
struct T_INDEX ind;
uint8_t vtype;
@ -6659,7 +6834,7 @@ exgc:
for (uint32_t ind = 0; ind < anum; ind++) {
char acbuff[32];
float *fp = arrays[ind];
dtostrfd(fp[aind], glob_script_mem.script_dprec, acbuff);
f2char(fp[aind], glob_script_mem.script_dprec, glob_script_mem.script_lzero, acbuff);
WSContentSend_PD("%s", acbuff);
if (ind<anum - 1) { WSContentSend_PD(","); }
}

View File

@ -54,11 +54,6 @@ AudioFileSourceID3 *id3;
AudioGeneratorMP3 *decoder = NULL;
void *mp3ram = NULL;
#ifdef USE_WEBRADIO
AudioFileSourceICYStream *ifile = NULL;
AudioFileSourceBuffer *buff = NULL;
char wr_title[64];
//char status[64];
#ifdef ESP8266
const int preallocateBufferSize = 5*1024;
@ -69,6 +64,12 @@ const int preallocateCodecSize = 29192; // MP3 codec max mem needed
//const int preallocateCodecSize = 85332; // AAC+SBR codec max mem needed
#endif
#ifdef USE_WEBRADIO
AudioFileSourceICYStream *ifile = NULL;
AudioFileSourceBuffer *buff = NULL;
char wr_title[64];
//char status[64];
void *preallocateBuffer = NULL;
void *preallocateCodec = NULL;
uint32_t retryms = 0;

View File

@ -64,6 +64,7 @@ struct TTGO_globs {
bool bma_double_click = false;
bool bma_click = false;
bool bma_button = false;
bool power_ok = false;
} ttgo_globs;
@ -115,15 +116,19 @@ void TTGO_Init(void) {
ttgo_globs.bma->enableWakeupInterrupt(true);
ttgo_globs.bma->enableAnyNoMotionInterrupt(true);
ttgo_globs.bma->enableAccel();
#endif
#endif // USE_BMA423
}
void initPower(void) {
int ret = ttgo_globs.ttgo_power->begin(axpReadBytes, axpWriteBytes);
if (ret == AXP_FAIL) {
//DBGX("AXP Power begin failed");
// Serial.printf("AXP202 failed\n" );
} else {
I2cSetActiveFound(AXP202_SLAVE_ADDRESS, "AXP202");
ttgo_globs.power_ok = true;
// Serial.printf("AXP202 OK\n" );
//Change the button boot time to 4 seconds
ttgo_globs.ttgo_power->setShutdownTime(AXP_POWER_OFF_TIME_4S);
// Turn off the charging instructions, there should be no
@ -162,6 +167,7 @@ void initPower(void) {
portYIELD_FROM_ISR ();
}
}, FALLING);
}
}
@ -205,6 +211,8 @@ const char HTTP_TTGO_BMA[] PROGMEM =
void TTGO_WebShow(uint32_t json) {
if (ttgo_globs.power_ok == false) return;
TTGO_GetADC();
char vstring[32];
@ -276,8 +284,9 @@ int32_t ttgo_sleeptime;
ttgo_sleeptime = stime;
#ifdef USE_DISPLAY
DisplayOnOff(0);
#endif
if (ttgo_sleeptime>=0) {
// ligh sleep mode
WifiShutdown();
@ -311,7 +320,9 @@ int32_t ttgo_sleeptime;
if (ttgo_sleeptime) {
ttgo_globs.lenergy = false;
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_240M);
#ifdef USE_DISPLAY
DisplayOnOff(1);
#endif
} else {
while (ttgo_globs.lenergy == true) {
TTGO_loop(0);
@ -332,7 +343,9 @@ uint8_t data;
if (ttgo_globs.lenergy) {
ttgo_globs.lenergy = false;
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_240M);
#ifdef USE_DISPLAY
DisplayOnOff(1);
#endif
}
#ifdef USE_BMA423