driver for incremental encoder

decoder.cpp

Committer:
ianaherne
Date:
2019-03-14
Revision:
5:1a5772466668
Parent:
4:9913990cfd78
Child:
6:2fdce74d2a7b

File content as of revision 5:1a5772466668:

#include "decoder.h"



/**
* @brief    constructor
* @details  initializes interrupt on channel A, and initialises internal variables
* @param    NA
* @return   NA
* @warning  interrupt functionallity on channel B is not used as it is not required.
* 
*/

decoder :: decoder(PinName channelA, PinName channelB):_channelA(channelA),_channelB(channelB) {
    
    _channelA.rise(this, &decoder::isr);   
   
    _count = 0;
    _direction = 0;   
    
}

/**
* @brief    isr
* @details  interrupt servive routine to decode the encoder output
* @param    NA
* @return   NA
* @warning  
* 
*/



void decoder :: isr(){
    //confirmed as x1 decoding y analysing waveform
    
    uint8_t chA = _channelA.read();
    
    uint8_t chB = _channelB.read();
    
    if(chA == 1){
        
        if(chB == 1){
        
           if( _count != UPPER_LIMIT){
              _count++;               
            }
            _direction = 1;
            
            //pc.printf(" 1  %d\n\n",_count);//debug
                      
        }
        
        
        
        if(chB == 0){
            
            if(_count != LOWER_LIMIT){
              _count--;  
            }
            _direction = 0;
            
            //pc.printf(" 0  %d\n\n",_count);//debug
            
        } 
        
    }   
            
}

/**
* @brief    getDirection()
* @details  function to return the class private variable _direction
* @param    NA
* @return   the direction of encoder
* @warning  
* 
*/

int8_t decoder :: getDirection(){
    
    return _direction;
    
    
    
}

/**
* @brief    getCount
* @details  function to return the class private variable _count
* @param    NA
* @return   the number of turns of encoder +/-
* @warning  
* 
*/

int16_t decoder :: getCount(){
    
    
    return _count;
    
}

/**
* @brief    reset()
* @details  function to reset all internal counting and directional variables
* @param    NA
* @return   
* @warning  
* 
*/

void decoder :: reset(){
    
    
    _count = 0;
    _direction = 0; 
       
}