Subversion Repositories HomeAutomation

Rev

Rev 1379 | Rev 1388 | 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. uint16_t pwmValueCAN[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, 1 , WITHOUT_CRC);
  41.         eeprom_write_byte_crc(EEDATA.ReportInterval, 0x05 , WITHOUT_CRC);
  42.         EEDATA_UPDATE_CRC;
  43.     //}
  44.     act_softPWM_ReportInterval = eeprom_read_byte(EEDATA.ReportInterval);
  45.     maxTimer = eeprom_read_word(EEDATA16.maxTimer);
  46.     resolution = eeprom_read_byte(EEDATA.resolution);
  47.     Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  48. #endif  
  49.     ///TODO: Initialize hardware etc here
  50. uint8_t i;
  51. for (i=0; i < NUMBEROFCHANNELS; i++) {
  52.     pwmValue[i] = 0;
  53.     pwmValueCAN[i] = 0;
  54. }
  55. #ifdef PIN_0
  56.     gpio_set_out(PIN_0);
  57.     gpio_clr_pin(PIN_0);
  58. #endif
  59. #ifdef PIN_1
  60.     gpio_set_out(PIN_1);
  61.     gpio_clr_pin(PIN_1);
  62. #endif
  63. #ifdef PIN_2
  64.     gpio_set_out(PIN_2);
  65.     gpio_clr_pin(PIN_2);
  66. #endif
  67. #ifdef PIN_3
  68.     gpio_set_out(PIN_3);
  69.     gpio_clr_pin(PIN_3);
  70. #endif
  71. #ifdef PIN_4
  72.     gpio_set_out(PIN_4);
  73.     gpio_clr_pin(PIN_4);
  74. #endif
  75. #ifdef PIN_5
  76.     gpio_set_out(PIN_5);
  77.     gpio_clr_pin(PIN_5);
  78. #endif
  79. #ifdef PIN_6
  80.     gpio_set_out(PIN_6);
  81.     gpio_clr_pin(PIN_6);
  82. #endif
  83. #ifdef PIN_7
  84.     gpio_set_out(PIN_7);
  85.     gpio_clr_pin(PIN_7);
  86. #endif
  87.    
  88.     Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
  89. }
  90.  
  91. void act_softPWM_Process(void)
  92. {
  93.     if (Timer_Expired(act_softPWM_TIMER)) {
  94.         currentTimer++;
  95.         if (currentTimer >= maxTimer) {
  96. //printf("cleard Timer %d\n",pwmValue[0]);
  97.             currentTimer=0;
  98.             #ifdef PIN_0
  99.                 if (pwmValue[0]!=0) {
  100.                     gpio_set_pin(PIN_0);
  101. //printf("pwm_0 On %d %d %d\n", pwmValue[0], currentTimer, maxTimer);
  102. }
  103.             #endif
  104.             #ifdef PIN_1
  105.                 if (pwmValue[1]!=0)
  106.                     gpio_set_pin(PIN_1);
  107.             #endif
  108.             #ifdef PIN_2
  109.                 if (pwmValue[2]!=0)
  110.                     gpio_set_pin(PIN_2);
  111.             #endif
  112.             #ifdef PIN_3
  113.                 if (pwmValue[3]!=0)
  114.                     gpio_set_pin(PIN_3);
  115.             #endif
  116.             #ifdef PIN_4
  117.                 if (pwmValue[4]!=0)
  118.                     gpio_set_pin(PIN_4);
  119.             #endif
  120.             #ifdef PIN_5
  121.                 if (pwmValue[5]!=0)
  122.                     gpio_set_pin(PIN_5);
  123.             #endif
  124.             #ifdef PIN_6
  125.                 if (pwmValue[6]!=0)
  126.                     gpio_set_pin(PIN_6);
  127.             #endif
  128.             #ifdef PIN_7
  129.                 if (pwmValue[7]!=0)
  130.                     gpio_set_pin(PIN_7);
  131.             #endif
  132.         }
  133.         #ifdef PIN_0
  134.         if (currentTimer >= pwmValue[0] && gpio_get_state(PIN_0) != 0) {
  135.             gpio_clr_pin(PIN_0);
  136. //printf("pwm_0 Off %d %d %d\n", pwmValue[0], currentTimer, maxTimer);
  137.         }
  138.         #endif
  139.         #ifdef PIN_1
  140.         if (currentTimer >= pwmValue[1] && gpio_get_state(PIN_1) != 0) {
  141.             gpio_clr_pin(PIN_1);
  142.         }
  143.         #endif
  144.         #ifdef PIN_2
  145.         if (currentTimer >= pwmValue[2] && gpio_get_state(PIN_2) != 0) {
  146.             gpio_clr_pin(PIN_2);
  147.         }
  148.         #endif
  149.         #ifdef PIN_3
  150.         if (currentTimer >= pwmValue[3] && gpio_get_state(PIN_3) != 0) {
  151.             gpio_clr_pin(PIN_3);
  152.         }
  153.         #endif
  154.         #ifdef PIN_4
  155.         if (currentTimer >= pwmValue[4] && gpio_get_state(PIN_4) != 0) {
  156.             gpio_clr_pin(PIN_4);
  157.         }
  158.         #endif
  159.         #ifdef PIN_5
  160.         if (currentTimer >= pwmValue[5] && gpio_get_state(PIN_5) != 0) {
  161.             gpio_clr_pin(PIN_5);
  162.         }
  163.         #endif
  164.         #ifdef PIN_6
  165.         if (currentTimer >= pwmValue[6] && gpio_get_state(PIN_6) != 0) {
  166.             gpio_clr_pin(PIN_6);
  167.         }
  168.         #endif
  169.         #ifdef PIN_7
  170.         if (currentTimer >= pwmValue[7] && gpio_get_state(PIN_7) != 0) {
  171.             gpio_clr_pin(PIN_7);
  172.         }
  173.         #endif
  174.     }
  175.     if (Timer_Expired(act_softPWM_SEND_TIMER)) {
  176. //printf("sending...\n");
  177.         while(1)
  178.         {
  179.             if (0
  180.                 #ifdef PIN_0
  181.                 || currentSendChannelId == 0
  182.                 #endif
  183.                 #ifdef PIN_1
  184.                 || currentSendChannelId == 1
  185.                 #endif
  186.                 #ifdef PIN_2
  187.                 || currentSendChannelId == 2
  188.                 #endif
  189.                 #ifdef PIN_3
  190.                 || currentSendChannelId == 3
  191.                 #endif
  192.                 #ifdef PIN_4
  193.                 || currentSendChannelId == 4
  194.                 #endif
  195.                 #ifdef PIN_5
  196.                 || currentSendChannelId == 5
  197.                 #endif
  198.                 #ifdef PIN_6
  199.                 || currentSendChannelId == 6
  200.                 #endif
  201.                 #ifdef PIN_7
  202.                 || currentSendChannelId == 7
  203.                 #endif
  204.             ) {
  205.                 break;
  206.             }
  207.             currentSendChannelId++;
  208.             if (currentSendChannelId >= NUMBEROFCHANNELS)
  209.                 currentSendChannelId=0;
  210.         }
  211. //printf(" %d\n",currentSendChannelId);
  212.         StdCan_Msg_t txMsg;
  213.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  214.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  215.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
  216.         txMsg.Header.ModuleId = act_softPWM_ID;
  217.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
  218.         txMsg.Length = 3;
  219.         txMsg.Data[0] = currentSendChannelId;
  220.         txMsg.Data[1] = (pwmValueCAN[currentSendChannelId]>>8)&0xff;
  221.         txMsg.Data[2] = (pwmValueCAN[currentSendChannelId])&0xff;
  222.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  223. //printf("sent pwm2: %d %d\n",pwmValueCAN[currentSendChannelId],currentSendChannelId);
  224. #if act_softPWM_ACTIVATE_AUTOOFF != 0
  225.         offCounter[currentSendChannelId]--;
  226.         if (offCounter[currentSendChannelId] == 0) {
  227.             pwmValue[currentSendChannelId] = 0;
  228.             pwmValueCAN[currentSendChannelId] = 0;
  229.             offCounter[currentSendChannelId] = 1;
  230.         }
  231. #endif
  232.         currentSendChannelId++;
  233.         if (currentSendChannelId >= NUMBEROFCHANNELS)
  234.             currentSendChannelId=0;
  235.     }
  236. }
  237.  
  238.  
  239.  
  240. void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg)
  241. {
  242.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  243.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  244.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM &&
  245.         rxMsg->Header.ModuleId == act_softPWM_ID)
  246.     {
  247.         switch (rxMsg->Header.Command)
  248.         {
  249. //printf("Got can\n");
  250.         case CAN_MODULE_CMD_PHYSICAL_PWM:
  251. //printf("Got pwm value\n");
  252.             if (rxMsg->Length == 3)
  253.             {
  254. //printf("3\n");
  255.                 if (rxMsg->Data[0] < NUMBEROFCHANNELS) {
  256.                     pwmValueCAN[rxMsg->Data[0]] = ((uint16_t)((rxMsg->Data[1]<<8) + rxMsg->Data[2]));
  257. if (softPWM_INVERT_PWM == 1) {
  258.                     pwmValue[rxMsg->Data[0]] = maxTimer - ((uint16_t)((((uint32_t)pwmValueCAN[rxMsg->Data[0]])*(maxTimer))/10000));
  259. }else {
  260. pwmValue[rxMsg->Data[0]] = ((uint16_t)((((uint32_t)pwmValueCAN[rxMsg->Data[0]])*(maxTimer))/10000));
  261. }
  262.                     rxMsg->Data[1] = (uint8_t)(0x00ff & (pwmValueCAN[rxMsg->Data[0]]>>8));
  263.                     rxMsg->Data[2] = (uint8_t)(0x00ff & pwmValueCAN[rxMsg->Data[0]]);
  264.                     StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  265.                     rxMsg->Length = 3;
  266.                     while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  267. //printf("sent pwm3: %d %d %d\n",pwmValue[rxMsg->Data[0]],maxTimer,resolution);
  268.                     #if act_softPWM_ACTIVATE_AUTOOFF != 0
  269.                     offCounter[rxMsg->Data[0]] = act_softPWM_ACTIVATE_AUTOOFF;
  270.                     #endif
  271.                 }
  272.             } else if (rxMsg->Length == 1) {
  273.                 rxMsg->Data[1] = (uint8_t)(0x00ff & (pwmValueCAN[rxMsg->Data[0]]>>8));
  274.                 rxMsg->Data[2] = (uint8_t)(0x00ff & pwmValueCAN[rxMsg->Data[0]]);
  275.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  276.                 rxMsg->Length = 3;
  277.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  278.             } else {
  279.                 rxMsg->Data[0] = (8);
  280.                 rxMsg->Data[1] = (uint8_t)(0x00ff & (pwmValueCAN[0]>>8));
  281.                 rxMsg->Data[2] = (uint8_t)(0x00ff & pwmValueCAN[0]);
  282.                 StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  283.                 rxMsg->Length = 3;
  284.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  285.             }
  286.             break;
  287.         case CAN_MODULE_CMD_SOFTPWM_CONFIG:
  288.             maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1];
  289.             resolution = rxMsg->Data[2];
  290.             Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
  291.             eeprom_write_word_crc(EEDATA16.maxTimer, maxTimer , WITHOUT_CRC);
  292.             eeprom_write_byte_crc(EEDATA.resolution, resolution , WITHOUT_CRC);
  293.             EEDATA_UPDATE_CRC;
  294.             StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  295.             while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  296.             break;
  297.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  298.             if (rxMsg->Length > 0)
  299.             {
  300.                 act_softPWM_ReportInterval = rxMsg->Data[0];
  301.                 eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC);
  302.                 Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  303.             }
  304.  
  305.             StdCan_Msg_t txMsg;
  306.  
  307.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  308.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  309.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
  310.             txMsg.Header.ModuleId = act_softPWM_ID;
  311.             txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  312.             txMsg.Length = 1;
  313.             txMsg.Data[0] = act_softPWM_ReportInterval;
  314.             while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  315.             break;
  316.         }
  317.     }
  318. }
  319.  
  320. void act_softPWM_List(uint8_t ModuleSequenceNumber)
  321. {
  322.     StdCan_Msg_t txMsg;
  323.    
  324.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); ///TODO: Change this to the actual class type
  325.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  326.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; ///TODO: Change this to the actual module type
  327.     txMsg.Header.ModuleId = act_softPWM_ID;
  328.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  329.     txMsg.Length = 6;
  330.  
  331.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  332.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  333.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  334.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  335.    
  336.     txMsg.Data[4] = NUMBER_OF_MODULES;
  337.     txMsg.Data[5] = ModuleSequenceNumber;
  338.    
  339.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  340. }
  341.