Subversion Repositories HomeAutomation

Rev

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

Rev 1712 Rev 1714
Line 2... Line 2...
2
#include "act_pcaPWM.h"
2
#include "act_pcaPWM.h"
3
#include "drivers/io/PCA9634.h"
3
#include "drivers/io/PCA9634.h"
4
 
4
 
5
static uint16_t setValue[8]={0,0,0,0,0,0,0,0};
5
static uint16_t setValue[8]={0,0,0,0,0,0,0,0};
6
static uint16_t isValue[8];
6
static uint16_t isValue[8];
-
 
7
 
-
 
8
int8_t fadeSpeed[8] = {0,0,0,0,0,0,0,0};
-
 
9
uint8_t fadeSpeedFrac[8] = {0,0,0,0,0,0,0,0};
-
 
10
uint16_t fadeTarget[8] = {0,0,0,0,0,0,0,0};
-
 
11
uint8_t fadeSpeedCnt[8] = {0,0,0,0,0,0,0,0};
-
 
12
 
-
 
13
uint16_t demoEndValue[8] = {0,0,0,0,0,0,0,0};
-
 
14
uint16_t demoHighValue[8] = {0,0,0,0,0,0,0,0};
-
 
15
uint8_t demoState[8] = {ACT_PCAPWM_DEMO_STATE_NOT_RUNNING, ACT_PCAPWM_DEMO_STATE_NOT_RUNNING,
-
 
16
                        ACT_PCAPWM_DEMO_STATE_NOT_RUNNING, ACT_PCAPWM_DEMO_STATE_NOT_RUNNING,
-
 
17
                        ACT_PCAPWM_DEMO_STATE_NOT_RUNNING, ACT_PCAPWM_DEMO_STATE_NOT_RUNNING,
-
 
18
                        ACT_PCAPWM_DEMO_STATE_NOT_RUNNING, ACT_PCAPWM_DEMO_STATE_NOT_RUNNING};
7
 
19
 
8
#ifdef act_pcaPWM_USEEEPROM
20
#ifdef act_pcaPWM_USEEEPROM
9
#include "act_pcaPWM_eeprom.h"
21
#include "act_pcaPWM_eeprom.h"
10
struct eeprom_act_pcaPWM EEMEM eeprom_act_pcaPWM =
22
struct eeprom_act_pcaPWM EEMEM eeprom_act_pcaPWM =
11
{
23
{
Line 20... Line 32...
20
        0x0000, // ch 7
32
        0x0000, // ch 7
21
        0x0000  // ch 8
33
        0x0000  // ch 8
22
    },
34
    },
23
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
35
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
24
};
36
};
-
 
37
#endif
-
 
38
 
-
 
39
#if act_pcaPWM_ENABLE_FADE == 1
-
 
40
void act_pcaPWM_timer_callback(uint8_t timer)
-
 
41
{
-
 
42
    uint8_t channel = 0;
-
 
43
    for (channel = 0; channel < 8; channel++) {
-
 
44
        /* Check demo states */
-
 
45
        if (setValue[channel] == fadeTarget[channel]) {
-
 
46
            switch (demoState[channel])
-
 
47
            {
-
 
48
            case ACT_PCAPWM_DEMO_STATE_NOT_RUNNING:
-
 
49
            break;
-
 
50
            case ACT_PCAPWM_DEMO_STATE_DECREASE:
-
 
51
                demoState[channel] = ACT_PCAPWM_DEMO_STATE_INCREASE;
-
 
52
                fadeTarget[channel] = demoHighValue[channel];
-
 
53
                fadeSpeed[channel] = -fadeSpeed[channel];
-
 
54
            break;
-
 
55
            case ACT_PCAPWM_DEMO_STATE_INCREASE:
-
 
56
                demoState[channel] = ACT_PCAPWM_DEMO_STATE_GOBACK;
-
 
57
                fadeTarget[channel] = demoEndValue[channel];
-
 
58
                fadeSpeed[channel] = -fadeSpeed[channel];
-
 
59
            break;
-
 
60
            case ACT_PCAPWM_DEMO_STATE_GOBACK:
-
 
61
                demoState[channel] = ACT_PCAPWM_DEMO_STATE_NOT_RUNNING;
-
 
62
            break;
-
 
63
            }
-
 
64
        }
-
 
65
       
-
 
66
        /* Change the dimmerValue according to the current fading */
-
 
67
        fadeSpeedCnt[channel]++;
-
 
68
        if (fadeSpeedCnt[channel] == fadeSpeedFrac[channel] && setValue[channel] != (fadeTarget[channel]))
-
 
69
        {
-
 
70
            fadeSpeedCnt[channel] = 0;
-
 
71
            uint16_t tempDimVal = setValue[channel];
-
 
72
            setValue[channel] += (fadeSpeed[channel]*39);
-
 
73
            if ((fadeSpeed[channel] > 0 && (setValue[channel] < tempDimVal || setValue[channel] >= (fadeTarget[channel]))) ||
-
 
74
                (fadeSpeed[channel] < 0 && (setValue[channel] > tempDimVal || setValue[channel] <= (fadeTarget[channel]))))
-
 
75
            {
-
 
76
                setValue[channel] = (fadeTarget[channel]);
-
 
77
            }
-
 
78
            if (setValue[channel] > 10000) {
-
 
79
                setValue[channel] = 10000;
-
 
80
            }
-
 
81
            /* if targetvalue was reached then send the netinfo packet */
-
 
82
            if (setValue[channel] == fadeTarget[channel])
-
 
83
            {
-
 
84
                //sendInfo[channel] = 1;
-
 
85
            }
-
 
86
        }
-
 
87
    }
-
 
88
 
-
 
89
}
25
#endif
90
#endif
26
 
