Subversion Repositories HomeAutomation

Rev

Rev 967 | Rev 1173 | 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. void sns_rotary_pcint_callback(uint8_t id, uint8_t status)
  10. {
  11.     uint8_t rot_data = 0;
  12.     static uint8_t rot_lastdir = 0, rot_laststate = 0;
  13.  
  14.     //Take care of button push
  15.     if (gpio_get_state(ROTARY_BTN) != rotaryEncoder_Button_Position){ //The buttonstate has changed!
  16.         rotaryEncoder_Button_Position = gpio_get_state(ROTARY_BTN);
  17.     }
  18.     //Take care of rotary encoder movement
  19.     if(gpio_get_state(ROTARY_CH1)){
  20.         rot_data |= 0x01;
  21.     }
  22.     if(gpio_get_state(ROTARY_CH2)){
  23.         rot_data |= 0x02;
  24.     }
  25.  
  26.     if( rot_data==0 || rot_data==3 ){ // Are both signals high or low?
  27.         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.
  28.             if( rot_lastdir&0x01 ){
  29.             #ifndef ROTARY_CHx_INVERT_DIRECTION
  30.                 rotaryEncoder_Position--;   // Moving clockwise
  31.             #else
  32.                 rotaryEncoder_Position++;   // Moving counter clockwise
  33.             #endif
  34.             }else{
  35.             #ifndef ROTARY_CHx_INVERT_DIRECTION
  36.                 rotaryEncoder_Position++;   // Moving counter clockwise
  37.             #else
  38.                 rotaryEncoder_Position--;   // Moving clockwise
  39.             #endif
  40.             }
  41.         }
  42.         rot_laststate = rot_data;
  43.     } else { // No, only one of the signals are high. We can use this to find out what direction we are moving.
  44.         rot_lastdir = rot_data;
  45.     }
  46. }
  47.  
  48.  
  49.  
  50. void sns_rotary_Init(void)
  51. {
  52.     /*
  53.      * Initialize rotaryencoders and buttons
  54.      */
  55.     rotaryEncoder_Position = 0; // Set initial value to 0
  56.     rotaryEncoder_Position_old = 0; // Set initial value to 0
  57.  
  58.     gpio_set_in(ROTARY_CH1);    // Set to input
  59.     gpio_set_pin(ROTARY_CH1);   // Enable pull-up
  60.     gpio_set_in(ROTARY_CH2);    // Set to input
  61.     gpio_set_pin(ROTARY_CH2);   // Enable pull-up
  62.     gpio_set_in(ROTARY_BTN);    // Set to input
  63.     gpio_set_pin(ROTARY_BTN);   // Enable pull-up
  64.  
  65.     // Enable IO-pin interrupt
  66.     Pcint_SetCallbackPin(sns_rotary_PCINT_CH1, ROTARY_CH1, &sns_rotary_pcint_callback);
  67.     Pcint_SetCallbackPin(sns_rotary_PCINT_CH2, ROTARY_CH2, &sns_rotary_pcint_callback);
  68.     Pcint_SetCallbackPin(sns_rotary_PCINT_BTN, ROTARY_BTN, &sns_rotary_pcint_callback);
  69. }
  70.  
  71. void sns_rotary_Process(void)
  72. {
  73.     if (rotaryEncoder_Position != rotaryEncoder_Position_old)
  74.     {
  75.         StdCan_Msg_t txMsg;
  76.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  77.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  78.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY;
  79.         txMsg.Header.ModuleId = sns_rotary_ID;
  80.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_ROTARY_SWITCH;
  81.         txMsg.Length = 4;
  82.         txMsg.Data[0] = 0x01;
  83.         if ((rotaryEncoder_Position > rotaryEncoder_Position_old || (rotaryEncoder_Position_old==0xff && rotaryEncoder_Position==0x00)) && !(rotaryEncoder_Position_old==0x00 && rotaryEncoder_Position==0xff))
  84.             txMsg.Data[1] = 0x00;   //Clockwice
  85.         else
  86.             txMsg.Data[1] = 0x01;   //Counter Clockwice
  87.         txMsg.Data[2] = 0x01;
  88.         txMsg.Data[3] = rotaryEncoder_Position;
  89.  
  90.         StdCan_Put(&txMsg);
  91.         rotaryEncoder_Position_old = rotaryEncoder_Position;
  92.     }
  93.     if (rotaryEncoder_Button_Position != rotaryEncoder_Button_Position_old)
  94.     {
  95.         rotaryEncoder_Button_Position_old = rotaryEncoder_Button_Position;
  96.         StdCan_Msg_t txMsg;
  97.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  98.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  99.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY;
  100.         txMsg.Header.ModuleId = sns_rotary_ID;
  101.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_BUTTON;
  102.         txMsg.Length = 2;
  103.         txMsg.Data[0] = 0x01;
  104. #ifndef ROTARY_BTN_INVERT_OUTPUT
  105.         if (rotaryEncoder_Button_Position_old)
  106.             txMsg.Data[1] = 0x00;
  107.         else
  108.             txMsg.Data[1] = 0x01;
  109.         StdCan_Put(&txMsg);
  110. #else
  111.         if (rotaryEncoder_Button_Position_old)
  112.             txMsg.Data[1] = 0x01;
  113.         else
  114.             txMsg.Data[1] = 0x00;
  115.         StdCan_Put(&txMsg);
  116. #endif
  117.     }
  118.  
  119. }
  120.  
  121. void sns_rotary_HandleMessage(StdCan_Msg_t *rxMsg)
  122. {
  123.  
  124. }
  125.  
  126. void sns_rotary_List(uint8_t ModuleSequenceNumber)
  127. {
  128.     StdCan_Msg_t txMsg;
  129.  
  130.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  131.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  132.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY; ///TODO: Change this to the actual module type
  133.     txMsg.Header.ModuleId = sns_rotary_ID;
  134.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  135.     txMsg.Length = 6;
  136.  
  137.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  138.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  139.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  140.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  141.  
  142.     txMsg.Data[4] = NUMBER_OF_MODULES;
  143.     txMsg.Data[5] = ModuleSequenceNumber;
  144.  
  145.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  146. }
  147.