|
| 1 | +/* |
| 2 | + * Copyright (C) EdgeTX |
| 3 | + * |
| 4 | + * (c) www.olliw.eu, OlliW, OlliW42 |
| 5 | + * |
| 6 | + * Based on code named |
| 7 | + * opentx - http://github.com/opentx/opentx |
| 8 | + * th9x - http://code.google.com/p/th9x |
| 9 | + * er9x - http://code.google.com/p/er9x |
| 10 | + * gruvin9x - http://code.google.com/p/gruvin9x |
| 11 | + * |
| 12 | + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html |
| 13 | + * |
| 14 | + * This program is free software; you can redistribute it and/or modify |
| 15 | + * it under the terms of the GNU General Public License version 2 as |
| 16 | + * published by the Free Software Foundation. |
| 17 | + * |
| 18 | + * This program is distributed in the hope that it will be useful, |
| 19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + * GNU General Public License for more details. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <ctype.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include "opentx.h" |
| 27 | +#include "lua_api.h" |
| 28 | +#include "telemetry/mavlink/mavlink_telem.h" |
| 29 | +#include "thirdparty/Mavlink/edgetx_lua_lib_constants.h" |
| 30 | +#include "thirdparty/Mavlink/edgetx_lua_lib_messages.h" |
| 31 | + |
| 32 | + |
| 33 | +static int luaMavlinkGetVersion(lua_State * L) |
| 34 | +{ |
| 35 | + lua_pushinteger(L, FASTMAVLINK_MAVLINK_VERSION); // this is the version reported also by the heartbeat |
| 36 | + return 1; |
| 37 | +} |
| 38 | + |
| 39 | +static int luaMavlinkGetChannelStatus(lua_State * L) |
| 40 | +{ |
| 41 | + lua_createtable(L, 0, 6); |
| 42 | + lua_pushtableinteger(L, "msg_rx_count", mavlinkTelem.msg_rx_count); |
| 43 | + lua_pushtableinteger(L, "msg_rx_per_sec", mavlinkTelem.msg_rx_persec); |
| 44 | + lua_pushtableinteger(L, "bytes_rx_per_sec", mavlinkTelem.bytes_rx_persec); |
| 45 | + lua_pushtableinteger(L, "msg_tx_count", mavlinkTelem.msg_tx_count); |
| 46 | + lua_pushtableinteger(L, "msg_tx_per_sec", mavlinkTelem.msg_tx_persec); |
| 47 | + lua_pushtableinteger(L, "bytes_tx_per_sec", mavlinkTelem.bytes_tx_persec); |
| 48 | + return 1; |
| 49 | +} |
| 50 | + |
| 51 | +//-- mavlink api -- |
| 52 | + |
| 53 | +static int luaMavlinkGetSystemId(lua_State *L) |
| 54 | +{ |
| 55 | + lua_pushinteger(L, mavlinkTelem.systemSysId()); |
| 56 | + return 1; |
| 57 | +} |
| 58 | + |
| 59 | +static int luaMavlinkGetAutopilotIds(lua_State *L) |
| 60 | +{ |
| 61 | + lua_pushinteger(L, mavlinkTelem.systemSysId()); |
| 62 | + lua_pushinteger(L, mavlinkTelem.autopilotCompId()); |
| 63 | + return 1; |
| 64 | +} |
| 65 | + |
| 66 | +static int luaMavlinkGetCameraIds(lua_State *L) |
| 67 | +{ |
| 68 | + lua_pushinteger(L, mavlinkTelem.systemSysId()); |
| 69 | + lua_pushinteger(L, mavlinkTelem.cameraCompId()); |
| 70 | + return 1; |
| 71 | +} |
| 72 | + |
| 73 | +static int luaMavlinkGetGimbalIds(lua_State *L) |
| 74 | +{ |
| 75 | + lua_pushinteger(L, mavlinkTelem.systemSysId()); |
| 76 | + lua_pushinteger(L, mavlinkTelem.gimbalCompId()); |
| 77 | + return 1; |
| 78 | +} |
| 79 | + |
| 80 | +static int luaMavlinkGetGimbalManagerIds(lua_State *L) |
| 81 | +{ |
| 82 | + lua_pushinteger(L, mavlinkTelem.systemSysId()); |
| 83 | + lua_pushinteger(L, mavlinkTelem.gimbalManagerCompId()); |
| 84 | + return 1; |
| 85 | +} |
| 86 | + |
| 87 | +//-- mavlink api, messages -- |
| 88 | + |
| 89 | +static int luaMavlinkInEnable(lua_State *L) |
| 90 | +{ |
| 91 | + bool flag = (luaL_checkinteger(L, 1) > 0); |
| 92 | + mavlinkTelem.mavapiMsgInEnable(flag); |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +static int luaMavlinkInCount(lua_State *L) |
| 97 | +{ |
| 98 | + lua_pushinteger(L, mavlinkTelem.mavapiMsgInCount()); |
| 99 | + return 1; |
| 100 | +} |
| 101 | + |
| 102 | +static int luaMavlinkGetMessage(lua_State *L) |
| 103 | +{ |
| 104 | + int msgid = luaL_checknumber(L, 1); |
| 105 | + |
| 106 | + MavlinkTelem::MavMsg* mavmsg = mavlinkTelem.mavapiMsgInGet(msgid); |
| 107 | + if (!mavmsg) { |
| 108 | + lua_pushnil(L); |
| 109 | + } |
| 110 | + else { |
| 111 | + luaMavlinkPushMavMsg(L, mavmsg); |
| 112 | + mavmsg->updated = false; |
| 113 | + } |
| 114 | + return 1; |
| 115 | +} |
| 116 | + |
| 117 | +static int luaMavlinkGetMessageLast(lua_State *L) |
| 118 | +{ |
| 119 | + MavlinkTelem::MavMsg* mavmsg = mavlinkTelem.mavapiMsgInGetLast(); |
| 120 | + if (!mavmsg) { |
| 121 | + lua_pushnil(L); |
| 122 | + } |
| 123 | + else { |
| 124 | + luaMavlinkPushMavMsg(L, mavmsg); |
| 125 | + } |
| 126 | + return 1; |
| 127 | +} |
| 128 | + |
| 129 | +static int luaMavlinkOutEnable(lua_State *L) |
| 130 | +{ |
| 131 | + bool flag = (luaL_checkinteger(L, 1) > 0); |
| 132 | + mavlinkTelem.mavapiMsgOutEnable(flag); |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +static int luaMavlinkIsFree(lua_State *L) |
| 137 | +{ |
| 138 | + lua_pushboolean(L, (mavlinkTelem.mavapiMsgOutPtr() != NULL)); |
| 139 | + return 1; |
| 140 | +} |
| 141 | + |
| 142 | +static int luaMavlinkSendMessage(lua_State *L) |
| 143 | +{ |
| 144 | + fmav_message_t* msg_out = mavlinkTelem.mavapiMsgOutPtr(); |
| 145 | + |
| 146 | + if (!lua_istable(L, -1) || !msg_out) { |
| 147 | + lua_pushnil(L); |
| 148 | + } |
| 149 | + else if (luaMavlinkCheckMsgOut(L, msg_out)) { |
| 150 | + mavlinkTelem.mavapiMsgOutSet(); |
| 151 | + lua_pushboolean(L, true); |
| 152 | + } |
| 153 | + else { |
| 154 | + lua_pushboolean(L, false); |
| 155 | + } |
| 156 | + return 1; |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +//------------------------------------------------------------ |
| 161 | +// mavlink luaL and luaR arrays |
| 162 | +//------------------------------------------------------------ |
| 163 | + |
| 164 | +const luaL_Reg mavlinkLib[] = { |
| 165 | + { "getVersion", luaMavlinkGetVersion }, |
| 166 | + { "getChannelStatus", luaMavlinkGetChannelStatus }, |
| 167 | + |
| 168 | + { "getSystemId", luaMavlinkGetSystemId }, |
| 169 | + { "getAutopilotIds", luaMavlinkGetAutopilotIds }, |
| 170 | + { "getCameraIds", luaMavlinkGetCameraIds }, |
| 171 | + { "getGimbalIds", luaMavlinkGetGimbalIds }, |
| 172 | + { "getGimbalManagerIds", luaMavlinkGetGimbalManagerIds }, |
| 173 | + |
| 174 | + { "enableIn", luaMavlinkInEnable }, |
| 175 | + { "getInCount", luaMavlinkInCount }, |
| 176 | + { "getMessage", luaMavlinkGetMessage }, |
| 177 | + { "getMessageLast", luaMavlinkGetMessageLast }, |
| 178 | + { "enableOut", luaMavlinkOutEnable }, |
| 179 | + { "isFree", luaMavlinkIsFree }, |
| 180 | + { "sendMessage", luaMavlinkSendMessage }, |
| 181 | + |
| 182 | + { nullptr, nullptr } /* sentinel */ |
| 183 | +}; |
| 184 | + |
| 185 | +const luaR_value_entry mavlinkConstants[] = { |
| 186 | + MAVLINK_LIB_CONSTANTS |
| 187 | + |
| 188 | + { nullptr, 0 } /* sentinel */ |
| 189 | +}; |
| 190 | + |
0 commit comments