91
 
27
void act_pcaPWM_Init(void)
92
void act_pcaPWM_Init(void)
28
{
93
{
29
#ifdef act_pcaPWM_USEEEPROM
94
#ifdef act_pcaPWM_USEEEPROM
Line 64... Line 129...
64
    gpio_clr_pin(act_pcaPWM_OUTPUT_ENABLE_IO);
129
    gpio_clr_pin(act_pcaPWM_OUTPUT_ENABLE_IO);
65
#endif
130
#endif
66
#ifdef act_pcaPWM_LED_OVERRIDE_IO
131
#ifdef act_pcaPWM_LED_OVERRIDE_IO
67
    gpio_set_out(act_pcaPWM_LED_OVERRIDE_IO);
132
    gpio_set_out(act_pcaPWM_LED_OVERRIDE_IO);
68
    gpio_clr_pin(act_pcaPWM_LED_OVERRIDE_IO);
133
    gpio_clr_pin(act_pcaPWM_LED_OVERRIDE_IO);
-
 
134
#endif
-
 
135
 
-
 
136
#if act_pcaPWM_ENABLE_FADE == 1
-
 
137
    Timer_SetTimeout(act_pcaPWM_FADE_TIMER, 10, TimerTypeFreeRunning, &act_pcaPWM_timer_callback);
69
#endif
138
#endif
70
}
139
}
71
 
140
 
