Subversion Repositories HomeAutomation

Rev

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