wipers.nas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # taken from b1900d
  2. var Wiper = {
  3. new : func(prop,power,settings){
  4. m = { parents : [Wiper] };
  5. m.direction = 1;
  6. m.delay_count = 0;
  7. m.spd_factor = 0;
  8. m.speed_prop=[];
  9. m.delay_prop=[];
  10. m.node = props.globals.getNode(prop,1);
  11. m.power = props.globals.getNode(power,1);
  12. if(m.power.getValue()==nil)m.power.setDoubleValue(0);
  13. m.position = m.node.getNode("position-norm", 1);
  14. m.position.setDoubleValue(0);
  15. m.switch = m.node.getNode("switch", 1);
  16. m.switch.setIntValue(0);
  17. for(var i=0; i<settings; i+=1) {
  18. append(m.speed_prop,m.node.getNode("arc-sec["~i~"]",1));
  19. if(m.speed_prop[i].getValue()==nil)m.speed_prop[i].setDoubleValue(i);
  20. append(m.delay_prop,m.node.getNode("delay-sec["~i~"]",1));
  21. if(m.delay_prop[i].getValue()==nil)m.delay_prop[i].setDoubleValue(i * 0.5);
  22. }
  23. return m;
  24. },
  25. active: func{
  26. if(me.power.getValue()<=5)return;
  27. var sw=me.switch.getValue();
  28. var sec =getprop("/sim/time/delta-sec");
  29. var spd_factor = 1/me.speed_prop[sw].getValue();
  30. var pos = me.position.getValue();
  31. if(sw==0){
  32. spd_factor = 1/me.speed_prop[1].getValue();
  33. if(pos <=0.45){
  34. me.position.setValue(0.45);
  35. return;
  36. }
  37. }
  38. if(pos >=1.000){
  39. me.direction=-1;
  40. }elsif(pos <=0){
  41. me.direction=1;
  42. var dly=me.delay_prop[sw].getValue();
  43. if(dly>0){
  44. me.direction=0;
  45. me.delay_count+=sec;
  46. if(me.delay_count >= dly){
  47. me.delay_count=0;
  48. me.direction=1;
  49. }
  50. }
  51. }
  52. var wiper_time = spd_factor*sec;
  53. pos =pos+(wiper_time * me.direction);
  54. me.position.setValue(pos);
  55. }
  56. };
  57. var passive = func{
  58. var pos = props.globals.getNode("/controls/electric/wiper/position-norm",1);
  59. var swt = props.globals.getNode("/controls/electric/wiper/switch").getValue() or 0;
  60. var pwr = props.globals.getNode("systems/electrical/volts").getValue() or 0;
  61. if (pwr <20){
  62. pos.setDoubleValue(0.45);
  63. }
  64. settimer(passive, 0);
  65. }
  66. passive();
  67. var wiper = Wiper.new("controls/electric/wiper","systems/electrical/volts",3);
  68. setlistener("/sim/signals/fdm-initialized", func {
  69. settimer(update_systems, 2);
  70. });
  71. var update_systems = func {
  72. power = getprop("/controls/switches/master-panel");
  73. volts = getprop("/systems/electrical/volts");
  74. wiper.active();
  75. settimer(update_systems, 0);
  76. }