2014-12-05 101 views
0

我試圖在arduino上運行一個循環緩衝區類。我在另一個C++ IDE上編寫了這個類,它編譯並運行得很好。我將.cpp和.h文件複製/粘貼到.ino文件旁邊的arduino項目文件夾中。當我試圖運行時,我得到了這個錯誤(http://i.imgur.com/Yhrh7ML.png)。Arduino庫錯誤,類名尚未定義

.h文件:

#include <stdint.h> 
#include <Arduino.h> 
#include "CircBuff.cpp" 

#ifndef CircBuff_h 
#define CircBuff_h 

#define BUFF_LENGTH 128 

/** 
* A circular buffer class. 
* Has a preset maximum size of BUFF_LENGTH bytes 
* Uses a byte array of size BUFF_LENGTH to store the data. 
* Has a start flag and an end flag to show where the start and end of our virtual buffer inside of the main array. 
* Whenever we write a byte to the buffer, it adds the byte to the index of "endFlag", and then moves the end flag along 
* When we call readByte(), it removes the first byte in our buffer (at the index of "startFlag"), then moves the start flag 
*/ 
class CircBuff 
{ 
    public: 
    CircBuff(); //constructs a new circular buffer object 

    uint16_t getLength(); //returns the number of bytes inside of our circular buffer 

    //functions that read and write single bytes 
    void writeByte(uint8_t newByte); //writes a single byte to the end of the array (to the index of the end flag, then moves the end flag forward) 
    uint8_t seekByte(); //returns the first byte of the buffer (at the index of the start flag) 
    uint8_t seekByte(uint16_t offset); //returns the byte at the given index of the buffer (at the index of the start flag + the offset) 
    uint8_t readByte(); //returns the first byte of the buffer and clears the that byte from the buffer (increments the start flag) 

    //functions that read and write multiple bytes 
    void writeBytes(uint8_t* newBuff, uint16_t count); //writes the given array to the end of the buffer, the count being the length of the given array 
    void seekBytes(uint8_t* newBuff, uint16_t count); //reads the first bytes (as many as the count) of the buffer to the given array. The "count" needs to be equal to the length of the array given. The function reads bytes and writes them to the given array 
    void readBytes(uint8_t* newBuff, uint16_t count); //reads the first "count" bytes of the buffer and writes them to the given array. Then it clears the bytes read from the array (by moving start flag). 

    //these should only be used for debugging 
    uint8_t readFromArray(uint16_t index);//returns the byte from index "index" of the array "buff" 
    uint16_t getStartFlag(); //returns the index of the start flag. 
    uint16_t getEndFlag(); //returns the index of the end flag. 

    private: 
    //when the length of the array is zero, the start flag and end flag are at the same position. 
    //there is no data at the index of the end flag. 
    //The first Byte is at the index "start". The last byte is before the index "end". 
    uint16_t startF;//the start flag. keeps track of the start of the imaginary array inside "buff" 
    uint16_t endF;//the end flag. keeps track of the end of the imaginary array inside "buff" 
    uint8_t buff[BUFF_LENGTH]; //the array that contains all of our data 
}; 

#endif 

.cpp文件:

#include <CircBuff.h> 

#define BUFF_LENGTH 128 

//constructor 
CircBuff::CircBuff(){} 

//destructor 
//CircBuff::~CircBuff(){} 

//returns the number of bytes inside of our circular buffer 
uint16_t CircBuff::getLength() 
{ 
    if(startF <= endF) return (endF-startF); //if the start flag comes before the end flag, length is the difference between the end and start 
    else return ((endF + BUFF_LENGTH) - startF); //else the start flag comes after the end flag, that means the data rolls over, thus we calculate accordingly 
} 

//writes a single byte to the end of the array 
void CircBuff::writeByte(uint8_t newByte) 
{ 
    buff[endF] = newByte; //add the new byte to the end 
    endF = ((endF + 1) % BUFF_LENGTH); //increment the end flag 
} 

//returns the first byte of the buffer 
uint8_t CircBuff::seekByte() 
{ 
    if(getLength() > 0) return buff[startF]; //the if makes sure we have data in the buffer 
    else return 0; 
} 

//returns the byte at the given index of the buffer 
uint8_t CircBuff::seekByte(uint16_t offset) 
{ 
    if(offset < getLength()) return buff[ (startF + offset)%BUFF_LENGTH ]; //if we're not trying to get a byte outside of our buffer, return the byte at buffer index "offset" 
    else return 0; //if we're trying to get a byte outside our buffer, return 0 
} 

//returns the first byte of the buffer and clears the that byte from the buffer 
uint8_t CircBuff::readByte() 
{ 
    if(getLength() > 0) //as long as there is data in our buffer 
    { 
    uint16_t tmp = startF; 
    startF = (startF+1) % BUFF_LENGTH; 
    return buff[tmp]; 
    } 
    else return 0; //if there isn't data in our buffer, just return 0 
} 

//writes the given array to the end of the buffer, the count being the length of the given array 
//The "count" needs to be equal to the length of the array given. 
//has no protection against being given the wrong count or size of newBuff 
void CircBuff::writeBytes(uint8_t* newBuff, uint16_t count) 
{ 
    for(int i = 0; i < count; i++)//for every byte we have in the new array 
    { 
    buff[endF] = newBuff[i]; //add the new byte to the end 
    endF = (endF+1) % BUFF_LENGTH; //increment the end flag 
    } 
} 

//reads the first bytes (as many as the count) of the buffer to the given array. 
//The "count" needs to be equal to the length of the array given. 
//The function reads bytes and writes them to the given array 
//has no protection against accidentally reading past the endF flag 
//has no protection against being given the wrong count or size of newBuff 
void CircBuff::seekBytes(uint8_t* newBuff, uint16_t count) 
{ 
    for(int i = 0; i < count; i++) //for every item in our out array 
    { 
    newBuff[i] = buff[(startF + i) % BUFF_LENGTH]; //set the byte at index "i" of our out array to the byte at "i" after the start flag in our array 
    } 
} 

//reads the first "count" bytes of the buffer and writes them to the given array. 
//clears the bytes read from the array (by moving start flag). 
//has no protection against accidentally reading past the endF flag 
void CircBuff::readBytes(uint8_t* newBuff, uint16_t count) 
{ 
    for(int i = 0; i < count; i++) 
    { 
    newBuff[i] = buff[startF];//set the byte at index "i" of the out array to the byte at the start of our array 
    startF = (startF+1) % BUFF_LENGTH;//increment the end flag 
    } 
} 

//debug methods 
uint8_t CircBuff::readFromArray(uint16_t index) //returns the byte from index "index" from array "buff" 
{ 
    return buff[index]; 
} 

//returns the index of the start flag 
uint16_t CircBuff::getStartFlag() 
{ 
    return startF; 
} 
//returns the index of the end flag 
uint16_t CircBuff::getEndFlag() 
{ 
    return endF; 
} 

的.ino文件:

#include "CircBuff.h" 

void setup() 
{ 
    Serial.println("Setup."); 
} 

void loop() 
{ 
    Serial.println("Looping."); 
    delay(1000); 
} 

回答

0
#include <stdint.h> 
#include <Arduino.h> 
#include "CircBuff.cpp" <<<<<<<<<<<<<<<<<<<<<<<<<<<<< PROBLEM 

附上.h文件中。 cpp`文件,但不是相反。

此外,包含在.cpp文件中的文件應該使用引號而不是括號。

+0

謝謝。我完全忽略了這一點。我不知道在IDE之間它是如何改變的。 – 2014-12-05 22:22:39