Subversion Repositories HomeAutomation

Rev

Rev 1370 | Rev 1379 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1156 linlun 1
 
2
#include "act_softPWM.h"
1183 linlun 3
 
4
 
1177 linlun 5
uint16_t currentTimer = 0;
6
uint16_t maxTimer = 10;
7
uint8_t resolution = 1;
1369 linlun 8
uint8_t act_softPWM_ReportInterval;
1180 linlun 9
uint8_t currentSendChannelId = 0;
1156 linlun 10
 
1183 linlun 11
 
1156 linlun 12
#ifdef act_softPWM_USEEEPROM
13
#include "act_softPWM_eeprom.h"
14
struct eeprom_act_softPWM EEMEM eeprom_act_softPWM =
15
{
16
    {
17
        ///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. 
1369 linlun 18
        10000,  // x
19
        10,
1180 linlun 20
        0x14    //Reportinteval 20 sec.
1156 linlun 21
    },
22
 
23
};
24
#endif
1183 linlun 25
 
1180 linlun 26
uint16_t pwmValue[NUMBEROFCHANNELS];
1369 linlun 27
float pwmValueFloat[NUMBEROFCHANNELS];
1370 linlun 28
#if act_softPWM_ACTIVATE_AUTOOFF != 0
29
uint8_t offCounter[NUMBEROFCHANNELS];
30
#endif
1156 linlun 31
void act_softPWM_Init(void)
32
{
33
#ifdef act_softPWM_USEEEPROM
34
    if (EEDATA_OK)
35
    {
1180 linlun 36
      ;
1156 linlun 37
    } else
38
    {   //The CRC of the EEPROM is not correct, store default values and update CRC
1369 linlun 39
        eeprom_write_word_crc(EEDATA16.maxTimer, 10000 , WITHOUT_CRC);
40
        eeprom_write_byte_crc(EEDATA.resolution, 0x10 , WITHOUT_CRC);
1180 linlun 41
        eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC);
42
        EEDATA_UPDATE_CRC;
1156 linlun 43
    }
1369 linlun 44
    maxTimer = eeprom_read_word(EEDATA16.maxTimer);
45
    resolution = eeprom_read_byte(EEDATA.resolution);
1180 linlun 46
    Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
1156 linlun 47
#endif  
48
    ///TODO: Initialize hardware etc here
1369 linlun 49
uint8_t i;
1373 linlun 50
for (i=0; i < NUMBEROFCHANNELS; i++) {
1369 linlun 51
    pwmValue[i] = 0;
52
    pwmValueFloat[i] = 0;
53
}
1177 linlun 54
#ifdef PIN_0
55
    gpio_set_out(PIN_0);
56
    gpio_clr_pin(PIN_0);
57
#endif
58
#ifdef PIN_1
59
    gpio_set_out(PIN_1);
60
    gpio_clr_pin(PIN_1);
61
#endif
62
#ifdef PIN_2
63
    gpio_set_out(PIN_2);
64
    gpio_clr_pin(PIN_2);
65
#endif
66
#ifdef PIN_3
67
    gpio_set_out(PIN_3);
68
    gpio_clr_pin(PIN_3);
69
#endif
70
#ifdef PIN_4
71
    gpio_set_out(PIN_4);
72
    gpio_clr_pin(PIN_4);
73
#endif
74
#ifdef PIN_5
75
    gpio_set_out(PIN_5);
76
    gpio_clr_pin(PIN_5);
77
#endif
78
#ifdef PIN_6
79
    gpio_set_out(PIN_6);
80
    gpio_clr_pin(PIN_6);
81
#endif
82
#ifdef PIN_7
83
    gpio_set_out(PIN_7);
84
    gpio_clr_pin(PIN_7);
85
#endif
1156 linlun 86
 
1177 linlun 87
    Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
1156 linlun 88
}
89
 
