Subversion Repositories HomeAutomation

Rev

Rev 1369 | Rev 1373 | 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. #if act_softPWM_ACTIVATE_AUTOOFF != 0
  29. uint8_t offCounter[NUMBEROFCHANNELS];
  30. #endif
  31. void act_softPWM_Init(void)
  32. {
  33. #ifdef act_softPWM_USEEEPROM
  34.     if (EEDATA_OK)
  35.     {
  36.       ;
  37.     } else
  38.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  39.         eeprom_write_word_crc(EEDATA16.maxTimer, 10000 , WITHOUT_CRC);
  40.         eeprom_write_byte_crc(EEDATA.resolution, 0x10 , WITHOUT_CRC);
  41.         eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC);
  42.         EEDATA_UPDATE_CRC;
  43.     }
  44.     maxTimer = eeprom_read_word(EEDATA16.maxTimer);
  45.     resolution = eeprom_read_byte(EEDATA.resolution);
  46.     Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  47. #endif  
  48.     ///TODO: Initialize hardware etc here
  49. uint8_t i;
  50. for (i=0, i < NUMBEROFCHANNELS; i++) {
  51.     pwmValue[i] = 0;
  52.     pwmValueFloat[i] = 0;
  53. }
  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
  86.    
  87.     Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
  88. }
  89.  
  90. void act_softPWM_Process(void)
  91. {
  92.     if (Timer_Expired(act_softPWM_TIMER)) {
  93.         currentTimer++;
  94.         if (currentTimer == maxTimer) {
  95.             currentTimer=0;
  96.             #ifdef PIN_0
  97.                 if (pwmValue[0]!=0)
  98.                     gpio_set_pin(PIN_0);
  99.             #endif
  100.             #ifdef PIN_1
  101.                 if (pwmValue[1]!=0)
  102.                     gpio_set_pin(PIN_1);
  103.             #endif
  104.             #ifdef PIN_2
  105.                 if (pwmValue[2]!=0)
  106.                     gpio_set_pin(PIN_2);
  107.             #endif
  108.             #ifdef PIN_3
  109.                 if (pwmValue[3]!=0)
  110.                     gpio_set_pin(PIN_3);
  111.             #endif
  112.             #ifdef PIN_4
  113.                 if (pwmValue[4]!=0)
  114.                     gpio_set_pin(PIN_4);
  115.             #endif
  116.             #ifdef PIN_5
  117.                 if (pwmValue[5]!=0)
  118.                     gpio_set_pin(PIN_5);
  119.             #endif
  120.             #ifdef PIN_6
  121.                 if (pwmValue[6]!=0)
  122.                     gpio_set_pin(PIN_6);
  123.             #endif
  124.             #ifdef PIN_7
  125.                 if (pwmValue[7]!=0)
  126.                     gpio_set_pin(PIN_7);
  127.             #endif
  128.         }
  129.         #ifdef PIN_0
  130.         if (currentTimer >= pwmValue[0]) {
  131.             gpio_clr_pin(PIN_0);
  132.         }
  133.         #endif
  134.         #ifdef PIN_1
  135.         if (currentTimer >= pwmValue[1]) {
  136.             gpio_clr_pin(PIN_1);
  137.         }
  138.         #endif
  139.         #ifdef PIN_2
  140.         if (currentTimer >= pwmValue[2]) {
  141.             gpio_clr_pin(PIN_2);
  142.         }
  143.         #endif
  144.         #ifdef PIN_3
  145.         if (currentTimer >= pwmValue[3]) {
  146.             gpio_clr_pin(PIN_3);
  147.         }
  148.         #endif
  149.         #ifdef PIN_4
  150.         if (currentTimer >= pwmValue[4]) {
  151.             gpio_clr_pin(PIN_4);
  152.         }
  153.         #endif
  154.         #ifdef PIN_5
  155.         if (currentTimer >= pwmValue[5]) {
  156.             gpio_clr_pin(PIN_5);
  157.         }
  158.         #endif
  159.         #ifdef PIN_6
  160.         if (currentTimer >= pwmValue[6]) {
  161.             gpio_clr_pin(PIN_6);
  162.         }
  163.         #endif
  164.         #ifdef PIN_7
  165.         if (currentTimer >= pwmValue[7]) {
  166.             gpio_clr_pin(PIN_7);
  167.         }
  168.         #endif
  169.     }
  170.     if (Timer_Expired(act_softPWM_SEND_TIMER)) {
  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.         }
  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;
  215.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  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
  224.         currentSendChannelId++;
  225.         if (currentSendChannelId >= NUMBEROFCHANNELS)
  226.             currentSendChannelId=0;
  227.     }
  228. }
  229.  
  230.  
  231.  
  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.         {
  241.         case CAN_MODULE_CMD_PHYSICAL_PWM:
  242.             if (rxMsg->Length == 3)
  243.             {
  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);
  252.                     #if act_softPWM_ACTIVATE_AUTOOFF != 0
  253.                     offCounter[rxMsg->Data[0]] = act_softPWM_ACTIVATE_AUTOOFF;
  254.                     #endif
  255.                 }
  256.             } else if (rxMsg->Length == 1) {
  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);
  259.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  260.                 rxMsg->Length = 3;
  261.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  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);
  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);
  273.             break;
  274.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  275.             if (rxMsg->Length > 0)
  276.             {
  277.                 act_softPWM_ReportInterval = rxMsg->Data[0];
  278.                 eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC);
  279.                 Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  280.             }
  281.  
  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;
  291.             while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  292.             break;
  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. }
  318.