Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
1422 linlun 1
 
2
loadScript("Logic/MenuItem.js");
3
 
4
function LED_dimmer(type, name, id)
5
{
6
    /* We must always call the parent constructor, initialization
7
       of variables could be done here, but initialize is a better place */
8
    this.Service(type, name, id);
9
}
10
 
11
/* We inherit from Service with this call, it must always be next
12
   after the constructor */
13
extend(LED_dimmer, Service);
14
 
15
/* Declaration of instance variables, for static variables remove prototype */
16
 
17
 
18
/* The display should regulary update and store the calendar */
19
 
20
 
21
LED_dimmer.prototype.myRotaryService = null;
22
LED_dimmer.prototype.myPWM = null;
23
LED_dimmer.prototype.myOutput = null;
24
LED_dimmer.prototype.myInput = null;
25
LED_dimmer.prototype.pwmValue = 50;
26
LED_dimmer.prototype.outputStatus = "Low";
27
LED_dimmer.prototype.myInterval = null;
28
LED_dimmer.prototype.oldPwmValue = 0;
29
LED_dimmer.prototype.turnOffCnt = 0;
1424 linlun 30
const MovementTimeout = 120;
31
const AdjustTimeout = 240;
1422 linlun 32
/*  */
33
/*  */
34
 
35
 
36
/* This function must always be declared, this is where all the startup code
37
   should be placed. Gets called with arguments like what ids to use etc. */
38
LED_dimmer.prototype.initialize = function(initialArguments)
39
{
40
    /* We must always call the parent initialize */
41
    this.Service.prototype.initialize.call(this, initialArguments);
42
 
43
    /* This is used for function declarations like the callbacks below */
44
    var self = this;
45
 
46
 
47
    if (!this.myInitialArguments["RotaryHex"])
48
    {
49
        log(this.myName + ":" + this.myId + "> Failed to initialize, RotaryHex-config missing from config.\n");
50
        return;
51
    }
52
 
53
    if (!this.myInitialArguments["output"])
54
    {
55
        log(this.myName + ":" + this.myId + "> Failed to initialize, output-config missing from config.\n");
56
        return;
57
    }
58
    if (!this.myInitialArguments["input"])
59
    {
60
        log(this.myName + ":" + this.myId + "> Failed to initialize, input-config missing from config.\n");
61
        return;
62
    }
63
 
64
    this.myInterval = new Interval(function() { self.timerUpdate() }, 5000);
65
    this.myInterval.start();
66
 
67
    /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
68
    this.myRotaryService = ServiceManager.getService("Can", "RotaryHex", this.myInitialArguments["RotaryHex"]["Id"]);
69
    /* Add a callback for when the encoder reports a new position */
70
    this.myRotaryService.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate(args); });
71
    /* Add a callback for when the encoder reports a new button status */
72
    this.myRotaryService.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate(args); });
73
    /* Add a callback for when the service goes online */
74
    this.myRotaryService.registerEventCallback("online", function(args) { self.rotaryOnline(); });
75
    /* If the service is already online we should call the handler here */
76
    this.rotaryOnline();
77
 
1424 linlun 78
    //this.myPWM = ServiceManager.getService("Can", "hwPWM", this.myInitialArguments["hwPWM"]["Id"]);
1422 linlun 79
    this.myOutput = ServiceManager.getService("Can", "output", this.myInitialArguments["output"]["Id"]);
80
    this.myInput = ServiceManager.getService("Can", "input", this.myInitialArguments["input"]["Id"]);
81
    /* Add a callback for when the PIR reports detection */
82
    this.myInput.registerEventCallback("newValue", function(args) { self.pirUpdate(args); });
83
 
84
}
85
 
