PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

149 líneas
3.8KB

  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. //ERRORS/////////////////////////////////////////////////
  38. typedef enum { OSC_OK = 0,
  39. BUFFER_FULL, INVALID_OSC, ALLOCFAILED, INDEX_OUT_OF_BOUNDS
  40. } OSCErrorCode;
  41. class OSCData
  42. {
  43. private:
  44. //friends
  45. friend class OSCMessage;
  46. //should only be used while decoding
  47. //leaves an invalid OSCMessage with a type, but no data
  48. OSCData(char t);
  49. public:
  50. //an error flag
  51. OSCErrorCode error;
  52. //the size (in bytes) of the data
  53. int bytes;
  54. //the type of the data
  55. int type;
  56. //the data
  57. union {
  58. char * s; //string
  59. int32_t i; //int
  60. float f; //float
  61. double d; //double
  62. uint64_t l; //long
  63. uint8_t * b; //blob
  64. osctime_t time;
  65. } data;
  66. //overload the constructor to account for all the types and sizes
  67. OSCData(const char * s);
  68. #if defined(__SAM3X8E__)
  69. OSCData (int16_t);
  70. #endif
  71. OSCData (int32_t);
  72. #ifndef ESP8266
  73. OSCData (int);
  74. #endif
  75. OSCData (unsigned int);
  76. OSCData (float);
  77. OSCData (double);
  78. OSCData (uint8_t *, int);
  79. //accepts another OSCData objects and clones it
  80. OSCData (OSCData *);
  81. OSCData (boolean);
  82. OSCData (osctime_t);
  83. //destructor
  84. ~OSCData();
  85. //GETTERS
  86. int32_t getInt();
  87. float getFloat();
  88. double getDouble();
  89. int getString(char *, int);
  90. int getBlob(uint8_t *, int);
  91. bool getBoolean();
  92. osctime_t getTime();
  93. //constructor from byte array with type and length
  94. OSCData(char, uint8_t *, int);
  95. //fill the passed in buffer with the data
  96. //uint8_t * asByteArray();
  97. };
  98. /*
  99. based on http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c
  100. if the system is little endian, it will flip the bits
  101. if the system is big endian, it'll do nothing
  102. */
  103. template<typename T>
  104. static inline T BigEndian(const T& x)
  105. {
  106. const int one = 1;
  107. const char sig = *(char*)&one;
  108. if (sig == 0) return x; // for big endian machine just return the input
  109. T ret;
  110. int size = sizeof(T);
  111. char* src = (char*)&x + sizeof(T) - 1;
  112. char* dst = (char*)&ret;
  113. while (size-- > 0){
  114. *dst++ = *src--;
  115. }
  116. return ret;
  117. }
  118. #endif