Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "sns_rotary.h"
  3. uint8_t rotaryEncoder_Position = 0;
  4. uint8_t rotaryEncoder_Position_old = 0;
  5. uint8_t rotaryEncoder_Button_Position = 0;
  6. uint8_t rotaryEncoder_Button_Position_old = 0;
  7.  
  8. /*********************************************************************//**
  9. Function: SIGNAL(ROTARY_CH1_SIGNAL)
  10. Purpose:  Executed when pin change on ROTARY_CH1 is seen.
  11. Input:    -
  12. Returns:  -
  13. **************************************************************************/
  14. SIGNAL(ROTARY_BTN_PCINT_vector) {
  15.     uint8_t rot_data = 0;
  16.     static uint8_t rot_lastdir = 0, rot_laststate = 0;
  17.  
  18.     //Take care of button push
  19.     if ((ROTARY_BTN_PIN&(1<<ROTARY_BTN)) != rotaryEncoder_Button_Position){ //The buttonstate has changed!
  20.         rotaryEncoder_Button_Position = ROTARY_BTN_PIN&(1<<ROTARY_BTN);
  21.     }
  22.  
  23.     //Take care of rotary encoder movement
  24.     if(ROTARY_CH1_PIN&(1<<ROTARY_CH1)){
  25.         rot_data |= 0x01;
  26.     }
  27.     if(ROTARY_CH2_PIN&(1<<ROTARY_CH2)){
  28.         rot_data |= 0x02;
  29.     }
  30.  
  31.     if( rot_data==0 || rot_data==3 ){ // Are both signals high or low?
  32.         if( rot_data==0 && rot_laststate!=rot_data ){ // Are both signals low? In that case we are finished with one turn and should print out the direction it went.
  33.             if( rot_lastdir&0x01 ){
  34.             #ifndef ROTARY_CHx_INVERT_DIRECTION
  35.                 rotaryEncoder_Position--;   // Moving clockwise
  36.             #else
  37.                 rotaryEncoder_Position++;   // Moving counter clockwise
  38.             #endif
  39.             }else{
  40.             #ifndef ROTARY_CHx_INVERT_DIRECTION
  41.                 rotaryEncoder_Position++;   // Moving counter clockwise
  42.             #else
  43.                 rotaryEncoder_Position--;   // Moving clockwise
  44.             #endif
  45.             }
  46.         }
  47.         rot_laststate = rot_data;
  48.     } else { // No, only one of the signals are high. We can use this to find out what direction we are moving.
  49.         rot_lastdir = rot_data;
  50.     }
  51. } /* SIGNAL(ROTARY_CH1_SIGNAL) */
  52.  
  53.  
  54. /*********************************************************************//**
  55. Function: SIGNAL(ROTARY_CH2_SIGNAL)
  56. Purpose:  Executed when pin change on ROTARY_CH1 is seen.
  57.           This executes SIGNAL(ROTARY_BTN_SIGNAL) interupt code
  58. Input:    -
  59. Returns:  -
  60. **************************************************************************/
  61. #if (ROTARY_CH1_PCINT_vector != ROTARY_BTN_PCINT_vector)
  62. ISR(ROTARY_CH1_PCINT_vector, ISR_ALIASOF(ROTARY_BTN_PCINT_vector));
  63. #endif
  64. /*********************************************************************//**
  65. Function: SIGNAL(ROTARY_CH2_SIGNAL)
  66. Purpose:  Executed when pin change on ROTARY_CH2 is seen.
  67.           This executes SIGNAL(ROTARY_BTN_SIGNAL) interupt code
  68. Input:    -
  69. Returns:  -
  70. **************************************************************************/
  71. #if (ROTARY_CH2_PCINT_vector != ROTARY_BTN_PCINT_vector && ROTARY_CH2_PCINT_vector != ROTARY_CH1_PCINT_vector)
  72. ISR(ROTARY_CH2_PCINT_vector, ISR_ALIASOF(ROTARY_BTN_PCINT_vector));
  73. #endif
  74.  
  75. void sns_rotary_Init(void)
  76. {
  77.     /*
  78.      * Initialize rotaryencoders and buttons
  79.      */
  80.     rotaryEncoder_Position = 0; // Set initial value to 0
  81.     rotaryEncoder_Position_old = 0; // Set initial value to 0
  82.  
  83.     ROTARY_CH1_DDR &= ~(1<<ROTARY_CH1); // set as input
  84.     ROTARY_CH2_DDR &= ~(1<<ROTARY_CH2); // set as input
  85.     ROTARY_BTN_DDR &= ~(1<<ROTARY_BTN); // set as input
  86.     ROTARY_CH1_PORT |= (1<<ROTARY_CH1); // Enable pull-up
  87.     ROTARY_CH2_PORT |= (1<<ROTARY_CH2); // Enable pull-up
  88.     ROTARY_BTN_PORT |= (1<<ROTARY_BTN); // Enable pull-up
  89.  
  90.     // Enable IO-pin interrupt
  91.     PCICR |= (1<<ROTARY_CH1_PCIE);
  92.     ROTARY_CH1_PCMSK |= (1<<ROTARY_CH1_PCINT);
  93.     PCICR |= (1<<ROTARY_CH2_PCIE);
  94.     ROTARY_CH2_PCMSK |= (1<<ROTARY_CH2_PCINT);
  95.     PCICR |= (1<<ROTARY_BTN_PCIE);
  96.     ROTARY_BTN_PCMSK |= (1<<ROTARY_BTN_PCINT);
  97. }
  98.  
  99. void sns_rotary_Process(void)
  100. {
  101.     if (rotaryEncoder_Position != rotaryEncoder_Position_old)
  102.     {
  103.         StdCan_Msg_t txMsg;
  104.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  105.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  106.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY;
  107.         txMsg.Header.ModuleId = sns_rotary_ID;
  108.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_ROTARY_SWITCH;
  109.         txMsg.Length = 4;
  110.         txMsg.Data[0] = 0x01;
  111.         if ((rotaryEncoder_Position > rotaryEncoder_Position_old || (rotaryEncoder_Position_old==0xff && rotaryEncoder_Position==0x00)) && !(rotaryEncoder_Position_old==0x00 && rotaryEncoder_Position==0xff))
  112.             txMsg.Data[1] = 0x00;   //Clockwice
  113.         else
  114.             txMsg.Data[1] = 0x01;   //Counter Clockwice
  115.         txMsg.Data[2] = 0x01;
  116.         txMsg.Data[3] = rotaryEncoder_Position;
  117.  
  118.         StdCan_Put(&txMsg);
  119.         rotaryEncoder_Position_old = rotaryEncoder_Position;
  120.     }
  121.     if (rotaryEncoder_Button_Position != rotaryEncoder_Button_Position_old)
  122.     {
  123.         rotaryEncoder_Button_Position_old = rotaryEncoder_Button_Position;
  124.         StdCan_Msg_t txMsg;
  125.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  126.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  127.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY;
  128.         txMsg.Header.ModuleId = sns_rotary_ID;
  129.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_BUTTON;
  130.         txMsg.Length = 2;
  131.         txMsg.Data[0] = 0x01;
  132. #ifndef ROTARY_BTN_INVERT_OUTPUT
  133.         if (rotaryEncoder_Button_Position_old)
  134.             txMsg.Data[1] = 0x00;
  135.         else
  136.             txMsg.Data[1] = 0x01;
  137.         StdCan_Put(&txMsg);
  138. #else
  139.         if (rotaryEncoder_Button_Position_old)
  140.             txMsg.Data[1] = 0x01;
  141.         else
  142.             txMsg.Data[1] = 0x00;
  143.         StdCan_Put(&txMsg);
  144. #endif
  145.     }
  146.  
  147. }
  148.  
  149. void sns_rotary_HandleMessage(StdCan_Msg_t *rxMsg)
  150. {
  151.  
  152. }
  153.  
  154. void sns_rotary_List(uint8_t ModuleSequenceNumber)
  155. {
  156.     StdCan_Msg_t txMsg;
  157.  
  158.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  159.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  160.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY; ///TODO: Change this to the actual module type
  161.     txMsg.Header.ModuleId = sns_rotary_ID;
  162.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  163.     txMsg.Length = 6;
  164.  
  165.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  166.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  167.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  168.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  169.  
  170.     txMsg.Data[4] = NUMBER_OF_MODULES;
  171.     txMsg.Data[5] = ModuleSequenceNumber;
  172.  
  173.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  174. }
  175.