90
void act_softPWM_Process(void)
91
{
92
    if (Timer_Expired(act_softPWM_TIMER)) {
93
        currentTimer++;
1369 linlun 94
        if (currentTimer == maxTimer) {
1156 linlun 95
            currentTimer=0;
1370 linlun 96
            #ifdef PIN_0
97
                if (pwmValue[0]!=0)
98
                    gpio_set_pin(PIN_0);
1177 linlun 99
            #endif
100
            #ifdef PIN_1 
1370 linlun 101
                if (pwmValue[1]!=0)
102
                    gpio_set_pin(PIN_1);
1177 linlun 103
            #endif
104
            #ifdef PIN_2 
1370 linlun 105
                if (pwmValue[2]!=0)
106
                    gpio_set_pin(PIN_2);
1177 linlun 107
            #endif
108
            #ifdef PIN_3 
1370 linlun 109
                if (pwmValue[3]!=0)
110
                    gpio_set_pin(PIN_3);
1177 linlun 111
            #endif
112
            #ifdef PIN_4 
1370 linlun 113
                if (pwmValue[4]!=0)
114
                    gpio_set_pin(PIN_4);
1177 linlun 115
            #endif
116
            #ifdef PIN_5 
1370 linlun 117
                if (pwmValue[5]!=0)
118
                    gpio_set_pin(PIN_5);
1177 linlun 119
            #endif
120
            #ifdef PIN_6 
1370 linlun 121
                if (pwmValue[6]!=0)
122
                    gpio_set_pin(PIN_6);
1177 linlun 123
            #endif
124
            #ifdef PIN_7 
1370 linlun 125
                if (pwmValue[7]!=0)
126
                    gpio_set_pin(PIN_7);
1177 linlun 127
            #endif
1156 linlun 128
        }
1177 linlun 129
        #ifdef PIN_0
1184 linlun 130
        if (currentTimer >= pwmValue[0]) {
1177 linlun 131
            gpio_clr_pin(PIN_0);
1156 linlun 132
        }
1177 linlun 133
        #endif
134
        #ifdef PIN_1
1184 linlun 135
        if (currentTimer >= pwmValue[1]) {
1177 linlun 136
            gpio_clr_pin(PIN_1);
1156 linlun 137
        }
1177 linlun 138
        #endif
139
        #ifdef PIN_2
1184 linlun 140
        if (currentTimer >= pwmValue[2]) {
1177 linlun 141
            gpio_clr_pin(PIN_2);
1156 linlun 142
        }
1177 linlun 143
        #endif
144
        #ifdef PIN_3
1184 linlun 145
        if (currentTimer >= pwmValue[3]) {
1177 linlun 146
            gpio_clr_pin(PIN_3);
147
        }
148
        #endif
149
        #ifdef PIN_4
1184 linlun 150
        if (currentTimer >= pwmValue[4]) {
1177 linlun 151
            gpio_clr_pin(PIN_4);
152
        }
153
        #endif
154
        #ifdef PIN_5
1184 linlun 155
        if (currentTimer >= pwmValue[5]) {
1177 linlun 156
            gpio_clr_pin(PIN_5);
157
        }
158
        #endif
159
        #ifdef PIN_6
1184 linlun 160
        if (currentTimer >= pwmValue[6]) {
1177 linlun 161
            gpio_clr_pin(PIN_6);
162
        }
163
        #endif
164
        #ifdef PIN_7
1184 linlun 165
        if (currentTimer >= pwmValue[7]) {
1177 linlun 166
            gpio_clr_pin(PIN_7);
167
        }
168
        #endif
1156 linlun 169
    }
1180 linlun 170
    if (Timer_Expired(act_softPWM_SEND_TIMER)) {
1184 linlun 171
        while(1)
172
        {
173
            if (0
174
                #ifdef PIN_0
175
                || currentSendChannelId == 0
176
                #endif
177
                #ifdef PIN_1
178
                || currentSendChannelId == 1
179
                #endif
180
                #ifdef PIN_2
181
                || currentSendChannelId == 2
182
                #endif
183
                #ifdef PIN_3
184
                || currentSendChannelId == 3
185
                #endif
186
                #ifdef PIN_4
187
                || currentSendChannelId == 4
188
                #endif
189
                #ifdef PIN_5
190
                || currentSendChannelId == 5
191
                #endif
192
                #ifdef PIN_6
193
                || currentSendChannelId == 6
194
                #endif
195
                #ifdef PIN_7
196
                || currentSendChannelId == 7
197
                #endif
198
            ) {
199
                break;
200
            }
201
            currentSendChannelId++;
202
            if (currentSendChannelId >= NUMBEROFCHANNELS)
203
                currentSendChannelId=0;
204
        }
1180 linlun 205
        StdCan_Msg_t txMsg;
206
        StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
207
        StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
208
        txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
209
        txMsg.Header.ModuleId = act_softPWM_ID;
210
        txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
211
        txMsg.Length = 3;
212
        txMsg.Data[0] = currentSendChannelId;
213
        txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff;
214
        txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff;
1369 linlun 215
        while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
1370 linlun 216
#if act_softPWM_ACTIVATE_AUTOOFF != 0
217
        offCounter[currentSendChannelId]--;
218
        if (offCounter[currentSendChannelId] == 0) {
219
            pwmValue[currentSendChannelId] = 0;
220
            pwmValueFloat[currentSendChannelId] = 0;
221
            offCounter[currentSendChannelId] = 1;
222
        }
223
#endif
1180 linlun 224
        currentSendChannelId++;
225
        if (currentSendChannelId >= NUMBEROFCHANNELS)
226
            currentSendChannelId=0;
227
    }
1156 linlun 228
}
229
 
