All pastes #774060 Raw Edit

Something

public text v1 · immutable
·published 2007-11-14 23:04 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)) {
    var bf = bits.buf(32);

    bits.setfld(bf, 0, 32, v);
    forindex(i; me.actions) {
      me.actions[i](bf[i]?1:0);
    }
  } elsif (me.old == v) {
    me.stable = me.stable + 1;
  } else {
    me.stable = 0;
    me.old = v;
  }
}