Skip to content

Commit

Permalink
AP_Scripting: Add I2C transfer binding
Browse files Browse the repository at this point in the history
  • Loading branch information
IanBurwell committed Jun 12, 2024
1 parent 1d945cc commit f50bd7f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libraries/AP_Scripting/docs/docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,13 @@ local AP_HAL__I2CDevice_ud = {}
---@param address integer
function AP_HAL__I2CDevice_ud:set_address(address) end

-- Performs an I2C transfer, sending data_table bytes and
-- returning a table of any requested read bytes
---@param data_table table
---@param read_length integer
---@return table|nil
function AP_HAL__I2CDevice_ud:transfer(data_table, read_length) end

-- If no read length is provided a single register will be read and returned.
-- If read length is provided a table of register values are returned.
---@param register_num integer
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Scripting/generator/description/bindings.desc
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ ap_object AP_HAL::I2CDevice semaphore-pointer
ap_object AP_HAL::I2CDevice method set_retries void uint8_t 0 20
ap_object AP_HAL::I2CDevice method write_register boolean uint8_t'skip_check uint8_t'skip_check
ap_object AP_HAL::I2CDevice manual read_registers AP_HAL__I2CDevice_read_registers 2 1
ap_object AP_HAL::I2CDevice manual transfer AP_HAL__I2CDevice_transfer 2 1
ap_object AP_HAL::I2CDevice method set_address void uint8_t'skip_check

include AP_HAL/utility/Socket.h depends (AP_NETWORKING_ENABLED==1)
Expand Down
42 changes: 42 additions & 0 deletions libraries/AP_Scripting/lua_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,48 @@ int AP_HAL__I2CDevice_read_registers(lua_State *L) {
return success;
}

int AP_HAL__I2CDevice_transfer(lua_State *L) {
const int args = lua_gettop(L);
if (args != 3){
return luaL_argerror(L, args, "expected 2 arguments");
}
if (!lua_istable(L, 2)) {
return luaL_argerror(L, 2, "expected a table as the first argument");
}

AP_HAL::I2CDevice * ud = *check_AP_HAL__I2CDevice(L, 1);

// Parse table of bytes to send
uint32_t send_len = lua_rawlen(L, 2);
uint8_t send_data[send_len] = {};
for (unsigned int i = 0; i < send_len; i++){
lua_rawgeti(L, 2, i + 1); // Grab value from table
send_data[i] = get_uint8_t(L, -1); // Parse/check uint8_t
lua_pop(L, 1);
}

// Parse and setup rx buffer
uint32_t rx_len = get_uint32(L, 3, 0, UINT32_MAX);
uint8_t rx_data[rx_len];

// Transfer
ud->get_semaphore()->take_blocking();
const bool success = static_cast<bool>(ud->transfer(send_data, send_len, rx_data, rx_len));
ud->get_semaphore()->give();

// Return a table
if (success) {
// push to table
lua_newtable(L);
for (uint8_t i=0; i < rx_len; i++) {
lua_pushinteger(L, i+1);
lua_pushinteger(L, rx_data[i]);
lua_settable(L, -3);
}
}
return success;
}

int AP_HAL__UARTDriver_readstring(lua_State *L) {
binding_argcheck(L, 2);

Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Scripting/lua_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ int lua_mission_receive(lua_State *L);
int AP_Logger_Write(lua_State *L);
int lua_get_i2c_device(lua_State *L);
int AP_HAL__I2CDevice_read_registers(lua_State *L);
int AP_HAL__I2CDevice_transfer(lua_State *L);
int AP_HAL__UARTDriver_readstring(lua_State *L);
int lua_get_CAN_device(lua_State *L);
int lua_get_CAN_device2(lua_State *L);
Expand Down

0 comments on commit f50bd7f

Please sign in to comment.