Subversion Repositories HomeAutomation

Rev

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

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