PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PatternMatching.ino 4.1KB

3 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. The library has four methods for doing pattern matching on address.
  3. 'match' and 'fullMatch' are specific to OSCMessages while route and dispatch work on both messages and bundles.
  4. OSCMessage:
  5. match - matches the message's address pattern against an address. returns the number of characters matched from the address passed in.
  6. fullMatch - returns true if the pattern was a complete match against the address
  7. OSCMessage && OSCBundle:
  8. route - calls a function with the matched OSCMessage(s) and the number of matched characters in the address as the parameters
  9. dispatch - calls a function with each OSCMessage which was fully matched by the pattern
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. OSC Regular expression pattern matching rules from http://opensoundcontrol.org/spec-1_0
  12. 1. '?' in the OSC Address Pattern matches any single character
  13. 2. '*' in the OSC Address Pattern matches any sequence of zero or more characters
  14. 3. A string of characters in square brackets (e.g., "[string]") in the OSC Address Pattern matches any character in the string.
  15. Inside square brackets, the minus sign (-) and exclamation point (!) have special meanings:
  16. two characters separated by a minus sign indicates the range of characters between the given two in ASCII collating sequence.
  17. An exclamation point at the beginning of a bracketed string negates the sense of the list, meaning that the list matches any character not in the list.
  18. 4. A comma-separated list of strings enclosed in curly braces (e.g., "{foo,bar}") in the OSC Address Pattern matches any of the strings in the list.
  19. 5. Any other character in an OSC Address Pattern can match only the same character.
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////
  21. */
  22. #include <OSCBundle.h>
  23. void setup() {
  24. Serial.begin(38400);
  25. }
  26. void loop(){
  27. //a heavily patterned message address
  28. OSCMessage msg0("/{input,output}/[0-2]/[!ab]/*");
  29. //match will traverse as far as it can in the pattern
  30. //it returns the number of characters matched from the pattern
  31. int patternOffset = msg0.match("/input/1");
  32. if (patternOffset>0){
  33. //string multiple 'match' methods together using the pattern offset parameter to continue matching where it left off
  34. //use 'fullMatch' to test if the entire pattern was matched.
  35. if(msg0.fullMatch("/c/anything", patternOffset)){
  36. Serial.println("Match: '/input/1/c/anything' against the pattern '/{input,output}/[0-2]/[abc]/*'");
  37. }
  38. }
  39. //write over the other message address
  40. OSCMessage msg1("/partialMatch");
  41. //match will return 0 if it did not reach the end or a '/'
  42. if(!msg1.match("/partial")){
  43. Serial.println("No Match: '/partial' against the pattern '/partialMatch'");
  44. }
  45. OSCMessage msg2("/output/[0-2]");
  46. //'route' is uses 'match' to allow for partial matches
  47. //it invokes the callback with the matched message and the pattern offset as parameters to the callback
  48. msg2.route("/output", routeOutput);
  49. //'dispatch' uses 'fullMatch' so it does not allow for partial matches
  50. //invokes the callback with only one argument which is the matched message
  51. msg2.dispatch("/output/1", routeOutputOne);
  52. delay(1000);
  53. }
  54. //called after matching '/output'
  55. //the matched message and the number of matched characters as the parameters
  56. void routeOutput(OSCMessage &msg, int patternOffset){
  57. Serial.println("Match: '/output'");
  58. //string multiple 'route' methods together using the pattern offset parameter.
  59. msg.route("/0", routeZero, patternOffset);
  60. }
  61. //called after matching '/0'
  62. void routeZero(OSCMessage &msg, int addressOffset){
  63. Serial.println("Match: '/output/0'");
  64. }
  65. //called after matching '/output/1'
  66. void routeOutputOne(OSCMessage &msg){
  67. Serial.println("Match: '/output/1'");
  68. }
  69. //
  70. // TROUBLESHOOTING:
  71. // Because of a bug in the Arduino IDE, it sometimes thinks that the '*' in combination with '/' is the opening or closing of a multiline comment
  72. // This can be fixed by escaping the '/' with '\' or using the octal value of '*' which is '\052'
  73. // for example:
  74. // "/*" == "/\052" == "\/*"
  75. //