Subversion Repositories HomeAutomation

Rev

Rev 1077 | Rev 1083 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1077 Rev 1081
Line 9... Line 9...
9
 
9
 
10
uint8_t demoEndValue = 0;
10
uint8_t demoEndValue = 0;
11
uint8_t demoHighValue = 0;
11
uint8_t demoHighValue = 0;
12
uint8_t demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
12
uint8_t demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
13
 
13
 
-
 
14
/*  netConnected stores the connection status, if connected to the net or not */
14
uint8_t netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
15
uint8_t netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
15
 
16
 
-
 
17
/*  frequency stores the evaluated frequency, 50 or 60 Hz
-
 
18
    don't calculate net frequency since the internal oscillator is not to be trusted, instead check if 50Hz or 60Hz system
-
 
19
    should be used for switching powerStepTable depending on frequency */
16
uint8_t frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN; 
20
uint8_t frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN; 
17
            //don't calculate net frequency since the internal oscillator is not to be trusted, instead check if 50Hz or 60Hz system
-
 
-
 
21
           
18
            //should be used for switching powerStepTable depending on frequency
22
/*  zero cross sync (time between pos flank of xc-detection and real zero cross) */
19
uint16_t xcTimeDiff=0;
23
uint16_t xcTimeDiff=0;
-
 
24
 
-
 
25
/*  dimmerValue is the current output
20
            //zero cross sync (time between pos flank of xc-detection and real zero cross)
26
    value of 0 is zero output, value of 255 is max output (ACT_DIMMMER230_MIN_DIM, ACT_DIMMMER230_MAX_DIM)
-
 
27
    stored in eeprom, and restored at startup */
21
uint8_t dimmerValue = ACT_DIMMMER230_MIN_DIM;
28
uint8_t dimmerValue = ACT_DIMMMER230_MIN_DIM;
22
            //store in eeprom later
29
           
23
            //value of 0 is zero output, value of 255 is max output (ACT_DIMMMER230_MIN_DIM, ACT_DIMMMER230_MAX_DIM)
-
 
24
uint8_t state = ACT_DIMMMER230_STATE_IDLE;
30
uint8_t state = ACT_DIMMMER230_STATE_IDLE;
-
 
31
 
-
 
32
/*  powerStepTable is used for stepping the output for constant increase/decrease in power, this should be a quater of a sin^2-wave (since the second quater can be calculated) */
25
const uint16_t powerStepTable[] PROGMEM = {845, 1068, 1225, 1352, 1459, 1553, 1638, 1716, 1788, 1855, 1917, 1976, 2032, 2086, 2138, 2188, 2236, 2282, 2327, 2370, 2412, 2453, 2493, 2532, 2570, 2607, 2643, 2679, 2714, 2748, 2782, 2815, 2848, 2880, 2911, 2942, 2972, 3002, 3032, 3061, 3090, 3119, 3147, 3175, 3203, 3230, 3257, 3284, 3311, 3337, 3363, 3389, 3415, 3440, 3465, 3490, 3515, 3539, 3563, 3587, 3611, 3635, 3659, 3682, 3705, 3728, 3751, 3774, 3797, 3820, 3843, 3865, 3887, 3909, 3931, 3953, 3975, 3997, 4019, 4041, 4062, 4083, 4104, 4125, 4146, 4167, 4188, 4209, 4230, 4251, 4272, 4292, 4313, 4333, 4354, 4374, 4395, 4415, 4435, 4455, 4476, 4496, 4516, 4536, 4556, 4576, 4596, 4615, 4635, 4655, 4675, 4694, 4714, 4734, 4754, 4773, 4793, 4813, 4832, 4852, 4872, 4891, 4911, 4931, 4951, 4970, 4990};
33
const uint16_t powerStepTable[] PROGMEM = {845, 1068, 1225, 1352, 1459, 1553, 1638, 1716, 1788, 1855, 1917, 1976, 2032, 2086, 2138, 2188, 2236, 2282, 2327, 2370, 2412, 2453, 2493, 2532, 2570, 2607, 2643, 2679, 2714, 2748, 2782, 2815, 2848, 2880, 2911, 2942, 2972, 3002, 3032, 3061, 3090, 3119, 3147, 3175, 3203, 3230, 3257, 3284, 3311, 3337, 3363, 3389, 3415, 3440, 3465, 3490, 3515, 3539, 3563, 3587, 3611, 3635, 3659, 3682, 3705, 3728, 3751, 3774, 3797, 3820, 3843, 3865, 3887, 3909, 3931, 3953, 3975, 3997, 4019, 4041, 4062, 4083, 4104, 4125, 4146, 4167, 4188, 4209, 4230, 4251, 4272, 4292, 4313, 4333, 4354, 4374, 4395, 4415, 4435, 4455, 4476, 4496, 4516, 4536, 4556, 4576, 4596, 4615, 4635, 4655, 4675, 4694, 4714, 4734, 4754, 4773, 4793, 4813, 4832, 4852, 4872, 4891, 4911, 4931, 4951, 4970, 4990};
26
 
