Subversion Repositories HomeAutomation

Rev

Rev 1899 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1786 linlun 1
 
2
Require("cLCD_MenuItems/MainMenuItem.js");
3
Require("cLCD_MenuItems/DimmerMenuItem.js");
4
Require("cLCD_MenuItems/SensorMenuItem.js");
5
Require("cLCD_MenuItems/HDMISwitchMenuItem.js");
1899 linlun 6
Require("cLCD_MenuItems/DtmfMenuItem.js");
1786 linlun 7
 
8
function CLCD_Display(aliasnameLCD, aliasnameRotary)
9
{
10
    /* We must always call the parent constructor, initialization
11
       of variables could be done here, but initialize is a better place */
12
    var self = this;
13
    this.myCLCDService = aliasnameLCD;
14
    this.myRotaryService = aliasnameRotary;
15
 
16
    Module_RegisterToOnMessage(this.myRotaryService, function(alias_name, command, variables) { self.Input_OnMessage(alias_name, command, variables) });
17
    Module_RegisterToOnChange(this.myRotaryService, function(alias_name, available,test) { self.rotaryOnline(alias_name, available,test) });
18
    Module_RegisterToOnChange(this.myCLCDService, function(alias_name, available) { self.lcdOnline(alias_name, available) });
19
 
20
    /* Start interval timer for sending timestamp to network. Arguments are the callback function and time in milliseconds
21
    used to make sure an eth-node gets its init packet (600s) */
22
    this.myInterval = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, 5000, true);
23
    Log("\033[33mmyInterval = "+this.myInterval +"[0m\n");
24
 
25
    this.myIntervalScreenSaver = Timer_SetTimer(function(timer) {self.timerUpdate(timer)}, 5000, true);
26
    Log("\033[33mmyIntervalScreenSaver = "+this.myIntervalScreenSaver +"[0m\n");
27
 
28
    this.screenSaverCnt = 0;
29
    this.mainScreenCnt = 0;
30
 
31
    /* create the first menu item */
32
    this.MainMenuItem = new MainMenuItem(this, this.myCLCDService);
33
    this.SensorMenuItem = new SensorMenuItem(this, this.myCLCDService);
34
    this.DimmerMenuItem = new DimmerMenuItem(this, this.myCLCDService);
35
    this.HdmiMenuItem = new HDMISwitchMenuItem(this, this.myCLCDService);
2300 linlun 36
    //this.PIDMenuItem = new DtmfMenuItem(this, this.myCLCDService);
1786 linlun 37
 
38
    this.currentMenuItem = this.MainMenuItem;
39
 
40
 
41
    /* create the screensaver menu item */
42
    this.SensorMenuItem.LeftItem=this.DimmerMenuItem;
43
    this.SensorMenuItem.RightItem=this.MainMenuItem;
44
 
45
    /* set the function that shall be executed when knob is turned */
46
    /* create the screensaver menu item */
47
    this.MainMenuItem.LeftItem=this.SensorMenuItem;
48
    this.MainMenuItem.RightItem=this.HdmiMenuItem;
49
    this.DimmerMenuItem.RightItem=this.SensorMenuItem;
2300 linlun 50
    this.DimmerMenuItem.LeftItem=this.HdmiMenuItem;
1786 linlun 51
    this.HdmiMenuItem.LeftItem=this.MainMenuItem
2300 linlun 52
    this.HdmiMenuItem.RightItem=this.DimmerMenuItem
53
    //this.PIDMenuItem.LeftItem=this.HdmiMenuItem
54
    //this.PIDMenuItem.RightItem=this.DimmerMenuItem
1786 linlun 55
}
56
 
57
/* Declaration of instance variables, for static variables remove prototype */
58
 
59
CLCD_Display.prototype.myCLCDService = null;
60
CLCD_Display.prototype.myCLCDServiceIsOnline = false;
61
CLCD_Display.prototype.myRotaryService = null;
62
 
63
CLCD_Display.prototype.myInterval = null;
64
CLCD_Display.prototype.myIntervalAlways = null;
65
CLCD_Display.prototype.myIntervalScreenSaver = null;
66
 
