Skip to content

Commit

Permalink
feat(lua): add support for CRSF 'direct command' packets (#4630)
Browse files Browse the repository at this point in the history
Co-authored-by: Raphael Coeffic <[email protected]>
  • Loading branch information
JimB40 and raphaelcoeffic authored Feb 15, 2024
1 parent 07e0bfa commit 3ebff87
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions radio/src/lua/api_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,12 @@ When called without parameters, it will only return the status of the output buf
@status current Introduced in 2.2.0, retval nil added in 2.3.4
*/
static int luaCrossfireTelemetryPush(lua_State * L)
static int luaCrossfireTelemetryPush(lua_State* L)
{
bool external = (moduleState[EXTERNAL_MODULE].protocol == PROTOCOL_CHANNELS_CROSSFIRE);
bool internal = (moduleState[INTERNAL_MODULE].protocol == PROTOCOL_CHANNELS_CROSSFIRE);
bool external =
(moduleState[EXTERNAL_MODULE].protocol == PROTOCOL_CHANNELS_CROSSFIRE);
bool internal =
(moduleState[INTERNAL_MODULE].protocol == PROTOCOL_CHANNELS_CROSSFIRE);

if (!internal && !external) {
lua_pushnil(L);
Expand All @@ -1185,27 +1187,51 @@ static int luaCrossfireTelemetryPush(lua_State * L)

if (lua_gettop(L) == 0) {
lua_pushboolean(L, outputTelemetryBuffer.isAvailable());
}
else if (lua_gettop(L) > TELEMETRY_OUTPUT_BUFFER_SIZE ) {
} else if (lua_gettop(L) > TELEMETRY_OUTPUT_BUFFER_SIZE) {
lua_pushboolean(L, false);
return 1;
}
else if (outputTelemetryBuffer.isAvailable()) {
} else if (outputTelemetryBuffer.isAvailable()) {
uint8_t command = luaL_checkunsigned(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
uint8_t length = luaL_len(L, 2);

outputTelemetryBuffer.pushByte(MODULE_ADDRESS);
outputTelemetryBuffer.pushByte(2 + length); // 1(COMMAND) + data length + 1(CRC)
outputTelemetryBuffer.pushByte(command); // COMMAND
for (int i=0; i<length; i++) {
lua_rawgeti(L, 2, i+1);

// LENGTH
if (command == COMMAND_ID) {
// 1(COMMAND) + length(data) + 1(CRC_BA) + 1(CRC_D5)
outputTelemetryBuffer.pushByte(3 + length);
} else {
// 1(COMMAND) + length(data) + 1(CRC_D5)
outputTelemetryBuffer.pushByte(2 + length);
}

// COMMAND
outputTelemetryBuffer.pushByte(command);

// PAYLOAD
for (int i = 0; i < length; i++) {
lua_rawgeti(L, 2, i + 1);
outputTelemetryBuffer.pushByte(luaL_checkunsigned(L, -1));
}
outputTelemetryBuffer.pushByte(crc8(outputTelemetryBuffer.data + 2, 1 + length));

// CRC
if (command == COMMAND_ID) {
// 1 byte CRC8_BA (counted from COMMAND byte)
outputTelemetryBuffer.pushByte(
crc8_BA(outputTelemetryBuffer.data + 2, 1 + length));
// 1 byte CRC8_D5 (counted from COMMAND byte including CRC8_BA byte)
outputTelemetryBuffer.pushByte(
crc8(outputTelemetryBuffer.data + 2, 2 + length));
} else {
// 1 byte CRC8_D5 (counted from COMMAND byte)
outputTelemetryBuffer.pushByte(
crc8(outputTelemetryBuffer.data + 2, 1 + length));
}

outputTelemetryBuffer.setDestination(internal ? 0 : TELEMETRY_ENDPOINT_SPORT);
lua_pushboolean(L, true);
}
else {
} else {
lua_pushboolean(L, false);
}
return 1;
Expand Down

0 comments on commit 3ebff87

Please sign in to comment.