view dmx_usb_module/dmx.pyx @ 1946:a362b892cb43

more makefile hacks to turn debug.js into an ES6 module Ignore-this: 79b9757e462e9007ae4c2c5eaaf9d917 (can't wait for the js build fairy to come clean all this up)
author Drew Perttula <drewp@bigasterisk.com>
date Wed, 05 Jun 2019 06:00:40 +0000
parents 295e803f6a5e
children
line wrap: on
line source

cdef extern from "fcntl.h":
    int open(char *pathname, int flags)
    int O_WRONLY

cdef extern from "unistd.h":
    int write(int fd, void *buf, int count)

cdef extern from "string.h":
    char *strncpy(char *dest, char *src, int n)

cdef extern from "Python.h":
    char* PyString_AsString(object string)

cdef class Dmx:
    cdef int fd
    def __cinit__(self, port="/dev/dmx0"):
        self.fd = open(port, O_WRONLY)
        if self.fd < 0:
            raise OSError("open failed")

    def write(self, buf):
        cdef char *cbuf
        cbuf = PyString_AsString(buf)
        if cbuf == NULL:
            raise ValueError("string buffer conversion failed")
        res = write(self.fd, cbuf, 513)
        if res < 0:
            raise OSError("write failed")