Subversion Repositories HomeAutomation

Rev

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