34
 
27
#if act_dimmer230_SYNC>0
35
#if act_dimmer230_SYNC>0
28
uint8_t zeroCrossCnt = 0;
36
uint8_t zeroCrossCnt = 0;
29
#endif
37
#endif
30
 
38
 
-
 
39
/*  Store dimmer value in eeprom if user has not changed it for a while */
-
 
40
void Store_value_callback(uint8_t timer)
-
 
41
{
-
 
42
    eeprom_write_byte_crc(EEDATA.eeDimmerValue, dimmerValue, WITH_CRC);
-
 
43
}
-
 
44
 
-
 
45
/*  Set the variable to "disconnected" if no zerocross have arrived for a while */
31
void Net_Connection_callback(uint8_t timer)
46
void Net_Connection_callback(uint8_t timer)
32
{
47
{
33
    netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
48
    netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
34
}
49
}
35
 
50
 
-
 
51
/*  This function will send the dimmer-status-packet containing connection status and frequency */
36
void Send_Status_callback(uint8_t timer)
52
void Send_Status_callback(uint8_t timer)
37
{
53
{
38
    StdCan_Msg_t txMsg;
54
    StdCan_Msg_t txMsg;
39
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
55
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
40
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
56
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
Line 44... Line 60...
44
    txMsg.Length = 1;
60
    txMsg.Length = 1;
45
    txMsg.Data[0] = ((netConnected&0x1)<<7 | (frequency&0x3)<<5);
61
    txMsg.Data[0] = ((netConnected&0x1)<<7 | (frequency&0x3)<<5);
46
    StdCan_Put(&txMsg);
62
    StdCan_Put(&txMsg);
47
}
63
}
48
 
64
 
-
 
65
/*  When the timer reaches its compare value the triac should be trigged */
49
ISR (TIMER1_COMPA_vect)
66
ISR (TIMER1_COMPA_vect)
50
{
67
{
51
    //testa hur lång denna loop behöver vara
-
 
52
    //verkar fungera upp till 249-250 sen blir det för korta pulser
-
 
53
    act_dimmer230_CHAN1_PORT |= _BV(act_dimmer230_CHAN1_BIT);
68
    act_dimmer230_CHAN1_PORT |= _BV(act_dimmer230_CHAN1_BIT);
54
    uint8_t dummycnt = 40;
69
    uint8_t dummycnt = 40;
55
    while (dummycnt > 0)
70
    while (dummycnt > 0)
56
    {
71
    {
57
        dummycnt++;
72
        dummycnt++;
Line 62... Line 77...
62
   
77
   
63
    TIMSK1=0;                           //disable timer interrupt
78
    TIMSK1=0;                           //disable timer interrupt
64
    state = ACT_DIMMMER230_STATE_IDLE;
79
    state = ACT_DIMMMER230_STATE_IDLE;
65
}
80
}
66
 
81
 
-
 
82
/*  Interrupted at a pin change on zero-cross pin
-
 
83
    The zero-cross pin will go high just before the real zero-cross and
-
 
84
    low just after the real zero-cross. The time difference is measured
-
 
85
    and adjusted for. */
67
ISR (act_dimmer230_ZC_PCINT_vect)
86
ISR (act_dimmer230_ZC_PCINT_vect)
68
{
87
{
69
    //only execute if zerocross pin is low (after real zerocross)
88
    /* only execute if zerocross pin is low (after real zerocross).
-
 
89
    Calculate the diff from real zero-cross.
-
 
90
    */
70
    if (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)))
91
    if (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)))
