Subversion Repositories HomeAutomation

Rev

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

Rev 1418 Rev 1425
Line 4... Line 4...
4
uint16_t pwmValue[4];
4
uint16_t pwmValue[4];
-
 
5
uint8_t sendInfo[4] = {0,0,0,0};
-
 
6
 
-
 
7
int8_t fadeSpeed[4] = {0,0,0,0};
-
 
8
uint8_t fadeSpeedFrac[4] = {0,0,0,0};
-
 
9
uint16_t fadeTarget[4] = {0,0,0,0};
-
 
10
uint8_t fadeSpeedCnt[4] = {0,0,0,0};
-
 
11
 
-
 
12
uint16_t demoEndValue[4] = {0,0,0,0};
-
 
13
uint16_t demoHighValue[4] = {0,0,0,0};
-
 
14
uint8_t demoState[4] = {ACT_HWPWM_DEMO_STATE_NOT_RUNNING, ACT_HWPWM_DEMO_STATE_NOT_RUNNING, ACT_HWPWM_DEMO_STATE_NOT_RUNNING,                   ACT_HWPWM_DEMO_STATE_NOT_RUNNING};
5
 
15
 
6
#ifdef act_hwPWM_USEEEPROM
16
#ifdef act_hwPWM_USEEEPROM
7
#include "act_hwPWM_eeprom.h"
17
#include "act_hwPWM_eeprom.h"
8
struct eeprom_act_hwPWM EEMEM eeprom_act_hwPWM =
18
struct eeprom_act_hwPWM EEMEM eeprom_act_hwPWM =
9
{
19
{
10
    {
20
    {
11
        ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file. 
21
        ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file. 
12
        0x0000, // ch 1
22
        0x0000, // ch 1
13
        0x0000, // ch 2
23
        0x0000, // ch 2
14
        0x0000, // ch 3
24
        0x0000, // ch 3
15
        0x0000  // ch 4
25
        0x0000  // ch 4
16
    },
26
    },
17
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
27
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
18
};
28
};
19
#endif
29
#endif
-
 
30
 
-
 
31
#if act_hwPWM_ENABLE_FADE == 1
-
 
32
void act_hwPWM_timer_callback(uint8_t timer)
-
 
33
{
-
 
34
    uint8_t channel = 0;
-
 
35
    for (channel = 0; channel < 4; channel++) {
-
 
36
        /* Check demo states */
-
 
37
        if (pwmValue[channel] == fadeTarget[channel]) {
-
 
38
            switch (demoState[channel])
-
 
39
            {
-
 
40
            case ACT_HWPWM_DEMO_STATE_NOT_RUNNING:
-
 
41
            break;
-
 
42
            case ACT_HWPWM_DEMO_STATE_DECREASE:
-
 
43
                demoState[channel] = ACT_HWPWM_DEMO_STATE_INCREASE;
-
 
44
                fadeTarget[channel] = demoHighValue[channel];
-
 
45
                fadeSpeed[channel] = -fadeSpeed[channel];
-
 
46
            break;
-
 
47
            case ACT_HWPWM_DEMO_STATE_INCREASE:
-
 
48
                demoState[channel] = ACT_HWPWM_DEMO_STATE_GOBACK;
-
 
49
                fadeTarget[channel] = demoEndValue[channel];
-
 
50
                fadeSpeed[channel] = -fadeSpeed[channel];
-
 
51
            break;
-
 
52
            case ACT_HWPWM_DEMO_STATE_GOBACK:
-
 
53
                demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
-
 
54
            break;
-
 
55
            }
-
 
56
        }
-
 
57
       
-
 
58
        /* Change the dimmerValue according to the current fading */
-
 
59
        fadeSpeedCnt[channel]++;
-
 
60
        if (fadeSpeedCnt[channel] == fadeSpeedFrac[channel] && pwmValue[channel] != (fadeTarget[channel]))
-
 
61
        {
-
 
62
            fadeSpeedCnt[channel] = 0;
-
 
63
            uint16_t tempDimVal = pwmValue[channel];
-
 
64
            pwmValue[channel] += (fadeSpeed[channel]*39);
-
 
65
            if ((fadeSpeed[channel] > 0 && (pwmValue[channel] < tempDimVal || pwmValue[channel] >= (fadeTarget[channel]))) ||
-
 
66
                (fadeSpeed[channel] < 0 && (pwmValue[channel] > tempDimVal || pwmValue[channel] <= (fadeTarget[channel]))))
-
 
67
            {
-
 
68
                pwmValue[channel] = (fadeTarget[channel]);
-
 
69
            }
-
 
70
            /* if targetvalue was reached then send the netinfo packet */
-
 
71
            if (pwmValue[channel] == fadeTarget[channel])
-
 
72
            {
-
 
73
                sendInfo[channel] = 1;
-
 
74
            }
-
 
75
        }
-
 
76
    }
-
 
77
 
-
 
78
}
-
 
