Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
1703 arune 1
 
2
#include "act_pcaPWM.h"
3
#include "drivers/io/PCA9634.h"
4
 
1712 arune 5
static uint16_t setValue[8]={0,0,0,0,0,0,0,0};
1711 arune 6
static uint16_t isValue[8];
7
 
1714 arune 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};
19
 
1978 arune 20
#if act_pcaPWM_USEEEPROM==1
1703 arune 21
#include "act_pcaPWM_eeprom.h"
22
struct eeprom_act_pcaPWM EEMEM eeprom_act_pcaPWM =
23
{
24
    {
25
        ///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.
1711 arune 26
        0x0000, // ch 1
27
        0x0000, // ch 2
28
        0x0000, // ch 3
29
        0x0000, // ch 4
30
        0x0000, // ch 5
31
        0x0000, // ch 6
32
        0x0000, // ch 7
33
        0x0000  // ch 8
1703 arune 34
    },
35
 
36
};
37
#endif
38
 
1714 arune 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
}
90
#endif
91
 
1703 arune 92
void act_pcaPWM_Init(void)
93
{
1978 arune 94
#if act_pcaPWM_USEEEPROM==1
1703 arune 95
    if (EEDATA_OK)
96
    {
1711 arune 97
      setValue[0] = eeprom_read_word(EEDATA16.ch1);
98
      setValue[1] = eeprom_read_word(EEDATA16.ch2);
99
      setValue[2] = eeprom_read_word(EEDATA16.ch3);
100
      setValue[3] = eeprom_read_word(EEDATA16.ch4);
101
      setValue[4] = eeprom_read_word(EEDATA16.ch5);
102
      setValue[5] = eeprom_read_word(EEDATA16.ch6);
103
      setValue[6] = eeprom_read_word(EEDATA16.ch7);
104
      setValue[7] = eeprom_read_word(EEDATA16.ch8);
1703 arune 105
    } else
106
    {   //The CRC of the EEPROM is not correct, store default values and update CRC
1711 arune 107
      eeprom_write_word_crc(EEDATA16.ch1, 0x0000, WITHOUT_CRC);
108
      eeprom_write_word_crc(EEDATA16.ch2, 0x0000, WITHOUT_CRC);
109
      eeprom_write_word_crc(EEDATA16.ch3, 0x0000, WITHOUT_CRC);
110
      eeprom_write_word_crc(EEDATA16.ch4, 0x0000, WITHOUT_CRC);
111
      eeprom_write_word_crc(EEDATA16.ch5, 0x0000, WITHOUT_CRC);
112
      eeprom_write_word_crc(EEDATA16.ch6, 0x0000, WITHOUT_CRC);
113
      eeprom_write_word_crc(EEDATA16.ch7, 0x0000, WITHOUT_CRC);
114
      eeprom_write_word_crc(EEDATA16.ch8, 0x0000, WITHOUT_CRC);
1703 arune 115
      EEDATA_UPDATE_CRC;
116
    }
117
#endif
118
 
1711 arune 119
    pca9634_init(act_pcaPWM_I2C_ADDRESS);
120
    for (uint8_t i=0; i<8; i++)
121
    {
122
        pca9634_setOpMode(i, PCA9634_OP_IND);
123
        /* Set "current" value to something impossible to force an update at start */
124
        isValue[i] = 10001;
125
    }
126
 
127
#ifdef act_pcaPWM_OUTPUT_ENABLE_IO
128
    gpio_set_out(act_pcaPWM_OUTPUT_ENABLE_IO);
129
    gpio_clr_pin(act_pcaPWM_OUTPUT_ENABLE_IO);
130
#endif
131
#ifdef act_pcaPWM_LED_OVERRIDE_IO
132
    gpio_set_out(act_pcaPWM_LED_OVERRIDE_IO);
133
    gpio_clr_pin(act_pcaPWM_LED_OVERRIDE_IO);
134
#endif
1714 arune 135
 
136
#if act_pcaPWM_ENABLE_FADE == 1
137
    Timer_SetTimeout(act_pcaPWM_FADE_TIMER, 10, TimerTypeFreeRunning, &act_pcaPWM_timer_callback);
138
#endif
1703 arune 139
}
140
 
