Exemplo n.º 1
0
def write(*args):
    
    pol = (MODE >> 1) & 1
    pha = MODE & 1 
    
    def SendByte(byte):
        for i in range(8):
            
            if pha == 0:
                if byte & 0x80:
                    if pol == 0:
                        GPIO.output(MOSI, HIGH)
                    else:
                        GPIO.output(MOSI, LOW)
                else:
                    if pol == 0:
                        GPIO.output(MOSI, LOW)
                    else:
                        GPIO.output(MOSI, HIGH)
            
            time.sleep(0.000001)
            if pol == 0:
                GPIO.output(SCK, HIGH)
            else:
                GPIO.output(SCK, LOW)
            if pha == 1:
                if byte & 0x80:
                    if pol == 0:
                        GPIO.output(MOSI, HIGH)
                    else:
                        GPIO.output(MOSI, LOW)
                else:
                    if pol == 0:
                        GPIO.output(MOSI, LOW)
                    else:
                        GPIO.output(MOSI, HIGH)
            time.sleep(0.000001)
            if pol == 0:
                GPIO.output(SCK, LOW)
            else:
                GPIO.output(SCK, HIGH)
        
            byte <<= 1
        
    
   
    
    GPIO.output(CS, LOW)
    time.sleep(0.000001)      
            
    
    for i in range(len(args)):
        
        SendByte(args[i])
        
    time.sleep(0.000001)
    GPIO.output(CS, HIGH)
        
    return
Exemplo n.º 2
0
def main():

    GPIO.init()
    GPIO.setcfg(GPIO.PIN37, GPIO.OUTPUT)

    while True:
        GPIO.output(GPIO.PIN37, GPIO.HIGH)
        time.sleep(1)
        GPIO.output(GPIO.PIN37, GPIO.LOW)
        time.sleep(1)

    return 0
Exemplo n.º 3
0
def main():
	
    GPIO.init()
    GPIO.setcfg(GPIO.PIN37, GPIO.OUTPUT)
    
    while True:
        GPIO.output(GPIO.PIN37, GPIO.HIGH)
        time.sleep(1)
        GPIO.output(GPIO.PIN37, GPIO.LOW)
        time.sleep(1)
    
    return 0
Exemplo n.º 4
0
 def ReadByte():
     byte = 0
     
     for i in range(8):
         
         time.sleep(0.000001)
         if pol == 0:
             GPIO.output(SCK, HIGH)
         else:
             GPIO.output(SCK, LOW)
         
         if pha == 0:
             if GPIO.input(MISO) == 1:
                 if pol == 0:
                     byte |= 1
                 else:
                     byte |= 0
             else:
                 if pol == 0:
                     byte |= 0
                 else:
                     byte |= 1
                     
             if i != 7:
                 byte <<= 1
         
         time.sleep(0.000001)
         GPIO.output(SCK, LOW)
         
         if pha == 1:
             if GPIO.input(MISO) == 1:
                 if pol == 0:
                     byte |= 1
                 else:
                     byte |= 0
             else:
                 if pol == 0:
                     byte |= 0
                 else:
                     byte |= 1
                     
             if i != 7:
                 byte <<= 1
         
     return byte;
Exemplo n.º 5
0
def main():

    usage = "usage: %prog [options] arg1 ...."
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="Print extra messages")
    parser.add_option("--host",
                      type="str",
                      dest="host",
                      default="127.0.0.1",
                      help="Scratch host")
    parser.add_option("--port",
                      type="int",
                      dest="port",
                      default=42001,
                      help="Scratch port")

    #parse options
    (options, args) = parser.parse_args()

    #check verbose mode
    DEBUG = parser.values.verbose

    #get HOST_IP
    HOST = parser.values.host

    #get HOST_PORT
    PORT = parser.values.port

    if (DEBUG):
        print("Initializating GPIOs..."),
    try:
        gpio.init()

        #Configure inputs
        gpio.setcfg(gpio.PIN9, gpio.INP)  #input 1
        gpio.setcfg(gpio.PIN8, gpio.INP)  #input 2
        gpio.setcfg(gpio.PIN7, gpio.INP)  #input 3
        gpio.setcfg(gpio.PIN6, gpio.INP)  #input 4

        #Configure outputs
        gpio.setcfg(gpio.PIN29, gpio.OUT)  #output 1
        gpio.setcfg(gpio.PIN30, gpio.OUT)  #output 2
        gpio.setcfg(gpio.PIN31, gpio.OUT)  #output 3
        gpio.setcfg(gpio.PIN32, gpio.OUT)  #output 4
        gpio.setcfg(gpio.PIN33, gpio.OUT)  #output 5
        gpio.setcfg(gpio.PIN34, gpio.OUT)  #output 6
        gpio.setcfg(gpio.PIN35, gpio.OUT)  #output 7
        gpio.setcfg(gpio.PIN36, gpio.OUT)  #output 8
        gpio.setcfg(gpio.PIN37, gpio.OUT)  #output 9
        gpio.setcfg(gpio.PIN38, gpio.OUT)  #output 10
        gpio.setcfg(gpio.PIN39, gpio.OUT)  #output 11

        #Set all outputs LOW
        gpio.output(gpio.PIN29, gpio.LOW)
        gpio.output(gpio.PIN30, gpio.LOW)
        gpio.output(gpio.PIN31, gpio.LOW)
        gpio.output(gpio.PIN32, gpio.LOW)
        gpio.output(gpio.PIN33, gpio.LOW)
        gpio.output(gpio.PIN34, gpio.LOW)
        gpio.output(gpio.PIN35, gpio.LOW)
        gpio.output(gpio.PIN36, gpio.LOW)
        gpio.output(gpio.PIN37, gpio.LOW)
        gpio.output(gpio.PIN38, gpio.LOW)
        gpio.output(gpio.PIN39, gpio.LOW)

        if (DEBUG):
            print("Done")
    except Exception:
        if (DEBUG):
            print("Fail")
        else:
            print("Failed to initialize ports")
        sys.exit()

    if (DEBUG):
        print("Connecting to SCRATCH @ [" + str(HOST) + ":" + str(PORT) +
              "]..."),
    try:
        s = scratch.Scratch(host=str(HOST), port=PORT)
        if (DEBUG):
            print("Done")
    except Exception:
        if (DEBUG):
            print("Fail")
        else:
            print("Failed to connect to SCRATCH.")
        sys.exit()

    for msg in listen(s):
        if (DEBUG):
            print("New message received: " + str(msg))
        if msg[0] == 'broadcast':
            if msg[1] == 'INPUT1':
                i = gpio.input(gpio.PIN9)
                s.sensorupdate({'INPUT1': i})
            elif msg[1] == 'INPUT2':
                i = gpio.input(gpio.PIN8)
                s.sensorupdate({'INPUT2': i})
            elif msg[1] == 'INPUT3':
                i = gpio.input(gpio.PIN7)
                s.sensorupdate({'INPUT3': i})
            elif msg[1] == 'INPUT4':
                i = gpio.input(gpio.PIN6)
                s.sensorupdate({'INPUT4': i})

        #Handle outputs
        elif msg[0] == 'sensor-update':

            g = msg[1]
            keys = g.viewkeys()
            for key in keys:
                #         if(DEBUG):
                #            print("Key: " + str(key) + "\tValue: " + str(g[key]))

                #Catch message for OUTPUT1
                if key == 'OUTPUT1':
                    pin = gpio.PIN29
                elif key == 'OUTPUT2':
                    pin = gpio.PIN30
                elif key == 'OUTPUT3':
                    pin = gpio.PIN31
                elif key == 'OUTPUT4':
                    pin = gpio.PIN32
                elif key == 'OUTPUT5':
                    pin = gpio.PIN33
                elif key == 'OUTPUT6':
                    pin = gpio.PIN34
                elif key == 'OUTPUT7':
                    pin = gpio.PIN35
                elif key == 'OUTPUT8':
                    pin = gpio.PIN36
                elif key == 'OUTPUT9':
                    pin = gpio.PIN37
                elif key == 'OUTPUT10':
                    pin = gpio.PIN38
                elif key == 'OUTPUT11':
                    pin = gpio.PIN39

                #toggle GPIO
                if g[key] == 1:
                    if (DEBUG):
                        print(str(key) + ": HIGH..."),
                    try:
                        gpio.output(pin, gpio.HIGH)
                        if (DEBUG):
                            print("Done")
                    except Exception:
                        if (DEBUG):
                            print("Fail")
                        else:
                            print("Failed to set " + str(key))
                elif g[key] == 0:
                    if (DEBUG):
                        print(str(key) + ": LOW..."),
                    try:
                        gpio.output(pin, gpio.LOW)
                        if (DEBUG):
                            print("Done")
                    except Exception:
                        if (DEBUG):
                            print("Fail")
                        else:
                            print("Failed to clear " + str(key))
                else:
                    print(str(key) + ": Invalid output state!")

    return 0
Exemplo n.º 6
0
 def SendByte(byte):
     for i in range(8):
         
         if pha == 0:
             if byte & 1:
                 if pol == 0:
                     GPIO.output(MOSI, HIGH)
                 else:
                     GPIO.output(MOSI, LOW)
             else:
                 if pol == 0:
                     GPIO.output(MOSI, LOW)
                 else:
                     GPIO.output(MOSI, HIGH)
         
         time.sleep(0.000001)
         if pol == 0:
             GPIO.output(SCK, HIGH)
         else:
             GPIO.output(SCK, LOW)
         if pha == 1:
             if byte & 1:
                 if pol == 0:
                     GPIO.output(MOSI, HIGH)
                 else:
                     GPIO.output(MOSI, LOW)
             else:
                 if pol == 0:
                     GPIO.output(MOSI, LOW)
                 else:
                     GPIO.output(MOSI, HIGH)
         time.sleep(0.000001)
         if pol == 0:
             GPIO.output(SCK, LOW)
         else:
             GPIO.output(SCK, HIGH)
     
         byte >>= 1
Exemplo n.º 7
0
def read(address, n):
    
    pol = (MODE >> 1) & 1
    pha = MODE & 1 
    
    def SendByte(byte):
        for i in range(8):
            
            if pha == 0:
                if byte & 1:
                    if pol == 0:
                        GPIO.output(MOSI, HIGH)
                    else:
                        GPIO.output(MOSI, LOW)
                else:
                    if pol == 0:
                        GPIO.output(MOSI, LOW)
                    else:
                        GPIO.output(MOSI, HIGH)
            
            time.sleep(0.000001)
            if pol == 0:
                GPIO.output(SCK, HIGH)
            else:
                GPIO.output(SCK, LOW)
            if pha == 1:
                if byte & 1:
                    if pol == 0:
                        GPIO.output(MOSI, HIGH)
                    else:
                        GPIO.output(MOSI, LOW)
                else:
                    if pol == 0:
                        GPIO.output(MOSI, LOW)
                    else:
                        GPIO.output(MOSI, HIGH)
            time.sleep(0.000001)
            if pol == 0:
                GPIO.output(SCK, LOW)
            else:
                GPIO.output(SCK, HIGH)
        
            byte >>= 1
            
    def ReadByte():
        byte = 0
        
        for i in range(8):
            
            time.sleep(0.000001)
            if pol == 0:
                GPIO.output(SCK, HIGH)
            else:
                GPIO.output(SCK, LOW)
            
            if pha == 0:
                if GPIO.input(MISO) == 1:
                    if pol == 0:
                        byte |= 1
                    else:
                        byte |= 0
                else:
                    if pol == 0:
                        byte |= 0
                    else:
                        byte |= 1
                        
                if i != 7:
                    byte <<= 1
            
            time.sleep(0.000001)
            GPIO.output(SCK, LOW)
            
            if pha == 1:
                if GPIO.input(MISO) == 1:
                    if pol == 0:
                        byte |= 1
                    else:
                        byte |= 0
                else:
                    if pol == 0:
                        byte |= 0
                    else:
                        byte |= 1
                        
                if i != 7:
                    byte <<= 1
            
        return byte;
        
        
    GPIO.output(CS, LOW)
    time.sleep(0.000001)      
            
    SendByte(address)
    args = []
    for i in range(n):
        
        args.append(ReadByte())
        
    time.sleep(0.000001)
    GPIO.output(CS, HIGH)    
    
    return args
Exemplo n.º 8
0
def init(mode):
    
    print ("Selected mode: " + str(mode))
    
    if mode < 0 or mode > 3:
        raise ModeError(mode);        
    
    global MODE
    MODE = mode
    
    GPIO.init()
  
    
    GPIO.setcfg(MOSI, OUT)
    GPIO.setcfg(MISO, IN)
    GPIO.setcfg(SCK, OUT)
    GPIO.setcfg(CS, OUT)
    
    if mode == 0 or mode == 1:
        GPIO.output(CS, HIGH)
        GPIO.output(SCK, LOW)
        GPIO.output(MOSI, LOW)
    
    else:
        GPIO.output(CS, HIGH)
        GPIO.output(SCK, HIGH)
        GPIO.output(MOSI, HIGH)
        
    return
Exemplo n.º 9
0
def main():
        
    usage = "usage: %prog [options] arg1 ...."
    parser = optparse.OptionParser(usage = usage)
    parser.add_option("-v", "--verbose", action="store_true",
                        dest="verbose", default=False,
                        help="Print extra messages")
    parser.add_option("--host", type="str", dest="host",
                        default="127.0.0.1",
                        help="Scratch host")
    parser.add_option("--port", type="int", dest="port",
                        default=42001,
                        help="Scratch port")
    
    #parse options
    (options, args) = parser.parse_args()
    
    #check verbose mode
    DEBUG = parser.values.verbose
    
    #get HOST_IP
    HOST = parser.values.host
    
    #get HOST_PORT
    PORT = parser.values.port
    
    if(DEBUG):
        print("Initializating GPIOs..."),
    try:
        gpio.init()
        
        #Configure inputs
        gpio.setcfg(gpio.PIN9, gpio.INP)    #input 1
        gpio.setcfg(gpio.PIN8, gpio.INP)    #input 2
        gpio.setcfg(gpio.PIN7, gpio.INP)    #input 3
        gpio.setcfg(gpio.PIN6, gpio.INP)    #input 4
        
        #Configure outputs
        gpio.setcfg(gpio.PIN29, gpio.OUT)   #output 1
        gpio.setcfg(gpio.PIN30, gpio.OUT)   #output 2
        gpio.setcfg(gpio.PIN31, gpio.OUT)   #output 3
        gpio.setcfg(gpio.PIN32, gpio.OUT)   #output 4
        gpio.setcfg(gpio.PIN33, gpio.OUT)   #output 5
        gpio.setcfg(gpio.PIN34, gpio.OUT)   #output 6
        gpio.setcfg(gpio.PIN35, gpio.OUT)   #output 7
        gpio.setcfg(gpio.PIN36, gpio.OUT)   #output 8
        gpio.setcfg(gpio.PIN37, gpio.OUT)   #output 9
        gpio.setcfg(gpio.PIN38, gpio.OUT)   #output 10
        gpio.setcfg(gpio.PIN39, gpio.OUT)   #output 11
        
        #Set all outputs LOW
        gpio.output(gpio.PIN29, gpio.LOW)
        gpio.output(gpio.PIN30, gpio.LOW)
        gpio.output(gpio.PIN31, gpio.LOW)
        gpio.output(gpio.PIN32, gpio.LOW)
        gpio.output(gpio.PIN33, gpio.LOW)
        gpio.output(gpio.PIN34, gpio.LOW)
        gpio.output(gpio.PIN35, gpio.LOW)
        gpio.output(gpio.PIN36, gpio.LOW)
        gpio.output(gpio.PIN37, gpio.LOW)
        gpio.output(gpio.PIN38, gpio.LOW)
        gpio.output(gpio.PIN39, gpio.LOW)
        
        if(DEBUG):
            print("Done")
    except Exception:
        if(DEBUG):
            print("Fail")
        else:
            print("Failed to initialize ports")
        sys.exit()
    
    
    if(DEBUG):
        print("Connecting to SCRATCH @ [" + str(HOST) + ":" + str(PORT) + "]..."),
    try:
        s = scratch.Scratch(host=str(HOST), port=PORT)
        if(DEBUG):
            print("Done")
    except Exception:
        if(DEBUG):
            print("Fail")
        else:
            print("Failed to connect to SCRATCH.")
        sys.exit()
        
        
                    
    for msg in listen(s):
        if(DEBUG):
            print("New message received: " + str(msg))
        if msg[0] == 'broadcast':
            if msg[1] == 'INPUT1':
                i = gpio.input(gpio.PIN9)
                s.sensorupdate({'INPUT1': i});
            elif msg[1] == 'INPUT2':
                i = gpio.input(gpio.PIN8)
                s.sensorupdate({'INPUT2': i});
            elif msg[1] == 'INPUT3':
                i = gpio.input(gpio.PIN7)
                s.sensorupdate({'INPUT3': i});
            elif msg[1] == 'INPUT4':
                i = gpio.input(gpio.PIN6)
                s.sensorupdate({'INPUT4': i});
        
        
        #Handle outputs
        elif msg[0] == 'sensor-update':
            
            g = msg[1]
            keys = g.viewkeys()
            for key in keys:
       #         if(DEBUG):
       #            print("Key: " + str(key) + "\tValue: " + str(g[key]))
                    
                #Catch message for OUTPUT1
                if key == 'OUTPUT1':
                    pin = gpio.PIN29
                elif key == 'OUTPUT2':
                    pin = gpio.PIN30
                elif key == 'OUTPUT3':
                    pin = gpio.PIN31
                elif key == 'OUTPUT4':
                    pin = gpio.PIN32
                elif key == 'OUTPUT5':
                    pin = gpio.PIN33
                elif key == 'OUTPUT6':
                    pin = gpio.PIN34
                elif key == 'OUTPUT7':
                    pin = gpio.PIN35
                elif key == 'OUTPUT8':
                    pin = gpio.PIN36
                elif key == 'OUTPUT9':
                    pin = gpio.PIN37
                elif key == 'OUTPUT10':
                    pin = gpio.PIN38
                elif key == 'OUTPUT11':
                    pin = gpio.PIN39
                    
                #toggle GPIO
                if g[key] == 1:
                    if(DEBUG):
                        print(str(key) + ": HIGH..."),
                    try:
                        gpio.output(pin, gpio.HIGH)
                        if(DEBUG):
                            print("Done");
                    except Exception:
                        if(DEBUG):
                            print("Fail")
                        else:
                            print("Failed to set " + str(key))
                elif g[key] == 0:
                    if(DEBUG):
                        print(str(key) + ": LOW..."),
                    try:
                        gpio.output(pin, gpio.LOW)
                        if(DEBUG):
                            print("Done");
                    except Exception:
                        if(DEBUG):
                            print("Fail")
                        else:
                            print("Failed to clear " + str(key))
                else:
                        print(str(key) + ": Invalid output state!")
   
    
             
    
    return 0