driver for incremental encoder

decoder.cpp

Committer:
ianaherne
Date:
2019-01-08
Revision:
0:c3a05fe15b84
Child:
1:017efde7ab29

File content as of revision 0:c3a05fe15b84:

#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++;}
        if(chB == 0){_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;
    
}