Jump to content
43oh

Writing a c++ library - what's wrong with my code


Recommended Posts

Hi everybody,

 

I never learned writing C++ (I learned java some time ago, so I have an idea of what classes, objects and methods are), I learned the very basics of programming uC in classic C at university. But since I'm using Energia and all its C++ libraries, I like using them, so I'd also like to be able writing some.

 

I've been playing around with an encoder in my project and used the code posted here on the board. That works fine.

 

Now I thought, writing my own library with methods Encoder.init, Encoder.readPosition and Encoder.readDirection would be a cool thing. I read an tutorial on how to write C++ libraries for Arduino and looked at some existing libraries I use and then started to code.

 

Now my problem seems to be, that the compiler doesn't find functions and definitions from other files included in quei.c for example. Although I know that I won't need every Item of the folders, I've copied the complete driverlib and inc folder that I downloaded from TI in my library's folder, so my folder structure looks like that

 

TivaC-Encoder

  driverlib

   quei.c

   quei.h

   (and a lot of other .h and .c files here)

  inc

   (even more .h and .c files here)

  TivaC-Encoder.h

  TivaC-Encoder.cpp

  keywords.txt

 

I started including all header files included in quei.c in TivaC-Encoder.cpp, that made some compiler errors disappear but there are still a lot like "error: 'GPIOPinConfigure' was not declared in this scope GPIOPinConfigure(0x000A0406); //GPIO_PL1_PHA0"

 

So my main question is: What am I doing wrong, so that the compiler doesn't find the right header files?

 

This is my code for TivaC-Encoder.h:

/
//  TivaC-Encoder.h
//  
//
//  Created by Janos on 12.08.15.
//
//

#ifndef ____TivaC_Encoder__
#define ____TivaC_Encoder__


#include "TivaC-Encoder.h"
#include <stdint.h>

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_qei.h"
#include "inc/hw_types.h"
#include "inc/hw_sysctl.h"
#include "driverlib/debug.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/qei.h"
#include "driverlib/pin_map.h"

class EncoderClass
{
    
private:
    
    int32_t _lastValue;
public:
    
    EncoderClass(void);
    uint32_t readPosition(void);
    int8_t readDirection(void);
    void init(void);
    
};

extern EncoderClass Encoder;


#endif /* defined(____TivaC_Encoder__) */
 

And this is my code for TivaC-Encoder.cpp:

//
//  TivaC-Encoder.cpp
//  
//
//  Created by Janos on 12.08.15.
//
//

#include "TivaC-Encoder.h"
#include <stdint.h>

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_qei.h"
#include "inc/hw_types.h"
#include "inc/hw_sysctl.h"
#include "driverlib/debug.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/qei.h"
#include "driverlib/pin_map.h"

EncoderClass::EncoderClass(void) {
}

uint32_t EncoderClass::readPosition(void){
    
    return QEIPositionGet(QEI0_BASE);
}

int8_t EncoderClass::readDirection(void){
    uint32_t dir = QEIPositionGet(QEI0_BASE)-_lastValue;
    
    if ((dir)>0) {
        return 1;
    }
    else if ((dir)<0){
        return -1;
    }
    else {
        return 0;
    }
    
    _lastValue=QEIPositionGet(QEI0_BASE);
    
}


void EncoderClass::init(void) {
    // Setup QEI Module
    SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI0);
    GPIOPinConfigure(0x000A0406); //GPIO_PL1_PHA0
    GPIOPinConfigure(0x000A0806); //GPIO_PL2_PHB0
    GPIOPinTypeQEI(GPIO_PORTL_BASE, GPIO_PIN_1 |  GPIO_PIN_2);
    
    // Configure and Enable QEI
    QEIConfigure(QEI0_BASE, (QEI_CONFIG_CAPTURE_A_B | QEI_CONFIG_NO_RESET | QEI_CONFIG_QUADRATURE | QEI_CONFIG_NO_SWAP), 20000);
    QEIVelocityConfigure(QEI0_BASE, QEI_VELDIV_1, SysCtlClockGet()); // Divide by clock speed to get counts/sec
    QEIEnable(QEI0_BASE);
    QEIVelocityEnable(QEI0_BASE);
    
    _lastValue=QEIPositionGet(QEI0_BASE);
}

EncoderClass Encoder;

Link to post
Share on other sites

Okay. Got the answer by myself. All I had to do was include <Energia.h>.

 

:D

 

And I noticed, that driverlib and inc are already inside Energia, so I deleted them from my library folder. Finally it looks like this:

#include "TivaC-Encoder.h"
#include <Energia.h>
#include <driverlib/sysctl.h>
#include <driverlib/qei.h>

If this library does a good job, I'll post it here.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...