include "ir.m"; ir := load Ir Ir->PATH; # for real remotes simir := load Ir Ir->SIMPATH; # for keyboard simulator init: fn(c, p: chan of int) : int; translate: fn(c: int) : int;
init
The init function takes the appropriate actions to initialize the device, and then spawns a process to return the c the codes on the irc channel and p the process id on the pid channel. This is done for both real and simulated devices. The init function is typically invoked once, such as by Mux, and the codes are then multiplexed between the applications. Most programs need not call init. translate
The translate function converts the device's raw codes into the constants defined by the module. For example, with the simulated remote control, translate('3') returns Three. The translate function is only necessary for programs that need to manage their own simulation of the remote control. Codes
implement Command; include "sys.m"; include "draw.m"; include "ir.m"; Command: module { init: fn(ref Draw->Context; list of string); }; init(ctxt: ref Draw->Context; argv: list of string); { sys:= load Sys Sys->PATH; # Tell mux to start sending input. ctxt.ctomux <-= Draw->AMstartinput; for(;;) { key:= <-ctxt.cir; sys->print("command %d\n", key); if(key == Ir->Enter) break; } # Tell mux this thread is going away. ctxt.ctomux <-= Draw->AMexit; }Programs such as Mux that drive the remote control directly must load the appropriate module and initialize it. This example uses the absence of a simulator module to infer that a real remote control is available.
implement Irtest; include "sys.m"; include "draw.m"; include "ir.m"; Irtest: module { init: fn(ctxt: ref Draw->Context, argv: list of string); }; init(nil: ref Draw->Context, nil: list of string) { sys:= load Sys Sys->PATH; # First try the keyboard Ir simulator. # If that is not present, use Ir directly. ir:= load Ir Ir->SIMPATH; if(ir == nil) ir = load Ir Ir->PATH; if(ir == nil){ sys->print("Ir module not loaded: %r\n"); return; } irc:= chan of int; pid:= chan of int; if(ir->init(irc,pid) < 0){ sys->print("Can't initialize Ir device: %r\n"); return; } <- pid; for(;;){ irval:= <-irc; sys->print("command %d\n", irval); } }
Draw Module in Chapter 11
|
Introduction to Limbo Modules in Chapter 7
|
limbo - Limbo compiler in Chapter 4
|