Library to allo USB PTP device to be hosted by the mbed platform
Dependents: class_project_main
PIMA15740/PIMAString.h@2:67753d738eb8, 2013-08-23 (annotated)
- Committer:
- jakowisp
- Date:
- Fri Aug 23 20:02:44 2013 +0000
- Revision:
- 2:67753d738eb8
- Parent:
- 1:71c0e9dc153d
- Child:
- 3:1fcb46ab18df
Document The PIMAString Class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jakowisp | 2:67753d738eb8 | 1 | /** |
jakowisp | 2:67753d738eb8 | 2 | * @file PIMAString.h |
jakowisp | 2:67753d738eb8 | 3 | * @brief PIMA String class definition |
jakowisp | 2:67753d738eb8 | 4 | * @author Dwayne Dilbeck |
jakowisp | 2:67753d738eb8 | 5 | * @date 8/23/2013 |
jakowisp | 2:67753d738eb8 | 6 | * |
jakowisp | 2:67753d738eb8 | 7 | * mbed USBHostPTP Library(PIMA15740 definitions) |
jakowisp | 2:67753d738eb8 | 8 | * Copyright (c) 2013 Dwayne Dilbeck |
jakowisp | 2:67753d738eb8 | 9 | * This software is distributed under the terms of the GNU Lesser General Public License |
jakowisp | 2:67753d738eb8 | 10 | */ |
jakowisp | 0:98cf19bcd828 | 11 | |
jakowisp | 2:67753d738eb8 | 12 | /** |
jakowisp | 2:67753d738eb8 | 13 | * Class PIMA String |
jakowisp | 2:67753d738eb8 | 14 | * |
jakowisp | 2:67753d738eb8 | 15 | * The PIMA15740 standard defines a string as an 8bit length followed by a list |
jakowisp | 2:67753d738eb8 | 16 | * of 16bit unicode characters |
jakowisp | 2:67753d738eb8 | 17 | * |
jakowisp | 2:67753d738eb8 | 18 | */ |
jakowisp | 0:98cf19bcd828 | 19 | class PIMAString { |
jakowisp | 0:98cf19bcd828 | 20 | public: |
jakowisp | 0:98cf19bcd828 | 21 | |
jakowisp | 2:67753d738eb8 | 22 | /** |
jakowisp | 2:67753d738eb8 | 23 | * Constructor |
jakowisp | 2:67753d738eb8 | 24 | * By default the pointers are set to NULL and length set to ZERO. |
jakowisp | 2:67753d738eb8 | 25 | */ |
jakowisp | 0:98cf19bcd828 | 26 | PIMAString() { |
jakowisp | 0:98cf19bcd828 | 27 | length=0; |
jakowisp | 0:98cf19bcd828 | 28 | StringChars=NULL; |
jakowisp | 0:98cf19bcd828 | 29 | vals=NULL; |
jakowisp | 0:98cf19bcd828 | 30 | }; |
jakowisp | 0:98cf19bcd828 | 31 | |
jakowisp | 2:67753d738eb8 | 32 | /** |
jakowisp | 2:67753d738eb8 | 33 | * Desctructor |
jakowisp | 2:67753d738eb8 | 34 | * Free the Memory allocated for cahracter storage |
jakowisp | 2:67753d738eb8 | 35 | */ |
jakowisp | 0:98cf19bcd828 | 36 | ~PIMAString() { |
jakowisp | 0:98cf19bcd828 | 37 | if( StringChars !=NULL) |
jakowisp | 0:98cf19bcd828 | 38 | free(StringChars); |
jakowisp | 0:98cf19bcd828 | 39 | if(vals!=NULL) |
jakowisp | 0:98cf19bcd828 | 40 | free(vals); |
jakowisp | 0:98cf19bcd828 | 41 | }; |
jakowisp | 0:98cf19bcd828 | 42 | |
jakowisp | 2:67753d738eb8 | 43 | /** |
jakowisp | 2:67753d738eb8 | 44 | * |
jakowisp | 2:67753d738eb8 | 45 | * @param currentPtr a unit8_t pointer to a buffer location where a PIMAString shoudl be read |
jakowisp | 2:67753d738eb8 | 46 | * |
jakowisp | 2:67753d738eb8 | 47 | * @return The number of bytes used to from the buffer to create the PIMA string. |
jakowisp | 2:67753d738eb8 | 48 | */ |
jakowisp | 0:98cf19bcd828 | 49 | int FillString(uint8_t *currentPtr) { |
jakowisp | 0:98cf19bcd828 | 50 | setLength(*currentPtr); |
jakowisp | 0:98cf19bcd828 | 51 | setStringChars((uint16_t *)(currentPtr+1)); |
jakowisp | 0:98cf19bcd828 | 52 | return 2*length +1; |
jakowisp | 0:98cf19bcd828 | 53 | }; |
jakowisp | 0:98cf19bcd828 | 54 | |
jakowisp | 2:67753d738eb8 | 55 | /** |
jakowisp | 2:67753d738eb8 | 56 | * Truncate the unicode characters to make a string for printf. |
jakowisp | 2:67753d738eb8 | 57 | * |
jakowisp | 2:67753d738eb8 | 58 | * @param none |
jakowisp | 2:67753d738eb8 | 59 | * @return pointer to an array of characters that is null terminated. |
jakowisp | 2:67753d738eb8 | 60 | */ |
jakowisp | 0:98cf19bcd828 | 61 | char * getString() { |
jakowisp | 0:98cf19bcd828 | 62 | if(vals!=NULL) |
jakowisp | 0:98cf19bcd828 | 63 | free(vals); |
jakowisp | 0:98cf19bcd828 | 64 | vals=(char *) malloc(sizeof(char)*(this->length+1)); |
jakowisp | 0:98cf19bcd828 | 65 | for(int i=0;i<this->length;i++) |
jakowisp | 0:98cf19bcd828 | 66 | vals[i]=StringChars[i]; //NOTE: Truncates the uint16_t value. |
jakowisp | 0:98cf19bcd828 | 67 | vals[length]='\0'; |
jakowisp | 0:98cf19bcd828 | 68 | return vals; |
jakowisp | 0:98cf19bcd828 | 69 | } |
jakowisp | 0:98cf19bcd828 | 70 | |
jakowisp | 2:67753d738eb8 | 71 | /** |
jakowisp | 2:67753d738eb8 | 72 | * Get the number of characters stored in the PIMA String |
jakowisp | 2:67753d738eb8 | 73 | * |
jakowisp | 2:67753d738eb8 | 74 | * @param none |
jakowisp | 2:67753d738eb8 | 75 | * @return uint8_t value that is the number of chracters notstored in the PIMA String |
jakowisp | 2:67753d738eb8 | 76 | */ |
jakowisp | 0:98cf19bcd828 | 77 | uint8_t getLength() { |
jakowisp | 0:98cf19bcd828 | 78 | return this->length; |
jakowisp | 0:98cf19bcd828 | 79 | } |
jakowisp | 0:98cf19bcd828 | 80 | |
jakowisp | 0:98cf19bcd828 | 81 | private: |
jakowisp | 2:67753d738eb8 | 82 | /** |
jakowisp | 2:67753d738eb8 | 83 | * Function to allocate unicode cahracter storage space and set the length value |
jakowisp | 2:67753d738eb8 | 84 | * |
jakowisp | 2:67753d738eb8 | 85 | * @param uint8_t The number of characters to create storage space for. |
jakowisp | 2:67753d738eb8 | 86 | * @return none |
jakowisp | 2:67753d738eb8 | 87 | */ |
jakowisp | 0:98cf19bcd828 | 88 | void setLength(uint8_t length) { |
jakowisp | 0:98cf19bcd828 | 89 | if(length > this->length) { |
jakowisp | 0:98cf19bcd828 | 90 | if( StringChars !=NULL) |
jakowisp | 0:98cf19bcd828 | 91 | free(StringChars); |
jakowisp | 0:98cf19bcd828 | 92 | StringChars = (uint16_t *) malloc(sizeof(uint16_t)*length); |
jakowisp | 0:98cf19bcd828 | 93 | } |
jakowisp | 0:98cf19bcd828 | 94 | this->length=length; |
jakowisp | 0:98cf19bcd828 | 95 | }; |
jakowisp | 0:98cf19bcd828 | 96 | |
jakowisp | 2:67753d738eb8 | 97 | /** |
jakowisp | 2:67753d738eb8 | 98 | * Function to read unicode charcaters form a uint8t buffer and store them. |
jakowisp | 2:67753d738eb8 | 99 | * @param pointer to a uint8_t buffer |
jakowisp | 2:67753d738eb8 | 100 | */ |
jakowisp | 0:98cf19bcd828 | 101 | void setStringChars(uint16_t *buffer){ |
jakowisp | 0:98cf19bcd828 | 102 | if(buffer!=NULL && StringChars !=NULL) |
jakowisp | 0:98cf19bcd828 | 103 | for(int i=0;i<this->length;i++) |
jakowisp | 0:98cf19bcd828 | 104 | StringChars[i]=buffer[i]; |
jakowisp | 0:98cf19bcd828 | 105 | }; |
jakowisp | 0:98cf19bcd828 | 106 | |
jakowisp | 2:67753d738eb8 | 107 | ///number of stored characters |
jakowisp | 0:98cf19bcd828 | 108 | uint8_t length; |
jakowisp | 2:67753d738eb8 | 109 | ///Pointer to the character storage |
jakowisp | 0:98cf19bcd828 | 110 | uint16_t *StringChars; |
jakowisp | 2:67753d738eb8 | 111 | ///Pointer to Character storge that are truncated unicode values. |
jakowisp | 0:98cf19bcd828 | 112 | char *vals; |
jakowisp | 0:98cf19bcd828 | 113 | }; |