67
CLCD_Display.prototype.MainMenuItem = null;
68
CLCD_Display.prototype.SensorMenuItem = null;
69
CLCD_Display.prototype.DimmerMenuItem = null;
70
CLCD_Display.prototype.HdmiMenuItem = null;
71
CLCD_Display.prototype.PIDMenuItem = null;
72
 
73
/* The currently displayed menuitem */
74
CLCD_Display.prototype.currentMenuItem = null;
75
 
76
/* counter for going to screensaver */
77
CLCD_Display.prototype.screenSaverCnt = null;
78
 
79
/* counter for going to main screen */
80
CLCD_Display.prototype.mainScreenCnt = null;
81
 
82
const Display_mainScreenTimeout = 20;
83
const Display_screenSaverTimeout = 25;
84
 
85
CLCD_Display.prototype.defaultBacklight = 10;
86
 
87
CLCD_Display.prototype.changeToLeft = function()
88
{
89
    if (this.currentMenuItem.LeftItem)
90
    {
91
        this.currentMenuItem.onExit();
92
        this.currentMenuItem = this.currentMenuItem.LeftItem;
93
        this.currentMenuItem.onEnter();
94
        var self = this;
95
        Timer_Cancel(this.myInterval);
96
        this.myInterval = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, this.currentMenuItem.UpdateTime, true);
97
    }
98
}
99
 
100
CLCD_Display.prototype.changeToRight = function()
101
{
102
    if (this.currentMenuItem.RightItem)
103
    {
104
        this.currentMenuItem.onExit();
105
        this.currentMenuItem = this.currentMenuItem.RightItem;
106
        this.currentMenuItem.onEnter();
107
        var self = this;
108
        Timer_Cancel(this.myInterval);
109
        this.myInterval = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, this.currentMenuItem.UpdateTime, true);
110
    }
111
}
112
 
113
CLCD_Display.prototype.changeToUp = function()
114
{
115
    if (this.currentMenuItem.UpItem)
116
    {
117
        this.currentMenuItem.onExit();
118
        this.currentMenuItem = this.currentMenuItem.UpItem;
119
        this.currentMenuItem.onEnter();
120
        var self = this;
121
        Timer_Cancel(this.myInterval);
122
        this.myInterval = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, this.currentMenuItem.UpdateTime, true);
123
    }
124
}
125
CLCD_Display.prototype.changeToDown = function()
126
{
127
    if (this.currentMenuItem.DownItem)
128
    {
129
        this.currentMenuItem.onExit();
130
        this.currentMenuItem = this.currentMenuItem.DownItem;
131
        this.currentMenuItem.onEnter();
132
        var self = this;
133
        Timer_Cancel(this.myInterval);
134
        this.myInterval = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, this.currentMenuItem.UpdateTime, true);
135
    }
136
}
137
 
138
CLCD_Display.prototype.changeToEnter = function()
139
{
140
    if (this.currentMenuItem.PressEnterItem)
141
    {
142
        this.currentMenuItem.onExit();
143
        this.currentMenuItem = this.currentMenuItem.PressEnterItem;
144
        this.currentMenuItem.onEnter();
145
        var self = this;
146
        Timer_Cancel(this.myInterval);
147
        this.myInterval = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, this.currentMenuItem.UpdateTime, true);
148
    }
149
}
150
 
151
CLCD_Display.prototype.changeToBack = function()
152
{
153
    if (this.currentMenuItem.PressBackItem)
154
    {
155
        this.currentMenuItem.onExit();
156
        this.currentMenuItem = this.currentMenuItem.PressBackItem;
157
        this.currentMenuItem.onEnter();
158
        var self = this;
159
        Timer_Cancel(this.myInterval);
160
        this.myInterval = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, this.currentMenuItem.UpdateTime, true);
161
    }
162
}
163
 
164
CLCD_Display.prototype.rotaryOnline = function(alias_name, available)
165
{
166
  Log("\033[33mRotary online/offline.\033[0m\n");
167
}
168
 
