From 828892869a3d199fba8e9ef8887fda831cc74721 Mon Sep 17 00:00:00 2001 From: Trent Gill Date: Mon, 24 Jul 2023 17:58:24 -0700 Subject: [PATCH] fix output[n].query() method --- lib/l_crowlib.c | 5 +++++ lib/lualink.c | 2 +- lua/output.lua | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/l_crowlib.c b/lib/l_crowlib.c index 528fb051..816daf7e 100644 --- a/lib/l_crowlib.c +++ b/lib/l_crowlib.c @@ -242,6 +242,11 @@ int l_crowlib_crow_reset( lua_State* L ){ lua_pushvalue(L, 2); // @4 copy of output[n] lua_pushstring(L, "none"); lua_call(L, 2, 0); + + // output[n].reset_events(output[n]) -- aka void method call + lua_getfield(L, 2, "reset_events"); // @3 + lua_pushvalue(L, 2); // @4 copy of output[n] + lua_call(L, 1, 0); } lua_settop(L, 0); diff --git a/lib/lualink.c b/lib/lualink.c index d04636d4..cc6d1578 100644 --- a/lib/lualink.c +++ b/lib/lualink.c @@ -819,7 +819,7 @@ static const struct luaL_Reg libCrow[]= , { "i2c_fastmode" , _i2c_set_timings } //, { "sys_cpu_load" , _sys_cpu } // io - // , { "get_state" , _get_state } + , { "get_state" , _get_state } , { "set_output_scale" , _set_scale } , { "io_get_input" , _io_get_input } , { "set_input_none" , _set_input_none } diff --git a/lua/output.lua b/lua/output.lua index 0ac580e5..0dd0b5ec 100644 --- a/lua/output.lua +++ b/lua/output.lua @@ -1,5 +1,7 @@ local Output = {} +Output.outputs = {1,2,3,4} + function Output.new( chan ) local o = { channel = chan , level = 5.0 @@ -13,9 +15,17 @@ function Output.new( chan ) , clock_div = 1 , ckcoro = false -- clock coroutine } - return setmetatable( o, Output ) + setmetatable( o, Output ) + o:reset_events() + Output.outputs[chan] = o -- save reference for callback engine + return o +end + +function Output:reset_events() + self.receive = function(v) _c.tell('output',self.channel,v) end end + function Output.clock(self, div) if type(div) == 'string' then -- 'off' or 'none' will cancel a running output clock if self.ckcoro then clock.cancel(self.ckcoro) end @@ -45,6 +55,8 @@ Output.__newindex = function(self, ix, val) self.asl:action() elseif ix == 'scale' then set_output_scale(self.channel, self.ji and just12(val) or val) + else + return rawset(self,ix,val) -- allows 'receive' handler to be written end end @@ -65,6 +77,10 @@ Output.__index = function(self, ix) set_output_scale(self.channel, table.unpack(args)) end elseif ix == 'dyn' then return self.asl.dyn + elseif ix == 'query' then + return function() soutput_handler(self.channel,LL_get_state(self.channel)) end + elseif ix == 'reset_events' then + return function() Output.reset_events(self) end end end @@ -78,4 +94,6 @@ end setmetatable(Output, Output) -- capture the metamethods +function soutput_handler(ch, v) Output.outputs[ch].receive(v) end + return Output