Пример #1
0
    def update(self, data):
        try:
            self._fixData(data)
            data = self._buf
            s = self._connect()

            universes = int(self.bufByteCount / self._universe_boundary) + 1
            countlast = self.bufByteCount % self._universe_boundary

            countboundary = self._universe_boundary * (universes - 1)
            udata = data[countboundary:countboundary + countlast]
            packet = E131Packet(universe=self._universe + universes - 1,
                                data=udata)
            s.sendto(packet.packet_data, (self._host, self._port))
            universes -= 1
            while universes > 0:
                countboundary = self._universe_boundary * (universes - 1)
                udata = data[countboundary:countboundary +
                             self._universe_boundary]
                packet = E131Packet(universe=self._universe + universes - 1,
                                    data=udata)
                s.sendto(packet.packet_data, (self._host, self._port))
                universes -= 1


#            packet = E131Packet(universe=self._universe, data=data)
#            s.sendto(packet.packet_data, (self._host, self._port))

            s.close()

        except Exception as e:
            log.exception(e)
            error = "Problem communicating with network receiver!"
            log.error(error)
            raise IOError(error)
Пример #2
0
 def getDeviceID(dev):
     packet = DriverSerial._generateHeader(CMDTYPE.GETID, 0)
     try:
         com = serial.Serial(dev,
                             baudrate=DriverSerial.baud_rate,
                             timeout=5)
         com.write(packet)
         resp = ord(com.read(1))
         return resp
     except serial.SerialException:
         log.error("Problem connecting to serial device.")
         return -1
Пример #3
0
 def getDeviceVer(dev):
     packet = DriverSerial._generateHeader(CMDTYPE.GETVER, 0)
     try:
         com = serial.Serial(dev,
                             baudrate=DriverSerial.baud_rate,
                             timeout=0.5)
         com.write(packet)
         ver = 0
         resp = com.read(1)
         if len(resp) > 0:
             resp = ord(resp)
             if resp == RETURN_CODES.SUCCESS:
                 ver = ord(com.read(1))
         return ver
     except serial.SerialException:
         log.error("Problem connecting to serial device.")
         return 0
Пример #4
0
    def _printError(error):
        fatal = True
        msg = "Unknown error occured."
        if error == RETURN_CODES.ERROR_SIZE:
            msg = "Data packet size incorrect."
            fatal = False
        elif error == RETURN_CODES.ERROR_UNSUPPORTED:
            msg = "Unsupported configuration attempted."
        elif error == RETURN_CODES.ERROR_PIXEL_COUNT:
            msg = "Too many pixels specified for device."
        elif error == RETURN_CODES.ERROR_BAD_CMD:
            msg = "Unsupported protocol command. Check your device version."

        if not fatal:
            log.info("%s: %s", error, msg)
            return

        log.error("%s: %s", error, msg)
        raise BiblioSerialError(msg)
Пример #5
0
    def _connect(self):
        try:
            self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

            if self._broadcast:
                # self._sock.bind((self._broadcast_interface, self._port))
                self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,
                                      1)

            ttl = struct.pack('b', 1)
            self._sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL,
                                  ttl)

            self._sock.settimeout(0.2)

            return self._sock
        except socket.gaierror:
            error = "Unable to connect to or resolve host: {}".format(
                self._host)
            log.error(error)
            raise IOError(error)
Пример #6
0
    def setDeviceID(dev, id):
        if id < 0 or id > 255:
            raise ValueError("ID must be an unsigned byte!")

        try:
            com = serial.Serial(dev,
                                baudrate=DriverSerial.baud_rate,
                                timeout=5)

            packet = DriverSerial._generateHeader(CMDTYPE.SETID, 1)
            packet.append(id)
            com.write(packet)

            resp = com.read(1)
            if len(resp) == 0:
                DriverSerial._comError()
            else:
                if ord(resp) != RETURN_CODES.SUCCESS:
                    DriverSerial._printError(ord(resp))

        except serial.SerialException:
            log.error("Problem connecting to serial device.")
            raise IOError("Problem connecting to serial device.")
    def getKeys(self):
        bits = 0
        try:
            packet = SerialGamePad._generateHeader(CMDTYPE.GET_BTNS, 0)
            self._com.write(packet)
            resp = self._com.read(1)
            if len(resp) == 0:
                SerialGamePad._comError()
            elif ord(resp) != RETURN_CODES.SUCCESS:
                SerialGamePad._printError(ord(resp))
            resp = self._com.read(2)
            if len(resp) != 2:
                SerialGamePad._comError()

            bits = ord(resp[0]) + (ord(resp[1]) << 8)
        except IOError:
            log.error("IO Error Communicatng With Game Pad!")

        index = 0
        result = {}
        for m in self._map:
            result[m] = bits & (1 << index) > 0
            index += 1
        return d(result)
