Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.

rpc_classes.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. """
  2. mbed SDK
  3. Copyright (c) 2011-2013 ARM Limited
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. """
  14. from os.path import join
  15. from jinja2 import Template
  16. from workspace_tools.paths import TOOLS_DATA, MBED_RPC
  17. RPC_TEMPLATES_PATH = join(TOOLS_DATA, "rpc")
  18. RPC_TEMPLATE = "RPCClasses.h"
  19. CLASS_TEMPLATE = "class.cpp"
  20. RPC_CLASSES_PATH = join(MBED_RPC, RPC_TEMPLATE)
  21. def get_template(name):
  22. return Template(open(join(RPC_TEMPLATES_PATH, name)).read())
  23. def write_rpc_classes(classes):
  24. template = get_template(RPC_TEMPLATE)
  25. open(RPC_CLASSES_PATH, "w").write(template.render({"classes":classes}))
  26. RPC_CLASSES = (
  27. {
  28. "name": "DigitalOut",
  29. "cons_args": ["PinName"],
  30. "methods": [
  31. (None , "write", ["int"]),
  32. ("int", "read" , []),
  33. ]
  34. },
  35. {
  36. "name": "DigitalIn",
  37. "cons_args": ["PinName"],
  38. "methods": [
  39. ("int", "read" , []),
  40. ]
  41. },
  42. {
  43. "name": "DigitalInOut",
  44. "cons_args": ["PinName"],
  45. "methods": [
  46. ("int", "read" , []),
  47. (None , "write" , ["int"]),
  48. (None , "input" , []),
  49. (None , "output", []),
  50. ]
  51. },
  52. {
  53. "name": "AnalogIn",
  54. "required": "ANALOGIN",
  55. "cons_args": ["PinName"],
  56. "methods": [
  57. ("float" , "read" , []),
  58. ("unsigned short", "read_u16", []),
  59. ]
  60. },
  61. {
  62. "name": "AnalogOut",
  63. "required": "ANALOGOUT",
  64. "cons_args": ["PinName"],
  65. "methods": [
  66. ("float", "read" , []),
  67. (None , "write" , ["float"]),
  68. (None , "write_u16", ["unsigned short"]),
  69. ]
  70. },
  71. {
  72. "name": "PwmOut",
  73. "required": "PWMOUT",
  74. "cons_args": ["PinName"],
  75. "methods": [
  76. ("float", "read" , []),
  77. (None , "write" , ["float"]),
  78. (None , "period" , ["float"]),
  79. (None , "period_ms" , ["int"]),
  80. (None , "pulsewidth" , ["float"]),
  81. (None , "pulsewidth_ms", ["int"]),
  82. ]
  83. },
  84. {
  85. "name": "SPI",
  86. "required": "SPI",
  87. "cons_args": ["PinName", "PinName", "PinName"],
  88. "methods": [
  89. (None , "format" , ["int", "int"]),
  90. (None , "frequency", ["int"]),
  91. ("int", "write" , ["int"]),
  92. ]
  93. },
  94. {
  95. "name": "Serial",
  96. "required": "SERIAL",
  97. "cons_args": ["PinName", "PinName"],
  98. "methods": [
  99. (None , "baud" , ["int"]),
  100. ("int", "readable" , []),
  101. ("int", "writeable", []),
  102. ("int", "putc" , ["int"]),
  103. ("int", "getc" , []),
  104. ("int", "puts" , ["const char *"]),
  105. ]
  106. },
  107. {
  108. "name": "Timer",
  109. "cons_args": [],
  110. "methods": [
  111. (None , "start" , []),
  112. (None , "stop" , []),
  113. (None , "reset" , []),
  114. ("float", "read" , []),
  115. ("int" , "read_ms", []),
  116. ("int" , "read_us", []),
  117. ]
  118. }
  119. )
  120. def get_args_proto(args_types, extra=None):
  121. args = ["%s a%d" % (s, n) for n, s in enumerate(args_types)]
  122. if extra:
  123. args.extend(extra)
  124. return ', '.join(args)
  125. def get_args_call(args):
  126. return ', '.join(["a%d" % (n) for n in range(len(args))])
  127. classes = []
  128. class_template = get_template(CLASS_TEMPLATE)
  129. for c in RPC_CLASSES:
  130. c_args = c['cons_args']
  131. data = {
  132. 'name': c['name'],
  133. 'cons_type': ', '.join(c_args + ['const char*']),
  134. "cons_proto": get_args_proto(c_args, ["const char *name=NULL"]),
  135. "cons_call": get_args_call(c_args)
  136. }
  137. c_name = "Rpc" + c['name']
  138. methods = []
  139. rpc_methods = []
  140. for r, m, a in c['methods']:
  141. ret_proto = r if r else "void"
  142. args_proto = "void"
  143. ret_defin = "return " if r else ""
  144. args_defin = ""
  145. if a:
  146. args_proto = get_args_proto(a)
  147. args_defin = get_args_call(a)
  148. proto = "%s %s(%s)" % (ret_proto, m, args_proto)
  149. defin = "{%so.%s(%s);}" % (ret_defin, m, args_defin)
  150. methods.append("%s %s" % (proto, defin))
  151. rpc_method_type = [r] if r else []
  152. rpc_method_type.append(c_name)
  153. rpc_method_type.extend(a)
  154. rpc_methods.append('{"%s", rpc_method_caller<%s, &%s::%s>}' % (m, ', '.join(rpc_method_type), c_name, m))
  155. data['methods'] = "\n ".join(methods)
  156. data['rpc_methods'] = ",\n ".join(rpc_methods)
  157. class_decl = class_template.render(data)
  158. if 'required' in c:
  159. class_decl = "#if DEVICE_%s\n%s\n#endif" % (c['required'], class_decl)
  160. classes.append(class_decl)
  161. write_rpc_classes('\n\n'.join(classes))