169
CLCD_Display.prototype.Input_OnMessage = function(alias_name, command, variables)
170
{
171
    switch (command)
172
    {
173
        case "Rotary_Switch":
174
        {
175
            this.screenSaverCnt = 0;
176
            this.mainScreenCnt = 0;
177
            var last_value_string = Storage_GetParameter("LastValues", this.myCLCDService);
178
            var last_value = eval("(" + last_value_string + ")");
179
 
180
            //Log("\033[33mValue: "+last_value["Strength"]["value"]+"\033[0m\n");
181
            if (last_value["Strength"]["value"] == 0) {
182
                Display_SetBacklight(this.myCLCDService, this.defaultBacklight);
183
            }
184
            for (var i = 0; i < variables["Steps"]; i++)
185
            {
186
                if (variables["Direction"] == "Clockwise")
187
                {
188
                    this.currentMenuItem.processEvent("left");
189
                }
190
                else
191
                {
192
                    this.currentMenuItem.processEvent("right");
193
                }
194
            }
195
 
196
            this.updateDisplay();
197
 
198
            break;
199
        }
200
        case "Button":
201
        {
202
            this.screenSaverCnt = 0;
203
            this.mainScreenCnt = 0;
204
            var last_value_string = Storage_GetParameter("LastValues", this.myCLCDService);
205
            var last_value = eval("(" + last_value_string + ")");
206
            if (last_value["Strength"]["value"] == 0) {
207
                Display_SetBacklight(this.myCLCDService, this.defaultBacklight);
208
            }
209
            if (variables["Status"] == "Released")
210
            {
211
                this.currentMenuItem.processEvent("enter");
212
                this.updateDisplay();
213
            }
214
 
215
            break;
216
        }
217
    }
218
 
219
}
220
 
221
CLCD_Display.prototype.updateDisplay = function(timer)
222
{
223
    if (this.myCLCDServiceIsOnline) {
224
        this.currentMenuItem.update();
225
    }
226
    //Log("\033[33mUpdate.\033[0m\n");
227
}
228
 
229
CLCD_Display.prototype.lcdCenterText = function(text)
230
{
231
    returnText = text;
232
    var displayWidth = 20;  //this.myLCDService.getWidth();
233
    /* pad on both sides with spaces to displayWidth */
234
    //returnText = text.pad(displayWidth, " ", 2);
235
 
236
    return returnText;
237
}
238
 
239
CLCD_Display.prototype.lcdOnline = function(alias_name, available)
240
{
241
 
242
    /* If service is not online do nothing */
243
    if (available)
244
    {
245
        Log("\033[33mLCD online.\033[0m\n");   
246
        this.myCLCDServiceIsOnline = true;
247
        /* Clear the LCD screen */
248
        Display_Clear(alias_name);
249
        /* Set backlight to max */
250
        Display_SetBacklight(alias_name, this.defaultBacklight);
251
        this.timerUpdate();
252
    } else {
253
        Log("\033[33mLCD offline.\033[0m\n");  
254
        this.myCLCDServiceIsOnline = false;
255
    }
256
}
257
 
258
CLCD_Display.prototype.timerUpdate = function(timer)
259
{
260
    /* If LCD service is not online do nothing */
261
    if (this.myCLCDServiceIsOnline)
262
    {
263
        //Log("\033[33mTmr update.\033[0m\n");
264
        var last_value_string = Storage_GetParameter("LastValues", this.myCLCDService);
265
        var last_value = eval("(" + last_value_string + ")");
266
 
267
 
268
        if (this.mainScreenCnt > Display_mainScreenTimeout/5  && this.currentMenuItem != this.MainMenuItem)
269
        {
270
            /* Go to main screen (time) */
271
            this.currentMenuItem.onExit();
272
            this.currentMenuItem = this.MainMenuItem;
273
            this.currentMenuItem.onEnter();
274
            /* update the info on display */
275
            this.updateDisplay();
276
        } else if (this.screenSaverCnt > Display_screenSaverTimeout/5 && last_value["Strength"]["value"] != 0)
277
        {
278
            /* Go to screensaver menu */
279
            Display_SetBacklight(this.myCLCDService, 0);
280
        } else {
281
            this.screenSaverCnt++;
282
            this.mainScreenCnt++;
283
        }
284
    }
285
}
286