Subversion Repositories HomeAutomation

Rev

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