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

Dependents:   class_project_main

PIMA15740/PIMAString.h

Committer:
jakowisp
Date:
2013-08-23
Revision:
1:71c0e9dc153d
Parent:
0:98cf19bcd828
Child:
2:67753d738eb8

File content as of revision 1:71c0e9dc153d:

/* mbed USBHostPTP Library(PIMA15740 definitions)
 * Copyright (c) 2013 Dwayne Dilbeck
 * This software is distributed under the terms of the GNU Lesser General Public License
 */


class PIMAString {
public:

  PIMAString() {
     length=0;
     StringChars=NULL;
     vals=NULL;
  };
  
  ~PIMAString() {
     if( StringChars !=NULL)
         free(StringChars);
     if(vals!=NULL)
         free(vals);
   };

 int FillString(uint8_t *currentPtr) {
     setLength(*currentPtr);
     setStringChars((uint16_t *)(currentPtr+1));
     return 2*length +1;
 };


  char * getString() {
     if(vals!=NULL)
        free(vals);  
     vals=(char *) malloc(sizeof(char)*(this->length+1));
     for(int i=0;i<this->length;i++)
           vals[i]=StringChars[i];  //NOTE: Truncates the uint16_t value.
     vals[length]='\0';
     return vals;
  }

  uint8_t getLength() {
     return this->length;
  }
 
 private:
 void setLength(uint8_t length) {
     if(length > this->length) {
         if( StringChars !=NULL)
            free(StringChars);
         StringChars = (uint16_t *) malloc(sizeof(uint16_t)*length);
     }
     this->length=length;

 };
 
 void setStringChars(uint16_t *buffer){
     if(buffer!=NULL && StringChars !=NULL)
       for(int i=0;i<this->length;i++)
           StringChars[i]=buffer[i];
 };
 
  
  uint8_t length;
  uint16_t *StringChars;
  char *vals;
    
};