c++ - Arduino Library Error, Class name hasn't been defined -
i'm trying run circular buffer class on arduino. wrote class on c++ ide, , compiled , ran fine. copy/pasted .cpp , .h files arduino project folder, next .ino file. when tried run, got (http://i.imgur.com/yhrh7ml.png) error.
the .h file:
#include <stdint.h> #include <arduino.h> #include "circbuff.cpp" #ifndef circbuff_h #define circbuff_h #define buff_length 128 /** * circular buffer class. * has preset maximum size of buff_length bytes * uses byte array of size buff_length store data. * has start flag , end flag show start , end of our virtual buffer inside of main array. * whenever write byte buffer, adds byte index of "endflag", , moves end flag along * when call readbyte(), removes first byte in our buffer (at index of "startflag"), moves start flag */ class circbuff { public: circbuff(); //constructs new circular buffer object uint16_t getlength(); //returns number of bytes inside of our circular buffer //functions read , write single bytes void writebyte(uint8_t newbyte); //writes single byte end of array (to index of end flag, moves end flag forward) uint8_t seekbyte(); //returns first byte of buffer (at index of start flag) uint8_t seekbyte(uint16_t offset); //returns byte @ given index of buffer (at index of start flag + offset) uint8_t readbyte(); //returns first byte of buffer , clears byte buffer (increments start flag) //functions read , write multiple bytes void writebytes(uint8_t* newbuff, uint16_t count); //writes given array end of buffer, count being length of given array void seekbytes(uint8_t* newbuff, uint16_t count); //reads first bytes (as many count) of buffer given array. "count" needs equal length of array given. function reads bytes , writes them given array void readbytes(uint8_t* newbuff, uint16_t count); //reads first "count" bytes of buffer , writes them given array. clears bytes read array (by moving start flag). //these should used debugging uint8_t readfromarray(uint16_t index);//returns byte index "index" of array "buff" uint16_t getstartflag(); //returns index of start flag. uint16_t getendflag(); //returns index of end flag. private: //when length of array zero, start flag , end flag @ same position. //there no data @ index of end flag. //the first byte @ index "start". last byte before index "end". uint16_t startf;//the start flag. keeps track of start of imaginary array inside "buff" uint16_t endf;//the end flag. keeps track of end of imaginary array inside "buff" uint8_t buff[buff_length]; //the array contains of our data }; #endif
the .cpp file:
#include <circbuff.h> #define buff_length 128 //constructor circbuff::circbuff(){} //destructor //circbuff::~circbuff(){} //returns number of bytes inside of our circular buffer uint16_t circbuff::getlength() { if(startf <= endf) return (endf-startf); //if start flag comes before end flag, length difference between end , start else return ( ( endf + buff_length ) - startf); //else start flag comes after end flag, means data rolls over, calculate accordingly } //writes single byte end of array void circbuff::writebyte(uint8_t newbyte) { buff[endf] = newbyte; //add new byte end endf = ( (endf + 1) % buff_length ); //increment end flag } //returns first byte of buffer uint8_t circbuff::seekbyte() { if(getlength() > 0) return buff[startf]; //the if makes sure have data in buffer else return 0; } //returns byte @ given index of buffer uint8_t circbuff::seekbyte(uint16_t offset) { if(offset < getlength()) return buff[ (startf + offset)%buff_length ]; //if we're not trying byte outside of our buffer, return byte @ buffer index "offset" else return 0; //if we're trying byte outside our buffer, return 0 } //returns first byte of buffer , clears byte buffer uint8_t circbuff::readbyte() { if(getlength() > 0) //as long there 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, return 0 } //writes given array end of buffer, count being length of given array //the "count" needs equal length of array given. //has no protection against being given wrong count or size of newbuff void circbuff::writebytes(uint8_t* newbuff, uint16_t count) { for(int = 0; < count; i++)//for every byte have in new array { buff[endf] = newbuff[i]; //add new byte end endf = (endf+1) % buff_length; //increment end flag } } //reads first bytes (as many count) of buffer given array. //the "count" needs equal length of array given. //the function reads bytes , writes them given array //has no protection against accidentally reading past endf flag //has no protection against being given wrong count or size of newbuff void circbuff::seekbytes(uint8_t* newbuff, uint16_t count) { for(int = 0; < count; i++) //for every item in our out array { newbuff[i] = buff[(startf + i) % buff_length]; //set byte @ index "i" of our out array byte @ "i" after start flag in our array } } //reads first "count" bytes of buffer , writes them given array. //clears bytes read array (by moving start flag). //has no protection against accidentally reading past endf flag void circbuff::readbytes(uint8_t* newbuff, uint16_t count) { for(int = 0; < count; i++) { newbuff[i] = buff[startf];//set byte @ index "i" of out array byte @ start of our array startf = (startf+1) % buff_length;//increment end flag } } //debug methods uint8_t circbuff::readfromarray(uint16_t index) //returns byte index "index" array "buff" { return buff[index]; } //returns index of start flag uint16_t circbuff::getstartflag() { return startf; } //returns index of end flag uint16_t circbuff::getendflag() { return endf; }
the .ino file:
#include "circbuff.h" void setup() { serial.println("setup."); } void loop() { serial.println("looping."); delay(1000); }
#include <stdint.h> #include <arduino.h> #include "circbuff.cpp" <<<<<<<<<<<<<<<<<<<<<<<<<<<<< problem
include .h
file in .cpp` file, not other way around.
also, include in .cpp
file should use quotes , not brackets.
Comments
Post a Comment