driver for incremental encoder

decoder.cpp

Committer:
ianaherne
Date:
2019-03-12
Revision:
4:9913990cfd78
Parent:
2:b82d31306c4b
Child:
5:1a5772466668

File content as of revision 4:9913990cfd78:

#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(){
    
    
    uint8_t chA = _channelA.read();
    
    uint8_t chB = _channelB.read();
    
    if(chA == 1){
        
        if(chB == 1){
        
           if( _count != UPPER_LIMIT){
              _count++;               
            }
            _direction = 1;
                      
        }
        
        
        
        if(chB == 0){
            
            if(_count != LOWER_LIMIT){
              _count--;  
            }
            _direction = 0;
            
        } 
        
    }   
            
}

/**
* @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; 
       
}