driver for incremental encoder

decoder.cpp

Committer:
ianaherne
Date:
2019-01-09
Revision:
2:b82d31306c4b
Parent:
1:017efde7ab29
Child:
4:9913990cfd78

File content as of revision 2:b82d31306c4b:

#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);   
    _oldCount = 0;
    _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 && _count != UPPER_LIMIT){_count++;}
        if(chB == 0 && _count != LOWER_LIMIT){_count--;}   
        
    }
    
    if(_oldCount < _count){_direction = 1;}
    if(_oldCount > _count){_direction = -1;}
    if(_oldCount == _count){_direction = 0;}
    
    _oldCount = _count;
            
}

/**
* @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(){
    
    _oldCount = 0;
    _count = 0;
    _direction = 0; 
       
}