PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
4.9KB

  1. #include "OSCData.h"
  2. osctime_t zerotime = {0,0};
  3. /*=============================================================================
  4. CONSTRUCTORS
  5. overloaded methods for each of the types which will
  6. set the type flag, the size (in bytes), and the data
  7. =============================================================================*/
  8. OSCData::OSCData(const char * s){
  9. error = OSC_OK;
  10. type = 's';
  11. bytes = (strlen(s) + 1);
  12. //own the data
  13. char * mem = (char *) malloc(bytes);
  14. if (mem == NULL){
  15. error = ALLOCFAILED;
  16. } else {
  17. strcpy(mem, s);
  18. data.s = mem;
  19. }
  20. }
  21. OSCData::OSCData(int32_t i){
  22. error = OSC_OK;
  23. type = 'i';
  24. bytes = 4;
  25. data.i = i;
  26. }
  27. #ifndef ESP8266
  28. OSCData::OSCData(int i){
  29. error = OSC_OK;
  30. type = 'i';
  31. bytes = 4;
  32. data.i = i;
  33. }
  34. #endif
  35. OSCData::OSCData(unsigned int i){
  36. error = OSC_OK;
  37. type = 'i';
  38. bytes = 4;
  39. data.i = i;
  40. }
  41. #if defined(__SAM3X8E__)
  42. OSCData::OSCData(int16_t i){
  43. error = OSC_OK;
  44. type = 'i';
  45. bytes = 4;
  46. data.i = i;
  47. }
  48. #endif
  49. OSCData::OSCData(float f){
  50. error = OSC_OK;
  51. type = 'f';
  52. bytes = 4;
  53. data.f = f;
  54. }
  55. OSCData::OSCData(osctime_t t){
  56. error = OSC_OK;
  57. type = 't';
  58. bytes = 8;
  59. data.time = t;
  60. }
  61. OSCData::OSCData(boolean b){
  62. error = OSC_OK;
  63. type = b?'T':'F';
  64. bytes = 0;
  65. }
  66. OSCData::OSCData(double d){
  67. error = OSC_OK;
  68. bytes = sizeof(double);
  69. //if it's not 8 bytes it's not a true double
  70. if (bytes == 8){
  71. type = 'd';
  72. data.d = d;
  73. } else {
  74. type = 'f';
  75. data.f = d;
  76. }
  77. }
  78. OSCData::OSCData(uint8_t * b, int len){
  79. error = OSC_OK;
  80. type = 'b';
  81. bytes = len + 4;
  82. //add the size to the front of the blob
  83. uint32_t len32 = (uint32_t) len;
  84. //make sure the length is endian-safe
  85. len32 = BigEndian(len32);
  86. uint8_t * lenPtr = (uint8_t *) (& len32);
  87. //own the data
  88. if(bytes>0)
  89. {
  90. uint8_t * mem = (uint8_t * ) malloc(bytes);
  91. if (mem == NULL){
  92. error = ALLOCFAILED;
  93. } else {
  94. //copy over the blob length
  95. memcpy(mem, lenPtr, 4);
  96. //copy over the blob data
  97. memcpy(mem + 4, b, len);
  98. data.b = mem;
  99. }
  100. }
  101. else
  102. data.b = 0;
  103. }
  104. OSCData::OSCData (OSCData * datum){
  105. error = OSC_OK;
  106. type = datum->type;
  107. bytes = datum->bytes;
  108. if ( (type == 'i') || (type == 'f') || (type == 'd') || (type == 't')
  109. || (type == 'h') || (type == 'c') || (type == 'r') || (type == 'm')
  110. )
  111. {
  112. data = datum->data;
  113. } else if ((type == 's') || (type == 'b')){
  114. //allocate a new piece of memory
  115. uint8_t * mem = (uint8_t * ) malloc(bytes);
  116. if (mem == NULL){
  117. error = ALLOCFAILED;
  118. } else {
  119. //copy over the blob length
  120. memcpy(mem, datum->data.b, bytes);
  121. data.b = mem;
  122. }
  123. }
  124. }
  125. //DESTRUCTOR
  126. OSCData::~OSCData(){
  127. //if there are no bytes, there is nothing to free
  128. if (bytes>0){
  129. //if the data is of type 's' or 'b', need to free that memory
  130. if (type == 's'){
  131. free(data.s);
  132. }else if( type == 'b'){
  133. free(data.b);
  134. }
  135. }
  136. }
  137. //sets just the type as a message placeholder
  138. //no data
  139. OSCData::OSCData(char t){
  140. error = INVALID_OSC;
  141. type = t;
  142. bytes = 0;
  143. }
  144. /*=============================================================================
  145. GETTERS
  146. perform a safety check to make sure the data type matches the request
  147. otherwise returns NULL
  148. =============================================================================*/
  149. int32_t OSCData::getInt(){
  150. if (type == 'i'){
  151. return data.i;
  152. } else {
  153. #ifndef ESP8266
  154. return (int32_t)NULL;
  155. #else
  156. return -1;
  157. #endif
  158. }
  159. }
  160. osctime_t OSCData::getTime(){
  161. if (type == 't'){
  162. return data.time;
  163. } else {
  164. return zerotime;
  165. }
  166. }
  167. float OSCData::getFloat(){
  168. if (type == 'f'){
  169. return data.f;
  170. } else {
  171. #ifndef ESP8266
  172. return (float)NULL;
  173. #else
  174. return -1;
  175. #endif
  176. }
  177. }
  178. double OSCData::getDouble(){
  179. if (type == 'd'){
  180. return data.d;
  181. } else {
  182. #ifndef ESP8266
  183. return (double)NULL;
  184. #else
  185. return -1;
  186. #endif
  187. }
  188. }
  189. bool OSCData::getBoolean(){
  190. if (type == 'T'){
  191. return true;
  192. } else if (type=='F'){
  193. return false;
  194. }
  195. else
  196. #ifndef ESP8266
  197. return NULL;
  198. #else
  199. return -1;
  200. #endif
  201. }
  202. int OSCData::getString(char * strBuffer, int length){
  203. if (type == 's' && bytes >= length){
  204. strncpy(strBuffer, data.s, length);
  205. return length;
  206. } else {
  207. #ifndef ESP8266
  208. return (int)NULL;
  209. #else
  210. return -1;
  211. #endif
  212. }
  213. }
  214. int OSCData::getBlob(uint8_t * blobBuffer, int length){
  215. //jump over the first 4 bytes which encode the length
  216. int blobLength = bytes - 4;
  217. if (type == 'b' && blobLength >= length){
  218. memcpy(blobBuffer, data.b + 4, length);
  219. return length;
  220. } else {
  221. #ifndef ESP8266
  222. return (int)NULL;
  223. #else
  224. return -1;
  225. #endif
  226. }
  227. }