Subversion Repositories HomeAutomation

Rev

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

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