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

Dependents:   class_project_main

Committer:
jakowisp
Date:
Wed Sep 18 01:48:07 2013 +0000
Revision:
10:fc1cb68fc91e
Parent:
4:9c6f5867f050
Child:
11:3b072cf16df8
Adding DST code;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakowisp 3:1fcb46ab18df 1 /**
jakowisp 3:1fcb46ab18df 2 * @file PIMAArray.h
jakowisp 3:1fcb46ab18df 3 * @brief PIMA Array class definition
jakowisp 3:1fcb46ab18df 4 * @author Dwayne Dilbeck
jakowisp 10:fc1cb68fc91e 5 * @date 9/16/2013
jakowisp 3:1fcb46ab18df 6 *
jakowisp 3:1fcb46ab18df 7 * mbed USBHostPTP Library(PIMA15740 Array definition)
jakowisp 4:9c6f5867f050 8 *
jakowisp 3:1fcb46ab18df 9 * @par Copyright:
jakowisp 3:1fcb46ab18df 10 * Copyright (c) 2013 Dwayne Dilbeck
jakowisp 3:1fcb46ab18df 11 * @par License:
jakowisp 3:1fcb46ab18df 12 * This software is distributed under the terms of the GNU Lesser General Public License
jakowisp 3:1fcb46ab18df 13 */
jakowisp 0:98cf19bcd828 14
jakowisp 0:98cf19bcd828 15 /**
jakowisp 0:98cf19bcd828 16 * Class PIMA array
jakowisp 0:98cf19bcd828 17 *
jakowisp 3:1fcb46ab18df 18 * The PIMA15740 standard defines an array as an unsigned 32bit number of elements followed by a list
jakowisp 3:1fcb46ab18df 19 * of insinged 16bit codes;
jakowisp 0:98cf19bcd828 20 */
jakowisp 10:fc1cb68fc91e 21 template <class TYPE>
jakowisp 0:98cf19bcd828 22 class PIMAArray {
jakowisp 0:98cf19bcd828 23 public:
jakowisp 0:98cf19bcd828 24 /**
jakowisp 0:98cf19bcd828 25 * Constructor
jakowisp 3:1fcb46ab18df 26 * @param None
jakowisp 0:98cf19bcd828 27 *
jakowisp 3:1fcb46ab18df 28 * Zeros the number of elements and sets codes pointer to NUll
jakowisp 0:98cf19bcd828 29 */
jakowisp 0:98cf19bcd828 30 PIMAArray() {
jakowisp 0:98cf19bcd828 31 numberOfElements=0;
jakowisp 10:fc1cb68fc91e 32 elements=NULL;
jakowisp 0:98cf19bcd828 33 };
jakowisp 0:98cf19bcd828 34
jakowisp 3:1fcb46ab18df 35 /**
jakowisp 3:1fcb46ab18df 36 * Destructor
jakowisp 3:1fcb46ab18df 37 *
jakowisp 3:1fcb46ab18df 38 * Frees assigned storage for codes.
jakowisp 3:1fcb46ab18df 39 * @param None
jakowisp 3:1fcb46ab18df 40 */
jakowisp 0:98cf19bcd828 41 ~PIMAArray() {
jakowisp 10:fc1cb68fc91e 42 if( elements !=NULL)
jakowisp 10:fc1cb68fc91e 43 free(elements);
jakowisp 0:98cf19bcd828 44 };
jakowisp 0:98cf19bcd828 45
jakowisp 3:1fcb46ab18df 46 /**
jakowisp 3:1fcb46ab18df 47 * Create and fill array storage from a supplied buffer pointer
jakowisp 3:1fcb46ab18df 48 *
jakowisp 3:1fcb46ab18df 49 * @param currentPtr a unit8_t pointer to a buffer location where a PIMAArray should be read
jakowisp 3:1fcb46ab18df 50 *
jakowisp 3:1fcb46ab18df 51 * @return The number of bytes used from the buffer to create the PIMA array.
jakowisp 3:1fcb46ab18df 52 */
jakowisp 10:fc1cb68fc91e 53
jakowisp 10:fc1cb68fc91e 54 int FillArray(uint8_t *currentPtr) {
jakowisp 0:98cf19bcd828 55 SetNumberOfElements(*((uint32_t *)currentPtr));
jakowisp 10:fc1cb68fc91e 56 SetElements((TYPE *)(currentPtr+sizeof(uint32_t)));
jakowisp 10:fc1cb68fc91e 57 return (sizeof(TYPE)*numberOfElements) + sizeof(uint32_t);
jakowisp 10:fc1cb68fc91e 58 }
jakowisp 10:fc1cb68fc91e 59
jakowisp 10:fc1cb68fc91e 60 TYPE GetElement(uint32_t index) {
jakowisp 10:fc1cb68fc91e 61 return elements[index];
jakowisp 10:fc1cb68fc91e 62 }
jakowisp 10:fc1cb68fc91e 63
jakowisp 10:fc1cb68fc91e 64 bool CheckValueInArray(TYPE value) {
jakowisp 10:fc1cb68fc91e 65 bool res=false;
jakowisp 10:fc1cb68fc91e 66 for(int i=0; i<numberOfElements;i++)
jakowisp 10:fc1cb68fc91e 67 if(value == elements[i])
jakowisp 10:fc1cb68fc91e 68 res=true;
jakowisp 10:fc1cb68fc91e 69 return res;
jakowisp 10:fc1cb68fc91e 70 }
jakowisp 10:fc1cb68fc91e 71
jakowisp 0:98cf19bcd828 72
jakowisp 3:1fcb46ab18df 73 ///Number of elelments stored in the array
jakowisp 3:1fcb46ab18df 74 uint32_t numberOfElements;
jakowisp 10:fc1cb68fc91e 75 ///Pointer to elements storage
jakowisp 10:fc1cb68fc91e 76 TYPE *elements;
jakowisp 10:fc1cb68fc91e 77
jakowisp 3:1fcb46ab18df 78 private:
jakowisp 3:1fcb46ab18df 79 /**
jakowisp 3:1fcb46ab18df 80 * Function to allocate array storage space and set the number of elements
jakowisp 3:1fcb46ab18df 81 *
jakowisp 3:1fcb46ab18df 82 * @param uint8_t The number of elements to create storage space for.
jakowisp 3:1fcb46ab18df 83 * @return none
jakowisp 3:1fcb46ab18df 84 */
jakowisp 0:98cf19bcd828 85 void SetNumberOfElements(uint8_t length) {
jakowisp 0:98cf19bcd828 86 this->numberOfElements=length;
jakowisp 10:fc1cb68fc91e 87 if( elements !=NULL)
jakowisp 10:fc1cb68fc91e 88 free(elements);
jakowisp 10:fc1cb68fc91e 89 elements = (TYPE *) malloc(sizeof(TYPE)*length);
jakowisp 0:98cf19bcd828 90 };
jakowisp 0:98cf19bcd828 91
jakowisp 3:1fcb46ab18df 92 /**
jakowisp 3:1fcb46ab18df 93 * Function to read codes form a uint8t buffer and store them.
jakowisp 3:1fcb46ab18df 94 * @param buffer pointer to a uint8_t buffer
jakowisp 3:1fcb46ab18df 95 */
jakowisp 10:fc1cb68fc91e 96 void SetElements(uint16_t *buffer){
jakowisp 10:fc1cb68fc91e 97 if(buffer!=NULL && elements !=NULL)
jakowisp 0:98cf19bcd828 98 for(int i=0;i<this->numberOfElements;i++)
jakowisp 10:fc1cb68fc91e 99 elements[i]=buffer[i];
jakowisp 0:98cf19bcd828 100 };
jakowisp 0:98cf19bcd828 101
jakowisp 0:98cf19bcd828 102 };