Guest User

processing recursion error

a guest
Apr 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. Serial myPort;        // The serial port
  4. int xPos = 1;         // horizontal position of the graph
  5. float inByte = 0;
  6.  
  7. /* Intensity is a measurement of how many beams are interrupted over a set of time. It increases as beams get interrupted up to a maximum of 1 and normalizes to 0 over time.*/
  8.  
  9. float intensity = 0;
  10. float increasePerInterrupt = .08;
  11. float decreasePerFrame = .003;
  12. int frameRate = 10;
  13.  
  14. void setup () {
  15.   size(600, 400);
  16.   setupMorseChars();
  17.   frameRate(frameRate);
  18.  
  19.   // List all the available serial ports
  20.   println(Serial.list());
  21.  
  22.   // Open whatever port is the one you're using. 1 on my desktop pc, will need to check for Raspberry Pi
  23.   myPort = new Serial(this, Serial.list()[0], 19200);
  24.  
  25.   // don't generate a serialEvent() unless you get a newline character:
  26.   myPort.bufferUntil('\n');
  27.  
  28.   // Set up the morse signal
  29.   setupMorseSignal();
  30.  
  31.   // Set up the morse playback thread
  32.   thread("morsePlayback");
  33.  
  34.   // Set initial background:
  35.   background(0);
  36. }
  37.  
  38. import processing.sound.*;
  39. SinOsc morseSignal;
  40.  
  41. /* An array of morse code waiting to be played */
  42. char[] character_queue = new char[0];
  43.  
  44. // Length of the current morse signal in ms
  45. int signal_length = 0;
  46.  
  47. // Delay until the next morse signal in ms
  48. int pause_length = 0;
  49.  
  50. // Debug to count recursion calls
  51. int recursion_count = 0;
  52.  
  53. /* Duration of dots and dashes, using common morse practices */
  54.  
  55. final int dot = 110;
  56. final int pause = dot;
  57. final int dash = dot * 3;
  58. final int separator_length = dot * 7;
  59.  
  60. /* Special characters: character separator and word separator */
  61. final char char_separator = ' ';
  62. final char word_separator = '/';
  63.  
  64. /* Hashmap of numbers 0-9 */
  65. HashMap<Character, String> code = new HashMap<Character, String>();
  66.  
  67. void setupMorseSignal() {
  68.   morseSignal = new SinOsc(this);
  69.   morseSignal.amp(.25);
  70.   morseSignal.freq(300);
  71.   //morseSignal.play();
  72. }
  73.  
  74. void setupMorseChars() {
  75.   code.put('0', "-----");
  76.   code.put('1', ".----");
  77.   code.put('2', "..---");
  78.   code.put('3', "...--");
  79.   code.put('4', "....-");
  80.   code.put('5', ".....");
  81.   code.put('6', "-....");
  82.   code.put('7', "--...");
  83.   code.put('8', "---..");
  84.   code.put('9', "----.");
  85. }
  86.  
  87. // Plays back a number as morse code
  88. void enqueueMorseCode(String number) {
  89.  
  90.   // Store the number in a char array
  91.   char[] chars = number.toCharArray();
  92.  
  93.   // Loop through the digits of the number
  94.   for (int i = 0; i < chars.length; i++) {
  95.     String morse = code.get(chars[i]);
  96.     //println(morse);
  97.  
  98.     // For each digit, loop through the morse representation and add it to the morse queue
  99.     for (int j = 0; j < morse.length(); j++) {
  100.       character_queue = append(character_queue, morse.charAt(j));
  101.     }
  102.  
  103.     // Add a space at the end of each digit, except the last
  104.     if (i < chars.length -1) {
  105.       character_queue = append(character_queue, char_separator);
  106.     }
  107.   }
  108.  
  109.   // Add a separator at the end of each number
  110.   character_queue = append(character_queue, word_separator);
  111.  
  112.  
  113.   //println(character_queue);
  114.  
  115.   return;
  116. }
  117.  
  118. void morsePlayback() {
  119.  
  120.   recursion_count++;
  121.   println(recursion_count);
  122.  
  123.   // If there are no characters in the queue, abort and try again later.
  124.   if(character_queue.length == 0) {
  125.     delay(100);
  126.     morsePlayback();
  127.     return;
  128.   }
  129.  
  130.   // The current character to be played
  131.   // println(character_queue[0]);
  132.   switch(character_queue[0]) {
  133.   case '-':
  134.     signal_length = dash;
  135.     pause_length = pause;
  136.     break;
  137.   case '.':
  138.     signal_length = dot;
  139.     pause_length = pause;
  140.     break;
  141.   case ' ':
  142.     signal_length = 0;
  143.     pause_length = dash;
  144.     break;
  145.   case '/':
  146.     signal_length = 0;
  147.     pause_length = separator_length;
  148.     break;
  149.   }
  150.  
  151.   // Remove the element we just used via some reversing and shortening
  152.   character_queue = reverse(character_queue);
  153.   character_queue = shorten(character_queue);
  154.   character_queue = reverse(character_queue);
  155.  
  156.   // Start signal for duration x, then stop
  157.   if(signal_length > 0) {
  158.     //println("Start signal playback");
  159.     morseSignal.play();
  160.   }
  161.  
  162.   delay(signal_length);
  163.   //println("Stop signal playback");
  164.   morseSignal.stop();
  165.  
  166.   // Wait the right amount of time until next signal
  167.   delay(pause_length);
  168.   morsePlayback();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment