Subversion Repositories HomeAutomation

Rev

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

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