79
#endif
-
 
80
 
20
 
81
 
21
void act_hwPWM_Init(void)
82
void act_hwPWM_Init(void)
22
{
83
{
23
#ifdef act_hwPWM_USEEEPROM
84
#ifdef act_hwPWM_USEEEPROM
24
    if (EEDATA_OK)
85
    if (EEDATA_OK)
Line 42... Line 103...
42
TCCR1B = 0;
103
    TCCR1B = 0;
43
TCCR0A = 0;
104
    TCCR0A = 0;
44
TCCR0B = 0;
105
    TCCR0B = 0;
45
 
106
 
46
/* set up pwm values */
107
    /* set up pwm values */
47
cli();
108
    cli();
48
#if act_hwPWM_CH1_COM>0
109
    #if act_hwPWM_CH1_COM>0
49
OCR_1=(uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8;
110
    OCR_1=(uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8;
50
#endif
111
    #endif
51
#if act_hwPWM_CH2_COM>0
112
    #if act_hwPWM_CH2_COM>0
52
OCR_2=(uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8;
113
    OCR_2=(uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8;
53
#endif
114
    #endif
54
#if act_hwPWM_CH3_COM>0
115
    #if act_hwPWM_CH3_COM>0
55
OCR_3=(uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8;
116
    OCR_3=(uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8;
56
#endif
117
    #endif
57
#if act_hwPWM_CH4_COM>0
118
    #if act_hwPWM_CH4_COM>0
58
OCR_4=(uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8;
119
    OCR_4=(uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8;
59
#endif
120
    #endif
60
sei();
121
    sei();
61
 
122
 
62
/* set up waveform generation mode for timer 1 */
123
    /* set up waveform generation mode for timer 1 */
63
#if act_hwPWM_CH1_COM>0
124
    #if act_hwPWM_CH1_COM>0
64
TCCR1A=((act_hwPWM_CH1_WGM&0x03)<<WGM00);
125
    TCCR1A=((act_hwPWM_CH1_WGM&0x03)<<WGM00);
65
TCCR1B=(((act_hwPWM_CH1_WGM>>2)&0x03)<<WGM12);
126
    TCCR1B=(((act_hwPWM_CH1_WGM>>2)&0x03)<<WGM12);
Line 69... Line 130...
69
#endif
130
    #endif
70
 
131
 
71
/* enable outputs for timer 1 */
132
    /* enable outputs for timer 1 */
72
#if act_hwPWM_CH1_COM>0
133
    #if act_hwPWM_CH1_COM>0
73
gpio_set_out(EXP_B);
134
    gpio_set_out(EXP_B);
74
#endif
135
    #endif
75
#if act_hwPWM_CH2_COM>0
136
    #if act_hwPWM_CH2_COM>0
76
gpio_set_out(EXP_C);
137
    gpio_set_out(EXP_C);
77
#endif
138
    #endif
78
 
139
 
79
/* set up counter mode for timer 1 */
140
    /* set up counter mode for timer 1 */
Line 81... Line 142...
81
if (pwmValue[0]>0)
142
    if (pwmValue[0]>0)
82
{
143
    {
83
    TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
144
        TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
84
}
145
    }
85
if (pwmValue[1]>0)
146
    if (pwmValue[1]>0)
86
{
147
    {
87
    TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
148
        TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
88
}
149
    }
89
#endif
150
    #endif
90
 
151
 
91
/* enable timer 1 */
152
    /* enable timer 1 */
92
#if act_hwPWM_CH1_COM>0
153
    #if act_hwPWM_CH1_COM>0
Line 109... Line 170...
109
#if act_hwPWM_CH3_COM>0
170
    #if act_hwPWM_CH3_COM>0
110
gpio_set_out(EXP_F);
171
    gpio_set_out(EXP_F);
111
#endif
172
    #endif
112
#if act_hwPWM_CH4_COM>0
173
    #if act_hwPWM_CH4_COM>0
113
gpio_set_out(EXP_G);
174
    gpio_set_out(EXP_G);
114
#endif
175
    #endif
115
 
176
 
116
/* set up counter mode for timer 0 */
177
    /* set up counter mode for timer 0 */
117
#if act_hwPWM_CH3_COM>0 || act_hwPWM_CH4_COM>0
178
    #if act_hwPWM_CH3_COM>0 || act_hwPWM_CH4_COM>0
118
if (pwmValue[2]>0)
179
    if (pwmValue[2]>0)
119
{
180
    {
120
    TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
181
        TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
121
}
182
    }
122
if (pwmValue[3]>0)
183
    if (pwmValue[3]>0)
123
{
184
    {
124
    TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
185
        TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
125
}
186
    }
126
#endif
187
    #endif
127
 
188
 
128
/* enable timer 0 */
189
    /* enable timer 0 */
129
#if act_hwPWM_CH3_COM>0
190
    #if act_hwPWM_CH3_COM>0
130
TCCR0B|=(act_hwPWM_CH3_CS<<CS00);
191
    TCCR0B|=(act_hwPWM_CH3_CS<<CS00);
131
#elif act_hwPWM_CH4_COM>0
192
    #elif act_hwPWM_CH4_COM>0
132
TCCR0B|=(act_hwPWM_CH4_CS<<CS00);
193
    TCCR0B|=(act_hwPWM_CH4_CS<<CS00);
133
#endif
194
    #endif
134
 
-
 
135
//printf("1A %x, 1B %x, 0A %x, 0B %x\n", TCCR1A, TCCR1B, TCCR0A, TCCR0B);
195
    //printf("1A %x, 1B %x, 0A %x, 0B %x\n", TCCR1A, TCCR1B, TCCR0A, TCCR0B);
136
 
196
   
-
 
197
    Timer_SetTimeout(act_hwPWM_FADE_TIMER, 10, TimerTypeFreeRunning, &act_hwPWM_timer_callback);
137
}
198
   
-
 
199
    /* Setup timeout for sending the status packet */
-
 
200
    Timer_SetTimeout(act_hwPWM_SEND_STATUS_TIMEOUT, act_hwPWM_SEND_STATUS_INTERVAL_S*1000, TimerTypeFreeRunning, 0);
-
 
201
    printf("Hello world!\n");
138
 
202
}
-
 
203
uint8_t channel_to_send = 1;
139
void act_hwPWM_Process(void)
204
void act_hwPWM_Process(void)
140
{
205
{
-
 
206
    if (Timer_Expired(act_hwPWM_SEND_STATUS_TIMEOUT))
-
 
207
    {
-
 
208
        if (channel_to_send >4) {
-
 
209
            channel_to_send=1;
-
 
210
        }
-
 
211
/*      while (0
-
 
212
#if act_hwPWM_CH1_COM>0
-
 
213
          || channel_to_send != 1
-
 
214
#endif
-
 
215
#if act_hwPWM_CH2_COM>0
-
 
216
          || channel_to_send != 2
-
 
217
#endif
-
 
218
#if act_hwPWM_CH3_COM>0
-
 
219
          || channel_to_send != 3
-
 
220
#endif
-
 
221
#if act_hwPWM_CH4_COM>0
-
 
222
          || channel_to_send != 4
-
 
223
#endif
-
 
224
        ) {
-
 
225
            channel_to_send++;
-
 
226
            if (channel_to_send >4) {
-
 
227
                  channel_to_send=1;
-
 
228
            }
-
 
229
        }
-
 
230
        */
-
 
231
        sendInfo[channel_to_send-1] = 1;
-
 
232
        channel_to_send++;
-
 
233
    }
-
 
234
    /* Send netinfo packet (if pwmvalue has changed, and periodically) */
-
 
235
    uint8_t index;
-
 
236
    for(index =0;index < 4; index++){
-
 
237
        if (pwmValue[index] > 10000) {
-
 
238
            pwmValue[index] = 10000;
-
 
239
        }
-
 
240
        if (sendInfo[index])
-
 
241
        {
-
 
242
            sendInfo[index] = 0;
-
 
243
            StdCan_Msg_t txMsg;
-
 
244
            StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
-
 
245
            StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
-
 
246
            txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_HWPWM;
-
 
247
            txMsg.Header.ModuleId = act_hwPWM_ID;
-
 
248
            txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
-
 
249
            txMsg.Length = 3;
-
 
250
            txMsg.Data[0] = index+1;
-
 
251
            txMsg.Data[1] = (0xff&(pwmValue[index]>>8));
-
 
252
            txMsg.Data[2] = (0xff&(pwmValue[index]));
-
 
253
            StdCan_Put(&txMsg);
-
 
254
        }
-
 
255
    }
-
 
256
   
141
    if (Timer_Expired(act_hwPWM_STORE_VALUE_TIMEOUT))
257
    if (Timer_Expired(act_hwPWM_STORE_VALUE_TIMEOUT))
142
    {
258
    {
143
        if (pwmValue[0] != eeprom_read_word(EEDATA16.ch1))
259
        if (pwmValue[0] != eeprom_read_word(EEDATA16.ch1))
144
        {
260
        {
145
            eeprom_write_word_crc(EEDATA16.ch1, pwmValue[0], WITH_CRC);
261
            eeprom_write_word_crc(EEDATA16.ch1, pwmValue[0], WITH_CRC);
Line 155... Line 271...
155
        if (pwmValue[3] != eeprom_read_word(EEDATA16.ch4))
271
        if (pwmValue[3] != eeprom_read_word(EEDATA16.ch4))
156
        {
272
        {
157
            eeprom_write_word_crc(EEDATA16.ch4, pwmValue[3], WITH_CRC);
273
            eeprom_write_word_crc(EEDATA16.ch4, pwmValue[3], WITH_CRC);
158
        }
274
        }
159
    }
275
    }
-
 
276
                   
-
 
277
#if act_hwPWM_CH1_COM>0
-
 
278
    if (OCR_1 != (uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8) {
-
 
279
        cli(); 
-
 
280
        OCR_1=(uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8;
-
 
281
        if (pwmValue[0]==0)
-
 
282
        {
-
 
283
            TCCR1A&=~((1<<COM1B0)|(1<<COM1B1));
-
 
284
        }
-
 
285
        else
-
 
286
        {
-
 
287
            TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
-
 
288
        }
-
 
289
        sei();
-
 
290
        Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
-
 
291
    }
-
 
292
#endif
-
 
293
#if act_hwPWM_CH2_COM>0
-
 
294
    if (OCR_2 != (uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8) {
-
 
295
        cli(); 
-
 
296
        OCR_2=(uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8;
-
 
297
        if (pwmValue[1]==0)
-
 
298
        {
-
 
299
            TCCR1A&=~((1<<COM1A0)|(1<<COM1A1));
-
 
300
        }
-
 
301
        else
-
 
302
        {
-
 
303
            TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
-
 
304
        }
-
 
305
        sei();
-
 
306
        Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
-
 
307
    }
-
 
308
#endif
-
 
309
#if act_hwPWM_CH3_COM>0
-
 
310
    if (OCR_3 != (uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8) {
-
 
311
        cli(); 
-
 
312
        OCR_3=(uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8;
-
 
313
        if (pwmValue[2]==0)
-
 
314
        {
-
 
315
            TCCR0A &= ~((1<<COM0A0)|(1<<COM0A1));
-
 
316
        }
-
 
317
        else
-
 
318
        {
-
 
319
            TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
-
 
320
        }
-
 
321
        sei();
-
 
322
        Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
-
 
323
    }
-
 
324
#endif
-
 
325
#if act_hwPWM_CH4_COM>0
-
 
326
    if (OCR_4 != (uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8) {
-
 
327
        cli(); 
-
 
328
        OCR_4=(uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8;
-
 
329
        if (pwmValue[3]==0)
-
 
330
        {
-
 
331
            TCCR0A &= ~((1<<COM0B0)|(1<<COM0B1));
-
 
332
        }
-
 
333
        else
-
 
334
        {
-
 
335
            TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
-
 
336
        }
-
 
337
        sei();
-
 
338
        Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
-
 
339
    }
-
 
340
#endif
160
}
341
}
161
 
342
 
162
void act_hwPWM_HandleMessage(StdCan_Msg_t *rxMsg)
343
void act_hwPWM_HandleMessage(StdCan_Msg_t *rxMsg)
163
{
344
{
164
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
345
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
165
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
346
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
166
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_HWPWM &&
347
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_HWPWM &&
167
        rxMsg->Header.ModuleId == act_hwPWM_ID)
348
        rxMsg->Header.ModuleId == act_hwPWM_ID)
168
    {
349
    {
169
        switch (rxMsg->Header.Command)
350
        switch (rxMsg->Header.Command)
170
        {
351
        {
171
        case CAN_MODULE_CMD_PHYSICAL_PWM:
352
        case CAN_MODULE_CMD_PHYSICAL_PWM:
172
            if (rxMsg->Length == 3) {
353
            if (rxMsg->Length == 3) {
173
                uint8_t channel = rxMsg->Data[0];
354
                uint8_t channel = rxMsg->Data[0];
174
                pwmValue[channel-1] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
355
                pwmValue[channel-1] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
175
                cli();
-
 
176
                switch (channel)
-
 
177
                {
-
 
178
#if act_hwPWM_CH1_COM>0
-
 
179
                    case 1:
-
 
180
                        OCR_1=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH1_FACT)>>8;
-
 
181
                        if (pwmValue[channel-1]==0)
-
 
182
                        {
-
 
183
                            TCCR1A&=~((1<<COM1B0)|(1<<COM1B1));
-
 
184
                        }
-
 
185
                        else
-
 
186
                        {
-
 
187
                            TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
-
 
188
                        }
-
 
189
                    break;
-
 
190
#endif
-
 
191
#if act_hwPWM_CH2_COM>0
-
 
192
                    case 2:
-
 
193
                        OCR_2=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH2_FACT)>>8;
-
 
194
                        if (pwmValue[channel-1]==0)
-
 
195
                        {
-
 
196
                            TCCR1A&=~((1<<COM1A0)|(1<<COM1A1));
-
 
197
                        }
-
 
198
                        else
-
 
199
                        {
-
 
200
                            TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
-
 
201
                        }
356
            }
-
 
357
           
202
                    break;
358
        break;
-
 
359
#if act_hwPWM_ENABLE_FADE == 1
-
 
360
        case CAN_MODULE_CMD_HWPWM_DEMO:     /* Demo(channel, speed, steps) */
-
 
361
            if (rxMsg->Length == 4) {
-
 
362
                uint8_t channel = rxMsg->Data[0]-1;
-
 
363
                uint8_t speed = rxMsg->Data[1];
-
 
364
                uint16_t steps = (rxMsg->Data[2]<<8)+(rxMsg->Data[3]);
203
#endif
365
               
-
 
366
                fadeSpeedCnt[channel] = 0;
204
#if act_hwPWM_CH3_COM>0
367
                fadeSpeed[channel] = 0;
-
 
368
                if (speed == 0) {
-
 
369
                    /* do nothing */
205
                    case 3:
370
                } else {
206
                        OCR_3=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH3_FACT)>>8;
371
                    uint16_t diffToMin = pwmValue[channel] - ACT_HWPWM_MIN_DIM;
-
 
372
                    uint16_t diffToMax = ACT_HWPWM_MAX_DIM - pwmValue[channel];
-
 
373
               
-
 
374
                    demoEndValue[channel] = pwmValue[channel];
-
 
375
                    if (diffToMin >= steps && diffToMax >= steps)
-
 
376
                    {
-
 
377
                        /* not close to min or max */
207
                        if (pwmValue[channel-1]==0)
378
                        fadeTarget[channel] = pwmValue[channel] - steps;
-
 
379
                        demoHighValue[channel] = pwmValue[channel] + steps;
-
 
380
                    }
-
 
381
                    else if (diffToMin >= steps)
-
 
382
                    {
-
 
383
                        /* close to max */
-
 
384
                        fadeTarget[channel] = pwmValue[channel] - steps - steps + diffToMax;
-
 
385
                        demoHighValue[channel] = ACT_HWPWM_MAX_DIM;
-
 
386
                    }
-
 
387
                    else if (diffToMax >= steps)
208
                        {
388
                    {
-
 
389
                        /* close to min */
-
 
390
                        fadeTarget[channel] = ACT_HWPWM_MIN_DIM;
-
 
391
                        demoHighValue[channel] = pwmValue[channel] + steps + steps - diffToMin;
-
 
392
                    }
-
 
393
                    demoState[channel] = ACT_HWPWM_DEMO_STATE_DECREASE;
-
 
394
               
-
 
395
                    if ((speed&0x80) == 0x80) {
-
 
396
                        fadeSpeed[channel] = (speed&0x7f)+1;
-
 
397
                        fadeSpeedFrac[channel] = 1;
-
 
398
                    } else {
-
 
399
                        fadeSpeed[channel] = 1;
-
 
400
                        fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
401
                    }
-
 
402
                    if (fadeTarget[channel] <= pwmValue[channel]) {
-
 
403
                        fadeSpeed[channel] = -fadeSpeed[channel];
-
 
404
                    }
-
 
405
                }
-
 
406
            }
-
 
407
        break;
-
 
408
       
-
 
409
        case CAN_MODULE_CMD_HWPWM_START_FADE:   /* StartFade(channel, speed, direction) */
-
 
410
            if (rxMsg->Length == 3) {
-
 
411
               
-
 
412
                uint8_t channel = rxMsg->Data[0]-1;
-
 
413
                uint8_t speed = rxMsg->Data[1];
-
 
414
                uint8_t direction = rxMsg->Data[2];
-
 
415
                demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
-
 
416
                fadeSpeedCnt[channel] = 0;
-
 
417
                fadeSpeed[channel] = 0;
-
 
418
                uint16_t endValue = 0;
-
 
419
                if (direction == CAN_MODULE_ENUM_HWPWM_START_FADE_DIRECTION_INCREASE) {
-
 
420
                    endValue = ACT_HWPWM_MAX_DIM;
-
 
421
                } else if (direction == CAN_MODULE_ENUM_HWPWM_START_FADE_DIRECTION_DECREASE) {
-
 
422
                    endValue = ACT_HWPWM_MIN_DIM;
-
 
423
                }
-
 
424
                   
-
 
425
                if (speed == 0) {
-
 
426
                    pwmValue[channel] = endValue;   /* set dimmer value immediately */
-
 
427
                    sendInfo[channel] = 1;      /* send netinfo with the current dimmervalue*/
-
 
428
                } else {
-
 
429
                    fadeTarget[channel] = endValue;
-
 
430
                    if (fadeTarget[channel] != pwmValue[channel]) {
209
                            TCCR0A &= ~((1<<COM0A0)|(1<<COM0A1));
431
                        if ((speed&0x80) == 0x80) {
-
 
432
                            fadeSpeed[channel] = (speed&0x7f)+1;
-
 
433
                            fadeSpeedFrac[channel] = 1;
-
 
434
                        } else {
-
 
435
                            fadeSpeed[channel] = 1;
-
 
436
                            fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
437
                        }
-
 
438
                        if (fadeTarget[channel] < pwmValue[channel]) {
-
 
439
                            fadeSpeed[channel] = -fadeSpeed[channel];
210
                        }
440
                        }
-
 
441
                    }
-
 
442
                }
-
 
443
            }
-
 
444
        break;
-
 
445
 
-
 
446
        case CAN_MODULE_CMD_HWPWM_STOP_FADE:    /* StopFade(channel) */
-
 
447
            if (rxMsg->Length == 1) {
-
 
448
                uint8_t channel = rxMsg->Data[0]-1;
-
 
449
                demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
-
 
450
                fadeSpeed[channel] = 0;
-
 
451
                sendInfo[channel] = 1;      /* send netinfo with the current dimmervalue*/
-
 
452
            }
-
 
453
        break;
-
 
454
 
-
 
455
        case CAN_MODULE_CMD_HWPWM_ABS_FADE: /* AbsFade(channel, speed, endValue) */
-
 
456
            if (rxMsg->Length == 4) {
-
 
457
                uint8_t channel = rxMsg->Data[0]-1;
-
 
458
                uint8_t speed = rxMsg->Data[1];
-
 
459
                uint16_t endValue = (rxMsg->Data[2]<<8)+(rxMsg->Data[3]);
-
 
460
                demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
-
 
461
                fadeSpeedCnt[channel] = 0;
-
 
462
                fadeSpeed[channel] = 0;
-
 
463
                if (speed == 0) {
-
 
464
                    pwmValue[channel] = endValue;   /* set dimmer value immediately */
-
 
465
                    sendInfo[channel] = 1;      /* send netinfo with the current dimmervalue*/
-
 
466
                } else {
-
 
467
                    fadeTarget[channel] = endValue;
-
 
468
                    if (fadeTarget[channel] != pwmValue[channel]) {
-
 
469
                        if ((speed&0x80) == 0x80) {
-
 
470
                            fadeSpeed[channel] = (speed&0x7f)+1;
-
 
471
                            fadeSpeedFrac[channel] = 1;
211
                        else
472
                        } else {
-
 
473
                            fadeSpeed[channel] = 1;
-
 
474
                            fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
475
                        }
-
 
476
                        if (fadeTarget[channel] < pwmValue[channel]) {
-
 
477
                            fadeSpeed[channel] = -fadeSpeed[channel];
212
                        {
478
                        }
-
 
479
                    }
-
 
480
                }
-
 
481
                printf("abs fade %d %d %d!\n",fadeTarget[channel], endValue ,pwmValue[channel] );
-
 
482
            }
-
 
483
        break;
-
 
484
 
-
 
485
        case CAN_MODULE_CMD_HWPWM_REL_FADE: /* RelFade(channel, speed, direction, steps) */
-
 
486
            if (rxMsg->Length == 5) {
-
 
487
               
-
 
488
                uint8_t channel = rxMsg->Data[0]-1;
-
 
489
                uint8_t speed = rxMsg->Data[1];
-
 
490
                uint8_t direction = rxMsg->Data[2];
-
 
491
                uint16_t steps = (rxMsg->Data[3]<<8)+(rxMsg->Data[4]);
-
 
492
               
-
 
493
                demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
-
 
494
                fadeSpeedCnt[channel] = 0;
-
 
495
                fadeSpeed[channel] = 0;
-
 
496
                uint16_t tempDimVal = pwmValue[channel];
-
 
497
                uint16_t tempDimVal2 = pwmValue[channel];
-
 
498
                if (direction == CAN_MODULE_ENUM_HWPWM_REL_FADE_DIRECTION_INCREASE) {                   /* if increase */
-
 
499
                    tempDimVal2 += steps;               /* calculate new value */
-
 
500
                    if (tempDimVal2 < tempDimVal) {     /* make overflow test */
-
 
501
                        tempDimVal2 = ACT_HWPWM_MAX_DIM;
-
 
502
                    }
-
 
503
                } else if (direction == CAN_MODULE_ENUM_HWPWM_REL_FADE_DIRECTION_DECREASE) {            /* if decrease */
-
 
504
                    tempDimVal2 -= steps;
-
 
505
                    if (tempDimVal2 > tempDimVal) {
213
                            TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
506
                        tempDimVal2 = ACT_HWPWM_MIN_DIM;
-
 
507
                    }
-
 
508
                }
-
 
509
                if (speed == 0) {
-
 
510
                    pwmValue[channel] = tempDimVal2;        /* set dimmer value immediately */
-
 
511
                    sendInfo[channel] = 1;              /* send netinfo with the current dimmervalue*/
-
 
512
                } else {
-
 
513
                    fadeTarget[channel] = tempDimVal2;      /* set the fade target */
-
 
514
                   
-
 
515
                    if (fadeTarget[channel] != pwmValue[channel]) {
-
 
516
                        if ((speed&0x80) == 0x80) {
-
 
517
                            fadeSpeed[channel] = (speed&0x7f)+1;
-
 
518
                            fadeSpeedFrac[channel] = 1;
-
 
519
                        } else {
-
 
520
                            fadeSpeed[channel] = 1;
-
 
521
                            fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
522
                        }
-
 
523
                        if (fadeTarget[channel] < pwmValue[channel]) {
-
 
524
                            fadeSpeed[channel] = -fadeSpeed[channel];
-
 
525
                        }
-
 
526
                    }
-
 
527
                }
214
                        }
528
            }
215
                    break;
529
        break;
216
#endif
530
#endif
217
#if act_hwPWM_CH4_COM>0
-
 
218
                    case 4:
-
 
219
                        OCR_4=(uint16_t)(pwmValue[channel-1]*act_hwPWM_CH4_FACT)>>8;
-
 
220
                        if (pwmValue[channel-1]==0)
-
 
221
                        {
-
 
222
                            TCCR0A &= ~((1<<COM0B0)|(1<<COM0B1));
-
 
223
                        }
-
 
224
                        else
-
 
225
                        {
-
 
226
                            TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
-
 
227
                        }
-
 
228
                    break;
-
 
229
#endif
-
 
230
                }
-
 
231
                sei();
-
 
232
                Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
-
 
233
            }
-
 
234
           
-
 
235
        break;
-
 
236
        }
531
        }
237
    }
532
    }
238
}
533
}
239
 
534
 
240
void act_hwPWM_List(uint8_t ModuleSequenceNumber)
535
void act_hwPWM_List(uint8_t ModuleSequenceNumber)