OSCData.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Written by Yotam Mann, The Center for New Music and Audio Technologies,
  3. University of California, Berkeley. Copyright (c) 2013, The Regents of
  4. the University of California (Regents).
  5. Permission to use, copy, modify, distribute, and distribute modified versions
  6. of this software and its documentation without fee and without a signed
  7. licensing agreement, is hereby granted, provided that the above copyright
  8. notice, this paragraph and the following two paragraphs appear in all copies,
  9. modifications, and distributions.
  10. IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  11. SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
  12. OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
  13. BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
  17. HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
  18. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  19. For bug reports and feature requests please email me at yotam@cnmat.berkeley.edu
  20. */
  21. #ifndef OSCDATA_h
  22. #define OSCDATA_h
  23. #include "Arduino.h"
  24. #include <stdlib.h>
  25. #include <stdint.h>
  26. #include <inttypes.h>
  27. #include <string.h>
  28. #include "OSCTiming.h"
  29. #if (defined(TEENSYDUINO) && defined(USB_SERIAL)) || (!defined(TEENSYDUINO) && defined(__AVR_ATmega32U4__)) || defined(__SAM3X8E__) || (defined(_USB) && defined(_USE_USB_FOR_SERIAL_)) || defined(BOARD_maple_mini)
  30. #define BOARD_HAS_USB_SERIAL
  31. #if defined(__SAM3X8E__)
  32. #define thisBoardsSerialUSB SerialUSB
  33. #else
  34. #define thisBoardsSerialUSB Serial
  35. #endif
  36. #endif
  37. #if defined(ESP8266) || defined(ESP32)
  38. #define ESPxx
  39. #endif
  40. //ERRORS/////////////////////////////////////////////////
  41. typedef enum { OSC_OK = 0,
  42. BUFFER_FULL, INVALID_OSC, ALLOCFAILED, INDEX_OUT_OF_BOUNDS
  43. } OSCErrorCode;
  44. class OSCData
  45. {
  46. private:
  47. //friends
  48. friend class OSCMessage;
  49. //should only be used while decoding
  50. //leaves an invalid OSCMessage with a type, but no data
  51. OSCData(char t);
  52. public:
  53. //an error flag
  54. OSCErrorCode error;
  55. //the size (in bytes) of the data
  56. int bytes;
  57. //the type of the data
  58. int type;
  59. //the data
  60. union {
  61. char * s; //string
  62. int32_t i; //int
  63. float f; //float
  64. double d; //double
  65. uint64_t l; //long
  66. uint8_t * b; //blob
  67. osctime_t time;
  68. } data;
  69. //overload the constructor to account for all the types and sizes
  70. OSCData(const char * s);
  71. #if defined(__SAM3X8E__)
  72. OSCData (int16_t);
  73. #endif
  74. OSCData (int32_t);
  75. #ifndef ESPxx
  76. OSCData (int);
  77. #endif
  78. OSCData (unsigned int);
  79. OSCData (float);
  80. OSCData (double);
  81. OSCData (uint8_t *, int);
  82. //accepts another OSCData objects and clones it
  83. OSCData (OSCData *);
  84. OSCData (boolean);
  85. OSCData (osctime_t);
  86. //destructor
  87. ~OSCData();
  88. //GETTERS
  89. int32_t getInt();
  90. float getFloat();
  91. double getDouble();
  92. int getString(char *);
  93. int getString(char *, int);
  94. int getString(char *, int, int, int);
  95. int getBlob(uint8_t *);
  96. int getBlob(uint8_t *, int);
  97. int getBlob(uint8_t *, int, int, int);
  98. int getBlobLength();
  99. bool getBoolean();
  100. osctime_t getTime();
  101. //constructor from byte array with type and length
  102. OSCData(char, uint8_t *, int);
  103. //fill the passed in buffer with the data
  104. //uint8_t * asByteArray();
  105. };
  106. /*
  107. based on http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c
  108. if the system is little endian, it will flip the bits
  109. if the system is big endian, it'll do nothing
  110. */
  111. template<typename T>
  112. static inline T BigEndian(const T& x)
  113. {
  114. const int one = 1;
  115. const char sig = *(char*)&one;
  116. if (sig == 0) return x; // for big endian machine just return the input
  117. T ret;
  118. int size = sizeof(T);
  119. char* src = (char*)&x + sizeof(T) - 1;
  120. char* dst = (char*)&ret;
  121. while (size-- > 0){
  122. *dst++ = *src--;
  123. }
  124. return ret;
  125. }
  126. #endif