All pastes #774086 Raw Edit

Something

public text v1 · immutable
·published 2007-11-14 23:35 UTC
rendered paste body
############################################################
# Switch decoder: Decodes a bitmask in an int property.
# actions is a list of action functions: func (b) {...}
# Due to interpolation the decoder needs to wait for a
# stable input value.
SwitchDecoder = {};
SwitchDecoder.new = func (src, actions) {
  obj = { parents : [SwitchDecoder],
          old     : 0,
          stable  : 0,
          src     : src,
          actions : actions };
  return obj;
}
SwitchDecoder.update = func (d = 0) {
  if (d and (me.src == nil)) {
    print("SwitchDecoder[");
    debug.dump(me.src);
    print("]");
  }

  var v = me.src.getValue();
  if ((me.old == v) and (me.stable > 5)) {
    forindex(i; me.actions) {
      var m = math.mod(v, 2);
      me.actions[i](m?1:0);
      v = (v - m)/2;
    }
  } elsif (me.old == v) {
    me.stable += 1;
  } else {
    me.stable = 0;
    me.old = me.src.getValue();
  }
}