141
void act_pcaPWM_Process(void)
142
{
1711 arune 143
 
144
    for (uint8_t i=0; i < 8; i++)
145
    {
146
        if (setValue[i] != isValue[i])
147
        {
148
            isValue[i] = setValue[i];
149
            pca9634_setDuty(i, isValue[i]*act_pcaPWM_FACT);
1978 arune 150
#if act_pcaPWM_USEEEPROM==1
1711 arune 151
            Timer_SetTimeout(act_pcaPWM_STORE_VALUE_TIMEOUT, act_pcaPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
1712 arune 152
#endif
1711 arune 153
        }
154
    }
155
 
1978 arune 156
#if act_pcaPWM_USEEEPROM==1
1711 arune 157
    if (Timer_Expired(act_pcaPWM_STORE_VALUE_TIMEOUT))
158
    {
159
        if (isValue[0] != eeprom_read_word(EEDATA16.ch1))
160
        {
161
            eeprom_write_word_crc(EEDATA16.ch1, isValue[0], WITH_CRC);
162
        }
163
        if (isValue[1] != eeprom_read_word(EEDATA16.ch2))
164
        {
165
            eeprom_write_word_crc(EEDATA16.ch2, isValue[1], WITH_CRC);
166
        }
167
        if (isValue[2] != eeprom_read_word(EEDATA16.ch3))
168
        {
169
            eeprom_write_word_crc(EEDATA16.ch3, isValue[2], WITH_CRC);
170
        }
171
        if (isValue[3] != eeprom_read_word(EEDATA16.ch4))
172
        {
173
            eeprom_write_word_crc(EEDATA16.ch4, isValue[3], WITH_CRC);
174
        }
175
        if (isValue[4] != eeprom_read_word(EEDATA16.ch5))
176
        {
177
            eeprom_write_word_crc(EEDATA16.ch5, isValue[4], WITH_CRC);
178
        }
179
        if (isValue[5] != eeprom_read_word(EEDATA16.ch6))
180
        {
181
            eeprom_write_word_crc(EEDATA16.ch6, isValue[5], WITH_CRC);
182
        }
183
        if (isValue[6] != eeprom_read_word(EEDATA16.ch7))
184
        {
185
            eeprom_write_word_crc(EEDATA16.ch7, isValue[6], WITH_CRC);
186
        }
187
        if (isValue[7] != eeprom_read_word(EEDATA16.ch8))
188
        {
189
            eeprom_write_word_crc(EEDATA16.ch8, isValue[7], WITH_CRC);
190
        }
191
    }
1712 arune 192
#endif
1703 arune 193
}
194
 
195
void act_pcaPWM_HandleMessage(StdCan_Msg_t *rxMsg)
196
{
1711 arune 197
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
198
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
199
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_PCAPWM &&
1703 arune 200
        rxMsg->Header.ModuleId == act_pcaPWM_ID)
201
    {
1711 arune 202
        uint8_t channel = 0;
1703 arune 203
        switch (rxMsg->Header.Command)
204
        {
1711 arune 205
        case CAN_MODULE_CMD_PHYSICAL_PWM:
206
            if (rxMsg->Length == 3) {
207
                channel = rxMsg->Data[0];
208
                setValue[channel] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
209
            }
1703 arune 210
        break;
1714 arune 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
                }
380
            }
381
        break;
382
#endif
1703 arune 383
        }
384
    }
385
}
386
 
387
void act_pcaPWM_List(uint8_t ModuleSequenceNumber)
388
{
389
    StdCan_Msg_t txMsg;
390
 
1711 arune 391
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
1703 arune 392
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
1711 arune 393
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_PCAPWM;
1703 arune 394
    txMsg.Header.ModuleId = act_pcaPWM_ID;
395
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
396
    txMsg.Length = 6;
397
 
1910 arune 398
    uint32_t HwId=BIOS_GetHwId();
399
    txMsg.Data[0] = HwId&0xff;
400
    txMsg.Data[1] = (HwId>>8)&0xff;
401
    txMsg.Data[2] = (HwId>>16)&0xff;
402
    txMsg.Data[3] = (HwId>>24)&0xff;
1703 arune 403
 
404
    txMsg.Data[4] = NUMBER_OF_MODULES;
405
    txMsg.Data[5] = ModuleSequenceNumber;
406
 
407
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
408
}