Skip to content

Commit

Permalink
fix output[n].query() method
Browse files Browse the repository at this point in the history
  • Loading branch information
trentgill committed Jul 25, 2023
1 parent b392bd4 commit 8288928
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/l_crowlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion lib/lualink.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
20 changes: 19 additions & 1 deletion lua/output.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local Output = {}

Output.outputs = {1,2,3,4}

function Output.new( chan )
local o = { channel = chan
, level = 5.0
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -78,4 +94,6 @@ end

setmetatable(Output, Output) -- capture the metamethods

function soutput_handler(ch, v) Output.outputs[ch].receive(v) end

return Output

0 comments on commit 8288928

Please sign in to comment.