71
    {
92
    {
72
        xcTimeDiff = TCNT1;
93
        xcTimeDiff = TCNT1;
73
        xcTimeDiff = xcTimeDiff >> 1; //divide by two
94
        xcTimeDiff = xcTimeDiff >> 1; //divide by two
74
        if (xcTimeDiff > 600) {
95
        if (xcTimeDiff > 600) {
75
            xcTimeDiff = 600;
96
            xcTimeDiff = 600;
76
        }
97
        }
77
    }  
98
    }  
78
    //only execute if zerocross pin is high (before real zerocross)
99
    /* only execute if zerocross pin is high (before real zerocross) */
79
    else if ((act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)))
100
    else if ((act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)))
80
    {
101
    {
81
 
-
 
-
 
102
        /* Reset the timer for checking net connection. If no zero-cross has come in 500ms then consider us disconnected */
82
        Timer_SetTimeout(act_dimmer230_NET_CONNECT_TIMEOUT, 500, TimerTypeOneShot, &Net_Connection_callback);
103
        Timer_SetTimeout(act_dimmer230_NET_CONNECT_TIMEOUT, 500, TimerTypeOneShot, &Net_Connection_callback);
-
 
104
        /* Currently we are connected */
83
        netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_CONNECTED;
105
        netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_CONNECTED;
84
       
106
       
85
        /* check frequency */
107
        /* Check frequency by measure the time since last zero-cross */
86
        uint16_t periodTime = TCNT1;
108
        uint16_t periodTime = TCNT1;
87
        if (periodTime > ACT_DIMMMER230_50HZ_PERIOD_TIME-ACT_DIMMMER230_50HZ_PERIOD_TIME/10 && periodTime < ACT_DIMMMER230_50HZ_PERIOD_TIME+ACT_DIMMMER230_50HZ_PERIOD_TIME/10)
109
        if (periodTime > ACT_DIMMMER230_50HZ_PERIOD_TIME-ACT_DIMMMER230_50HZ_PERIOD_TIME/10 && periodTime < ACT_DIMMMER230_50HZ_PERIOD_TIME+ACT_DIMMMER230_50HZ_PERIOD_TIME/10)
88
        {
110
        {
89
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_50HZ;
111
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_50HZ;
90
        }
112
        }
Line 95... Line 117...
95
        else
117
        else
96
        {
118
        {
97
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN;
119
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN;
98
        }
120
        }
99
       
121
       
100
        /* check demo states */
122
        /* Check demo states */
101
        if (dimmerValue == fadeTarget) {
123
        if (dimmerValue == fadeTarget) {
102
            switch (demoState)
124
            switch (demoState)
103
            {
125
            {
104
            case ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING:
126
            case ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING:
105
            break;
127
            break;
Line 117... Line 139...
117
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
139
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
118
            break;
140
            break;
119
            }
141
            }
120
        }
142
        }
121
       
143
       
122
        /* calculate new time to triac triggering */
144
        /* Calculate new time to triac triggering */
123
        uint16_t newTimerVal = xcTimeDiff;
145
        uint16_t newTimerVal = xcTimeDiff;      /* add the zerocross timediff */
124
 
146
 
125
        if (dimmerValue > 0 && dimmerValue <= 127)
147
        if (dimmerValue > 0 && dimmerValue <= 127)
126
        {
148
        {
127
            newTimerVal += ACT_DIMMMER230_50HZ_PERIOD_TIME - pgm_read_word(&powerStepTable[dimmerValue-1]);
149
            newTimerVal += ACT_DIMMMER230_50HZ_PERIOD_TIME - pgm_read_word(&powerStepTable[dimmerValue-1]);
128
        }
150
        }
129
        else if (dimmerValue < 255)
151
        else if (dimmerValue < 255)
130
        {
152
        {
131
            newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValue]);
153
            newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValue]);
132
        }
154
        }
133
        else if (dimmerValue == 255)
155
        else if (dimmerValue == 255)
134
        {
156
        {
135
            newTimerVal += 100; //almost max, to make sure we don't fire before zerocross
157
            newTimerVal += 100; /* almost max, to make sure we don't fire before zerocross */
136
        }
158
        }
137
       
159
       
