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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/python
  2. #
  3. # Uploads raw audio files to Teensy + Audio board with SPI Flash on board. To use this program, first
  4. # load the 'CopyFromSerial' example sketch. When it first runs, it will format the SPI flash chip
  5. # (this may take a long time for larger chips; a 128MB chip that I am using can take almost 10 minutes,
  6. # but smaller 16MB ones should be faster).
  7. #
  8. # While the chip is being formatted, the LED (pin 13) will toggle at 1Hz rate. When the formatting is
  9. # done, it flashes quickly (10Hz) for one second, then stays on solid. When nothing has been received
  10. # for 3 seconds, the upload is assumed to be completed, and the light goes off.
  11. #
  12. # You can start this program immediately upon plugging in the Teensy. It will buffer and wait until
  13. # the Teensy starts to read the serial data from USB.
  14. #
  15. ###################
  16. import serial, sys, os, time
  17. if (len(sys.argv) <= 2):
  18. print("Usage: '" + sys.argv[0] + " <port> <files>' where:\n\t<port> is the TTY USB port connected to Drum Master\n\t<files> is a list of .RAW files (bash globs work).")
  19. sys.exit()
  20. #Special bytes
  21. BYTE_START = "\x7e"
  22. BYTE_ESCAPE = "\x7d"
  23. BYTE_SEPARATOR = "\x7c"
  24. #Flash size (in MB). Change this to match how much space you have on your chip.
  25. FLASH_SIZE = 16
  26. totalFileSize = 0;
  27. for i, filename in enumerate(sys.argv):
  28. if (i >= 2):
  29. totalFileSize = totalFileSize + os.path.getsize(filename)
  30. flashSizeBytes = FLASH_SIZE * 1024 * 1024
  31. if (totalFileSize > flashSizeBytes):
  32. print("Too many files selsected.\n\tTotal flash size:\t" + "{:>14,}".format(flashSizeBytes) + " bytes\n\tTotal file size:\t" + "{:>14,}".format(totalFileSize) + " bytes")
  33. sys.exit()
  34. ser = serial.Serial(sys.argv[1])
  35. print("Uploading " + str(len(sys.argv) - 2) + " files...")
  36. for i, filename in enumerate(sys.argv):
  37. if (i >= 2):
  38. startTime = time.time();
  39. sys.stdout.write(str(i - 1) + ": ")
  40. sys.stdout.write(filename)
  41. sys.stdout.flush()
  42. f = open(filename, "rb")
  43. fileLength = os.path.getsize(filename)
  44. try:
  45. encoded = []
  46. #Start byte
  47. encoded.append(BYTE_START)
  48. #Filename
  49. for byte in os.path.basename(filename):
  50. encoded.append(byte)
  51. #End of filename
  52. encoded.append(BYTE_SEPARATOR)
  53. #File length (uint32_t)
  54. encoded.append(chr((fileLength >> 24) & 0xFF));
  55. encoded.append(chr((fileLength >> 16) & 0xFF));
  56. encoded.append(chr((fileLength >> 8) & 0xFF));
  57. encoded.append(chr((fileLength >> 0) & 0xFF));
  58. encoded.append(BYTE_SEPARATOR)
  59. #Binary data, with escaping
  60. for byte in f.read():
  61. if byte == BYTE_START or byte == BYTE_ESCAPE:
  62. encoded.append(BYTE_ESCAPE)
  63. encoded.append(chr(ord(byte) ^ 0x20))
  64. else:
  65. encoded.append(byte);
  66. #Write end of data byte
  67. encoded.append(BYTE_START)
  68. ser.write("".join(encoded))
  69. finally:
  70. f.close()
  71. endTime = time.time();
  72. print(" (" + str(round(fileLength / 1024 / (endTime - startTime), 2)) + " KB/s)");
  73. print("All files uploaded")