PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

183 lines
5.8KB

  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. import shutil
  5. import subprocess
  6. import argparse
  7. from pprint import pprint
  8. from midi import *
  9. from tester import *
  10. # ------------------------------------------------------------------------------
  11. rootDir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../..'))
  12. logsDir = os.path.join(rootDir, 'logs')
  13. resDir = os.path.join(rootDir, 'res')
  14. srcDir = os.path.join(rootDir, 'src')
  15. # ------------------------------------------------------------------------------
  16. class Dict(dict):
  17. def __init__(self, *args, **kwargs):
  18. super().__init__(*args, **kwargs)
  19. self.__dict__ = self
  20. # ------------------------------------------------------------------------------
  21. class Arduino:
  22. if sys.platform == 'darwin':
  23. binary = '/Applications/Arduino.app/Contents/MacOS/JavaApplicationStub'
  24. home = os.path.expanduser('~/Documents/Arduino')
  25. elif sys.platform == 'win32':
  26. binary = 'arduino.exe'
  27. home = os.path.expanduser('~/My Documents/Arduino')
  28. elif sys.platform == 'linux':
  29. binary = 'arduino'
  30. home = os.path.expanduser('~/Arduino')
  31. else:
  32. print('Unsupported platform %s' % str(sys.platform))
  33. sys.exit(1)
  34. libraryDir = os.path.join(home, 'libraries')
  35. boards = [
  36. Dict({
  37. 'name': 'Uno',
  38. 'id': 'arduino:avr:uno',
  39. 'port': None,
  40. }),
  41. Dict({
  42. 'name': 'Leonardo',
  43. 'id': 'arduino:avr:leonardo',
  44. 'port': None,
  45. }),
  46. Dict({
  47. 'name': 'Mega',
  48. 'id': 'arduino:avr:mega',
  49. 'port': None,
  50. }),
  51. Dict({
  52. 'name': 'Due',
  53. 'id': 'arduino:sam:due',
  54. 'port': None,
  55. }),
  56. ]
  57. def checkReturnCode(code):
  58. if code == 0:
  59. return True
  60. if code == 1:
  61. print('Operation failed.')
  62. if code == 2:
  63. print('File not found')
  64. if code == 3:
  65. print('Invalid argument')
  66. return False
  67. def verify(sketch, boardId):
  68. return Arduino.checkReturnCode(subprocess.call([
  69. Arduino.binary,
  70. '--verify', sketch,
  71. '--board', boardId,
  72. '--verbose-build',
  73. ]))
  74. #], stdout = open(os.devnull, 'wb')))
  75. # ------------------------------------------------------------------------------
  76. class ArduinoMidiLibrary:
  77. def __init__(self):
  78. self.path = os.path.join(Arduino.libraryDir, 'MIDI')
  79. self.sources = self.getSources()
  80. self.resources = self.getResources()
  81. def getSources(self):
  82. sources = dict()
  83. for root, dirs, files in os.walk(srcDir):
  84. for name, ext in [os.path.splitext(f) for f in files]:
  85. if ext in ('.cpp', '.hpp', '.h'):
  86. source = os.path.join(root, name + ext)
  87. dest = os.path.join(self.path, os.path.relpath(source, srcDir))
  88. sources[source] = dest
  89. return sources
  90. def getResources(self):
  91. return {
  92. os.path.join(resDir, 'keywords.txt'): os.path.join(self.path, 'keywords.txt'),
  93. os.path.join(resDir, 'examples/'): os.path.join(self.path, 'examples/'),
  94. }
  95. def install(self):
  96. payloads = dict(list(self.sources.items()) + list(self.resources.items()))
  97. for s,d in payloads.items():
  98. if not os.path.exists(os.path.dirname(d)):
  99. os.makedirs(os.path.dirname(d))
  100. if os.path.isfile(s):
  101. shutil.copy2(s, d)
  102. elif os.path.isdir(s):
  103. if os.path.exists(d):
  104. shutil.rmtree(d)
  105. shutil.copytree(s, d)
  106. def getInstalledExamples(self):
  107. exDir = os.path.join(self.path, 'examples')
  108. return [os.path.join(exDir, x, x + '.ino') for x in next(os.walk(exDir))[1]]
  109. def validate(self):
  110. for board in Arduino.boards:
  111. # Validate examples
  112. print('Validation for Arduino %s' % board.name)
  113. for example in self.getInstalledExamples():
  114. if not Arduino.verify(example, board.id):
  115. print('{0:40} {1}'.format(os.path.basename(example), 'FAILED'))
  116. return False
  117. else:
  118. print('{0:40} {1}'.format(os.path.basename(example), 'PASSED'))
  119. return True
  120. # ------------------------------------------------------------------------------
  121. def main():
  122. info = "Validator script for the Arduino MIDI Library."
  123. arg_parser = argparse.ArgumentParser(description = info)
  124. arg_parser.add_argument('--compile', '-c',
  125. action="store_true",
  126. help="Test compilation of the example sketches")
  127. arg_parser.add_argument('--runtime', '-r',
  128. action="store_true",
  129. help="Test runtime")
  130. args = arg_parser.parse_args()
  131. if args.compile:
  132. lib = ArduinoMidiLibrary()
  133. lib.install()
  134. if lib.validate():
  135. print('Compilation test passed')
  136. else:
  137. print('Compilation test failed')
  138. if args.runtime:
  139. midiInterface = MidiInterface()
  140. tester = Tester(midiInterface)
  141. midiInterface.listenerCallback = tester.handleMidiInput
  142. tester.checkThru([Midi.NoteOn, 64, 80])
  143. tester.checkThru([Midi.AfterTouchChannel, 1])
  144. tester.checkThru([2])
  145. tester.checkThru([3])
  146. tester.checkThru([Midi.NoteOn, 64, 0])
  147. tester.checkThru([65, 127])
  148. tester.checkThru([65, 0])
  149. tester.checkThru([66, 127])
  150. tester.checkThru([66, 0])
  151. # ------------------------------------------------------------------------------
  152. if __name__ == '__main__':
  153. main()