Library to allo USB PTP device to be hosted by the mbed platform
Dependents: class_project_main
Diff: PIMA15740/PIMAArray.h
- Revision:
- 10:fc1cb68fc91e
- Parent:
- 4:9c6f5867f050
- Child:
- 11:3b072cf16df8
diff -r 961c3357504d -r fc1cb68fc91e PIMA15740/PIMAArray.h --- a/PIMA15740/PIMAArray.h Tue Sep 10 07:40:44 2013 +0000 +++ b/PIMA15740/PIMAArray.h Wed Sep 18 01:48:07 2013 +0000 @@ -2,7 +2,7 @@ * @file PIMAArray.h * @brief PIMA Array class definition * @author Dwayne Dilbeck -* @date 8/23/2013 +* @date 9/16/2013 * * mbed USBHostPTP Library(PIMA15740 Array definition) * @@ -18,6 +18,7 @@ * The PIMA15740 standard defines an array as an unsigned 32bit number of elements followed by a list * of insinged 16bit codes; */ +template <class TYPE> class PIMAArray { public: /** @@ -28,7 +29,7 @@ */ PIMAArray() { numberOfElements=0; - codes=NULL; + elements=NULL; }; /** @@ -38,8 +39,8 @@ * @param None */ ~PIMAArray() { - if( codes !=NULL) - free(codes); + if( elements !=NULL) + free(elements); }; /** @@ -49,17 +50,31 @@ * * @return The number of bytes used from the buffer to create the PIMA array. */ - int FillArray(uint8_t *currentPtr) { + + int FillArray(uint8_t *currentPtr) { SetNumberOfElements(*((uint32_t *)currentPtr)); - SetCodes((uint16_t *)(currentPtr+4)); - return (2*numberOfElements) + 4; -} + SetElements((TYPE *)(currentPtr+sizeof(uint32_t))); + return (sizeof(TYPE)*numberOfElements) + sizeof(uint32_t); + } + + TYPE GetElement(uint32_t index) { + return elements[index]; + } + + bool CheckValueInArray(TYPE value) { + bool res=false; + for(int i=0; i<numberOfElements;i++) + if(value == elements[i]) + res=true; + return res; + } + ///Number of elelments stored in the array uint32_t numberOfElements; - ///Pointer to Code storage - uint16_t *codes; - + ///Pointer to elements storage + TYPE *elements; + private: /** * Function to allocate array storage space and set the number of elements @@ -69,21 +84,19 @@ */ void SetNumberOfElements(uint8_t length) { this->numberOfElements=length; - if( codes !=NULL) - free(codes); - codes = (uint16_t *) malloc(sizeof(uint16_t)*length); + if( elements !=NULL) + free(elements); + elements = (TYPE *) malloc(sizeof(TYPE)*length); }; /** * Function to read codes form a uint8t buffer and store them. * @param buffer pointer to a uint8_t buffer */ - void SetCodes(uint16_t *buffer){ - if(buffer!=NULL && codes !=NULL) + void SetElements(uint16_t *buffer){ + if(buffer!=NULL && elements !=NULL) for(int i=0;i<this->numberOfElements;i++) - codes[i]=buffer[i]; + elements[i]=buffer[i]; }; }; - -