在python和arduino之间共享枚举 [英] share enum between python and arduino
问题描述
我在python中创建了一个gui,允许arduino控制的麦克纳姆轮推车四处移动.
I have created a gui in python that allows an arduino controlled mecanum wheel cart to move around.
手推车允许8个不同的移动方向,并且可以左右旋转.命令是通过笔记本电脑(w10,python)和arduino之间的串行连接发送的.
The cart allows for 8 different move directions and can rotate left and right. Commands are sent over a serial connection between the laptop(w10, python) and the arduino.
我在python中有一个Enum类,表示不同的移动方向.
I have an Enum class in python representing the different move directions.
我在arduino中有一个对应的枚举,用于解释python任务中的命令.
I have a corresponding enum in the arduino to interprete the commands from the python task.
在两种编码环境中共享一个通用枚举定义的简便方法是什么?
What is an easy way to share a single common enum definition for both coding environments?
推荐答案
否.C/C ++中的枚举和python中的枚举是两个非常不同的东西.但是,有一个强大的解决方案.我建议从您的python代码中编写C/C ++/Arduino标头 .借助python强大的自省功能,可以轻松地使用 __ dict __
扫描python枚举/类,并编写Arduino代码可以使用的 .h
文件.这就是我生成与相关python项目中的枚举和常量匹配的Verilog和SystemVerilog标头的方式.运行python应用程序会生成一个始终同步的新头文件.
No. Enums in C/C++ and enum.Enum in python are two very different things. However, there is a powerful solution. I suggest writing the C/C++/Arduino header from your python code. With python's powerful introspection, it's easy to scan through your python enum/class with __dict__
and write a .h
file that your Arduino code can use. This is how I generate Verilog and SystemVerilog headers that match the enums and constants in related python projects. Running the python applications produces a new header file, always in sync.
更明确的示例
我已经为基于FPGA的微处理器构建了一个汇编器.汇编器使用python编写,而处理器使用Verilog编程.因此,我创建了这样的Verilog头文件:
I've built an assembler for an FPGA-based microprocessor. The assembler is in python, while the processor is programmed in Verilog. So I create a Verilog header file like this:
# 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
这篇关于在python和arduino之间共享枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!