static int gprs_test( lua_State* L ) { const char *NMEA = luaL_checkstring( L, 1 ); double LAT; double LONG; double ALT; double DOP; char saida[15]; int ret = 0; ret = parse_gps((char*)NMEA, &LAT, &LONG, &ALT, &DOP); if( ret == 4 ) { c_memset(saida,0,15); c_sprintf(saida,"%.06f",LAT); lua_pushstring(L, saida); c_memset(saida,0,15); c_sprintf(saida,"%.06f",LONG); lua_pushstring(L, saida); c_memset(saida,0,15); c_sprintf(saida,"%.01f",ALT); lua_pushstring(L, saida); c_memset(saida,0,15); c_sprintf(saida,"%.01f",DOP); lua_pushstring(L, saida); return 4; // 4 é o número de valores a serem retornados } return 0; }
// Lua: table = wifi.ap.getclient() static int wifi_ap_listclient( lua_State* L ) { if (wifi_get_opmode() == STATION_MODE) { return luaL_error( L, "Can't list client in STATION_MODE mode" ); } char temp[64]; lua_newtable(L); struct station_info * station = wifi_softap_get_station_info(); struct station_info * next_station; while (station != NULL) { c_sprintf(temp, IPSTR, IP2STR(&station->ip)); lua_pushstring(L, temp); c_sprintf(temp, MACSTR, MAC2STR(station->bssid)); lua_setfield(L, -2, temp); next_station = STAILQ_NEXT(station, next); c_free(station); station = next_station; } return 1; }
// Lua: ip = wifi.ap.dhcp.config() static int wifi_ap_dhcp_config( lua_State* L ) { if (!lua_istable(L, 1)) return luaL_error( L, "wrong arg type" ); struct dhcps_lease lease; uint32_t ip; ip = parse_key(L, "start"); if (ip == 0) return luaL_error( L, "wrong arg type" ); lease.start_ip = ip; NODE_DBG(IPSTR, IP2STR(&lease.start_ip)); NODE_DBG("\n"); // use configured max_connection to determine end struct softap_config config; wifi_softap_get_config(&config); lease.end_ip = lease.start_ip; ip4_addr4(&lease.end_ip) += config.max_connection - 1; char temp[64]; c_sprintf(temp, IPSTR, IP2STR(&lease.start_ip)); lua_pushstring(L, temp); c_sprintf(temp, IPSTR, IP2STR(&lease.end_ip)); lua_pushstring(L, temp); // note: DHCP max range = 101 from start_ip to end_ip wifi_softap_dhcps_stop(); wifi_softap_set_dhcps_lease(&lease); wifi_softap_dhcps_start(); return 2; }
static void mqttConnectedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; MQTT_DBG("MQTT: Connected"); int i; char * buff = (char *)os_zalloc(64); uint32_t vdd33 = readvdd33(); sensor_data data; sensors_get_data(&data); c_sprintf(buff,"%f",data.sht21.temp); MQTT_Publish(&mqtt_client, "temperature/"SERIAL_NUMBER, buff, strlen(buff), 0, 1); os_memset(buff,0,64); c_sprintf(buff,"%f",data.sht21.hum); MQTT_Publish(&mqtt_client, "humidity/"SERIAL_NUMBER, buff, strlen(buff), 0, 1); os_memset(buff,0,64); c_sprintf(buff,"%f",(float)(vdd33/1000.0)); MQTT_Publish(&mqtt_client, "voltage/"SERIAL_NUMBER, buff, strlen(buff), 0, 1); os_free(buff); }
static int gprs_getlocation( lua_State* L ) { size_t len; //uint32_t timeout = luaL_checkinteger(L, 1)*1000; uint32_t period = luaL_checkinteger(L, 3); char *position; double LAT = 0.0f; double LONG = 0.0f; double ALT = 0.0f; double DOP = 0.0f; char saida[15]; char cmd[15]; int ret = 0; /* * $GPGGA,180857.000,2524.83983,S,04917.42591,W,1,03,3.1,0.0,M,,M,,0000*47 $GPGSA,A,2,09,23,22,,,,,,,,,,3.3,3.1,1.0*38 $GPGSV,3,1,09,09,37,236,26,23,53,188,34,22,49,061,28,30,02,319,*77 $GPGSV,3,2,09,26,20,117,,07,36,317,,06,18,230,,03,73,108,*77 $GPGSV,3,3,09,01,29,352,*4E $GPRMC,180857.000,A,2524.83983,S,04917.42591,W,0.70,315.11,120617,,,A*69 $GPVTG,315.11,T,,M,0.70,N,1.30,K,A*3F * */ if (ReadEnable == 0) { memset(cmd,0,15); c_sprintf(cmd,"%s=%d",GPSRD,period); if( sendCommonCommand(L,cmd) == 1) ReadEnable = 1; } //if( sendCommonCommand(L,cmd) == 1) if( (position = readCommand(NULL, "$GNACC", "$GNACC", GPSRDLOCAL, EOL, L)) != NULL ) { ret = parse_gps(position, &LAT, &LONG, &ALT, &DOP); if( ret == 4 ) { c_memset(saida,0,15); c_sprintf(saida,"%.6f",LAT); lua_pushstring(L, saida); c_memset(saida,0,15); c_sprintf(saida,"%.6f",LONG); lua_pushstring(L, saida); c_memset(saida,0,15); c_sprintf(saida,"%.1f",ALT); lua_pushstring(L, saida); c_memset(saida,0,15); c_sprintf(saida,"%.1f",DOP); lua_pushstring(L, saida); return 4; // 4 é o número de valores a serem retornados } } return 0; }
/** * @brief Wifi ap scan over callback to display. * @param arg: contain the aps information * @param status: scan over status * @retval None */ static void wifi_scan_done(void *arg, STATUS status) { uint8 ssid[33]; char temp[128]; if(wifi_scan_succeed == LUA_NOREF) return; if(arg == NULL) return; lua_rawgeti(gL, LUA_REGISTRYINDEX, wifi_scan_succeed); if (status == OK) { struct bss_info *bss_link = (struct bss_info *)arg; bss_link = bss_link->next.stqe_next;//ignore first lua_newtable( gL ); while (bss_link != NULL) { c_memset(ssid, 0, 33); if (c_strlen(bss_link->ssid) <= 32) { c_memcpy(ssid, bss_link->ssid, c_strlen(bss_link->ssid)); } else { c_memcpy(ssid, bss_link->ssid, 32); } if(getap_output_format==1) //use new format(BSSID : SSID, RSSI, Authmode, Channel) { c_sprintf(temp,"%s,%d,%d,%d", ssid, bss_link->rssi, bss_link->authmode, bss_link->channel); lua_pushstring(gL, temp); NODE_DBG(MACSTR" : %s\n",MAC2STR(bss_link->bssid) , temp); c_sprintf(temp,MACSTR, MAC2STR(bss_link->bssid)); lua_setfield( gL, -2, temp); } else//use old format(SSID : Authmode, RSSI, BSSID, Channel) { c_sprintf(temp,"%d,%d,"MACSTR",%d", bss_link->authmode, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel); lua_pushstring(gL, temp); lua_setfield( gL, -2, ssid ); NODE_DBG("%s : %s\n", ssid, temp); } bss_link = bss_link->next.stqe_next; } } else { lua_newtable( gL ); } lua_call(gL, 1, 0); if(wifi_scan_succeed != LUA_NOREF) { luaL_unref(gL, LUA_REGISTRYINDEX, wifi_scan_succeed); wifi_scan_succeed = LUA_NOREF; } }
static void tls_socket_dns_cb( const char* domain, const ip_addr_t *ip_addr, tls_socket_ud *ud ) { if (ud->self_ref == LUA_NOREF) return; ip_addr_t addr; if (ip_addr) addr = *ip_addr; else addr.addr = 0xFFFFFFFF; lua_State *L = lua_getstate(); if (ud->cb_dns_ref != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, ud->cb_dns_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref); if (addr.addr == 0xFFFFFFFF) { lua_pushnil(L); } else { char tmp[20]; c_sprintf(tmp, IPSTR, IP2STR(&addr.addr)); lua_pushstring(L, tmp); } lua_call(L, 2, 0); } if (addr.addr == 0xFFFFFFFF) { lua_gc(L, LUA_GCSTOP, 0); luaL_unref(L, LUA_REGISTRYINDEX, ud->self_ref); ud->self_ref = LUA_NOREF; lua_gc(L, LUA_GCRESTART, 0); } else { os_memcpy(ud->pesp_conn->proto.tcp->remote_ip, &addr.addr, 4); espconn_secure_connect(ud->pesp_conn); } }
// Lua: ip,port, local_port = sk:getpeer() static int net_socket_getpeer( lua_State* L ) { lnet_userdata *nud; const char *mt = "net.socket"; nud = (lnet_userdata *)luaL_checkudata(L, 1, mt); luaL_argcheck(L, nud, 1, "Server/Socket expected"); if(nud!=NULL && nud->pesp_conn!=NULL ){ char temp[20] = {0}; c_sprintf(temp, IPSTR, IP2STR( &(nud->pesp_conn->proto.tcp->remote_ip) ) ); if ( nud->pesp_conn->proto.tcp->remote_port != 0 ) { lua_pushstring( L, temp ); lua_pushinteger( L, nud->pesp_conn->proto.tcp->remote_port ); lua_pushinteger( L, nud->pesp_conn->proto.tcp->local_port ); } else { lua_pushnil( L ); lua_pushnil( L ); lua_pushnil( L ); } } else { lua_pushnil( L ); lua_pushnil( L ); lua_pushnil( L ); } return 3; }
// Lua: mac = wifi.xx.getmac() static int wifi_getmac( lua_State* L, uint8_t mode ) { char temp[64]; uint8_t mac[6]; wifi_get_macaddr(mode, mac); c_sprintf(temp, MACSTR, MAC2STR(mac)); lua_pushstring( L, temp ); return 1; }
// Lua: ip = wifi.xx.getip() static int wifi_getip( lua_State* L, uint8_t mode ) { struct ip_info pTempIp; char temp[64]; wifi_get_ip_info(mode, &pTempIp); if(pTempIp.ip.addr==0){ lua_pushnil(L); return 1; } else { c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.ip) ); lua_pushstring( L, temp ); c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.netmask) ); lua_pushstring( L, temp ); c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.gw) ); lua_pushstring( L, temp ); return 3; } }
// Lua: mac = wifi.xx.getmac() static int wifi_getmac( lua_State* L, uint8_t mode ) { char temp[64]; uint8_t mac[6]; wifi_get_macaddr(mode, mac); c_sprintf(temp, "%02X-%02X-%02X-%02X-%02X-%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); lua_pushstring( L, temp ); return 1; }
int lib_stdio_printf(struct machine_t *machine) { int string_index = TOP_INT(machine); char const *format = _REG_SS(_REGS(machine)) + string_index; char b[100]; c_sprintf(b, format, _REG_VIP(_REGS(machine)), -1); machine->printf(machine, b); return 1; }
static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { NODE_DBG("net_dns_found is called.\n"); struct espconn *pesp_conn = arg; if(pesp_conn == NULL){ NODE_DBG("pesp_conn null.\n"); return; } lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse; if(nud == NULL){ NODE_DBG("nud null.\n"); return; } if(nud->cb_dns_found_ref == LUA_NOREF){ NODE_DBG("cb_dns_found_ref null.\n"); return; } if(ipaddr == NULL) { NODE_ERR( "DNS Fail!\n" ); return; } // ipaddr->addr is a uint32_t ip char ip_str[20]; c_memset(ip_str, 0, sizeof(ip_str)); if(host_ip.addr == 0 && ipaddr->addr != 0) { c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr))); } if(nud->self_ref == LUA_NOREF){ NODE_DBG("self_ref null.\n"); return; } lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua lua_pushstring(gL, ip_str); // the ip para lua_call(gL, 2, 0); if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0) || (pesp_conn->type == ESPCONN_UDP && pesp_conn->proto.udp->remote_port == 0) ){ lua_gc(gL, LUA_GCSTOP, 0); if(nud->self_ref != LUA_NOREF){ luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref); nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self } lua_gc(gL, LUA_GCRESTART, 0); } }
static int gprs_switchgps( lua_State* L ) { size_t status = luaL_checkinteger( L, 2 ); char buffer[9]; buffer[8] = 0; c_sprintf(buffer,GPSONOFF,status); if( sendCommonCommand(L, buffer) == 1 ) { lua_pushinteger(L, 1); return 1; } return 0; }
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { size_t l; const char *s = luaL_checklstring(L, arg, &l); luaL_addchar(b, '"'); while (l--) { if (*s == '"' || *s == '\\' || *s == '\n') { luaL_addchar(b, '\\'); luaL_addchar(b, *s); } else if (*s == '\0' || iscntrl(uchar(*s))) { char buff[10]; if (!isdigit(uchar(*(s+1)))) c_sprintf(buff, "\\%d", (int)uchar(*s)); else c_sprintf(buff, "\\%03d", (int)uchar(*s)); luaL_addstring(b, buff); } else luaL_addchar(b, *s); s++; } luaL_addchar(b, '"'); }
static void ICACHE_FLASH_ATTR sensor_read_timer_cb(void *arg){ char * buff = (char *)os_zalloc(64); sensor_data data; sensors_get_data(&data); c_sprintf(buff,"%f",data.dht22.temp); MQTT_Publish(&mqtt_client, "temperature/"SERIAL_NUMBER, data.dht22.temp_string, strlen(data.dht22.temp_string), 0, 1); os_memset(buff,0,64); c_sprintf(buff,"%f",data.dht22.hum); MQTT_Publish(&mqtt_client, "humidity/"SERIAL_NUMBER, data.dht22.hum_string, strlen(data.dht22.hum_string), 0, 1); os_memset(buff,0,64); c_sprintf(buff,"%d",data.bmp180.press); MQTT_Publish(&mqtt_client, "pressure/"SERIAL_NUMBER, buff, strlen(buff), 0, 1); os_free(buff); }
void ICACHE_FLASH_ATTR init_params(){ user_esp_platform_load_param(&esp_param); char ver[32]; c_sprintf(ver, "SIZE:%u", sizeof(esp_param)); if(os_strncmp(esp_param.version, ver, strlen(ver))){ memset(&esp_param, 0, sizeof(esp_param)); strcpy(esp_param.version, ver); params_save(); } }
/** * @brief Wifi ap scan over callback to display. * @param arg: contain the aps information * @param status: scan over status * @retval None */ static void wifi_scan_done(void *arg, STATUS status) { uint8 ssid[33]; char temp[128]; if(wifi_scan_succeed == LUA_NOREF) return; if(arg == NULL) return; lua_rawgeti(gL, LUA_REGISTRYINDEX, wifi_scan_succeed); if (status == OK) { struct bss_info *bss_link = (struct bss_info *)arg; bss_link = bss_link->next.stqe_next;//ignore first lua_newtable( gL ); while (bss_link != NULL) { c_memset(ssid, 0, 33); if (c_strlen(bss_link->ssid) <= 32) { c_memcpy(ssid, bss_link->ssid, c_strlen(bss_link->ssid)); } else { c_memcpy(ssid, bss_link->ssid, 32); } c_sprintf(temp,"%d,%d,"MACSTR",%d", bss_link->authmode, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel); lua_pushstring(gL, temp); lua_setfield( gL, -2, ssid ); // NODE_DBG(temp); bss_link = bss_link->next.stqe_next; } } else { lua_pushnil(gL); } lua_call(gL, 1, 0); }
unsigned char gprs_SendHeader(lua_State *L, char *REQ) { int timeout = luaL_checkinteger(L, 1)*1000000; const char *URL = luaL_checkstring( L, 2 ); const char *PATH = luaL_checkstring( L, 3 ); const char *DATA = luaL_checkstring( L, 4 ); char Aux[BUFFER_SZ]; c_memset(Aux,0,BUFFER_SZ); c_sprintf(Aux, HEADER,REQ, PATH, URL, strlen(DATA) ); if( sendCommand(CIPSEND, ">", ">", strlen(CIPSEND), timeout) == 1 ) { sendCommand(Aux, EOL, EOL, strlen(Aux), timeout); return 1; } return 0; }
// Lua: s = net.dns.getdnsserver([index]) static int net_getdnsserver( lua_State* L ) { int numdns = luaL_optint(L, 1, 0); if (numdns >= DNS_MAX_SERVERS) return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1); ip_addr_t ipaddr; dns_getserver(numdns,&ipaddr); if ( ip_addr_isany(&ipaddr) ) { lua_pushnil( L ); } else { char temp[20] = {0}; c_sprintf(temp, IPSTR, IP2STR( &ipaddr ) ); lua_pushstring( L, temp ); } return 1; }
static int tls_socket_getpeer( lua_State *L ) { tls_socket_ud *ud = (tls_socket_ud *)luaL_checkudata(L, 1, "tls.socket"); luaL_argcheck(L, ud, 1, "TLS socket expected"); if(ud==NULL){ NODE_DBG("userdata is nil.\n"); return 0; } if(ud->pesp_conn && ud->pesp_conn->proto.tcp->remote_port != 0){ char temp[20] = {0}; c_sprintf(temp, IPSTR, IP2STR( &(ud->pesp_conn->proto.tcp->remote_ip) ) ); lua_pushstring( L, temp ); lua_pushinteger( L, ud->pesp_conn->proto.tcp->remote_port ); } else { lua_pushnil( L ); lua_pushnil( L ); } return 2; }
static void net_server_disconnected(void *arg) // for tcp server only { NODE_DBG("net_server_disconnected is called.\n"); struct espconn *pesp_conn = arg; if(pesp_conn == NULL) return; lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse; if(nud == NULL) return; if(gL == NULL) return; #if 0 char temp[20] = {0}; c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) ); NODE_DBG("remote "); NODE_DBG(temp); NODE_DBG(":"); NODE_DBG("%d",pesp_conn->proto.tcp->remote_port); NODE_DBG(" disconnected.\n"); #endif if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF) { lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref); lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua lua_call(gL, 1, 0); } int i; lua_gc(gL, LUA_GCSTOP, 0); for(i=0;i<MAX_SOCKET;i++){ if( (LUA_NOREF!=socket[i]) && (socket[i] == nud->self_ref) ){ // found the saved client nud->pesp_conn->reverse = NULL; nud->pesp_conn = NULL; // the espconn is made by low level sdk, do not need to free, delete() will not free it. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self luaL_unref(gL, LUA_REGISTRYINDEX, socket[i]); socket[i] = LUA_NOREF; socket_num--; break; } } lua_gc(gL, LUA_GCRESTART, 0); }
/** * wifi.sta.getconfig() * Description: * Get current Station configuration. * Note: if bssid_set==1 STATION is configured to connect to specified BSSID * if bssid_set==0 specified BSSID address is irrelevant. * Syntax: * ssid, pwd, bssid_set, bssid=wifi.sta.getconfig() * Parameters: * none * Returns: * SSID, Password, BSSID_set, BSSID */ static int wifi_station_getconfig( lua_State* L ) { struct station_config sta_conf; char bssid[17]; wifi_station_get_config(&sta_conf); if(sta_conf.ssid==0) { lua_pushnil(L); return 1; } else { lua_pushstring( L, sta_conf.ssid ); lua_pushstring( L, sta_conf.password ); lua_pushinteger( L, sta_conf.bssid_set); c_sprintf(bssid, MACSTR, MAC2STR(sta_conf.bssid)); lua_pushstring( L, bssid); return 4; } }
// Lua: s = net.dns.getdnsserver([index]) static int net_getdnsserver( lua_State* L ) { int numdns = luaL_optint(L, 1, 0); if (numdns >= DNS_MAX_SERVERS) return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1); // ip_addr_t ipaddr; // dns_getserver(numdns,&ipaddr); // Bug fix by @md5crypt https://github.com/nodemcu/nodemcu-firmware/pull/500 ip_addr_t ipaddr = dns_getserver(numdns); if ( ip_addr_isany(&ipaddr) ) { lua_pushnil( L ); } else { char temp[20] = {0}; c_sprintf(temp, IPSTR, IP2STR( &ipaddr.addr ) ); lua_pushstring( L, temp ); } return 1; }
// Lua: broadcast = wifi.xx.getbroadcast() static int wifi_getbroadcast( lua_State* L, uint8_t mode ) { struct ip_info pTempIp; char temp[64]; wifi_get_ip_info(mode, &pTempIp); if(pTempIp.ip.addr==0){ lua_pushnil(L); return 1; } else { struct ip_addr broadcast_address; uint32 subnet_mask32 = pTempIp.netmask.addr & pTempIp.ip.addr; uint32 broadcast_address32 = ~pTempIp.netmask.addr | subnet_mask32; broadcast_address.addr = broadcast_address32; c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&broadcast_address) ); lua_pushstring( L, temp ); return 1; } }
static void net_dns_static_cb(const char *name, ip_addr_t *ipaddr, void *callback_arg) { ip_addr_t addr; if (ipaddr != NULL) addr = *ipaddr; else addr.addr = 0xFFFFFFFF; int cb_ref = ((int*)callback_arg)[0]; c_free(callback_arg); lua_State *L = lua_getstate(); lua_rawgeti(L, LUA_REGISTRYINDEX, cb_ref); lua_pushnil(L); if (addr.addr != 0xFFFFFFFF) { char iptmp[20]; size_t ipl = c_sprintf(iptmp, IPSTR, IP2STR(&addr.addr)); lua_pushlstring(L, iptmp, ipl); } else { lua_pushnil(L); } lua_call(L, 2, 0); luaL_unref(L, LUA_REGISTRYINDEX, cb_ref); }
int gprs_StartSocket(lua_State *L) { int timeout = luaL_checkinteger(L, 1)*1000000; const char *URL = luaL_checkstring( L, 2 ); char Aux[BUFFER_SZ]; char cont; if( URL == NULL ) return 0; c_memset(Aux,0,BUFFER_SZ); c_sprintf(Aux, CIPSTART, URL ); sendCommand(GETSTATUS, MSG_OK, MSG_NOK, 2, timeout); sendCommand(GETSTATUS, MSG_OK, MSG_NOK, 2, timeout); //milisec(1000); for( cont = 0; cont < 5; cont++ ) { if( sendCommonCommand(L, Aux) == 1 ) return 1; else sendCommand(CIPCLOSE,MSG_OK,MSG_NOK,strlen(CIPCLOSE),2000000); milisec(500); } return 0; }
static int str_format (lua_State *L) { int top = lua_gettop(L); int arg = 1; size_t sfl; const char *strfrmt = luaL_checklstring(L, arg, &sfl); const char *strfrmt_end = strfrmt+sfl; luaL_Buffer b; luaL_buffinit(L, &b); while (strfrmt < strfrmt_end) { if (*strfrmt != L_ESC) luaL_addchar(&b, *strfrmt++); else if (*++strfrmt == L_ESC) luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format (`%...') */ char buff[MAX_ITEM]; /* to store the formatted item */ if (++arg > top) luaL_argerror(L, arg, "no value"); strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { c_sprintf(buff, form, (int)luaL_checknumber(L, arg)); break; } case 'd': case 'i': { addintlen(form); c_sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); break; } case 'o': case 'u': case 'x': case 'X': { addintlen(form); c_sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); break; } #if !defined LUA_NUMBER_INTEGRAL case 'e': case 'E': case 'f': case 'g': case 'G': { c_sprintf(buff, form, (double)luaL_checknumber(L, arg)); break; } #endif case 'q': { addquoted(L, &b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { size_t l; const char *s = luaL_checklstring(L, arg, &l); if (!c_strchr(form, '.') && l >= 100) { /* no precision and string is too long to be formatted; keep original string */ lua_pushvalue(L, arg); luaL_addvalue(&b); continue; /* skip the `addsize' at the end */ } else { c_sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " LUA_QL("format"), *(strfrmt - 1)); } } luaL_addlstring(&b, buff, c_strlen(buff)); } } luaL_pushresult(&b); return 1; }
int ICACHE_FLASH_ATTR ds18b20_read(ds18b20_data *read) { byte i; byte present = 0; byte type_s; byte data[12]; byte addr[8]; byte crc; float celsius; //float fahrenheit; DS18B20_DBG("Look for OneWire Devices on PIN =%d", gpioPin); if (!onewire_search(gpioPin, addr)) { DS18B20_DBG("No more addresses."); onewire_reset_search(gpioPin); delay_ms(250); return; } DS18B20_DBG("ADDRESS = %02x %02x %02x %02x %02x %02x %02x %02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); // the first ROM byte indicates which chip switch (addr[0]) { case 0x10: DS18B20_DBG(" Chip = DS18S20"); // or old DS1820 type_s = 1; break; case 0x28: DS18B20_DBG(" Chip = DS18B20"); type_s = 0; break; case 0x22: DS18B20_DBG(" Chip = DS1822"); type_s = 0; break; default: DS18B20_DBG("Device is not a DS18x20 family device."); return; } onewire_reset(gpioPin); onewire_select(gpioPin, addr); onewire_write(gpioPin, 0x44, 1); // start conversion, use ds.write(0x44,1) with parasite power on at the end delay_ms(1000); present = onewire_reset(gpioPin); onewire_select(gpioPin, addr); onewire_write(gpioPin, 0xBE, 1); // Read Scratchpad //DS18B20_DBG(" Present = %d", present); for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = onewire_read(gpioPin); DS18B20_DBG(" DATA: %02x ", data[i]); } crc = onewire_crc8(data, 8); DS18B20_DBG(" CRC=%d ", crc); if (crc != data[8]) { DS18B20_DBG("CRC is not valid!"); return; } // Convert the data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits // even when compiled on a 32 bit processor. int16_t raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; // 9 bit resolution default if (data[7] == 0x10) { // "count remain" gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time } celsius = (float) raw / 16.0; //fahrenheit = celsius * 1.8 + 32.0; read->temp = celsius; DS18B20_DBG("Temperature = "); char *temp_string = (char*) os_zalloc(64); c_sprintf(temp_string, "%f", read->temp); DS18B20_DBG(" %s Celsius", temp_string); os_free(temp_string); //DS18B20_DBG(" %2.2f Fahrenheit", fahrenheit); return 1; }
static void c_eml_qrsolve(const emlrtStack *sp, const emxArray_real_T *A, emxArray_real_T *B, emxArray_real_T *Y) { emxArray_real_T *b_A; emxArray_real_T *work; int32_T mn; int32_T i51; int32_T ix; emxArray_real_T *tau; emxArray_int32_T *jpvt; int32_T m; int32_T n; int32_T b_mn; emxArray_real_T *vn1; emxArray_real_T *vn2; int32_T k; boolean_T overflow; boolean_T b12; int32_T i; int32_T i_i; int32_T nmi; int32_T mmi; int32_T pvt; int32_T iy; boolean_T b13; real_T xnorm; int32_T i52; real_T atmp; real_T d16; boolean_T b14; boolean_T b_i; ptrdiff_t n_t; ptrdiff_t incx_t; double * xix0_t; boolean_T exitg1; const mxArray *y; static const int32_T iv78[2] = { 1, 8 }; const mxArray *m14; char_T cv76[8]; static const char_T cv77[8] = { '%', '%', '%', 'd', '.', '%', 'd', 'e' }; char_T cv78[14]; uint32_T unnamed_idx_0; emlrtStack st; emlrtStack b_st; emlrtStack c_st; emlrtStack d_st; emlrtStack e_st; emlrtStack f_st; emlrtStack g_st; emlrtStack h_st; st.prev = sp; st.tls = sp->tls; b_st.prev = &st; b_st.tls = st.tls; c_st.prev = &b_st; c_st.tls = b_st.tls; d_st.prev = &c_st; d_st.tls = c_st.tls; e_st.prev = &d_st; e_st.tls = d_st.tls; f_st.prev = &e_st; f_st.tls = e_st.tls; g_st.prev = &f_st; g_st.tls = f_st.tls; h_st.prev = &g_st; h_st.tls = g_st.tls; emlrtHeapReferenceStackEnterFcnR2012b(sp); emxInit_real_T(sp, &b_A, 2, &m_emlrtRTEI, true); b_emxInit_real_T(sp, &work, 1, &rb_emlrtRTEI, true); mn = (int32_T)muDoubleScalarMin(A->size[0], A->size[1]); st.site = &mc_emlrtRSI; b_st.site = &nc_emlrtRSI; c_st.site = &oc_emlrtRSI; i51 = b_A->size[0] * b_A->size[1]; b_A->size[0] = A->size[0]; b_A->size[1] = A->size[1]; emxEnsureCapacity(&c_st, (emxArray__common *)b_A, i51, (int32_T)sizeof(real_T), &m_emlrtRTEI); ix = A->size[0] * A->size[1]; for (i51 = 0; i51 < ix; i51++) { b_A->data[i51] = A->data[i51]; } b_emxInit_real_T(&c_st, &tau, 1, &m_emlrtRTEI, true); b_emxInit_int32_T(&c_st, &jpvt, 2, &m_emlrtRTEI, true); m = b_A->size[0]; n = b_A->size[1]; b_mn = muIntScalarMin_sint32(b_A->size[0], b_A->size[1]); i51 = tau->size[0]; tau->size[0] = b_mn; emxEnsureCapacity(&c_st, (emxArray__common *)tau, i51, (int32_T)sizeof(real_T), &n_emlrtRTEI); d_st.site = &mf_emlrtRSI; e_st.site = &rb_emlrtRSI; f_st.site = &sb_emlrtRSI; g_st.site = &tb_emlrtRSI; eml_signed_integer_colon(&g_st, b_A->size[1], jpvt); if ((b_A->size[0] == 0) || (b_A->size[1] == 0)) { } else { ix = b_A->size[1]; i51 = work->size[0]; work->size[0] = ix; emxEnsureCapacity(&c_st, (emxArray__common *)work, i51, (int32_T)sizeof (real_T), &m_emlrtRTEI); for (i51 = 0; i51 < ix; i51++) { work->data[i51] = 0.0; } b_emxInit_real_T(&c_st, &vn1, 1, &pb_emlrtRTEI, true); b_emxInit_real_T(&c_st, &vn2, 1, &qb_emlrtRTEI, true); d_st.site = &tc_emlrtRSI; ix = b_A->size[1]; i51 = vn1->size[0]; vn1->size[0] = ix; emxEnsureCapacity(&c_st, (emxArray__common *)vn1, i51, (int32_T)sizeof (real_T), &pb_emlrtRTEI); i51 = vn2->size[0]; vn2->size[0] = ix; emxEnsureCapacity(&c_st, (emxArray__common *)vn2, i51, (int32_T)sizeof (real_T), &qb_emlrtRTEI); k = 1; d_st.site = &nf_emlrtRSI; overflow = (b_A->size[1] > 2147483646); if (overflow) { e_st.site = &db_emlrtRSI; check_forloop_overflow_error(&e_st); } for (ix = 0; ix + 1 <= b_A->size[1]; ix++) { d_st.site = &sc_emlrtRSI; vn1->data[ix] = b_eml_xnrm2(&d_st, b_A->size[0], b_A, k); vn2->data[ix] = vn1->data[ix]; k += b_A->size[0]; } d_st.site = &rc_emlrtRSI; if (1 > b_mn) { b12 = false; } else { b12 = (b_mn > 2147483646); } if (b12) { e_st.site = &db_emlrtRSI; check_forloop_overflow_error(&e_st); } for (i = 1; i <= b_mn; i++) { i_i = (i + (i - 1) * m) - 1; nmi = n - i; mmi = m - i; d_st.site = &of_emlrtRSI; ix = eml_ixamax(&d_st, 1 + nmi, vn1, i); pvt = (i + ix) - 2; if (pvt + 1 != i) { d_st.site = &pf_emlrtRSI; e_st.site = &bc_emlrtRSI; f_st.site = &cc_emlrtRSI; ix = 1 + m * pvt; iy = 1 + m * (i - 1); g_st.site = &dc_emlrtRSI; if (1 > m) { b13 = false; } else { b13 = (m > 2147483646); } if (b13) { h_st.site = &db_emlrtRSI; check_forloop_overflow_error(&h_st); } for (k = 1; k <= m; k++) { i51 = b_A->size[0] * b_A->size[1]; xnorm = b_A->data[emlrtDynamicBoundsCheckFastR2012b(ix, 1, i51, &le_emlrtBCI, &f_st) - 1]; i51 = b_A->size[0] * b_A->size[1]; i52 = b_A->size[0] * b_A->size[1]; b_A->data[emlrtDynamicBoundsCheckFastR2012b(ix, 1, i51, &le_emlrtBCI, &f_st) - 1] = b_A->data[emlrtDynamicBoundsCheckFastR2012b(iy, 1, i52, &le_emlrtBCI, &f_st) - 1]; i51 = b_A->size[0] * b_A->size[1]; b_A->data[emlrtDynamicBoundsCheckFastR2012b(iy, 1, i51, &le_emlrtBCI, &f_st) - 1] = xnorm; ix++; iy++; } ix = jpvt->data[pvt]; jpvt->data[pvt] = jpvt->data[i - 1]; jpvt->data[i - 1] = ix; vn1->data[pvt] = vn1->data[i - 1]; vn2->data[pvt] = vn2->data[i - 1]; } if (i < m) { d_st.site = &qc_emlrtRSI; atmp = b_A->data[i_i]; d16 = 0.0; if (1 + mmi <= 0) { } else { e_st.site = &wc_emlrtRSI; xnorm = b_eml_xnrm2(&e_st, mmi, b_A, i_i + 2); if (xnorm != 0.0) { xnorm = muDoubleScalarHypot(b_A->data[i_i], xnorm); if (b_A->data[i_i] >= 0.0) { xnorm = -xnorm; } if (muDoubleScalarAbs(xnorm) < 1.0020841800044864E-292) { ix = 0; do { ix++; e_st.site = &xc_emlrtRSI; b_eml_xscal(&e_st, mmi, 9.9792015476736E+291, b_A, i_i + 2); xnorm *= 9.9792015476736E+291; atmp *= 9.9792015476736E+291; } while (!(muDoubleScalarAbs(xnorm) >= 1.0020841800044864E-292)); e_st.site = &yc_emlrtRSI; xnorm = b_eml_xnrm2(&e_st, mmi, b_A, i_i + 2); xnorm = muDoubleScalarHypot(atmp, xnorm); if (atmp >= 0.0) { xnorm = -xnorm; } d16 = (xnorm - atmp) / xnorm; e_st.site = &ad_emlrtRSI; b_eml_xscal(&e_st, mmi, 1.0 / (atmp - xnorm), b_A, i_i + 2); e_st.site = &bd_emlrtRSI; if (1 > ix) { b14 = false; } else { b14 = (ix > 2147483646); } if (b14) { f_st.site = &db_emlrtRSI; check_forloop_overflow_error(&f_st); } for (k = 1; k <= ix; k++) { xnorm *= 1.0020841800044864E-292; } atmp = xnorm; } else { d16 = (xnorm - b_A->data[i_i]) / xnorm; atmp = 1.0 / (b_A->data[i_i] - xnorm); e_st.site = &cd_emlrtRSI; b_eml_xscal(&e_st, mmi, atmp, b_A, i_i + 2); atmp = xnorm; } } } tau->data[i - 1] = d16; } else { atmp = b_A->data[i_i]; d_st.site = &pc_emlrtRSI; tau->data[i - 1] = eml_matlab_zlarfg(); } b_A->data[i_i] = atmp; if (i < n) { atmp = b_A->data[i_i]; b_A->data[i_i] = 1.0; d_st.site = &qf_emlrtRSI; eml_matlab_zlarf(&d_st, mmi + 1, nmi, i_i + 1, tau->data[i - 1], b_A, i + i * m, m, work); b_A->data[i_i] = atmp; } d_st.site = &rf_emlrtRSI; if (i + 1 > n) { b_i = false; } else { b_i = (n > 2147483646); } if (b_i) { e_st.site = &db_emlrtRSI; check_forloop_overflow_error(&e_st); } for (ix = i; ix + 1 <= n; ix++) { if (vn1->data[ix] != 0.0) { xnorm = muDoubleScalarAbs(b_A->data[(i + b_A->size[0] * ix) - 1]) / vn1->data[ix]; xnorm = 1.0 - xnorm * xnorm; if (xnorm < 0.0) { xnorm = 0.0; } atmp = vn1->data[ix] / vn2->data[ix]; atmp = xnorm * (atmp * atmp); if (atmp <= 1.4901161193847656E-8) { if (i < m) { d_st.site = &sf_emlrtRSI; e_st.site = &uc_emlrtRSI; if (mmi < 1) { xnorm = 0.0; } else { f_st.site = &vc_emlrtRSI; g_st.site = &vc_emlrtRSI; n_t = (ptrdiff_t)(mmi); g_st.site = &vc_emlrtRSI; incx_t = (ptrdiff_t)(1); i51 = b_A->size[0] * b_A->size[1]; i52 = (i + m * ix) + 1; xix0_t = (double *)(&b_A->data[emlrtDynamicBoundsCheckFastR2012b (i52, 1, i51, &vb_emlrtBCI, &f_st) - 1]); xnorm = dnrm2(&n_t, xix0_t, &incx_t); } vn1->data[ix] = xnorm; vn2->data[ix] = vn1->data[ix]; } else { vn1->data[ix] = 0.0; vn2->data[ix] = 0.0; } } else { d_st.site = &tf_emlrtRSI; vn1->data[ix] *= muDoubleScalarSqrt(xnorm); } } } } emxFree_real_T(&vn2); emxFree_real_T(&vn1); } atmp = 0.0; if (mn > 0) { xnorm = muDoubleScalarMax(A->size[0], A->size[1]) * muDoubleScalarAbs (b_A->data[0]) * 2.2204460492503131E-16; k = 0; exitg1 = false; while ((!exitg1) && (k <= mn - 1)) { if (muDoubleScalarAbs(b_A->data[k + b_A->size[0] * k]) <= xnorm) { st.site = &lc_emlrtRSI; y = NULL; m14 = emlrtCreateCharArray(2, iv78); for (i = 0; i < 8; i++) { cv76[i] = cv77[i]; } emlrtInitCharArrayR2013a(&st, 8, m14, cv76); emlrtAssign(&y, m14); b_st.site = &tg_emlrtRSI; emlrt_marshallIn(&b_st, c_sprintf(&b_st, b_sprintf(&b_st, y, emlrt_marshallOut(14.0), emlrt_marshallOut(6.0), &o_emlrtMCI), emlrt_marshallOut(xnorm), &p_emlrtMCI), "sprintf", cv78); st.site = &kc_emlrtRSI; b_eml_warning(&st, atmp, cv78); exitg1 = true; } else { atmp++; k++; } } } unnamed_idx_0 = (uint32_T)A->size[1]; i51 = Y->size[0]; Y->size[0] = (int32_T)unnamed_idx_0; emxEnsureCapacity(sp, (emxArray__common *)Y, i51, (int32_T)sizeof(real_T), &m_emlrtRTEI); ix = (int32_T)unnamed_idx_0; for (i51 = 0; i51 < ix; i51++) { Y->data[i51] = 0.0; } for (ix = 0; ix < mn; ix++) { if (tau->data[ix] != 0.0) { xnorm = B->data[ix]; i51 = A->size[0] + (int32_T)(1.0 - ((1.0 + (real_T)ix) + 1.0)); emlrtForLoopVectorCheckR2012b((1.0 + (real_T)ix) + 1.0, 1.0, A->size[0], mxDOUBLE_CLASS, i51, &ac_emlrtRTEI, sp); for (i = 0; i < i51; i++) { unnamed_idx_0 = ((uint32_T)ix + i) + 2U; xnorm += b_A->data[((int32_T)unnamed_idx_0 + b_A->size[0] * ix) - 1] * B->data[(int32_T)unnamed_idx_0 - 1]; } xnorm *= tau->data[ix]; if (xnorm != 0.0) { B->data[ix] -= xnorm; i51 = A->size[0] + (int32_T)(1.0 - ((1.0 + (real_T)ix) + 1.0)); emlrtForLoopVectorCheckR2012b((1.0 + (real_T)ix) + 1.0, 1.0, A->size[0], mxDOUBLE_CLASS, i51, &yb_emlrtRTEI, sp); for (i = 0; i < i51; i++) { unnamed_idx_0 = ((uint32_T)ix + i) + 2U; B->data[(int32_T)unnamed_idx_0 - 1] -= b_A->data[((int32_T) unnamed_idx_0 + b_A->size[0] * ix) - 1] * xnorm; } } } } emxFree_real_T(&tau); emlrtForLoopVectorCheckR2012b(1.0, 1.0, atmp, mxDOUBLE_CLASS, (int32_T)atmp, &xb_emlrtRTEI, sp); for (i = 0; i < (int32_T)atmp; i++) { Y->data[jpvt->data[i] - 1] = B->data[i]; } emlrtForLoopVectorCheckR2012b(atmp, -1.0, 1.0, mxDOUBLE_CLASS, (int32_T)-(1.0 + (-1.0 - atmp)), &wb_emlrtRTEI, sp); for (ix = 0; ix < (int32_T)-(1.0 + (-1.0 - atmp)); ix++) { xnorm = atmp + -(real_T)ix; Y->data[jpvt->data[(int32_T)xnorm - 1] - 1] = eml_div(Y->data[jpvt->data [(int32_T)xnorm - 1] - 1], b_A->data[((int32_T)xnorm + b_A->size[0] * ((int32_T)xnorm - 1)) - 1]); for (i = 0; i < (int32_T)(xnorm - 1.0); i++) { Y->data[jpvt->data[i] - 1] -= Y->data[jpvt->data[(int32_T)xnorm - 1] - 1] * b_A->data[i + b_A->size[0] * ((int32_T)xnorm - 1)]; } } emxFree_int32_T(&jpvt); emxFree_real_T(&work); emxFree_real_T(&b_A); emlrtHeapReferenceStackLeaveFcnR2012b(sp); }