138
        if (newTimerVal > ACT_DIMMMER230_50HZ_PERIOD_TIME)
160
        if (newTimerVal > ACT_DIMMMER230_50HZ_PERIOD_TIME)
139
        {
161
        {
140
            newTimerVal -= ACT_DIMMMER230_50HZ_PERIOD_TIME;
162
            newTimerVal -= ACT_DIMMMER230_50HZ_PERIOD_TIME;
141
        }
163
        }
142
 
164
 
143
        TCNT1 = 0;              //reset timer1 count register
165
        TCNT1 = 0;              /* reset timer1 count register */
144
        if (dimmerValue > 0)
166
        if (dimmerValue > 0)    /* dont trigger the triac if dimmerValue is 0 */
145
        {
167
        {
146
            OCR1A = newTimerVal;
168
            OCR1A = newTimerVal;
147
 
169
 
148
            //enable timer and timer interrupt
170
            /* enable timer and timer interrupt */
149
            TIFR1=(1<<OCF1A);       //clear interrupt flag before enabling interrupt
171
            TIFR1=(1<<OCF1A);       /* clear interrupt flag before enabling interrupt */
150
            TIMSK1|=(1<<OCIE1A);
172
            TIMSK1|=(1<<OCIE1A);
151
 
173
 
152
            state = ACT_DIMMMER230_STATE_TIMER_ON;
174
            state = ACT_DIMMMER230_STATE_TIMER_ON;
153
        }
175
        }
154
       
176
       
-
 
177
        /* Change the dimmerValue according to the current fading */
155
        fadeSpeedCnt++;
178
        fadeSpeedCnt++;
156
        if (fadeSpeedCnt == fadeSpeedFrac && dimmerValue != fadeTarget)
179
        if (fadeSpeedCnt == fadeSpeedFrac && dimmerValue != fadeTarget)
157
        {
180
        {
158
            fadeSpeedCnt = 0;
181
            fadeSpeedCnt = 0;
159
            uint8_t tempDimVal = dimmerValue;
182
            uint8_t tempDimVal = dimmerValue;
Line 161... Line 184...
161
            if ((fadeSpeed > 0 && (dimmerValue < tempDimVal || dimmerValue >= fadeTarget)) ||
184
            if ((fadeSpeed > 0 && (dimmerValue < tempDimVal || dimmerValue >= fadeTarget)) ||
162
                (fadeSpeed < 0 && (dimmerValue > tempDimVal || dimmerValue <= fadeTarget)))
185
                (fadeSpeed < 0 && (dimmerValue > tempDimVal || dimmerValue <= fadeTarget)))
163
            {
186
            {
164
                dimmerValue = fadeTarget;
187
                dimmerValue = fadeTarget;
165
            }
188
            }
-
 
189
            /* 5 seconds after we have stopped fading we should store the current dimmerValue to eeprom */
-
 
190
            Timer_SetTimeout(act_dimmer230_STORE_VALUE_TIMEOUT, 5000, TimerTypeOneShot, &Store_value_callback);
166
        }
191
        }
167
       
192
       
168
#if act_dimmer230_SYNC>0
193
#if act_dimmer230_SYNC>0
-
 
194
        /* Currently disabled, must add this packet to data.xml */
169
        zeroCrossCnt++;
195
        zeroCrossCnt++;
170
        if (zeroCrossCnt >= act_dimmer230_SYNC)
196
        if (zeroCrossCnt >= act_dimmer230_SYNC)
171
        {
197
        {
172
            zeroCrossCnt = 0;
198
            zeroCrossCnt = 0;
173
            //TODO: send sync-message on can
199
            //TODO: send sync-message on can
174
 
200
#if 0
175
            /*StdCan_Msg_t txMsg;
201
            StdCan_Msg_t txMsg;
176
            txMsg.Length = 6;
202
            txMsg.Length = 6;
177
            txMsg.Data[0] = (newTimerVal>>8)&0xff;
203
            txMsg.Data[0] = (newTimerVal>>8)&0xff;
178
            txMsg.Data[1] = newTimerVal&0xff;
204
            txMsg.Data[1] = newTimerVal&0xff;
179
            txMsg.Data[2] = frequency;  //(periodTime>>8)&0xff;
205
            txMsg.Data[2] = frequency;  //(periodTime>>8)&0xff;
180
            //txMsg.Data[3] = periodTime&0xff;
206
            //txMsg.Data[3] = periodTime&0xff;
181
            txMsg.Data[4] = (xcTimeDiff>>8)&0xff;
207
            txMsg.Data[4] = (xcTimeDiff>>8)&0xff;
182
            txMsg.Data[5] = xcTimeDiff&0xff;
208
            txMsg.Data[5] = xcTimeDiff&0xff;
183
            StdCan_Put(&txMsg); */
209
            StdCan_Put(&txMsg);
-
 
210
#endif
184
        }
211
        }
