MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / lua_get_i2c_device

Function lua_get_i2c_device

libraries/AP_Scripting/lua_bindings.cpp:564–618  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

562#endif // HAL_LOGGING_ENABLED
563
564int lua_get_i2c_device(lua_State *L) {
565
566 // Allow : and . access
567 const int arg_offset = (luaL_testudata(L, 1, "i2c") != NULL) ? 1 : 0;
568
569 const int args = lua_gettop(L) - arg_offset;
570 if (args < 2) {
571 return luaL_argerror(L, args, "require i2c bus and address");
572 }
573 if (args > 4) {
574 return luaL_argerror(L, args, "too many arguments");
575 }
576
577 const lua_Integer bus_in = get_integer(L, 1 + arg_offset, 0, 4);
578 const uint8_t bus = static_cast<uint8_t>(bus_in);
579
580 const lua_Integer address_in = get_integer(L, 2 + arg_offset, 0, 128);
581 const uint8_t address = static_cast<uint8_t>(address_in);
582
583 // optional arguments, use the same defaults as the hal get_device function
584 uint32_t bus_clock = 400000;
585 bool use_smbus = false;
586
587 if (args > 2) {
588 bus_clock = coerce_to_uint32_t(L, 3 + arg_offset);
589
590 if (args > 3) {
591 use_smbus = static_cast<bool>(lua_toboolean(L, 4 + arg_offset));
592 }
593 }
594
595 auto *scripting = AP::scripting();
596
597 static_assert(SCRIPTING_MAX_NUM_I2C_DEVICE >= 0, "There cannot be a negative number of I2C devices");
598 if (scripting->num_i2c_devices >= SCRIPTING_MAX_NUM_I2C_DEVICE) {
599 return luaL_argerror(L, 1, "no i2c devices available");
600 }
601
602 scripting->_i2c_dev[scripting->num_i2c_devices] = NEW_NOTHROW AP_HAL::OwnPtr<AP_HAL::I2CDevice>;
603 if (scripting->_i2c_dev[scripting->num_i2c_devices] == nullptr) {
604 return luaL_argerror(L, 1, "i2c device nullptr");
605 }
606
607 *scripting->_i2c_dev[scripting->num_i2c_devices] = std::move(hal.i2c_mgr->get_device(bus, address, bus_clock, use_smbus));
608
609 if (scripting->_i2c_dev[scripting->num_i2c_devices] == nullptr || scripting->_i2c_dev[scripting->num_i2c_devices]->get() == nullptr) {
610 return luaL_argerror(L, 1, "i2c device nullptr");
611 }
612
613 *new_AP_HAL__I2CDevice(L) = scripting->_i2c_dev[scripting->num_i2c_devices]->get();
614
615 scripting->num_i2c_devices++;
616
617 return 1;
618}
619
620int AP_HAL__I2CDevice_read_registers(lua_State *L) {
621 const int args = lua_gettop(L);

Callers

nothing calls this directly

Calls 8

luaL_testudataFunction · 0.85
lua_gettopFunction · 0.85
luaL_argerrorFunction · 0.85
coerce_to_uint32_tFunction · 0.85
lua_tobooleanFunction · 0.85
scriptingFunction · 0.85
get_deviceMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected