Exemple #1
0
def read_output():
    """Returns the values of the output pins"""
    global rpi_emulator
    data = __read_pins(rpi_emulator.emu_screen.output_pins)

    global pfio_connect
    if pfio_connect:
        data |= pfio.read_output()

    return data
Exemple #2
0
def start_pfio_server(callback=None, verbose=False, port=DEFAULT_PORT):
    """Starts listening for pfio packets over the network"""
    pfio.init()
    try:
        # this returns the loopback ip on the RPi :-(
        #hostname = socket.gethostname()

        ###################################################
        # this is pretty hacky, if anyone can find a better
        # solution, then please change this!
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80)) # try to connect to google's dns
        hostname = s.getsockname()[0] # get this device's hostname
        s.close()
        # blergh, nasty stuff
        ###################################################

        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((hostname, port))

    except socket.error as e:
        print "There was an error setting up the server socket!"
        print e
        return

    else:
        if verbose:
            print "Listening at %s on port %d" % (hostname, port)

    while True:
        # get the packet
        packet, sender = sock.recvfrom(BUFFER_SIZE)
        if verbose:
            print "Recieved packet from", sender
        # make it something sensible
        packet = PfionPacket().from_network(packet)

        if packet.command == WRITE_OUT_CMD:
            pfio.write_output(packet.bit_pattern)
            p = PfionPacket(WRITE_OUT_ACK)
            sock.sendto(p.for_network(), sender)

        elif packet.command == READ_OUT_CMD:
            output_bitp = pfio.read_output()
            p = PfionPacket(READ_OUT_ACK)
            p.bit_pattern = output_bitp
            sock.sendto(p.for_network(), sender)

        elif packet.command == READ_IN_CMD:
            input_bitp = pfio.read_input()
            p = PfionPacket(READ_IN_ACK)
            p.bit_pattern = input_bitp
            sock.sendto(p.for_network(), sender)

        elif packet.command == DIGITAL_WRITE_CMD:
            pfio.digital_write(packet.pin_number, packet.pin_value)
            p = PfionPacket(DIGITAL_WRITE_ACK)
            sock.sendto(p.for_network(), sender)

        elif packet.command ==  DIGITAL_READ_CMD:
            pin_value = pfio.digital_read(packet.pin_number)
            p = PfionPacket(DIGITAL_READ_ACK)
            p.pin_number = packet.pin_number
            p.pin_value  = pin_value
            sock.sendto(p.for_network(), sender)

        elif callback != None:
            callback(packet, sender)

        elif verbose:
            print "Unkown packet command (%d). Ignoring." % packet.command
Exemple #3
0
def start_pfio_server(callback=None, verbose=False, port=DEFAULT_PORT):
    """Starts listening for pfio packets over the network"""
    pfio.init()
    try:
        # this returns the loopback ip on the RPi :-(
        #hostname = socket.gethostname()

        ###################################################
        # this is pretty hacky, if anyone can find a better
        # solution, then please change this!
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))  # try to connect to google's dns
        hostname = s.getsockname()[0]  # get this device's hostname
        s.close()
        # blergh, nasty stuff
        ###################################################

        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((hostname, port))

    except socket.error as e:
        print "There was an error setting up the server socket!"
        print e
        return

    else:
        if verbose:
            print "Listening at %s on port %d" % (hostname, port)

    while True:
        # get the packet
        packet, sender = sock.recvfrom(BUFFER_SIZE)
        if verbose:
            print "Recieved packet from", sender
        # make it something sensible
        packet = PfionPacket().from_network(packet)

        if packet.command == WRITE_OUT_CMD:
            pfio.write_output(packet.bit_pattern)
            p = PfionPacket(WRITE_OUT_ACK)
            sock.sendto(p.for_network(), sender)

        elif packet.command == READ_OUT_CMD:
            output_bitp = pfio.read_output()
            p = PfionPacket(READ_OUT_ACK)
            p.bit_pattern = output_bitp
            sock.sendto(p.for_network(), sender)

        elif packet.command == READ_IN_CMD:
            input_bitp = pfio.read_input()
            p = PfionPacket(READ_IN_ACK)
            p.bit_pattern = input_bitp
            sock.sendto(p.for_network(), sender)

        elif packet.command == DIGITAL_WRITE_CMD:
            pfio.digital_write(packet.pin_number, packet.pin_value)
            p = PfionPacket(DIGITAL_WRITE_ACK)
            sock.sendto(p.for_network(), sender)

        elif packet.command == DIGITAL_READ_CMD:
            pin_value = pfio.digital_read(packet.pin_number)
            p = PfionPacket(DIGITAL_READ_ACK)
            p.pin_number = packet.pin_number
            p.pin_value = pin_value
            sock.sendto(p.for_network(), sender)

        elif callback != None:
            callback(packet, sender)

        elif verbose:
            print "Unkown packet command (%d). Ignoring." % packet.command