Serial bindings were dependant on HAL_GCS_ENABLED but this is not ideal, it should be dependant on AP_SERIALMANAGER_ENABLED so that serial can function without GCS
Move the string checks into the load functions to avoid duplicating it
for each binding.
Also sync up the return types to avoid an unnecessary conversion.
Saves ~1.5K.
The Lua stack is guaranteed to have at least LUA_MINSTACK (default 20)
slots upon entry to C. Check to see if we might need more than that
minimum and only in that case call the function to check and resize the
stack. In virtually all cases the check can then be optimized away.
Additionally remove the redundant "Out of stack" message. Lua already
says "stack overflow" and a null message is valid.
Saves ~330B.
This adds bindings for an I2CDevice's transfer() function, an example,
and removes the nil return hint from the get_device() docs as it never
actually returns nil.
Only create the binding object (singleton metatable/userdata or C
function reference) once the user first references a particular
singleton or userdata creation function. Once created, the object is
stored into the script's environment so it doesn't get recreated on the
next reference and there isn't any further overhead. The userdatas are
no longer shared between scripts which imposes a slight memory penalty
for multiple scripts using the same singleton but this avoids an
additional lookup time cost.
Userdata and ap_objects aren't eligible for this optimization as the C++
code might want a particular metatable at any time.
Saves ~9.3K Lua heap.
The global table is then used as the __index metamethod of each state's
environment table. Avoids the overhead of loading binding objects into
each state. The binding objects are immutable from Lua so sandboxing is
not violated.
Does have the slight downside that a script can no longer know all the
binding names by enumerating _ENV.
Saves ~700B of memory per loaded script.
The __call metamethod was set to the metatable itself. With __call not
present, Lua will try to call the metatable (and fail), which is the
same behavior as with the __call metamethod set to the metatable.
Saves ~2K Lua heap.
Allows a script to simulate a device attached via any serial protocol.
The script can read and write data and have it handled according to the
protocol as if exchanged over a serial port. The script can then do
protocol translation, data filtering and validation,
hardware-in-the-loop simulation, experimentation, etc., especially in
combination with the scripting protocol which lets the script itself
handle an attached device and so interpose any communication.
Using `luaL_Buffer` avoids the need for any heap allocation in the
common case (count <= 512 bytes) and avoids stressing out the system
heap for large reads, instead using the script heap.
Zero net flash usage change.
Adding another layer instead of just exposing UARTDriver bindings allows
substitution of the different functions for device simulation later.
Also take the opportunity to rework the docs a little.
Passing -1 to the argument count for the `creation` tag (name does not
matter) will stop the generator from giving Lua a function to construct
that userdata. The C `new_<name>` function still works.