Subversion Repositories HomeAutomation

Rev

Rev 1344 | Blame | Last modification | View Log | SVN | RSS feed

  1.  
  2. loadScript("Logic/gLCD_MenuItems/MainMenuItem.js");
  3. loadScript("Logic/gLCD_MenuItems/SettingsMenuItem.js");
  4. loadScript("Logic/gLCD_MenuItems/DimmerGlcdMenuItem.js");
  5.  
  6. function DorrDisplay(type, name, id)
  7. {
  8.     /* We must always call the parent constructor, initialization
  9.        of variables could be done here, but initialize is a better place */
  10.     this.Service(type, name, id);
  11. }
  12.  
  13. /* We inherit from Service with this call, it must always be next
  14.    after the constructor */
  15. extend(DorrDisplay, Service);
  16.  
  17. /* Declaration of instance variables, for static variables remove prototype */
  18.  
  19. DorrDisplay.prototype.myGLCDService = null;
  20. DorrDisplay.prototype.myRotaryService1 = null;
  21.  
  22. DorrDisplay.prototype.myInterval = null;
  23. DorrDisplay.prototype.myIntervalAlways = null;
  24.  
  25. DorrDisplay.prototype.MainMenuItem = null;
  26. DorrDisplay.prototype.SettingMenuItem = null;
  27. DorrDisplay.prototype.DimmerGlcdMenuItem = null;
  28.  
  29. /* The currently displayed menuitem */
  30. DorrDisplay.prototype.currentMenuItem = null;
  31.  
  32. /* counter for going to screensaver */
  33. DorrDisplay.prototype.screenSaverCnt = null;
  34.  
  35. /* counter for going to main screen */
  36. DorrDisplay.prototype.mainScreenCnt = null;
  37.  
  38. const DorrDisplay_mainScreenTimeout = 20;
  39. const DorrDisplay_screenSaverTimeout = 25;
  40.  
  41. DorrDisplay.prototype.defaultBacklight = 60;
  42.  
  43. /* This function must always be declared, this is where all the startup code
  44.    should be placed. Gets called with arguments like what ids to use etc. */
  45. DorrDisplay.prototype.initialize = function(initialArguments)
  46. {
  47.     /* We must always call the parent initialize */
  48.     this.Service.prototype.initialize.call(this, initialArguments);
  49.  
  50.  
  51.  
  52.     /* This is used for function declarations like the callbacks below */
  53.     var self = this;
  54.  
  55.     if (!this.myInitialArguments["KS0108"])
  56.     {
  57.         log(this.myName + ":" + this.myId + "> Failed to initialize, KS0108-config missing from config.\n");
  58.         return;
  59.     }
  60.    
  61.     if (!this.myInitialArguments["Rotary"])
  62.     {
  63.         log(this.myName + ":" + this.myId + "> Failed to initialize, Rotary-config missing from config.\n");
  64.         return;
  65.     }
  66.  
  67.     /* Start interval timer for sending timestamp to network. Arguments are the callback function and time in milliseconds
  68.     used to make sure an eth-node gets its init packet (600s) */
  69.     this.myIntervalAlways = new Interval(function() { self.sendTimeStamp() }, 600000);
  70.     this.myIntervalAlways.start();
  71.  
  72.     this.myInterval = new Interval(function() { self.timerUpdate() }, 5000);
  73.     this.myInterval.start();
  74.  
  75.     this.screenSaverCnt = 0;
  76.     this.mainScreenCnt = 0;
  77.  
  78.    
  79.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  80.     this.myGLCDService = ServiceManager.getService("Can", "KS0108", this.myInitialArguments["KS0108"]["Id"]);
  81.     /* Add a callback for when the service goes online */
  82.     this.myGLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
  83.     /* If the service is already online we should call the handler here */
  84.     this.lcdOnline();
  85.     /* Add a callback for when the service goes offline */
  86.     this.myGLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
  87.    
  88.     /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
  89.     this.myRotaryService1 = ServiceManager.getService("Can", "Rotary", this.myInitialArguments["Rotary"]["Id"]);
  90.     /* Add a callback for when the encoder reports a new position */
  91.     this.myRotaryService1.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate1(args); });
  92.     /* Add a callback for when the encoder reports a new button status */
  93.     this.myRotaryService1.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate1(args); });
  94.     /* Add a callback for when the service goes online */
  95.     this.myRotaryService1.registerEventCallback("online", function(args) { self.rotaryOnline(); });
  96.     /* If the service is already online we should call the handler here */
  97.     this.rotaryOnline();
  98.  
  99.         /* create the first menu item */
  100.     this.MainMenuItem = new MainMenuItem(this, this.myGLCDService);
  101. this.SettingMenuItem = new SettingsMenuItem(this, this.myGLCDService);
  102. this.DimmerGlcdMenuItem = new DimmerGlcdMenuItem(this, this.myGLCDService);
  103.    
  104.     this.currentMenuItem = this.MainMenuItem;
  105.  
  106.    
  107.     /* create the screensaver menu item */
  108.     this.SettingMenuItem.LeftItem=this.MainMenuItem;
  109.     this.SettingMenuItem.RightItem=this.DimmerGlcdMenuItem;
  110.  
  111.     /* set the function that shall be executed when knob is turned */
  112.     /* create the screensaver menu item */
  113.     this.MainMenuItem.LeftItem=this.DimmerGlcdMenuItem;
  114.     this.MainMenuItem.RightItem=this.SettingMenuItem;
  115.     this.DimmerGlcdMenuItem.RightItem=this.MainMenuItem;
  116.     this.DimmerGlcdMenuItem.LeftItem=this.SettingMenuItem;
  117.  
  118.  
  119. }
  120. //
  121. DorrDisplay.prototype.changeToLeft = function()
  122. {
  123.     if (this.currentMenuItem.LeftItem)
  124.     {
  125.         this.currentMenuItem.onExit();
  126.         this.currentMenuItem = this.currentMenuItem.LeftItem;
  127.         this.currentMenuItem.onEnter();
  128.     }
  129. }
  130.  
  131. DorrDisplay.prototype.changeToRight = function()
  132. {
  133.     if (this.currentMenuItem.RightItem)
  134.     {
  135.         this.currentMenuItem.onExit();
  136.         this.currentMenuItem = this.currentMenuItem.RightItem;
  137.         this.currentMenuItem.onEnter();
  138.     }
  139. }
  140.  
  141. DorrDisplay.prototype.changeToUp = function()
  142. {
  143.     if (this.currentMenuItem.UpItem)
  144.     {
  145.         this.currentMenuItem.onExit();
  146.         this.currentMenuItem = this.currentMenuItem.UpItem;
  147.         this.currentMenuItem.onEnter();
  148.     }
  149. }
  150. DorrDisplay.prototype.changeToDown = function()
  151. {
  152.     if (this.currentMenuItem.DownItem)
  153.     {
  154.         this.currentMenuItem.onExit();
  155.         this.currentMenuItem = this.currentMenuItem.DownItem;
  156.         this.currentMenuItem.onEnter();
  157.     }
  158. }
  159.  
  160. DorrDisplay.prototype.changeToEnter = function()
  161. {
  162.     if (this.currentMenuItem.PressEnterItem)
  163.     {
  164.         this.currentMenuItem.onExit();
  165.         this.currentMenuItem = this.currentMenuItem.PressEnterItem;
  166.         this.currentMenuItem.onEnter();
  167.     }
  168. }
  169.  
  170. DorrDisplay.prototype.changeToBack = function()
  171. {
  172.     if (this.currentMenuItem.PressBackItem)
  173.     {
  174.         this.currentMenuItem.onExit();
  175.         this.currentMenuItem = this.currentMenuItem.PressBackItem;
  176.         this.currentMenuItem.onEnter();
  177.     }
  178. }
  179.  
  180. DorrDisplay.prototype.rotaryOnline = function()
  181. {
  182. }
  183.  
  184. DorrDisplay.prototype.rotaryPosUpdate1 = function(SwitchId)
  185. {
  186.     this.screenSaverCnt = 0;
  187.     this.mainScreenCnt = 0;
  188. if (SwitchId != 1) {
  189.     for (var i = 0; i < this.myRotaryService1.getSteps(SwitchId); i++)
  190.     {
  191.         if (this.myRotaryService1.getDirection(SwitchId) == "Clockwise")
  192.         {
  193.             this.currentMenuItem.processEvent("right");
  194.         }
  195.         else
  196.         {
  197.             this.currentMenuItem.processEvent("left");
  198.         }
  199.     }
  200. } else {
  201. for (var i = 0; i < this.myRotaryService1.getSteps(SwitchId); i++)
  202.     {
  203.         if (this.myRotaryService1.getDirection(SwitchId) == "Clockwise")
  204.         {
  205.             this.currentMenuItem.processEvent("up");
  206.         }
  207.         else
  208.         {
  209.             this.currentMenuItem.processEvent("down");
  210.         }
  211.     }
  212. }
  213.     this.updateDisplay();
  214. }
  215.  
  216. DorrDisplay.prototype.rotaryBtnUpdate1 = function(SwitchId)
  217. {
  218.     this.screenSaverCnt = 0;
  219.     this.mainScreenCnt = 0;
  220.     if (SwitchId != 1) {
  221. if (this.myRotaryService1.getButtonStatus(SwitchId) == "Released")
  222.     {
  223.         this.currentMenuItem.processEvent("enter");
  224.         this.updateDisplay();
  225.     }
  226. }else {
  227. if (this.myRotaryService1.getButtonStatus(SwitchId) == "Released")
  228.     {
  229.         this.currentMenuItem.processEvent("back");
  230.         this.updateDisplay();
  231.     }
  232. }
  233. }
  234.  
  235. DorrDisplay.prototype.updateDisplay = function()
  236. {
  237. if (this.myGLCDService.getBacklight() == 0) {
  238. this.myGLCDService.setBacklight(this.defaultBacklight);
  239. }
  240.     this.currentMenuItem.update();
  241. }
  242.  
  243. DorrDisplay.prototype.lcdCenterText = function(text)
  244. {
  245.     returnText = text;
  246.     var displayWidth = 20;  //this.myLCDService.getWidth();
  247.     /* pad on both sides with spaces to displayWidth */
  248.     returnText = text.pad(displayWidth, " ", 2);
  249.    
  250.     return returnText;
  251. }
  252.  
  253. DorrDisplay.prototype.lcdOffline = function()
  254. {
  255. }
  256.  
  257. DorrDisplay.prototype.lcdOnline = function()
  258. {
  259.     /* If service is not online do nothing */
  260.     if (this.myGLCDService.isOnline())
  261.     {
  262.         /* Clear the LCD screen */
  263.         this.myGLCDService.clearScreen("Standard");
  264.         /* Set backlight to max */
  265.         this.myGLCDService.setBacklight(this.defaultBacklight);
  266.         this.timerUpdate();
  267.     }
  268. }
  269.  
  270.  
  271. DorrDisplay.prototype.sendTimeStamp = function()
  272. {
  273.     var canMessage = new CanNMTMessage("nmt", "Time");
  274.     canMessage.send();
  275. }
  276.  
  277. DorrDisplay.prototype.timerUpdate = function()
  278. {
  279.     /* If LCD service is not online do nothing */
  280.     if (this.myGLCDService.isOnline())
  281.     {
  282.         this.screenSaverCnt++;
  283.         this.mainScreenCnt++;
  284.         if (this.mainScreenCnt > DorrDisplay_mainScreenTimeout/5  && this.currentMenuItem != this.MainMenuItem)
  285.         {
  286.             /* Go to main screen (time) */
  287. this.currentMenuItem.onExit();
  288.             this.currentMenuItem = this.MainMenuItem;
  289. this.currentMenuItem.onEnter();
  290.             /* update the info on display */
  291.             this.updateDisplay();
  292.  
  293.             this.mainScreenCnt = 0;
  294.  
  295.         }
  296.        
  297.         if (this.screenSaverCnt > DorrDisplay_screenSaverTimeout/5)
  298.         {
  299.             /* Go to screensaver menu */
  300. this.myGLCDService.setBacklight(0);
  301.            
  302.             this.screenSaverCnt = 0;
  303.         }
  304.  
  305.     }
  306. }
  307.  
  308.