86
LED_dimmer.prototype.timerUpdate = function()
87
{
88
    if (this.turnOffCnt > 0) {
89
        this.turnOffCnt -= 5;
90
        if (this.turnOffCnt < 0) {
91
            this.turnOffCnt = 0;
92
        }
93
        if (this.turnOffCnt == 0) {
94
            //Turn off light
95
            log(this.myName + ":" + this.myId + "> Light off.\n");
1424 linlun 96
            this.oldPwmValue = getDimmerValue('Badrum');
97
            //this.myPWM.setPWMValue(0,2);
98
            getDimmerService('Badrum').absFade(2,129, 0);
1422 linlun 99
        }
100
    }
101
}
102
 
103
LED_dimmer.prototype.rotaryOnline = function()
104
{
105
}
106
 
107
LED_dimmer.prototype.rotaryPosUpdate = function(SwitchId)
108
{
109
 
110
    for (var i = 0; i < this.myRotaryService.getSteps(SwitchId); i++)
111
    {
112
        if (this.myRotaryService.getDirection(SwitchId) == "Clockwise")
113
        {
1424 linlun 114
            getDimmerService('Badrum').relFade(2,132, "Increase", 25);
115
this.pwmValue = this.pwmValue +10;
1422 linlun 116
            if (this.pwmValue > 100) {
117
                this.pwmValue = 100;
118
            }
119
        }
120
        else
121
        {
1424 linlun 122
            getDimmerService('Badrum').relFade(2,132, "Decrease", 25);
1422 linlun 123
            this.pwmValue = this.pwmValue -10;
124
            if (this.pwmValue < 0) {
125
                this.pwmValue = 0;
126
            }
127
        }
128
    }
129
    if (this.turnOffCnt <  AdjustTimeout) {
130
        this.turnOffCnt = AdjustTimeout;
131
    }
132
//log(this.myName + ":" + this.myId + "> Sent pwm value: "+ this.pwmValue+"\n");
1424 linlun 133
    //this.myPWM.setPWMValue(this.pwmValue,2);
134
 
1422 linlun 135
}
136
 
137
LED_dimmer.prototype.rotaryBtnUpdate = function(SwitchId)
138
{
139
    if (this.myRotaryService.getButtonStatus(SwitchId) == "Pressed") {
1424 linlun 140
        if (getDimmerValue('Badrum') == 0) {
1422 linlun 141
            if (this.oldPwmValue == 0) {
142
                this.oldPwmValue = 50;
143
            }
1424 linlun 144
            getDimmerService('Badrum').absFade(2,129, 128);
145
            //this.myPWM.setPWMValue(this.oldPwmValue,2);
1422 linlun 146
        }
147
        if (this.turnOffCnt <  AdjustTimeout) {
148
            this.turnOffCnt = AdjustTimeout;
149
        }
150
        if (this.myOutput.getValue(0) == "High") {
151
            this.myOutput.setPin("Low",0);
152
        } else {
153
            this.myOutput.setPin("High",0);
154
        }
155
    }
156
}
157
 
158
LED_dimmer.prototype.pirUpdate = function(SwitchId)
159
{
160
    //log(this.myName + ":" + this.myId + "> Value is: "+ this.myInput.getValue(SwitchId)+"\n");
161
    if (this.myInput.getValue(SwitchId) == "Low") {
1424 linlun 162
          getDimmerService('Badrum').absFade(1,129, 255);
1422 linlun 163
        //this.myOutput.setPin("High",0);
164
        log(this.myName + ":" + this.myId + "> Trigg.\n");
1424 linlun 165
        if (getDimmerValue('Badrum') == 0) {
1422 linlun 166
            log(this.myName + ":" + this.myId + "> Light on.\n");
167
            if (this.oldPwmValue == 0) {
168
                this.oldPwmValue = 50;
169
            }
1424 linlun 170
            //this.myPWM.setPWMValue(this.oldPwmValue,2);
171
            getDimmerService('Badrum').absFade(2,129, 128);
1422 linlun 172
        }
1424 linlun 173
    } else {
174
        getDimmerService('Badrum').absFade(1,129, 0);
1422 linlun 175
    }
1424 linlun 176
    if (this.turnOffCnt <  MovementTimeout) {
177
        this.turnOffCnt = MovementTimeout;
178
    }
1422 linlun 179
}