static int _sock_getsockopt(lua_State *L) { int err; socket_t *sock = _getsock(L, 1); int level = luaL_checkint(L, 2); int optname = luaL_checkint(L, 3); socklen_t buflen = luaL_optunsigned(L, 4, 0); if(buflen > 1024) { return luaL_argerror(L, 4, "should less than 1024"); } if(buflen == 0) { int flag = 0; socklen_t flagsize = sizeof(flag); err = getsockopt(sock->fd, level, optname, (void*)&flag, &flagsize); if(err < 0) { goto failed; } lua_pushinteger(L, flag); } else { void *optval = lua_newuserdata(L, buflen); err = getsockopt(sock->fd, level, optname, optval, &buflen); if(err < 0) { goto failed; } lua_pushlstring(L, optval, buflen); } return 1; failed: lua_pushnil(L); lua_pushinteger(L, errno); return 2; }
/* integer width integer height integer size uinteger color string l/r/c ret: userdata */ static int lnewlabel(lua_State *L) { struct pack_label label; label.width = (int)luaL_checkinteger(L,1); label.height = (int)luaL_checkinteger(L,2); label.size = (int)luaL_checkinteger(L,3); label.color = (uint32_t)luaL_optunsigned(L,4,0xffffffff); const char * align = lua_tostring(L,5); if (align == NULL) { label.align = LABEL_ALIGN_LEFT; } else { switch(align[0]) { case 'l': case 'L': label.align = LABEL_ALIGN_LEFT; break; case 'r': case 'R': label.align = LABEL_ALIGN_RIGHT; break; case 'c': case 'C': label.align = LABEL_ALIGN_CENTER; break; default: return luaL_error(L, "Align must be left/right/center"); } } newlabel(L, &label); return 1; }
static int _sock_send(lua_State *L) { socket_t *sock = _getsock(L, 1); size_t len; const char* buf = luaL_checklstring(L, 2, &len); size_t from = luaL_optunsigned(L, 3, 0); int flags = 0; int nwrite; #ifdef MSG_NOSIGNAL flags = MSG_NOSIGNAL; #endif if (len <= from) { return luaL_argerror(L, 3, "should be less than length of argument #2"); } nwrite = send(sock->fd, buf+from, len - from, flags); if(nwrite < 0) { lua_pushnil(L); lua_pushinteger(L, errno); return 2; } lua_pushinteger(L, nwrite); return 1; }
static int lclear(lua_State *L) { uint32_t c = luaL_optunsigned(L, 1, 0xff000000); shader_clear(c); return 0; }
/*luadoc @function playNumber(value, unit [, attributes]) Play a numerical value (text to speech) @param value (number) number to play. Value is interpreted as integer. @param unit (number) unit identifier (see table todo) @param attributes (unsigned number) possible values: * `0 or not present` plays integral part of the number (for a number 123 it plays 123) * `PREC1` plays a number with one decimal place (for a number 123 it plays 12.3) * `PREC2` plays a number with two decimal places (for a number 123 it plays 1.23) @status current Introduced in 2.0.0 @notice 2.0 Only - automatic conversion of units for distance, speed, and temperature. OpenTX 2.0: | Unit | Sound | File (.wav) | Automatic conversion rules | | --- | --- | --- | --- | | 0 | | | | | 1 | Volts | 116 | | | 2 | Amps | 118 | | | 3 | Meters per Second | 120 | | | 4 | *missing file* | 122 | | | 5 | Kilometers per Hour / Miles per Hour | 124 / 142 | Input value is KPH | | 6 | Meters / Feet | 126 / 140 | Input value is meters | | 7 | Degrees | 128 | Input value is celsius, converted to Fahrenheit for Imperial | | 8 | Percent | 130 | | | 9 | Milliamps | 132 | | | 10 | Milliamp Hours | 134 | | | 11 | Watts | 136 | | | 12 | DB | 138 | | | 13 | Feet | 140 | | | 14 | Kilometers per Hour / Miles per Hour | 124 / 142 | Input value is in Knots, converted to KPH or MPH | | 15 | Hours | 144 | | | 16 | Minutes | 146 | | | 17 | Seconds | 148 | | | 18 | RPM | 150 | | | 19 | Gee | 152 | | | 20 | Degrees | 128 | | OpenTX 2.1: | 2.1 Unit | Sound | Sound File (.wav) | | --- | --- | --- | | 1 | Volts | 116 | | 2 | Amps | 118 | | 3 | Milliamps | 120 | | 4 | Knots | 122 | | 5 | Meters per Second | 124 | | 6 | Feet per Second | 126 | | 7 | Kilometers per Hour | 128 | | 8 | Miles per Hour | 130 | | 9 | Meters | 132 | | 10 | Feet | 134 | | 11 | Degrees Celsius | 136 | | 12 | Degrees Fahrenheit | 138 | | 13 | Percent | 140 | | 14 | Milliamp Hours | 142 | | 15 | Watts | 144 | | 16 | DB | 146 | | 17 | RPM | 148 | | 18 | Gee | 150 | | 19 | Degrees | 152 | | 20 | Milliliters | 154 | | 21 | Fluid Ounces | 156 | | 22 | Hours | 158 | | 23 | Minutes | 160 | | 24 | Seconds | 162 | */ static int luaPlayNumber(lua_State *L) { int number = luaL_checkinteger(L, 1); int unit = luaL_checkinteger(L, 2); unsigned int att = luaL_optunsigned(L, 3, 0); playNumber(number, unit, att, 0); return 0; }
/*luadoc @function lcd.drawNumber(x, y, value [, flags]) Display a number at (x,y) @param x,y (positive numbers) starting coordinate @param value (number) value to display @param flags (unsigned number) drawing flags: * `0 or not specified` normal representation * `PREC1` display with one decimal place (number 386 is displayed as 38.6) * `PREC2` display with tow decimal places (number 386 is displayed as 3.86) * other general LCD flag also apply @status current Introduced in 2.0.0 */ static int luaLcdDrawNumber(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int val = luaL_checkinteger(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); lcd_outdezAtt(x, y, val, att); return 0; }
/*luadoc @function lcd.drawTimer(x, y, value [, flags]) Display a value formatted as time at (x,y) @param x,y (positive numbers) starting coordinate @param value (number) time in seconds @param flags (unsigned number) drawing flags: * `0 or not specified` normal representation (minutes and seconds) * `TIMEHOUR` display hours * other general LCD flag also apply @status current Introduced in 2.0.0 */ static int luaLcdDrawTimer(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int seconds = luaL_checkinteger(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); putsTimer(x, y, seconds, att|LEFT, att); return 0; }
/*luadoc @function lcd.drawText(x, y, text [, flags]) Draws a text beginning at (x,y) @param x,y (positive numbers) starting coordinate @param text (string) text to display @param flags (unsigned number) drawing flags. All values can be combined together using the + character. ie BLINK + DBLSIZE. See the Appendix for available characters in each font set. * `0 or not specified` normal font * `XXLSIZE` jumbo sized font * `DBLSIZE` double size font * `MIDSIZE` mid sized font * `SMLSIZE` small font * `INVERS` inverted display * `BLINK` blinking text @status current Introduced in 2.0.0 */ static int luaLcdDrawText(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); const char * s = luaL_checkstring(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); lcd_putsAtt(x, y, s, att); return 0; }
/*luadoc @function lcd.drawSource(x, y, source [, flags]) Displays the name of the corresponding input as defined by the source at (x,y) @param x,y (positive numbers) starting coordinate @param source (number) source index @param flags (unsigned number) drawing flags @status current Introduced in 2.0.0 */ static int luaLcdDrawSource(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int s = luaL_checkinteger(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); putsMixerSource(x, y, s, att); return 0; }
/*luadoc @function lcd.drawFilledRectangle(x, y, w, h [, flags]) Draws a solid rectangle from top left corner (x,y) of specified width and height @param x,y (positive numbers) top left corner position @param w (number) width in pixels @param h (number) height in pixels @param flags (unsigned number) drawing flags @status current Introduced in 2.0.0 */ static int luaLcdDrawFilledRectangle(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); int h = luaL_checkinteger(L, 4); unsigned int flags = luaL_optunsigned(L, 5, 0); drawFilledRect(x, y, w, h, SOLID, flags); return 0; }
static int lclear(lua_State *L) { uint32_t c = luaL_optunsigned(L, 1, 0xff000000); float a = ((c >> 24) & 0xff ) / 255.0; float r = ((c >> 16) & 0xff ) / 255.0; float g = ((c >> 8) & 0xff ) / 255.0; float b = ((c >> 0) & 0xff ) / 255.0; glClearColor(r,g,b,a); glClear(GL_COLOR_BUFFER_BIT); return 0; }
/* int texture table float[16] uint32_t color uint32_t additive */ static int ldraw(lua_State *L) { int tex = (int)luaL_checkinteger(L,1); int texid = texture_glid(tex); if (texid == 0) { lua_pushboolean(L,0); return 1; } luaL_checktype(L, 2, LUA_TTABLE); uint32_t color = 0xffffffff; if (!lua_isnoneornil(L,3)) { color = (uint32_t)lua_tounsigned(L,3); } uint32_t additive = (uint32_t)luaL_optunsigned(L,4,0); shader_program(PROGRAM_PICTURE,additive); shader_texture(texid); int n = lua_rawlen(L, 2); int point = n/4; if (point * 4 != n) { return luaL_error(L, "Invalid polygon"); } #if !defined(_MSC_VER) float vb[n]; #else msvc::dynarray<float> vb(n); #endif int i; for (i=0;i<point;i++) { lua_rawgeti(L, 2, i*2+1); lua_rawgeti(L, 2, i*2+2); lua_rawgeti(L, 2, point*2+i*2+1); lua_rawgeti(L, 2, point*2+i*2+2); float tx = lua_tonumber(L, -4); float ty = lua_tonumber(L, -3); float vx = lua_tonumber(L, -2); float vy = lua_tonumber(L, -1); lua_pop(L,4); screen_trans(&vx,&vy); texture_coord(tex, &tx, &ty); vb[i*4+0] = vx + 1.0f; vb[i*4+1] = vy - 1.0f; vb[i*4+2] = tx; vb[i*4+3] = ty; } if (point == 4) { shader_draw(vb, color); } else { shader_drawpolygon(point, vb, color); } return 0; }
/* int texture table float[16] uint32_t color uint32_t additive */ static int ldraw(lua_State *L) { int tex = (int)luaL_checkinteger(L,1); int texid = texture_glid(tex); if (texid == 0) { lua_pushboolean(L,0); return 1; } luaL_checktype(L, 2, LUA_TTABLE); uint32_t color = 0xffffffff; if (!lua_isnoneornil(L,3)) { color = (uint32_t)lua_tounsigned(L,3); } uint32_t additive = (uint32_t)luaL_optunsigned(L,4,0); shader_program(PROGRAM_PICTURE); shader_texture(texid); int n = lua_rawlen(L, 2); int point = n/4; if (point * 4 != n) { return luaL_error(L, "Invalid polygon"); } ARRAY(struct vertex_pack, vb, point); int i; for (i=0;i<point;i++) { lua_rawgeti(L, 2, i*2+1); lua_rawgeti(L, 2, i*2+2); lua_rawgeti(L, 2, point*2+i*2+1); lua_rawgeti(L, 2, point*2+i*2+2); float tx = lua_tonumber(L, -4); float ty = lua_tonumber(L, -3); float vx = lua_tonumber(L, -2); float vy = lua_tonumber(L, -1); uint16_t u,v; lua_pop(L,4); screen_trans(&vx,&vy); texture_coord(tex, tx, ty, &u, &v); vb[i].vx = vx + 1.0f; vb[i].vy = vy - 1.0f; vb[i].tx = u; vb[i].ty = v; } if (point == 4) { shader_draw(vb, color, additive); } else { shader_drawpolygon(point, vb, color, additive); } return 0; }
/*luadoc @function lcd.drawCombobox(x, y, w, list, idx [, flags]) Draws a combo box @param x,y (positive numbers) top left corner position @param w (number) width of combo box in pixels @param list (table) combo box elements, each element is a string @param idx (integer) index of entry to highlight @param page (number) page number @param flags (unsigned number) drawing flags, the flags can not be combined: * `BLINK` combo box is expanded * `INVERS` combo box collapsed, text inversed * `0 or not present` combo box collapsed, text normal @status current Introduced in 2.0.0 */ static int luaLcdDrawCombobox(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); luaL_checktype(L, 4, LUA_TTABLE); int count = luaL_len(L, 4); /* get size of table */ int idx = luaL_checkinteger(L, 5); unsigned int flags = luaL_optunsigned(L, 6, 0); if (idx >= count) { // TODO error } if (flags & BLINK) { drawFilledRect(x, y, w-9, count*9+2, SOLID, ERASE); lcd_rect(x, y, w-9, count*9+2); for (int i=0; i<count; i++) { lua_rawgeti(L, 4, i+1); const char * item = luaL_checkstring(L, -1); lcd_putsAtt(x+2, y+2+9*i, item, 0); } drawFilledRect(x+1, y+1+9*idx, w-11, 9); drawFilledRect(x+w-10, y, 10, 11, SOLID, ERASE); lcd_rect(x+w-10, y, 10, 11); } else if (flags & INVERS) { drawFilledRect(x, y, w, 11); drawFilledRect(x+w-9, y+1, 8, 9, SOLID, ERASE); lua_rawgeti(L, 4, idx+1); const char * item = luaL_checkstring(L, -1); lcd_putsAtt(x+2, y+2, item, INVERS); } else { drawFilledRect(x, y, w, 11, SOLID, ERASE); lcd_rect(x, y, w, 11); drawFilledRect(x+w-10, y+1, 9, 9, SOLID); lua_rawgeti(L, 4, idx+1); const char * item = luaL_checkstring(L, -1); lcd_putsAtt(x+2, y+2, item, 0); } lcd_hline(x+w-8, y+3, 6); lcd_hline(x+w-8, y+5, 6); lcd_hline(x+w-8, y+7, 6); return 0; }
static int luaLcdDrawNumber(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); float val = luaL_checknumber(L, 3); unsigned int att = luaL_optunsigned(L, 4, 0); int n; if ((att & PREC2) == PREC2) n = val * 100; else if ((att & PREC1) == PREC1) n = val * 10; else n = val; lcd_outdezAtt(x, y, n, att); return 0; }
static int _sock_sendto(lua_State *L) { const char *host, *port, *buf; size_t from, len; int flags = 0; int err, nwrite; struct addrinfo *res = 0; socket_t *sock = _getsock(L, 1); host = luaL_checkstring(L, 2); luaL_checkint(L, 3); port = lua_tostring(L, 3); buf = luaL_checklstring(L, 4, &len); from = luaL_optunsigned(L, 5, 0); #ifdef MSG_NOSIGNAL flags = MSG_NOSIGNAL; #endif if (len <= from) { return luaL_argerror(L, 5, "should be less than length of argument #4"); } err = _getsockaddrarg(sock, host, port, &res); if(err != 0) { lua_pushnil(L); lua_pushinteger(L, err); return 1; } nwrite = sendto(sock->fd, buf + from, len - from, flags, res->ai_addr, res->ai_addrlen); if(nwrite < 0) { lua_pushnil(L); lua_pushinteger(L, errno); return 2; } lua_pushinteger(L, nwrite); return 1; }
/*luadoc @function lcd.drawChannel(x, y, source, flags) Display a telemetry value at (x,y) @param x,y (positive numbers) starting coordinate @param source can be a source identifier (number) or a source name (string). See getValue() @param flags (unsigned number) drawing flags @status current Introduced in 2.0.6, changed in 2.1.0 (only telemetry sources are valid) */ static int luaLcdDrawChannel(lua_State *L) { if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int channel = -1; if (lua_isnumber(L, 3)) { channel = luaL_checkinteger(L, 3); } else { const char * what = luaL_checkstring(L, 3); LuaField field; bool found = luaFindFieldByName(what, field); if (found) { channel = field.id; } } unsigned int att = luaL_optunsigned(L, 4, 0); getvalue_t value = getValue(channel); putsTelemetryChannelValue(x, y, (channel-MIXSRC_FIRST_TELEM)/3, value, att); return 0; }
template<> uint32 Eluna::CHECKVAL<uint32>(lua_State* L, int narg, uint32 def) { if (lua_isnoneornil(L, narg) || !lua_isnumber(L, narg)) return def; return luaL_optunsigned(L, narg, def); }
inline lua_Unsigned opt<lua_Unsigned>(lua_State* L, const lua_Unsigned& def) { LUNA_INDEX_ASSERT(L, arg); return luaL_optunsigned(L, arg); }
static int test_optunsigned (lua_State *L) { lua_Unsigned u = luaL_optunsigned(L, 1, 17u); lua_pushunsigned(L, u); return 1; }