185
#endif      
212
#endif      
-
 
213
    }
186
    }
214
}
187
 
215
 
188
//lastDimmerValue = dimmerValue;
216
#ifdef act_dimmer230_USEEEPROM
-
 
217
struct eeprom_act_dimmer230 EEMEM eeprom_act_dimmer230 =
-
 
218
{
-
 
219
    {
-
 
220
        /* Define initialization values on the EEPROM variables here,
-
 
221
        this will generate a *.eep file that can be used to store this values to the node,
-
 
222
        can in future be done with a EEPROM module and the make-scrips.
-
 
223
        Write the values in the exact same order as the struct is defined in the *.h file. */
189
//timerValue = TCNT1;
224
        0x00    /* eeDimmerValue */
-
 
225
    },
-
 
226
    0   /* crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts */
190
}
227
};
-
 
228
#endif
191
 
229
 
-
 
230
/*  Init function of dimmer-module, will init hardware such as timer and gpio and load
-
 
231
    saved eeprom value */
192
void act_dimmer230_Init(void)
232
void act_dimmer230_Init(void)
193
{
233
{
-
 
234
#ifdef act_dimmer230_USEEEPROM
-
 
235
    if (EEDATA_OK)
-
 
236
    {
-
 
237
      /* Use stored data to set initial values for the module */
-
 
238
      dimmerValue = eeprom_read_byte(EEDATA.eeDimmerValue);
-
 
239
    } else
-
 
240
    {   /* The CRC of the EEPROM is not correct, store default values and update CRC */
-
 
241
      eeprom_write_byte_crc(EEDATA.eeDimmerValue, 0x00, WITHOUT_CRC);
-
 
242
      EEDATA_UPDATE_CRC;
-
 
243
    }
-
 
244
#endif  
-
 
245
 
194
    // set dimmer channel1 port to output 0
246
    /* set dimmer port to output 0 */
195
    act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT);
247
    act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT);
196
    act_dimmer230_CHAN1_DDR |= _BV(act_dimmer230_CHAN1_BIT);
248
    act_dimmer230_CHAN1_DDR |= _BV(act_dimmer230_CHAN1_BIT);
197
    // set zero cross detection port to input, no pullup
249
    /* set zero cross detection port to input, no pullup */
198
    act_dimmer230_ZC_PORT &= ~_BV(act_dimmer230_ZC_BIT);
250
    act_dimmer230_ZC_PORT &= ~_BV(act_dimmer230_ZC_BIT);
199
    act_dimmer230_ZC_DDR &= ~_BV(act_dimmer230_ZC_BIT);
251
    act_dimmer230_ZC_DDR &= ~_BV(act_dimmer230_ZC_BIT);
200
   
252
   
201
    TIMSK1=0;                           //disable timer interrupt
253
    TIMSK1=0;                           /* disable timer interrupt */
202
    TCCR1A=0;
254
    TCCR1A=0;
203
   
255
   
204
    TCCR1B=(1<<CS11);                   //enable timer, set to prescaler 8, must be changed if cpu freq is changed
256
    TCCR1B=(1<<CS11);                   /* enable timer, set to prescaler 8, must be changed if cpu freq is changed */
205
 
257
 
206
    //setup interrupt on zerocross, pcint
258
    /* setup interrupt on zerocross, pcint */
207
    act_dimmer230_ZC_PCMSK=(1<<(act_dimmer230_ZC_PCINT_BIT));
259
    act_dimmer230_ZC_PCMSK=(1<<(act_dimmer230_ZC_PCINT_BIT));
208
    PCIFR=(1<<act_dimmer230_ZC_PCIF);   //clear any pending interrupt before enabling interrupts
