Subversion Repositories HomeAutomation

Rev

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

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