72
void act_pcaPWM_Process(void)
141
void act_pcaPWM_Process(void)
73
{
142
{
74
 
143
 
75
    for (uint8_t i=0; i < 8; i++)
144
    for (uint8_t i=0; i < 8; i++)
76
    {
145
    {
77
        if (setValue[i] != isValue[i])
146
        if (setValue[i] != isValue[i])
78
        {
147
        {
79
            isValue[i] = setValue[i];
148
            isValue[i] = setValue[i];
80
            pca9634_setDuty(i, isValue[i]*act_pcaPWM_FACT);
149
            pca9634_setDuty(i, isValue[i]*act_pcaPWM_FACT);
81
#ifdef act_pcaPWM_USEEEPROM
150
#ifdef act_pcaPWM_USEEEPROM
82
            Timer_SetTimeout(act_pcaPWM_STORE_VALUE_TIMEOUT, act_pcaPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
151
            Timer_SetTimeout(act_pcaPWM_STORE_VALUE_TIMEOUT, act_pcaPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
83
#endif
152
#endif
84
        }
153
        }
85
    }
154
    }
86
 
155
 
87
#ifdef act_pcaPWM_USEEEPROM
156
#ifdef act_pcaPWM_USEEEPROM
88
    if (Timer_Expired(act_pcaPWM_STORE_VALUE_TIMEOUT))
157
    if (Timer_Expired(act_pcaPWM_STORE_VALUE_TIMEOUT))
89
    {
158
    {
90
        if (isValue[0] != eeprom_read_word(EEDATA16.ch1))
159
        if (isValue[0] != eeprom_read_word(EEDATA16.ch1))
91
        {
160
        {
92
            eeprom_write_word_crc(EEDATA16.ch1, isValue[0], WITH_CRC);
161
            eeprom_write_word_crc(EEDATA16.ch1, isValue[0], WITH_CRC);
93
        }
162
        }
94
        if (isValue[1] != eeprom_read_word(EEDATA16.ch2))
163
        if (isValue[1] != eeprom_read_word(EEDATA16.ch2))
95
        {
164
        {
96
            eeprom_write_word_crc(EEDATA16.ch2, isValue[1], WITH_CRC);
165
            eeprom_write_word_crc(EEDATA16.ch2, isValue[1], WITH_CRC);
Line 116... Line 185...
116
            eeprom_write_word_crc(EEDATA16.ch7, isValue[6], WITH_CRC);
185
            eeprom_write_word_crc(EEDATA16.ch7, isValue[6], WITH_CRC);
117
        }
186
        }
118
        if (isValue[7] != eeprom_read_word(EEDATA16.ch8))
187
        if (isValue[7] != eeprom_read_word(EEDATA16.ch8))
119
        {
188
        {
120
            eeprom_write_word_crc(EEDATA16.ch8, isValue[7], WITH_CRC);
189
            eeprom_write_word_crc(EEDATA16.ch8, isValue[7], WITH_CRC);
121
        }
190
        }
122
    }
191
    }
123
#endif
192
#endif
124
}
193
}
125
 
194
 
126
void act_pcaPWM_HandleMessage(StdCan_Msg_t *rxMsg)
195
void act_pcaPWM_HandleMessage(StdCan_Msg_t *rxMsg)
127
{
196
{
128
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
197
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
129
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
198
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
130
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_PCAPWM &&
199
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_PCAPWM &&
131
        rxMsg->Header.ModuleId == act_pcaPWM_ID)
200
        rxMsg->Header.ModuleId == act_pcaPWM_ID)
132
    {
201
    {
133
        uint8_t channel = 0;
202
        uint8_t channel = 0;
134
        switch (rxMsg->Header.Command)
203
        switch (rxMsg->Header.Command)
135
        {
204
        {
136
        case CAN_MODULE_CMD_PHYSICAL_PWM:
205
        case CAN_MODULE_CMD_PHYSICAL_PWM:
137
            if (rxMsg->Length == 3) {
206
            if (rxMsg->Length == 3) {
138
                channel = rxMsg->Data[0];
207
                channel = rxMsg->Data[0];
139
                setValue[channel] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
208
                setValue[channel] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
-
 
209
            }
-
 
210
        break;
-
 
211
#if act_pcaPWM_ENABLE_FADE == 1
-
 
212
        case CAN_MODULE_CMD_PCAPWM_DEMO:        /* Demo(channel, speed, steps) */
-
 
213
            if (rxMsg->Length == 4) {
-
 
214
                channel = rxMsg->Data[0];
-
 
215
                uint8_t speed = rxMsg->Data[1];
-
 
216
                uint16_t steps = (rxMsg->Data[2]<<8)+(rxMsg->Data[3]);
-
 
217
               
-
 
218
                fadeSpeedCnt[channel] = 0;
-
 
219
                fadeSpeed[channel] = 0;
-
 
220
                if (speed == 0) {
-
 
221
                    /* do nothing */
-
 
222
                } else {
-
 
223
                    uint16_t diffToMin = setValue[channel] - ACT_PCAPWM_MIN_DIM;
-
 
224
                    uint16_t diffToMax = ACT_PCAPWM_MAX_DIM - setValue[channel];
-
 
225
               
-
 
226
                    demoEndValue[channel] = setValue[channel];
-
 
227
                    if (diffToMin >= steps && diffToMax >= steps)
-
 
228
                    {
-
 
229
                        /* not close to min or max */
-
 
230
                        fadeTarget[channel] = setValue[channel] - steps;
-
 
231
                        demoHighValue[channel] = setValue[channel] + steps;
-
 
232
                    }
-
 
233
                    else if (diffToMin >= steps)
-
 
234
                    {
-
 
235
                        /* close to max */
-
 
236
                        fadeTarget[channel] = setValue[channel] - steps - steps + diffToMax;
-
 
237
                        demoHighValue[channel] = ACT_PCAPWM_MAX_DIM;
-
 
238
                    }
-
 
239
                    else if (diffToMax >= steps)
-
 
240
                    {
-
 
241
                        /* close to min */
-
 
242
                        fadeTarget[channel] = ACT_PCAPWM_MIN_DIM;
-
 
243
                        demoHighValue[channel] = setValue[channel] + steps + steps - diffToMin;
-
 
244
                    }
-
 
245
                    demoState[channel] = ACT_PCAPWM_DEMO_STATE_DECREASE;
-
 
246
               
-
 
247
                    if ((speed&0x80) == 0x80) {
-
 
248
                        fadeSpeed[channel] = (speed&0x7f)+1;
-
 
249
                        fadeSpeedFrac[channel] = 1;
-
 
250
                    } else {
-
 
251
                        fadeSpeed[channel] = 1;
-
 
252
                        fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
253
                    }
-
 
254
                    if (fadeTarget[channel] <= setValue[channel]) {
-
 
255
                        fadeSpeed[channel] = -fadeSpeed[channel];
-
 
256
                    }
-
 
257
                }
-
 
258
            }
-
 
259
        break;
-
 
260
       
-
 
261
        case CAN_MODULE_CMD_PCAPWM_START_FADE:  /* StartFade(channel, speed, direction) */
-
 
262
            if (rxMsg->Length == 3) {
-
 
263
               
-
 
264
                channel = rxMsg->Data[0];
-
 
265
                uint8_t speed = rxMsg->Data[1];
-
 
266
                uint8_t direction = rxMsg->Data[2];
-
 
267
                demoState[channel] = ACT_PCAPWM_DEMO_STATE_NOT_RUNNING;
-
 
268
                fadeSpeedCnt[channel] = 0;
-
 
269
                fadeSpeed[channel] = 0;
-
 
270
                uint16_t endValue = 0;
-
 
271
                if (direction == CAN_MODULE_ENUM_PCAPWM_START_FADE_DIRECTION_INCREASE) {
-
 
272
                    endValue = ACT_PCAPWM_MAX_DIM;
-
 
273
                } else if (direction == CAN_MODULE_ENUM_PCAPWM_START_FADE_DIRECTION_DECREASE) {
-
 
274
                    endValue = ACT_PCAPWM_MIN_DIM;
-
 
275
                }
-
 
276
                   
-
 
277
                if (speed == 0) {
-
 
278
                    setValue[channel] = endValue;   /* set dimmer value immediately */
-
 
279
                    //sendInfo[channel] = 1;        /* send netinfo with the current dimmervalue*/
-
 
280
                } else {
-
 
281
                    fadeTarget[channel] = endValue;
-
 
282
                    if (fadeTarget[channel] != setValue[channel]) {
-
 
283
                        if ((speed&0x80) == 0x80) {
-
 
284
                            fadeSpeed[channel] = (speed&0x7f)+1;
-
 
285
                            fadeSpeedFrac[channel] = 1;
-
 
286
                        } else {
-
 
287
                            fadeSpeed[channel] = 1;
-
 
288
                            fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
289
                        }
-
 
290
                        if (fadeTarget[channel] < setValue[channel]) {
-
 
291
                            fadeSpeed[channel] = -fadeSpeed[channel];
-
 
292
                        }
-
 
293
                    }
-
 
294
                }
-
 
295
            }
-
 
296
        break;
-
 
297
       
-
 
298
        case CAN_MODULE_CMD_PCAPWM_STOP_FADE:   /* StopFade(channel) */
-
 
299
            if (rxMsg->Length == 1) {
-
 
300
                channel = rxMsg->Data[0];
-
 
301
                demoState[channel] = ACT_PCAPWM_DEMO_STATE_NOT_RUNNING;
-
 
302
                fadeSpeed[channel] = 0;
-
 
303
                //sendInfo[channel] = 1;        /* send netinfo with the current dimmervalue*/
-
 
304
            }
-
 
305
        break;
-
 
306
 
-
 
307
        case CAN_MODULE_CMD_PCAPWM_ABS_FADE:    /* AbsFade(channel, speed, endValue) */
-
 
308
            if (rxMsg->Length == 4) {
-
 
309
                channel = rxMsg->Data[0];
-
 
310
                uint8_t speed = rxMsg->Data[1];
-
 
311
                uint16_t endValue = (rxMsg->Data[2]<<8)+(rxMsg->Data[3]);
-
 
312
                demoState[channel] = ACT_PCAPWM_DEMO_STATE_NOT_RUNNING;
-
 
313
                fadeSpeedCnt[channel] = 0;
-
 
314
                fadeSpeed[channel] = 0;
-
 
315
                if (speed == 0) {
-
 
316
                    setValue[channel] = endValue;   /* set dimmer value immediately */
-
 
317
                    //sendInfo[channel] = 1;        /* send netinfo with the current dimmervalue*/
-
 
318
                } else {
-
 
319
                    fadeTarget[channel] = endValue;
-
 
320
                    if (fadeTarget[channel] != setValue[channel]) {
-
 
321
                        if ((speed&0x80) == 0x80) {
-
 
322
                            fadeSpeed[channel] = (speed&0x7f)+1;
-
 
323
                            fadeSpeedFrac[channel] = 1;
-
 
324
                        } else {
-
 
325
                            fadeSpeed[channel] = 1;
-
 
326
                            fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
327
                        }
-
 
328
                        if (fadeTarget[channel] < setValue[channel]) {
-
 
329
                            fadeSpeed[channel] = -fadeSpeed[channel];
-
 
330
                        }
-
 
331
                    }
-
 
332
                }
-
 
333
                //printf("abs fade %d %d %d!\n",fadeTarget[channel], endValue ,pwmValue[channel] );
-
 
334
            }
-
 
335
        break;
-
 
336
       
-
 
337
        case CAN_MODULE_CMD_PCAPWM_REL_FADE:    /* RelFade(channel, speed, direction, steps) */
-
 
338
            if (rxMsg->Length == 5) {
-
 
339
               
-
 
340
                channel = rxMsg->Data[0];
-
 
341
                uint8_t speed = rxMsg->Data[1];
-
 
342
                uint8_t direction = rxMsg->Data[2];
-
 
343
                uint16_t steps = (rxMsg->Data[3]<<8)+(rxMsg->Data[4]);
-
 
344
               
-
 
345
                demoState[channel] = ACT_PCAPWM_DEMO_STATE_NOT_RUNNING;
-
 
346
                fadeSpeedCnt[channel] = 0;
-
 
347
                fadeSpeed[channel] = 0;
-
 
348
                uint16_t tempDimVal = setValue[channel];
-
 
349
                uint16_t tempDimVal2 = setValue[channel];
-
 
350
                if (direction == CAN_MODULE_ENUM_PCAPWM_REL_FADE_DIRECTION_INCREASE) {                  /* if increase */
-
 
351
                    tempDimVal2 += steps;               /* calculate new value */
-
 
352
                    if (tempDimVal2 < tempDimVal) {     /* make overflow test */
-
 
353
                        tempDimVal2 = ACT_PCAPWM_MAX_DIM;
-
 
354
                    }
-
 
355
                } else if (direction == CAN_MODULE_ENUM_PCAPWM_REL_FADE_DIRECTION_DECREASE) {           /* if decrease */
-
 
356
                    tempDimVal2 -= steps;
-
 
357
                    if (tempDimVal2 > tempDimVal) {
-
 
358
                        tempDimVal2 = ACT_PCAPWM_MIN_DIM;
-
 
359
                    }
-
 
360
                }
-
 
361
                if (speed == 0) {
-
 
362
                    setValue[channel] = tempDimVal2;        /* set dimmer value immediately */
-
 
363
                    //sendInfo[channel] = 1;                /* send netinfo with the current dimmervalue*/
-
 
364
                } else {
-
 
365
                    fadeTarget[channel] = tempDimVal2;      /* set the fade target */
-
 
366
                   
-
 
367
                    if (fadeTarget[channel] != setValue[channel]) {
-
 
368
                        if ((speed&0x80) == 0x80) {
-
 
369
                            fadeSpeed[channel] = (speed&0x7f)+1;
-
 
370
                            fadeSpeedFrac[channel] = 1;
-
 
371
                        } else {
-
 
372
                            fadeSpeed[channel] = 1;
-
 
373
                            fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
-
 
374
                        }
-
 
375
                        if (fadeTarget[channel] < setValue[channel]) {
-
 
376
                            fadeSpeed[channel] = -fadeSpeed[channel];
-
 
377
                        }
-
 
378
                    }
-
 
379
                }
140
            }
380
            }
141
        break;
381
        break;
-
 
382
#endif
142
        }
383
        }
143
    }
384
    }
144
}
385
}
145
 
386
 
146
void act_pcaPWM_List(uint8_t ModuleSequenceNumber)
387
void act_pcaPWM_List(uint8_t ModuleSequenceNumber)