Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. loadScript("Logic/MenuItem.js");
  3.  
  4. function LED_dimmer(type, name, id)
  5. {
  6.     /* We must always call the parent constructor, initialization
  7.        of variables could be done here, but initialize is a better place */
  8.     this.Service(type, name, id);
  9. }
  10.  
  11. /* We inherit from Service with this call, it must always be next
  12.    after the constructor */
  13. extend(LED_dimmer, Service);
  14.  
  15. /* Declaration of instance variables, for static variables remove prototype */
  16.  
  17.  
  18. /* The display should regulary update and store the calendar */
  19.  
  20.  
  21. LED_dimmer.prototype.myRotaryService = null;
  22. LED_dimmer.prototype.myPWM = null;
  23. LED_dimmer.prototype.myOutput = null;
  24. LED_dimmer.prototype.myInput = null;
  25. LED_dimmer.prototype.pwmValue = 50;
  26. LED_dimmer.prototype.outputStatus = "Low";
  27. LED_dimmer.prototype.myInterval = null;
  28. LED_dimmer.prototype.oldPwmValue = 0;
  29. LED_dimmer.prototype.turnOffCnt = 0;
  30. const MovementTimeout = 45;
  31. const AdjustTimeout = 180;
  32. /*  */
  33. /*  */
  34.  
  35.  
  36. /* This function must always be declared, this is where all the startup code
  37.    should be placed. Gets called with arguments like what ids to use etc. */
  38. LED_dimmer.prototype.initialize = function(initialArguments)
  39. {
  40.     /* We must always call the parent initialize */
  41.     this.Service.prototype.initialize.call(this, initialArguments);
  42.  
  43.     /* This is used for function declarations like the callbacks below */
  44.     var self = this;
  45.  
  46.  
  47.     if (!this.myInitialArguments["RotaryHex"])
  48.     {
  49.         log(this.myName + ":" + this.myId + "> Failed to initialize, RotaryHex-config missing from config.\n");
  50.         return;
  51.     }
  52.  
  53.     if (!this.myInitialArguments["hwPWM"])
  54.     {
  55.         log(this.myName + ":" + this.myId + "> Failed to initialize, hwPWM-config missing from config.\n");
  56.         return;
  57.     }
  58.    
  59.     if (!this.myInitialArguments["output"])
  60.     {
  61.         log(this.myName + ":" + this.myId + "> Failed to initialize, output-config missing from config.\n");
  62.         return;
  63.     }
  64.     if (!this.myInitialArguments["input"])
  65.     {
  66.         log(this.myName + ":" + this.myId + "> Failed to initialize, input-config missing from config.\n");
  67.         return;
  68.     }
  69.  
  70.     this.myInterval = new Interval(function() { self.timerUpdate() }, 5000);
  71.     this.myInterval.start();
  72.  
  73.     /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
  74.     this.myRotaryService = ServiceManager.getService("Can", "RotaryHex", this.myInitialArguments["RotaryHex"]["Id"]);
  75.     /* Add a callback for when the encoder reports a new position */
  76.     this.myRotaryService.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate(args); });
  77.     /* Add a callback for when the encoder reports a new button status */
  78.     this.myRotaryService.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate(args); });
  79.     /* Add a callback for when the service goes online */
  80.     this.myRotaryService.registerEventCallback("online", function(args) { self.rotaryOnline(); });
  81.     /* If the service is already online we should call the handler here */
  82.     this.rotaryOnline();
  83.  
  84.     this.myPWM = ServiceManager.getService("Can", "hwPWM", this.myInitialArguments["hwPWM"]["Id"]);
  85.     this.myOutput = ServiceManager.getService("Can", "output", this.myInitialArguments["output"]["Id"]);
  86.     this.myInput = ServiceManager.getService("Can", "input", this.myInitialArguments["input"]["Id"]);
  87.     /* Add a callback for when the PIR reports detection */
  88.     this.myInput.registerEventCallback("newValue", function(args) { self.pirUpdate(args); });
  89.    
  90. }
  91.  
  92. LED_dimmer.prototype.timerUpdate = function()
  93. {
  94.     if (this.turnOffCnt > 0) {
  95.         this.turnOffCnt -= 5;
  96.         if (this.turnOffCnt < 0) {
  97.             this.turnOffCnt = 0;
  98.         }
  99.         if (this.turnOffCnt == 0) {
  100.             //Turn off light
  101.             log(this.myName + ":" + this.myId + "> Light off.\n");
  102.             this.oldPwmValue = this.myPWM.getValue(2);
  103.             this.myPWM.setPWMValue(0,2);
  104.         }
  105.     }
  106. }
  107.  
  108. LED_dimmer.prototype.rotaryOnline = function()
  109. {
  110. }
  111.  
  112. LED_dimmer.prototype.rotaryPosUpdate = function(SwitchId)
  113. {
  114.    
  115.     for (var i = 0; i < this.myRotaryService.getSteps(SwitchId); i++)
  116.     {
  117.         if (this.myRotaryService.getDirection(SwitchId) == "Clockwise")
  118.         {
  119.             this.pwmValue = this.pwmValue +10;
  120.             if (this.pwmValue > 100) {
  121.                 this.pwmValue = 100;
  122.             }
  123.         }
  124.         else
  125.         {
  126.             this.pwmValue = this.pwmValue -10;
  127.             if (this.pwmValue < 0) {
  128.                 this.pwmValue = 0;
  129.             }
  130.         }
  131.     }
  132.     if (this.turnOffCnt <  AdjustTimeout) {
  133.         this.turnOffCnt = AdjustTimeout;
  134.     }
  135. //log(this.myName + ":" + this.myId + "> Sent pwm value: "+ this.pwmValue+"\n");
  136.     this.myPWM.setPWMValue(this.pwmValue,2);
  137.     this.myPWM.setPWMValue(this.pwmValue,1);
  138. }
  139.  
  140. LED_dimmer.prototype.rotaryBtnUpdate = function(SwitchId)
  141. {
  142.     if (this.myRotaryService.getButtonStatus(SwitchId) == "Pressed") {
  143.         if (this.myPWM.getValue(2) == 0) {
  144.             if (this.oldPwmValue == 0) {
  145.                 this.oldPwmValue = 50;
  146.             }
  147.             this.myPWM.setPWMValue(this.oldPwmValue,2);
  148.         }
  149.         if (this.turnOffCnt <  AdjustTimeout) {
  150.             this.turnOffCnt = AdjustTimeout;
  151.         }
  152.         if (this.myOutput.getValue(0) == "High") {
  153.             this.myOutput.setPin("Low",0);
  154.         } else {
  155.             this.myOutput.setPin("High",0);
  156.         }
  157.     }
  158. }
  159.  
  160. LED_dimmer.prototype.pirUpdate = function(SwitchId)
  161. {
  162.     //log(this.myName + ":" + this.myId + "> Value is: "+ this.myInput.getValue(SwitchId)+"\n");
  163.     if (this.myInput.getValue(SwitchId) == "Low") {
  164.         //this.myOutput.setPin("High",0);
  165.         log(this.myName + ":" + this.myId + "> Trigg.\n");
  166.         if (this.myPWM.getValue(2) == 0) {
  167.             log(this.myName + ":" + this.myId + "> Light on.\n");
  168.             if (this.oldPwmValue == 0) {
  169.                 this.oldPwmValue = 50;
  170.             }
  171.             this.myPWM.setPWMValue(this.oldPwmValue,2);
  172.             if (this.turnOffCnt <  MovementTimeout) {
  173.                 this.turnOffCnt = MovementTimeout;
  174.             }
  175.         }
  176.     }
  177. }
  178.