Subversion Repositories HomeAutomation

Rev

Rev 1496 | Rev 1566 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "act_hwPWM.h"
  3.  
  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};
  15.  
  16. #ifdef act_hwPWM_USEEEPROM
  17. #include "act_hwPWM_eeprom.h"
  18. struct eeprom_act_hwPWM EEMEM eeprom_act_hwPWM =
  19. {
  20.     {
  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.
  22.         0x0000, // ch 1
  23.         0x0000, // ch 2
  24.         0x0000, // ch 3
  25.         0x0000  // ch 4
  26.     },
  27.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  28. };
  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.  
  81.  
  82. void act_hwPWM_Init(void)
  83. {
  84. #ifdef act_hwPWM_USEEEPROM
  85.     if (EEDATA_OK)
  86.     {
  87.       ///TODO: Use stored data to set initial values for the module
  88.       pwmValue[0] = eeprom_read_word(EEDATA16.ch1);
  89.       pwmValue[1] = eeprom_read_word(EEDATA16.ch2);
  90.       pwmValue[2] = eeprom_read_word(EEDATA16.ch3);
  91.       pwmValue[3] = eeprom_read_word(EEDATA16.ch4);
  92.     } else
  93.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  94.       eeprom_write_word_crc(EEDATA16.ch1, 0x0000, WITHOUT_CRC);
  95.       eeprom_write_word_crc(EEDATA16.ch2, 0x0000, WITHOUT_CRC);
  96.       eeprom_write_word_crc(EEDATA16.ch3, 0x0000, WITHOUT_CRC);
  97.       eeprom_write_word_crc(EEDATA16.ch4, 0x0000, WITHOUT_CRC);
  98.       EEDATA_UPDATE_CRC;
  99.     }
  100. #endif
  101.  
  102.     TCCR1A = 0;
  103.     TCCR1B = 0;
  104.     TCCR0A = 0;
  105.     TCCR0B = 0;
  106.  
  107.     /* set up pwm values */
  108.     cli();
  109.     #if act_hwPWM_CH1_COM>0
  110.     OCR_1=(uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8;
  111.     #endif
  112.     #if act_hwPWM_CH2_COM>0
  113.     OCR_2=(uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8;
  114.     #endif
  115.     #if act_hwPWM_CH3_COM>0
  116.     OCR_3=(uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8;
  117.     #endif
  118.     #if act_hwPWM_CH4_COM>0
  119.     OCR_4=(uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8;
  120.     #endif
  121.     sei();
  122.  
  123.     /* set up waveform generation mode for timer 1 */
  124.     #if act_hwPWM_CH1_COM>0
  125.     TCCR1A=((act_hwPWM_CH1_WGM&0x03)<<WGM00);
  126.     TCCR1B=(((act_hwPWM_CH1_WGM>>2)&0x03)<<WGM12);
  127.     #elif act_hwPWM_CH2_COM>0
  128.     TCCR1A=((act_hwPWM_CH2_WGM&0x03)<<WGM00);
  129.     TCCR1B=(((act_hwPWM_CH2_WGM>>2)&0x03)<<WGM12);
  130.     #endif
  131.  
  132.     /* enable outputs for timer 1 */
  133.     #if act_hwPWM_CH1_COM>0
  134.     gpio_set_out(EXP_B);
  135.     #endif
  136.     #if act_hwPWM_CH2_COM>0
  137.     gpio_set_out(EXP_C);
  138.     #endif
  139.  
  140.     /* set up counter mode for timer 1 */
  141.     #if act_hwPWM_CH1_COM>0 || act_hwPWM_CH2_COM>0
  142.     if (pwmValue[0]>0)
  143.     {
  144.         TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
  145.     }
  146.     if (pwmValue[1]>0)
  147.     {
  148.         TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
  149.     }
  150.     #endif
  151.  
  152.     /* enable timer 1 */
  153.     #if act_hwPWM_CH1_COM>0
  154.     TCCR1B|=(act_hwPWM_CH1_CS<<CS10);
  155.     #elif act_hwPWM_CH2_COM>0
  156.     TCCR1B|=(act_hwPWM_CH2_CS<<CS10);
  157.     #endif
  158.  
  159.  
  160.     /* set up waveform generation mode for timer 0 */
  161.     #if act_hwPWM_CH3_COM>0
  162.     TCCR0A=((act_hwPWM_CH3_WGM&0x03)<<WGM00);
  163.     TCCR0B=(((act_hwPWM_CH3_WGM>>2)&0x01)<<WGM02);
  164.     #elif act_hwPWM_CH4_COM>0
  165.     TCCR0A=((act_hwPWM_CH4_WGM&0x03)<<WGM00);
  166.     TCCR0B=(((act_hwPWM_CH4_WGM>>2)&0x01)<<WGM02);
  167.     #endif
  168.  
  169.     /* enable outputs for timer 0 */
  170.     #if act_hwPWM_CH3_COM>0
  171.     gpio_set_out(EXP_F);
  172.     #endif
  173.     #if act_hwPWM_CH4_COM>0
  174.     gpio_set_out(EXP_G);
  175.     #endif
  176.  
  177.     /* set up counter mode for timer 0 */
  178.     #if act_hwPWM_CH3_COM>0 || act_hwPWM_CH4_COM>0
  179.     if (pwmValue[2]>0)
  180.     {
  181.         TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
  182.     }
  183.     if (pwmValue[3]>0)
  184.     {
  185.         TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
  186.     }
  187.     #endif
  188.  
  189.     /* enable timer 0 */
  190.     #if act_hwPWM_CH3_COM>0
  191.     TCCR0B|=(act_hwPWM_CH3_CS<<CS00);
  192.     #elif act_hwPWM_CH4_COM>0
  193.     TCCR0B|=(act_hwPWM_CH4_CS<<CS00);
  194.     #endif
  195.     //printf("1A %x, 1B %x, 0A %x, 0B %x\n", TCCR1A, TCCR1B, TCCR0A, TCCR0B);
  196.    
  197. #if act_hwPWM_ENABLE_FADE == 1
  198.     Timer_SetTimeout(act_hwPWM_FADE_TIMER, 10, TimerTypeFreeRunning, &act_hwPWM_timer_callback);
  199. #endif
  200.    
  201.     /* Setup timeout for sending the status packet */
  202.     Timer_SetTimeout(act_hwPWM_SEND_STATUS_TIMEOUT, act_hwPWM_SEND_STATUS_INTERVAL_S*1000, TimerTypeFreeRunning, 0);
  203.     //printf("Hello world!\n");
  204. }
  205. uint8_t channel_to_send = 1;
  206. void act_hwPWM_Process(void)
  207. {
  208.     if (Timer_Expired(act_hwPWM_SEND_STATUS_TIMEOUT))
  209.     {
  210.         if (channel_to_send >4) {
  211.             channel_to_send=1;
  212.         }
  213. /*      while (0
  214. #if act_hwPWM_CH1_COM>0
  215.           || channel_to_send != 1
  216. #endif
  217. #if act_hwPWM_CH2_COM>0
  218.           || channel_to_send != 2
  219. #endif
  220. #if act_hwPWM_CH3_COM>0
  221.           || channel_to_send != 3
  222. #endif
  223. #if act_hwPWM_CH4_COM>0
  224.           || channel_to_send != 4
  225. #endif
  226.         ) {
  227.             channel_to_send++;
  228.             if (channel_to_send >4) {
  229.                   channel_to_send=1;
  230.             }
  231.         }
  232.         */
  233.         sendInfo[channel_to_send-1] = 1;
  234.         channel_to_send++;
  235. #if act_hwPWM_CH1_COM==0
  236.           if (channel_to_send == 1)
  237.             channel_to_send++;
  238. #endif     
  239. #if act_hwPWM_CH2_COM==0
  240.           if (channel_to_send == 2)
  241.             channel_to_send++;
  242. #endif 
  243. #if act_hwPWM_CH3_COM==0
  244.           if (channel_to_send == 3)
  245.             channel_to_send++;
  246. #endif 
  247. #if act_hwPWM_CH4_COM==0
  248.           if (channel_to_send == 4)
  249.             channel_to_send++;
  250. #endif 
  251.        
  252.        
  253.     }
  254.     /* Send netinfo packet (if pwmvalue has changed, and periodically) */
  255.     uint8_t index;
  256.     for(index =0;index < 4; index++){
  257.         if (pwmValue[index] > 10000) {
  258.             pwmValue[index] = 10000;
  259.         }
  260.         if (sendInfo[index])
  261.         {
  262.             sendInfo[index] = 0;
  263.             StdCan_Msg_t txMsg;
  264.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  265.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  266.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_HWPWM;
  267.             txMsg.Header.ModuleId = act_hwPWM_ID;
  268.             txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
  269.             txMsg.Length = 3;
  270.             txMsg.Data[0] = index+1;
  271.             txMsg.Data[1] = (0xff&(pwmValue[index]>>8));
  272.             txMsg.Data[2] = (0xff&(pwmValue[index]));
  273.             StdCan_Put(&txMsg);
  274.         }
  275.     }
  276.    
  277.     if (Timer_Expired(act_hwPWM_STORE_VALUE_TIMEOUT))
  278.     {
  279.         if (pwmValue[0] != eeprom_read_word(EEDATA16.ch1))
  280.         {
  281.             eeprom_write_word_crc(EEDATA16.ch1, pwmValue[0], WITH_CRC);
  282.         }
  283.         if (pwmValue[1] != eeprom_read_word(EEDATA16.ch2))
  284.         {
  285.             eeprom_write_word_crc(EEDATA16.ch2, pwmValue[1], WITH_CRC);
  286.         }
  287.         if (pwmValue[2] != eeprom_read_word(EEDATA16.ch3))
  288.         {
  289.             eeprom_write_word_crc(EEDATA16.ch3, pwmValue[2], WITH_CRC);
  290.         }
  291.         if (pwmValue[3] != eeprom_read_word(EEDATA16.ch4))
  292.         {
  293.             eeprom_write_word_crc(EEDATA16.ch4, pwmValue[3], WITH_CRC);
  294.         }
  295.     }
  296.                    
  297. #if act_hwPWM_CH1_COM>0
  298.     if (OCR_1 != (uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8) {
  299.         cli(); 
  300.         OCR_1=(uint16_t)(pwmValue[0]*act_hwPWM_CH1_FACT)>>8;
  301.         if (OCR_1==0)
  302.         {
  303.             TCCR1A&=~((1<<COM1B0)|(1<<COM1B1));
  304.         }
  305.         else
  306.         {
  307.             TCCR1A|=(act_hwPWM_CH1_COM<<COM1B0);
  308.         }
  309.         sei();
  310.         Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
  311.     }
  312. #endif
  313. #if act_hwPWM_CH2_COM>0
  314.     if (OCR_2 != (uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8) {
  315.         cli(); 
  316.         OCR_2=(uint16_t)(pwmValue[1]*act_hwPWM_CH2_FACT)>>8;
  317.         if (OCR_2==0)
  318.         {
  319.             TCCR1A&=~((1<<COM1A0)|(1<<COM1A1));
  320.         }
  321.         else
  322.         {
  323.             TCCR1A|=(act_hwPWM_CH2_COM<<COM1A0);
  324.         }
  325.         sei();
  326.         Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
  327.     }
  328. #endif
  329. #if act_hwPWM_CH3_COM>0
  330.     if (OCR_3 != (uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8) {
  331.         cli(); 
  332.         OCR_3=(uint16_t)(pwmValue[2]*act_hwPWM_CH3_FACT)>>8;
  333.         if (OCR_3==0)
  334.         {
  335.             TCCR0A &= ~((1<<COM0A0)|(1<<COM0A1));
  336.         }
  337.         else
  338.         {
  339.             TCCR0A|=(act_hwPWM_CH3_COM<<COM0A0);
  340.         }
  341.         sei();
  342.         Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
  343.     }
  344. #endif
  345. #if act_hwPWM_CH4_COM>0
  346.     if (OCR_4 != (uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8) {
  347.         cli(); 
  348.         OCR_4=(uint16_t)(pwmValue[3]*act_hwPWM_CH4_FACT)>>8;
  349.         if (OCR_4==0)
  350.         {
  351.             TCCR0A &= ~((1<<COM0B0)|(1<<COM0B1));
  352.         }
  353.         else
  354.         {
  355.             TCCR0A|=(act_hwPWM_CH4_COM<<COM0B0);
  356.         }
  357.         sei();
  358.         Timer_SetTimeout(act_hwPWM_STORE_VALUE_TIMEOUT, act_hwPWM_STORE_VALUE_TIMEOUT_TIME*1000, TimerTypeOneShot, 0);
  359.     }
  360. #endif
  361. }
  362.  
  363. void act_hwPWM_HandleMessage(StdCan_Msg_t *rxMsg)
  364. {
  365.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  366.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  367.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_HWPWM &&
  368.         rxMsg->Header.ModuleId == act_hwPWM_ID)
  369.     {
  370.         switch (rxMsg->Header.Command)
  371.         {
  372.         case CAN_MODULE_CMD_PHYSICAL_PWM:
  373.             if (rxMsg->Length == 3) {
  374.                 uint8_t channel = rxMsg->Data[0];
  375.                 pwmValue[channel-1] = (rxMsg->Data[1]<<8)+(rxMsg->Data[2]);
  376.             }
  377.            
  378.         break;
  379. #if act_hwPWM_ENABLE_FADE == 1
  380.         case CAN_MODULE_CMD_HWPWM_DEMO:     /* Demo(channel, speed, steps) */
  381.             if (rxMsg->Length == 4) {
  382.                 uint8_t channel = rxMsg->Data[0]-1;
  383.                 uint8_t speed = rxMsg->Data[1];
  384.                 uint16_t steps = (rxMsg->Data[2]<<8)+(rxMsg->Data[3]);
  385.                
  386.                 fadeSpeedCnt[channel] = 0;
  387.                 fadeSpeed[channel] = 0;
  388.                 if (speed == 0) {
  389.                     /* do nothing */
  390.                 } else {
  391.                     uint16_t diffToMin = pwmValue[channel] - ACT_HWPWM_MIN_DIM;
  392.                     uint16_t diffToMax = ACT_HWPWM_MAX_DIM - pwmValue[channel];
  393.                
  394.                     demoEndValue[channel] = pwmValue[channel];
  395.                     if (diffToMin >= steps && diffToMax >= steps)
  396.                     {
  397.                         /* not close to min or max */
  398.                         fadeTarget[channel] = pwmValue[channel] - steps;
  399.                         demoHighValue[channel] = pwmValue[channel] + steps;
  400.                     }
  401.                     else if (diffToMin >= steps)
  402.                     {
  403.                         /* close to max */
  404.                         fadeTarget[channel] = pwmValue[channel] - steps - steps + diffToMax;
  405.                         demoHighValue[channel] = ACT_HWPWM_MAX_DIM;
  406.                     }
  407.                     else if (diffToMax >= steps)
  408.                     {
  409.                         /* close to min */
  410.                         fadeTarget[channel] = ACT_HWPWM_MIN_DIM;
  411.                         demoHighValue[channel] = pwmValue[channel] + steps + steps - diffToMin;
  412.                     }
  413.                     demoState[channel] = ACT_HWPWM_DEMO_STATE_DECREASE;
  414.                
  415.                     if ((speed&0x80) == 0x80) {
  416.                         fadeSpeed[channel] = (speed&0x7f)+1;
  417.                         fadeSpeedFrac[channel] = 1;
  418.                     } else {
  419.                         fadeSpeed[channel] = 1;
  420.                         fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
  421.                     }
  422.                     if (fadeTarget[channel] <= pwmValue[channel]) {
  423.                         fadeSpeed[channel] = -fadeSpeed[channel];
  424.                     }
  425.                 }
  426.             }
  427.         break;
  428.        
  429.         case CAN_MODULE_CMD_HWPWM_START_FADE:   /* StartFade(channel, speed, direction) */
  430.             if (rxMsg->Length == 3) {
  431.                
  432.                 uint8_t channel = rxMsg->Data[0]-1;
  433.                 uint8_t speed = rxMsg->Data[1];
  434.                 uint8_t direction = rxMsg->Data[2];
  435.                 demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
  436.                 fadeSpeedCnt[channel] = 0;
  437.                 fadeSpeed[channel] = 0;
  438.                 uint16_t endValue = 0;
  439.                 if (direction == CAN_MODULE_ENUM_HWPWM_START_FADE_DIRECTION_INCREASE) {
  440.                     endValue = ACT_HWPWM_MAX_DIM;
  441.                 } else if (direction == CAN_MODULE_ENUM_HWPWM_START_FADE_DIRECTION_DECREASE) {
  442.                     endValue = ACT_HWPWM_MIN_DIM;
  443.                 }
  444.                    
  445.                 if (speed == 0) {
  446.                     pwmValue[channel] = endValue;   /* set dimmer value immediately */
  447.                     sendInfo[channel] = 1;      /* send netinfo with the current dimmervalue*/
  448.                 } else {
  449.                     fadeTarget[channel] = endValue;
  450.                     if (fadeTarget[channel] != pwmValue[channel]) {
  451.                         if ((speed&0x80) == 0x80) {
  452.                             fadeSpeed[channel] = (speed&0x7f)+1;
  453.                             fadeSpeedFrac[channel] = 1;
  454.                         } else {
  455.                             fadeSpeed[channel] = 1;
  456.                             fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
  457.                         }
  458.                         if (fadeTarget[channel] < pwmValue[channel]) {
  459.                             fadeSpeed[channel] = -fadeSpeed[channel];
  460.                         }
  461.                     }
  462.                 }
  463.             }
  464.         break;
  465.  
  466.         case CAN_MODULE_CMD_HWPWM_STOP_FADE:    /* StopFade(channel) */
  467.             if (rxMsg->Length == 1) {
  468.                 uint8_t channel = rxMsg->Data[0]-1;
  469.                 demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
  470.                 fadeSpeed[channel] = 0;
  471.                 sendInfo[channel] = 1;      /* send netinfo with the current dimmervalue*/
  472.             }
  473.         break;
  474.  
  475.         case CAN_MODULE_CMD_HWPWM_ABS_FADE: /* AbsFade(channel, speed, endValue) */
  476.             if (rxMsg->Length == 4) {
  477.                 uint8_t channel = rxMsg->Data[0]-1;
  478.                 uint8_t speed = rxMsg->Data[1];
  479.                 uint16_t endValue = (rxMsg->Data[2]<<8)+(rxMsg->Data[3]);
  480.                 demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
  481.                 fadeSpeedCnt[channel] = 0;
  482.                 fadeSpeed[channel] = 0;
  483.                 if (speed == 0) {
  484.                     pwmValue[channel] = endValue;   /* set dimmer value immediately */
  485.                     sendInfo[channel] = 1;      /* send netinfo with the current dimmervalue*/
  486.                 } else {
  487.                     fadeTarget[channel] = endValue;
  488.                     if (fadeTarget[channel] != pwmValue[channel]) {
  489.                         if ((speed&0x80) == 0x80) {
  490.                             fadeSpeed[channel] = (speed&0x7f)+1;
  491.                             fadeSpeedFrac[channel] = 1;
  492.                         } else {
  493.                             fadeSpeed[channel] = 1;
  494.                             fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
  495.                         }
  496.                         if (fadeTarget[channel] < pwmValue[channel]) {
  497.                             fadeSpeed[channel] = -fadeSpeed[channel];
  498.                         }
  499.                     }
  500.                 }
  501.                 printf("abs fade %d %d %d!\n",fadeTarget[channel], endValue ,pwmValue[channel] );
  502.             }
  503.         break;
  504.  
  505.         case CAN_MODULE_CMD_HWPWM_REL_FADE: /* RelFade(channel, speed, direction, steps) */
  506.             if (rxMsg->Length == 5) {
  507.                
  508.                 uint8_t channel = rxMsg->Data[0]-1;
  509.                 uint8_t speed = rxMsg->Data[1];
  510.                 uint8_t direction = rxMsg->Data[2];
  511.                 uint16_t steps = (rxMsg->Data[3]<<8)+(rxMsg->Data[4]);
  512.                
  513.                 demoState[channel] = ACT_HWPWM_DEMO_STATE_NOT_RUNNING;
  514.                 fadeSpeedCnt[channel] = 0;
  515.                 fadeSpeed[channel] = 0;
  516.                 uint16_t tempDimVal = pwmValue[channel];
  517.                 uint16_t tempDimVal2 = pwmValue[channel];
  518.                 if (direction == CAN_MODULE_ENUM_HWPWM_REL_FADE_DIRECTION_INCREASE) {                   /* if increase */
  519.                     tempDimVal2 += steps;               /* calculate new value */
  520.                     if (tempDimVal2 < tempDimVal) {     /* make overflow test */
  521.                         tempDimVal2 = ACT_HWPWM_MAX_DIM;
  522.                     }
  523.                 } else if (direction == CAN_MODULE_ENUM_HWPWM_REL_FADE_DIRECTION_DECREASE) {            /* if decrease */
  524.                     tempDimVal2 -= steps;
  525.                     if (tempDimVal2 > tempDimVal) {
  526.                         tempDimVal2 = ACT_HWPWM_MIN_DIM;
  527.                     }
  528.                 }
  529.                 if (speed == 0) {
  530.                     pwmValue[channel] = tempDimVal2;        /* set dimmer value immediately */
  531.                     sendInfo[channel] = 1;              /* send netinfo with the current dimmervalue*/
  532.                 } else {
  533.                     fadeTarget[channel] = tempDimVal2;      /* set the fade target */
  534.                    
  535.                     if (fadeTarget[channel] != pwmValue[channel]) {
  536.                         if ((speed&0x80) == 0x80) {
  537.                             fadeSpeed[channel] = (speed&0x7f)+1;
  538.                             fadeSpeedFrac[channel] = 1;
  539.                         } else {
  540.                             fadeSpeed[channel] = 1;
  541.                             fadeSpeedFrac[channel] = 0x80-(speed&0x7f);
  542.                         }
  543.                         if (fadeTarget[channel] < pwmValue[channel]) {
  544.                             fadeSpeed[channel] = -fadeSpeed[channel];
  545.                         }
  546.                     }
  547.                 }
  548.             }
  549.         break;
  550. #endif
  551.         }
  552.     }
  553. }
  554.  
  555. void act_hwPWM_List(uint8_t ModuleSequenceNumber)
  556. {
  557.     StdCan_Msg_t txMsg;
  558.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  559.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  560.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_HWPWM;
  561.     txMsg.Header.ModuleId = act_hwPWM_ID;
  562.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  563.     txMsg.Length = 6;
  564.  
  565.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  566.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  567.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  568.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  569.    
  570.     txMsg.Data[4] = NUMBER_OF_MODULES;
  571.     txMsg.Data[5] = ModuleSequenceNumber;
  572.  
  573.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  574. }
  575.