我在python中创建了一个gui,它允许arduino控制的mecanum轮车四处移动。
手推车允许8种不同的移动方向,可以左右旋转。命令通过膝上型计算机(w10、python)和arduino之间的串行连接发送。
我在python中有一个Enum类,代表不同的移动方向。
我在arduino中有一个相应的枚举来解释python任务中的命令。
对于这两种编码环境,共享一个公共枚举定义的简单方法是什么?
发布于 2020-11-28 22:00:43
不是的。C/C++中的Enum和python中的enum.Enum是两种截然不同的东西。然而,有一个强有力的解决办法。我建议从您的python代码编写C/C++/Arduino头。使用python强大的内省功能,可以轻松地使用__dict__
扫描python/class,并编写一个Arduino代码可以使用的.h
文件。这就是我如何生成与相关python项目中的枚举和常量相匹配的Verilog和SystemVerilog头。运行python应用程序将生成一个新的头文件,始终保持同步。
编辑:一个更明确的例子
我为基于FPGA的微处理器建立了一个汇编程序。汇编程序在python中,而处理器是用Verilog编写的。所以我创建了一个Verilog头文件,如下所示:
# Compute the 'after' content.
content = '// opcodes.vh\n'
content += '`ifndef _opcodes_vh\n'
content += '`define _opcodes_vh\n'
content += '// DO NOT EDIT -- FILE GENERATED BY ASSEMBLER\n'
content += '// ------------------------------------\n'
content += '// OPCODES\n'
content += '// ------------------------------------\n'
A = Arch
for i in A.__dict__:
if i.startswith('OPC_'):
o = i.replace('OPC_', 'OPCODE_')
s = '`define ' + o
while len(s) < 40:
s = s + ' '
hexval = str(hex(A.__dict__[i])).replace('0x', '')
decval = str(A.__dict__[i])
s = s + "7'h" + hexval + '\t\t// ' + str(decval) + '\n'
content += s
content += '// END OF GENERATED FILE.\n'
content += '`endif'
# Write to very specific location for Vivado to see it.
file = open(self.opcodes_filename, 'w', encoding='utf-8')
file.write(content)
file.close()
最后的输出如下:
// opcodes.vh
`ifndef _opcodes_vh
`define _opcodes_vh
// DO NOT EDIT -- FILE GENERATED BY ASSEMBLER
// ------------------------------------
// OPCODES
// ------------------------------------
`define OPCODE_LD_GPR_EXPR 7'h0 // 0
`define OPCODE_LD_GPR_GPTR 7'h1 // 1
`define OPCODE_SV_EXPR_GPR 7'h2 // 2
...
`define OPCODE_IO_T 7'h4a // 74
`define OPCODE_TX_T 7'h4b // 75
// END OF GENERATED FILE.
`endif
https://stackoverflow.com/questions/65048495
复制相似问题