260
    PCIFR=(1<<act_dimmer230_ZC_PCIF);   /* clear any pending interrupt before enabling interrupts */
209
    PCICR=(1<<act_dimmer230_ZC_PCIE);   //enable interrupt for PCINT
261
    PCICR=(1<<act_dimmer230_ZC_PCIE);   /* enable interrupt for PCINT */
210
 
262
 
-
 
263
    /* Setup timeout for sending the net status packet */
211
    Timer_SetTimeout(act_dimmer230_SEND_STATUS_TIMEOUT, act_dimmer230_SEND_STATUS_INTERVAL*1000, TimerTypeFreeRunning, &Send_Status_callback);
264
    Timer_SetTimeout(act_dimmer230_SEND_STATUS_TIMEOUT, act_dimmer230_SEND_STATUS_INTERVAL*1000, TimerTypeFreeRunning, &Send_Status_callback);
212
}
265
}
213
 
266
 
-
 
267
/*  Process funtion of dimmer-module, currently everything is done interrupted */
214
void act_dimmer230_Process(void)
268
void act_dimmer230_Process(void)
215
{
269
{
216
    ///TODO: Stuff that needs doing is done here
-
 
217
 
270
 
218
}
271
}
219
 
272
 
-
 
273
/*  Handle incoming message function of dimmer-module */
220
void act_dimmer230_HandleMessage(StdCan_Msg_t *rxMsg)
274
void act_dimmer230_HandleMessage(StdCan_Msg_t *rxMsg)
221
{
275
{
222
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
276
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
223
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
277
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
224
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_DIMMER230 &&
278
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_DIMMER230 &&
225
        rxMsg->Header.ModuleId == act_dimmer230_ID)
279
        rxMsg->Header.ModuleId == act_dimmer230_ID)
