Library to allo USB PTP device to be hosted by the mbed platform

Dependents:   class_project_main

PIMA15740/PIMAArray.h

Committer:
jakowisp
Date:
2013-09-18
Revision:
10:fc1cb68fc91e
Parent:
4:9c6f5867f050
Child:
11:3b072cf16df8

File content as of revision 10:fc1cb68fc91e:

/**
*  @file PIMAArray.h
*  @brief PIMA Array class definition
*  @author Dwayne Dilbeck
*  @date 9/16/2013
* 
* mbed USBHostPTP Library(PIMA15740 Array definition)
*
* @par Copyright:
*  Copyright (c) 2013 Dwayne Dilbeck
* @par License:
*  This software is distributed under the terms of the GNU Lesser General Public License
*/

/**
*  Class PIMA array
*
*  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:
  /**
  *   Constructor
  *  @param None
  *
  *   Zeros the number of elements and sets codes pointer to NUll
  */
  PIMAArray() {
     numberOfElements=0;
     elements=NULL;
  };
  
  /**
  * Destructor
  *
  * Frees assigned storage for codes.
  * @param None
  */
  ~PIMAArray() {
    if( elements !=NULL)
        free(elements);
  };

  /**
   *  Create and fill array storage from a supplied buffer pointer
   *
   *  @param currentPtr a unit8_t pointer to a buffer location where a PIMAArray should be read
   *  
   *  @return The number of bytes used from the buffer to create the PIMA array.
   */
   
   int FillArray(uint8_t *currentPtr) {
     SetNumberOfElements(*((uint32_t *)currentPtr));
     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 elements storage
   TYPE *elements;
      
private:
/**
 * Function to allocate array storage space and set the number of elements
 *
 * @param uint8_t The number of elements to create storage space for.
 * @return none
 */
 void SetNumberOfElements(uint8_t length) {
     this->numberOfElements=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 SetElements(uint16_t *buffer){
     if(buffer!=NULL && elements !=NULL)
       for(int i=0;i<this->numberOfElements;i++)
           elements[i]=buffer[i];
 };
  
};