Ver código fonte

fix periphery

blueloveTH 2 meses atrás
pai
commit
b4d035a912

+ 3 - 5
3rd/periphery/src/periphery.c

@@ -1644,11 +1644,9 @@ static bool cfunc__serial_set_parity(int argc, py_Ref argv) {
     serial_t* _0;
     if(!py_checkint(py_arg(0))) return false;
     _0 = (serial_t*)py_toint(py_arg(0));
-    enum serial_parity _1;
-    do {
-        if(!py_checktype(py_arg(1), tp_user_enum serial_parity)) return false;
-        _1 = *(enum serial_parity*)py_touserdata(py_arg(1));
-    } while(0);
+    serial_parity_t _1;
+    if(!py_checkint(py_arg(1))) return false;
+    _1 = (serial_parity_t)py_toint(py_arg(1));
     int res = serial_set_parity(_0, _1);
     py_newint(py_retval(), res);
     return true;

+ 1 - 1
ffigen/ffigen/meta/parser.py

@@ -33,7 +33,7 @@ class Header:
         self.type_aliases[k] = v
 
     def build_enum(self, node: c_ast.Enum, alias_name: str | None = None):
-        enum = Enum(node.name)
+        enum = Enum(node.name, alias_name)
         for item in node.values.enumerators:
             enum.values.append(item.name)
         self.types.append(enum)

+ 2 - 1
ffigen/ffigen/meta/schema.py

@@ -23,8 +23,9 @@ class Struct(NamedFields): pass
 class Union(NamedFields): pass
     
 class Enum:
-    def __init__(self, name: str):
+    def __init__(self, name: str, typedef_name: str | None = None):
         self.name = name
+        self.typedef_name = typedef_name
         self.values = [] # type: list[str]
 
     def __repr__(self):

+ 11 - 0
ffigen/gen_periphery.py

@@ -10,6 +10,17 @@ file_dir = os.path.dirname(os.path.abspath(__file__))
 path = '3rd/periphery/include/periphery.h'
 code = pcpp.CmdPreprocessor([None, path, '-o', 'tmp.h', '-I', os.path.join(file_dir, 'libc_include')])
 
+mapping = {
+    'enum serial_parity parity': 'serial_parity_t parity',
+}
+# remap tmp.h
+with open('tmp.h', 'r') as f:
+    content = f.read()
+for k, v in mapping.items():
+    content = content.replace(k, v)
+with open('tmp.h', 'w') as f:
+    f.write(content)
+
 ast = pycparser.parse_file('tmp.h')
 os.remove('tmp.h')
 

+ 2 - 2
include/typings/periphery.pyi

@@ -352,8 +352,8 @@ def serial_set_baudrate(serial: intptr, baudrate: int, /) -> int:
 def serial_set_databits(serial: intptr, databits: int, /) -> int:
     """Wraps `int serial_set_databits(serial_t* serial, unsigned databits)`"""
 
-def serial_set_parity(serial: intptr, parity: enum serial_parity, /) -> int:
-    """Wraps `int serial_set_parity(serial_t* serial, enum serial_parity parity)`"""
+def serial_set_parity(serial: intptr, parity: int, /) -> int:
+    """Wraps `int serial_set_parity(serial_t* serial, serial_parity_t parity)`"""
 
 def serial_set_stopbits(serial: intptr, stopbits: int, /) -> int:
     """Wraps `int serial_set_stopbits(serial_t* serial, unsigned stopbits)`"""