1180 linlun 230
 
231
 
1156 linlun 232
void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg)
233
{
234
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
235
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
236
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM &&
237
        rxMsg->Header.ModuleId == act_softPWM_ID)
238
    {
239
        switch (rxMsg->Header.Command)
240
        {
1177 linlun 241
        case CAN_MODULE_CMD_PHYSICAL_PWM:
1369 linlun 242
            if (rxMsg->Length == 3)
1184 linlun 243
            {
1369 linlun 244
                if (rxMsg->Data[0] < NUMBEROFCHANNELS) {
245
                    pwmValueFloat[rxMsg->Data[0]] = ((rxMsg->Data[1]<<8) + rxMsg->Data[2])/64;
246
                    pwmValue[rxMsg->Data[0]] = (uint16_t)(pwmValueFloat[rxMsg->Data[0]]*maxTimer/(resolution*100));
247
                    rxMsg->Data[1] = (uint8_t)0x00ff & (((uint32_t)(pwmValueFloat[rxMsg->Data[0]]*64))>>8);
248
                    rxMsg->Data[2] = (uint8_t)0x00ff & ((uint32_t)pwmValueFloat[rxMsg->Data[0]]*64);
249
                    StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
250
                    rxMsg->Length = 3;
251
                    while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
1370 linlun 252
                    #if act_softPWM_ACTIVATE_AUTOOFF != 0
253
                    offCounter[rxMsg->Data[0]] = act_softPWM_ACTIVATE_AUTOOFF;
254
                    #endif
1369 linlun 255
                }
1243 arune 256
            } else if (rxMsg->Length == 1) {
1369 linlun 257
                rxMsg->Data[1] = (uint8_t)0x00ff & (((uint32_t)(pwmValueFloat[rxMsg->Data[0]]*64))>>8);
258
                rxMsg->Data[2] = (uint8_t)0x00ff & ((uint32_t)pwmValueFloat[rxMsg->Data[0]]*64);
1184 linlun 259
                StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
1369 linlun 260
                rxMsg->Length = 3;
261
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
1177 linlun 262
            }
263
            break;
264
        case CAN_MODULE_CMD_SOFTPWM_CONFIG:
265
            maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1];
266
            resolution = rxMsg->Data[2];
267
            Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
1369 linlun 268
            eeprom_write_word_crc(EEDATA16.maxTimer, maxTimer , WITHOUT_CRC);
269
            eeprom_write_byte_crc(EEDATA.resolution, resolution , WITHOUT_CRC);
270
            EEDATA_UPDATE_CRC;
271
            StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
272
            while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
1177 linlun 273
            break;
1180 linlun 274
        case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
275
            if (rxMsg->Length > 0)
276
            {
277
                act_softPWM_ReportInterval = rxMsg->Data[0];
1242 arune 278
                eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC);
1180 linlun 279
                Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
280
            }
1177 linlun 281
 
1180 linlun 282
            StdCan_Msg_t txMsg;
283
 
284
            StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
285
            StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
286
            txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
287
            txMsg.Header.ModuleId = act_softPWM_ID;
288
            txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
289
            txMsg.Length = 1;
290
            txMsg.Data[0] = act_softPWM_ReportInterval;
1369 linlun 291
            while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
1180 linlun 292
            break;
1156 linlun 293
        }
294
    }
295
}
296
 
297
void act_softPWM_List(uint8_t ModuleSequenceNumber)
298
{
299
    StdCan_Msg_t txMsg;
300
 
301
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); ///TODO: Change this to the actual class type
302
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
303
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; ///TODO: Change this to the actual module type
304
    txMsg.Header.ModuleId = act_softPWM_ID;
305
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
306
    txMsg.Length = 6;
307
 
308
    txMsg.Data[0] = NODE_HW_ID_BYTE0;
309
    txMsg.Data[1] = NODE_HW_ID_BYTE1;
310
    txMsg.Data[2] = NODE_HW_ID_BYTE2;
311
    txMsg.Data[3] = NODE_HW_ID_BYTE3;
312
 
313
    txMsg.Data[4] = NUMBER_OF_MODULES;
314
    txMsg.Data[5] = ModuleSequenceNumber;
315
 
316
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
317
}