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