226
    {
280
    {
227
        switch (rxMsg->Header.Command)
281
        switch (rxMsg->Header.Command)
228
        {
282
        {
229
        case CAN_MODULE_CMD_DIMMER230_DEMO:     // Demo(channel, speed, steps)
283
        case CAN_MODULE_CMD_DIMMER230_DEMO:     /* Demo(channel, speed, steps) */
230
            if (rxMsg->Length == 3) {
284
            if (rxMsg->Length == 3) {
231
                uint8_t channel = rxMsg->Data[0];
285
                uint8_t channel = rxMsg->Data[0];
232
                uint8_t speed = rxMsg->Data[1];
286
                uint8_t speed = rxMsg->Data[1];
233
                uint8_t steps = rxMsg->Data[2];
287
                uint8_t steps = rxMsg->Data[2];
234
               
288
               
235
                if (speed == 0) {
289
                if (speed == 0) {
236
                    //do nothing
290
                    /* do nothing */
237
                } else {
291
                } else {
238
                    uint8_t diffToMin = dimmerValue - ACT_DIMMMER230_MIN_DIM;
292
                    uint8_t diffToMin = dimmerValue - ACT_DIMMMER230_MIN_DIM;
239
                    uint8_t diffToMax = ACT_DIMMMER230_MAX_DIM - dimmerValue;
293
                    uint8_t diffToMax = ACT_DIMMMER230_MAX_DIM - dimmerValue;
240
               
294
               
241
                    demoEndValue = dimmerValue;
295
                    demoEndValue = dimmerValue;
242
                    if (diffToMin >= steps && diffToMax >= steps)
296
                    if (diffToMin >= steps && diffToMax >= steps)
243
                    {
297
                    {
244
                        //not close to min or max
298
                        /* not close to min or max */
245
                        fadeTarget = dimmerValue - steps;
299
                        fadeTarget = dimmerValue - steps;
246
                        demoHighValue = dimmerValue + steps;
300
                        demoHighValue = dimmerValue + steps;
247
                    }
301
                    }
248
                    else if (diffToMin >= steps)
302
                    else if (diffToMin >= steps)
249
                    {
303
                    {
250
                        //close to max
304
                        /* close to max */
251
                        fadeTarget = dimmerValue - steps - diffToMax;
305
                        fadeTarget = dimmerValue - steps - diffToMax;
252
                        demoHighValue = ACT_DIMMMER230_MAX_DIM;
306
                        demoHighValue = ACT_DIMMMER230_MAX_DIM;
253
                    }
307
                    }
254
                    else if (diffToMax >= steps)
308
                    else if (diffToMax >= steps)
255
                    {
309
                    {
256
                        //close to min
310
                        /* close to min */
257
                        fadeTarget = ACT_DIMMMER230_MIN_DIM;
311
                        fadeTarget = ACT_DIMMMER230_MIN_DIM;
258
                        demoHighValue = dimmerValue + steps + diffToMin;
312
                        demoHighValue = dimmerValue + steps + diffToMin;
259
                    }
313
                    }
260
                    demoState = ACT_DIMMMER230_DEMO_STATE_DECREASE;
314
                    demoState = ACT_DIMMMER230_DEMO_STATE_DECREASE;
261
               
315
               
Line 273... Line 327...
273
                    }
327
                    }
274
                }
328
                }
275
            }
329
            }
276
        break;
330
        break;
277
       
331
       
278
        case CAN_MODULE_CMD_DIMMER230_START_FADE:   // StartFade(channel, speed, direction), just an alias of AbsFade
332
        case CAN_MODULE_CMD_DIMMER230_START_FADE:   /* StartFade(channel, speed, direction) */
279
            if (rxMsg->Length == 3) {
333
            if (rxMsg->Length == 3) {
280
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
334
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
281
                uint8_t channel = rxMsg->Data[0];
335
                uint8_t channel = rxMsg->Data[0];
282
                uint8_t speed = rxMsg->Data[1];
336
                uint8_t speed = rxMsg->Data[1];
283
                uint8_t direction = rxMsg->Data[2];
337
                uint8_t direction = rxMsg->Data[2];
Line 290... Line 344...
290
                } else if (direction == 0) {
344
                } else if (direction == 0) {
291
                    endValue = ACT_DIMMMER230_MIN_DIM;
345
                    endValue = ACT_DIMMMER230_MIN_DIM;
292
                }
346
                }
293
                   
347
                   
294
                if (speed == 0) {
348
                if (speed == 0) {
295
                    dimmerValue = endValue; //set dimmer value immediately
349
                    dimmerValue = endValue; /* set dimmer value immediately */
296
                } else {
350
                } else {
297
                    fadeTarget = endValue;
351
                    fadeTarget = endValue;
298
                    if (fadeTarget != dimmerValue) {
352
                    if (fadeTarget != dimmerValue) {
299
                        if ((speed&0x80) == 0) {
353
                        if ((speed&0x80) == 0) {
300
                            fadeSpeed = speed&0x7f;
354
                            fadeSpeed = speed&0x7f;
Line 309... Line 363...
309
                    }
363
                    }
310
                }
364
                }
311
            }
365
            }
312
        break;
366
        break;
313
 
367
 
314
        case CAN_MODULE_CMD_DIMMER230_STOP_FADE:    // StopFade(channel)
368
        case CAN_MODULE_CMD_DIMMER230_STOP_FADE:    /* StopFade(channel) */
315
            if (rxMsg->Length == 1) {
369
            if (rxMsg->Length == 1) {
316
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
370
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
317
                fadeSpeed = 0;
371
                fadeSpeed = 0;
318
            }
372
            }
319
        break;
373
        break;
320
 
374
 
321
        case CAN_MODULE_CMD_DIMMER230_ABS_FADE: // AbsFade(channel, speed, endValue)
375
        case CAN_MODULE_CMD_DIMMER230_ABS_FADE: /* AbsFade(channel, speed, endValue) */
322
            if (rxMsg->Length == 3) {
376
            if (rxMsg->Length == 3) {
323
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
377
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
324
                uint8_t channel = rxMsg->Data[0];
378
                uint8_t channel = rxMsg->Data[0];
325
                uint8_t speed = rxMsg->Data[1];
379
                uint8_t speed = rxMsg->Data[1];
326
                uint8_t endValue = rxMsg->Data[2];
380
                uint8_t endValue = rxMsg->Data[2];
327
 
381
 
328
                fadeSpeedCnt = 0;
382
                fadeSpeedCnt = 0;
329
                fadeSpeed = 0;
383
                fadeSpeed = 0;
330
                if (speed == 0) {
384
                if (speed == 0) {
331
                    dimmerValue = endValue; //set dimmer value immediately
385
                    dimmerValue = endValue; /* set dimmer value immediately */
332
                } else {
386
                } else {
333
                    fadeTarget = endValue;
387
                    fadeTarget = endValue;
334
                    if (fadeTarget != dimmerValue) {
388
                    if (fadeTarget != dimmerValue) {
335
                        if ((speed&0x80) == 0) {
389
                        if ((speed&0x80) == 0) {
336
                            fadeSpeed = speed&0x7f;
390
                            fadeSpeed = speed&0x7f;
Line 345... Line 399...
345
                    }
399
                    }
346
                }
400
                }
347
            }
401
            }
348
        break;
402
        break;
349
 
403
 
350
        case CAN_MODULE_CMD_DIMMER230_REL_FADE: // RelFade(channel, speed, direction, steps)
404
        case CAN_MODULE_CMD_DIMMER230_REL_FADE: /* RelFade(channel, speed, direction, steps) */
351
            if (rxMsg->Length == 4) {
405
            if (rxMsg->Length == 4) {
352
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
406
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
353
                uint8_t channel = rxMsg->Data[0];
407
                uint8_t channel = rxMsg->Data[0];
354
                uint8_t speed = rxMsg->Data[1];
408
                uint8_t speed = rxMsg->Data[1];
355
                uint8_t direction = rxMsg->Data[2];
409
                uint8_t direction = rxMsg->Data[2];
Line 357... Line 411...
357
               
411
               
358
                fadeSpeedCnt = 0;
412
                fadeSpeedCnt = 0;
359
                fadeSpeed = 0;
413
                fadeSpeed = 0;
360
                uint8_t tempDimVal = dimmerValue;
414
                uint8_t tempDimVal = dimmerValue;
361
                uint8_t tempDimVal2 = dimmerValue;
415
                uint8_t tempDimVal2 = dimmerValue;
362
                if (direction == 1) {                   //if increase
416
                if (direction == 1) {                   /* if increase */
363
                    tempDimVal2 += steps;               //calculate new value
417
                    tempDimVal2 += steps;               /* calculate new value */
364
                    if (tempDimVal2 < tempDimVal) {     //make overflow test
418
                    if (tempDimVal2 < tempDimVal) {     /* make overflow test */
365
                        tempDimVal2 = ACT_DIMMMER230_MAX_DIM;
419
                        tempDimVal2 = ACT_DIMMMER230_MAX_DIM;
366
                    }
420
                    }
367
                } else if (direction == 0) {            //if decrease
421
                } else if (direction == 0) {            /* if decrease */
368
                    tempDimVal2 -= steps;
422
                    tempDimVal2 -= steps;
369
                    if (tempDimVal2 > tempDimVal) {
423
                    if (tempDimVal2 > tempDimVal) {
370
                        tempDimVal2 = ACT_DIMMMER230_MIN_DIM;
424
                        tempDimVal2 = ACT_DIMMMER230_MIN_DIM;
371
                    }
425
                    }
372
                }
426
                }
373
                if (speed == 0) {
427
                if (speed == 0) {
374
                    dimmerValue = tempDimVal2;      //set dimmer value immediately
428
                    dimmerValue = tempDimVal2;      /* set dimmer value immediately */
375
                } else {
429
                } else {
376
                    fadeTarget = tempDimVal2;       //set the fade target
430
                    fadeTarget = tempDimVal2;       /* set the fade target */
377
                   
431
                   
378
                    if (fadeTarget != dimmerValue) {
432
                    if (fadeTarget != dimmerValue) {
379
                        if ((speed&0x80) == 0) {
433
                        if ((speed&0x80) == 0) {
380
                            fadeSpeed = speed&0x7f;
434
                            fadeSpeed = speed&0x7f;
381
                            fadeSpeedFrac = 1;
435
                            fadeSpeedFrac = 1;
Line 392... Line 446...
392
        break;
446
        break;
393
}
447
}
394
    }
448
    }
395
}
449
}
396
 
450
 
-
 
451
/* List function of dimmer-module */
397
void act_dimmer230_List(uint8_t ModuleSequenceNumber)
452
void act_dimmer230_List(uint8_t ModuleSequenceNumber)
398
{
453
{
399
    StdCan_Msg_t txMsg;
454
    StdCan_Msg_t txMsg;
400
   
455
   
401
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
456
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);