This commit is contained in:
rusefillc 2024-02-23 22:00:07 -05:00 committed by rusefillc
parent 6b8e98715a
commit 3807d1f356
3 changed files with 50 additions and 16 deletions

View File

@ -30,10 +30,36 @@ function printPacket(bus, id, dlc, data)
print('Received ' .. arrayToString(data))
end
function onCanRx(bus, id, dlc, data)
count = 0
function onVehicleDetect(bus, id, dlc, data)
count = count + 1
if count < 10 then
print('onVehicleDetect ' ..id)
end
end
function onCanRx(bus, id, dlc, data)
count = count + 1
if count < 10 then
print('onVehicleDetect ' ..id)
end
end
canRxAddMask(0, 0xFFFFFF00, onVehicleDetect)
everySecondTimer = Timer.new()
keepAlive = 0
function onTick()
if everySecondTimer : getElapsedSeconds() > 1 then
everySecondTimer : reset()
keepAlive = keepAlive + 1
print("Alive " ..keepAlive .. " counter " .. count)
end
end
)", efi::size(config->luaScript));

View File

@ -326,12 +326,12 @@ static bool runOneLua(lua_Alloc alloc, const char* script) {
while (!needsReset && !chThdShouldTerminateX()) {
efitick_t beforeNt = getTimeNowNt();
#if EFI_CAN_SUPPORT
//#if EFI_CAN_SUPPORT
// First, process any pending can RX messages
totalRxCount += recentRxCount;
recentRxCount = doLuaCanRx(ls);
rxTime = getTimeNowNt() - beforeNt;
#endif // EFI_CAN_SUPPORT
//#endif // EFI_CAN_SUPPORT
// Next, check if there is a pending interactive command entered by the user
doInteractive(ls);

View File

@ -57,6 +57,7 @@ void processLuaCan(const size_t busIndex, const CANRxFrame& frame) {
static void handleCanFrame(LuaHandle& ls, CanFrameData* data) {
ScopePerf perf(PE::LuaOneCanRxCallback);
data->Callback = NO_CALLBACK;
if (data->Callback == NO_CALLBACK) {
// No callback, use catch-all function
lua_getglobal(ls, "onCanRx");
@ -103,27 +104,34 @@ static void handleCanFrame(LuaHandle& ls, CanFrameData* data) {
static bool doOneLuaCanRx(LuaHandle& ls) {
ScopePerf perf(PE::LuaOneCanRxFunction);
CanFrameData* data;
msg_t msg = filledBuffers.fetch(&data, TIME_IMMEDIATE);
CanFrameData x;
x.BusIndex = 1;
x.Frame.DLC = 8;
if (msg == MSG_TIMEOUT) {
// No new CAN messages rx'd, nothing more to do.
return false;
}
CanFrameData* data = &x;
if (msg != MSG_OK) {
// Message was otherwise not OK
// TODO: what do here?
return false;
}
// msg_t msg = filledBuffers.fetch(&data, TIME_IMMEDIATE);
//
// if (msg == MSG_TIMEOUT) {
// // No new CAN messages rx'd, nothing more to do.
// return false;
// }
//
// if (msg != MSG_OK) {
// // Message was otherwise not OK
// // TODO: what do here?
// return false;
// }
// We've accepted the frame, process it in Lua.
handleCanFrame(ls, data);
// We're done, return this frame to the free list
msg = freeBuffers.post(data, TIME_IMMEDIATE);
efiAssert(ObdCode::OBD_PCM_Processor_Fault, msg == MSG_OK, "lua can post to free buffer fail", false);
//msg_t msg = freeBuffers.post(data, TIME_IMMEDIATE);
//efiAssert(ObdCode::OBD_PCM_Processor_Fault, msg == MSG_OK, "lua can post to free buffer fail", false);
// We processed a frame so we should check again
return true;