Subversion Repositories HomeAutomation

Rev

Rev 1168 | Rev 1174 | 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. 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] = 0x00;   //Clockwice
  94.             txMsg.Data[2] = rotaryEncoder_Position-rotaryEncoder_Position_old;
  95.         } else {
  96.             txMsg.Data[1] = 0x01;   //Counter Clockwice
  97.             txMsg.Data[2] = rotaryEncoder_Position_old-rotaryEncoder_Position;
  98.         }
  99.         txMsg.Data[3] = rotaryEncoder_Position;
  100.  
  101.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  102.         rotaryEncoder_Position_old = rotaryEncoder_Position;
  103.     }
  104.     if (rotaryEncoder_Button_Position != rotaryEncoder_Button_Position_old)
  105.     {
  106.         rotaryEncoder_Button_Position_old = rotaryEncoder_Button_Position;
  107.         StdCan_Msg_t txMsg;
  108.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  109.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  110.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY;
  111.         txMsg.Header.ModuleId = sns_rotary_ID;
  112.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_BUTTON;
  113.         txMsg.Length = 2;
  114.         txMsg.Data[0] = 0x01;
  115. #if ROTARY_BTN_INVERT_OUTPUT==1
  116.         if (rotaryEncoder_Button_Position_old)
  117.             txMsg.Data[1] = 0x00;
  118.         else
  119.             txMsg.Data[1] = 0x01;
  120.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  121. #else
  122.         if (rotaryEncoder_Button_Position_old)
  123.             txMsg.Data[1] = 0x01;
  124.         else
  125.             txMsg.Data[1] = 0x00;
  126.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  127. #endif
  128.     }
  129.  
  130. }
  131.  
  132. void sns_rotary_HandleMessage(StdCan_Msg_t *rxMsg)
  133. {
  134.  
  135. }
  136.  
  137. void sns_rotary_List(uint8_t ModuleSequenceNumber)
  138. {
  139.     StdCan_Msg_t txMsg;
  140.  
  141.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  142.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  143.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_ROTARY; ///TODO: Change this to the actual module type
  144.     txMsg.Header.ModuleId = sns_rotary_ID;
  145.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  146.     txMsg.Length = 6;
  147.  
  148.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  149.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  150.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  151.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  152.  
  153.     txMsg.Data[4] = NUMBER_OF_MODULES;
  154.     txMsg.Data[5] = ModuleSequenceNumber;
  155.  
  156.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  157. }
  158.