Пример #8
0
    def _connect(self):
        try:
            if self.dev == "" or self.dev is None:
                DriverSerial.findSerialDevices(self._hardwareID)

                if self.deviceID is not None:
                    if self.deviceID in DriverSerial.deviceIDS:
                        self.dev = DriverSerial.deviceIDS[self.deviceID]
                        self.devVer = 0
                        try:
                            i = DriverSerial.foundDevices.index(self.dev)
                            self.devVer = DriverSerial.deviceVers[i]
                        except:
                            pass
                        log.info(
                            "Using COM Port: %s, Device ID: %s, Device Ver: %s",
                            self.dev, self.deviceID, self.devVer)

                    if self.dev == "" or self.dev is None:
                        error = "Unable to find device with ID: {}".format(
                            self.deviceID)
                        log.error(error)
                        raise ValueError(error)
                elif len(DriverSerial.foundDevices) > 0:
                    self.dev = DriverSerial.foundDevices[0]
                    self.devVer = 0
                    try:
                        i = DriverSerial.foundDevices.index(self.dev)
                        self.devVer = DriverSerial.deviceVers[i]
                    except:
                        pass
                    devID = -1
                    for id in DriverSerial.deviceIDS:
                        if DriverSerial.deviceIDS[id] == self.dev:
                            devID = id

                    log.info(
                        "Using COM Port: %s, Device ID: %s, Device Ver: %s",
                        self.dev, devID, self.devVer)

            try:
                self._com = serial.Serial(self.dev,
                                          baudrate=DriverSerial.baud_rate,
                                          timeout=5)
            except serial.SerialException as e:
                ports = DriverSerial.findSerialDevices(self._hardwareID)
                error = "Invalid port specified. No COM ports available."
                if len(ports) > 0:
                    error = "Invalid port specified. Try using one of: \n" + \
                        "\n".join(ports)
                log.info(error)
                raise BiblioSerialError(error)

            packet = DriverSerial._generateHeader(CMDTYPE.SETUP_DATA, 4)
            packet.append(self._type)  # set strip type
            byteCount = self.bufByteCount
            if self._type in BufferChipsets:
                if self._type == LEDTYPE.APA102 and self.devVer >= 2:
                    pass
                else:
                    self._bufPad = BufferChipsets[self._type](self.numLEDs) * 3
                    byteCount += self._bufPad

            packet.append(byteCount & 0xFF)  # set 1st byte of byteCount
            packet.append(byteCount >> 8)  # set 2nd byte of byteCount
            packet.append(self._SPISpeed)
            self._com.write(packet)
            resp = self._com.read(1)

            if len(resp) == 0:
                # Hack I had to add for my Arduinos to correct IOError
                # self._com.write(packet)
                # resp = self._com.read(1)
                # if len(resp) == 0:
                DriverSerial._comError()

            return ord(resp)

        except serial.SerialException as e:
            error = "Unable to connect to the device. Please check that it is connected and the correct port is selected."
            log.error(traceback.format_exc())
            log.error(error)
            raise e
Пример #9
0
 def _comError():
     error = "There was an unknown error communicating with the device."
     log.error(error)
     raise IOError(error)
Пример #10
0
    SOFTWARE.
"""

from bibliopixel.drivers.driver_base import DriverBase, ChannelOrder
import sys
import time
import os
from bibliopixel import gamma, log
import traceback

try:
    import serial
    import serial.tools.list_ports
except ImportError as e:
    error = "Please install pyserial 2.7+! pip install pyserial"
    log.error(error)
    raise ImportError(error)

from distutils.version import LooseVersion

if LooseVersion(serial.VERSION) < LooseVersion('2.7'):
    error = "pyserial v{} found, please upgrade to v2.7+! pip install pyserial --upgrade".format(
        serial.VERSION)
    log.error(error)
    raise ImportError(error)


class BiblioSerialError(Exception):
    pass