Beispiel #1
0
    def getJSON(self):
        json = "{"
        first = True
        for (alt, value) in FUNCTIONS.items():
            if not first:
                json += ", "
            json += '"%s": %d' % (alt, value["enabled"])
            first = False
        
        json += ', "GPIO":{\n'
        first = True
        for gpio in range(GPIO.GPIO_COUNT):
            if not first:
                json += ", \n"

            function = GPIO.getFunctionString(gpio)
            value = GPIO.input(gpio)
                    
            json += '"%d": {"function": "%s", "value": %d' % (gpio, function, value)
            if GPIO.getFunction(gpio) == GPIO.PWM:
                (type, value) = GPIO.getPulse(gpio).split(':')
                json  += ', "%s": %s' %  (type, value)
            json += '}'
            first = False
            
        json += "\n}}"
        return json
Beispiel #2
0
 def __portWrite__(self, value):
     if len(self.export) < 54:
         for i in self.export:
             if GPIO.getFunction(i) == GPIO.OUT:
                 GPIO.digitalWrite(i, (value >> i) & 1)
     else:
         raise Exception("Please limit exported GPIO to write integers")
Beispiel #3
0
 def close(self):
     for g in self.gpio_reset:
         gpio = g["gpio"]
         debug("Reset GPIO %d" % gpio)
         GPIO.setFunction(gpio, g["func"])
         if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
             GPIO.digitalWrite(gpio, g["value"])
Beispiel #4
0
 def close(self):
     for g in self.gpio_reset:
         gpio = g["gpio"]
         debug("Reset GPIO %d" % gpio)
         GPIO.setFunction(gpio, g["func"])
         if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
             GPIO.digitalWrite(gpio, g["value"])
    def getJSON(self):
        json = "{"
        first = True
        for (alt, value) in FUNCTIONS.items():
            if not first:
                json += ", "
            json += '"%s": %d' % (alt, value["enabled"])
            first = False
        
        json += ', "GPIO":{\n'
        first = True
        for gpio in range(GPIO.GPIO_COUNT):
            if not first:
                json += ", \n"

            function = GPIO.getFunctionString(gpio)
            value = GPIO.input(gpio)
                    
            json += '"%d": {"function": "%s", "value": %d' % (gpio, function, value)
            if GPIO.getFunction(gpio) == GPIO.PWM:
                (type, value) = GPIO.getPulse(gpio).split(':')
                json  += ', "%s": %s' %  (type, value)
            json += '}'
            first = False
            
        json += "\n}}"
        return json
Beispiel #6
0
 def getJSON(self, compact=False):
     if compact:
         f = 'f'
         v = 'v'
     else:
         f = 'function'
         v = 'value'
     
     json = {}
     for (bus, value) in BUSLIST.items():
         json[bus] = int(value["enabled"])
     
     gpios = {}
     if len(self.export) > 0:
         export = self.export
     else:
         export = range(GPIO.GPIO_COUNT)
 
     for gpio in export:
         gpios[gpio] = {}
         if compact:
             gpios[gpio][f] = GPIO.getFunction(gpio)
         else:
             gpios[gpio][f] = GPIO.getFunctionString(gpio)
         gpios[gpio][v] = int(GPIO.input(gpio))
 
         if GPIO.getFunction(gpio) == GPIO.PWM:
             (pwmType, value) = GPIO.getPulse(gpio).split(':')
             gpios[gpio][pwmType] = value
     
     json['GPIO'] = gpios
     return types.jsonDumps(json)
Beispiel #7
0
def GPIOSetup():
    for g in GPIO_SETUP:
        gpio = g["gpio"]
        debug("Setup GPIO %d" % gpio)
        GPIO.setFunction(gpio, g["func"])
        if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
            GPIO.output(gpio, g["value"])
Beispiel #8
0
 def __portWrite__(self, value):
     if len(self.export) < 54:
         for i in self.export:
             if GPIO.getFunction(i) == GPIO.OUT:
                 GPIO.digitalWrite(i, (value >> i) & 1)
     else:
         raise Exception("Please limit exported GPIO to write integers")
Beispiel #9
0
def GPIOReset():
    for g in GPIO_RESET:
        gpio = g["gpio"]
        debug("Reset GPIO %d" % gpio)
        GPIO.setFunction(gpio, g["func"])
        if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
            GPIO.output(gpio, g["value"])
Beispiel #10
0
 def setup(self):
     for g in self.gpio_setup:
         gpio = g["gpio"]
         debug("Setup GPIO %d" % gpio)
         GPIO.setFunction(gpio, g["func"])
         if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
             GPIO.digitalWrite(gpio, g["value"])
Beispiel #11
0
 def setup(self):
     for g in self.gpio_setup:
         gpio = g["gpio"]
         debug("Setup GPIO %d" % gpio)
         GPIO.setFunction(gpio, g["func"])
         if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
             GPIO.digitalWrite(gpio, g["value"])
Beispiel #12
0
def GPIOSetup():
    for g in GPIO_SETUP:
        gpio = g["gpio"]
        debug("Setup GPIO %d" % gpio)
        GPIO.setFunction(gpio, g["func"])
        if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
            GPIO.output(gpio, g["value"])
Beispiel #13
0
def GPIOReset():
    for g in GPIO_RESET:
        gpio = g["gpio"]
        debug("Reset GPIO %d" % gpio)
        GPIO.setFunction(gpio, g["func"])
        if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT:
            GPIO.output(gpio, g["value"])
Beispiel #14
0
    def getJSON(self, compact=False):
        if compact:
            f = 'f'
            v = 'v'
        else:
            f = 'function'
            v = 'value'

        json = {}
        for (bus, value) in BUSLIST.items():
            json[bus] = int(value["enabled"])

        gpios = {}
        if len(self.export) > 0:
            export = self.export
        else:
            export = range(GPIO.GPIO_COUNT)

        for gpio in export:
            gpios[gpio] = {}
            if compact:
                gpios[gpio][f] = GPIO.getFunction(gpio)
            else:
                gpios[gpio][f] = GPIO.getFunctionString(gpio)
            gpios[gpio][v] = int(GPIO.input(gpio))

            if GPIO.getFunction(gpio) == GPIO.PWM:
                (pwmType, value) = GPIO.getPulse(gpio).split(':')
                gpios[gpio][pwmType] = value

        json['GPIO'] = gpios
        return types.jsonDumps(json)
    def do_GET(self, relativePath):
        # JSON full state
        if relativePath == "*":
            return (200, self.getJSON(), M_JSON)
            
        # RPi header map
        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            json = json.replace("'", '"')
            return (200, json, M_JSON)

        # server version
        elif relativePath == "version":
            return (200, SERVER_VERSION, M_PLAIN)

        # board revision
        elif relativePath == "revision":
            revision = "%s" % GPIO.BOARD_REVISION
            return (200, revision, M_PLAIN)

         # get temperature
        elif relativePath == "TEMP":
            strPath="/home/pi/test/thermostate/lastTemp.txt"
            f = open(strPath)
            strText = f.read()
            return (200, strText, M_PLAIN)


        # Single GPIO getter
        elif relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if operation == "value":
                if GPIO.input(gpio):
                    value = "1"
                else:
                    value = "0"
    
            elif operation == "function":
                value = GPIO.getFunctionString(gpio)
    
            elif operation == "pwm":
                if GPIO.isPWMEnabled(gpio):
                    value = "enabled"
                else:
                    value = "disabled"
                
            elif operation == "pulse":
                value = GPIO.getPulse(gpio)
                
            else:
                return (404, operation + " Not Found", M_PLAIN)
                
            return (200, value, M_PLAIN)

        else:
            return (0, None, None)
Beispiel #16
0
 def outputSequence(self, channel, args):
     self.checkDigitalChannelExported(channel)
     self.checkPostingValueAllowed()
     self.checkDigitalChannel(channel)
     (period, sequence) = args.split(",")
     period = int(period)
     GPIO.outputSequence(channel, period, sequence)
     return int(sequence[-1])
Beispiel #17
0
 def outputSequence(self, channel, args):
     self.checkDigitalChannelExported(channel)
     self.checkPostingValueAllowed()
     self.checkDigitalChannel(channel)
     (period, sequence) = args.split(",")
     period = int(period)
     GPIO.outputSequence(channel, period, sequence)
     return int(sequence[-1])
    def do_GET(self, relativePath):
        # JSON full state
        if relativePath == "*":
            return (200, self.getJSON(), M_JSON)

        # RPi header map
        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            json = json.replace("'", '"')
            return (200, json, M_JSON)

        # server version
        elif relativePath == "version":
            return (200, SERVER_VERSION, M_PLAIN)

        # board revision
        elif relativePath == "revision":
            revision = "%s" % GPIO.BOARD_REVISION
            return (200, revision, M_PLAIN)

        # get temperature
        elif relativePath == "TEMP":
            strPath = "/home/pi/test/thermostate/lastTemp.txt"
            f = open(strPath)
            strText = f.read()
            return (200, strText, M_PLAIN)

        # Single GPIO getter
        elif relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if operation == "value":
                if GPIO.input(gpio):
                    value = "1"
                else:
                    value = "0"

            elif operation == "function":
                value = GPIO.getFunctionString(gpio)

            elif operation == "pwm":
                if GPIO.isPWMEnabled(gpio):
                    value = "enabled"
                else:
                    value = "disabled"

            elif operation == "pulse":
                value = GPIO.getPulse(gpio)

            else:
                return (404, operation + " Not Found", M_PLAIN)

            return (200, value, M_PLAIN)

        else:
            return (0, None, None)
Beispiel #19
0
 def setHWPWMoutput(self, channel, value):
     self.checkPostingValueAllowed()
     if value == "enable":
         GPIO.HWPWMenable(channel)
     elif value == "disable":
         GPIO.HWPWMdisable(channel)
     else:
         raise ValueError("Bad Function")
     return self.getHWPWMoutput(channel)
Beispiel #20
0
    def wildcard(self, compact=False):
        if compact:
            f = "f"
            v = "v"
        else:
            f = "function"
            v = "value"

        values = {}
        print(self.export)
        for i in self.export:
            if compact:
                func = GPIO.getFunction(i)
            else:
                func = GPIO.getFunctionString(i)
            values[i] = {f: func, v: int(GPIO.digitalRead(i))}
        return values
Beispiel #21
0
 def wildcard(self, compact=False):
     if compact:
         f = "f"
         v = "v"
     else:
         f = "function"
         v = "value"
         
     values = {}
     print(self.export)
     for i in self.export:
         if compact:
             func = GPIO.getFunction(i)
         else:
             func = GPIO.getFunctionString(i)
         values[i] = {f: func, v: int(GPIO.digitalRead(i))}
     return values
Beispiel #22
0
    def writeJSON(self, out):
        json = "{"
        first = True
        for (alt, value) in FUNCTIONS.items():
            if not first:
                json += ", "
            json += '"%s": %d' % (alt, value["enabled"])
            first = False
        
        json += ', "GPIO":{\n'
        first = True
        for gpio in range(GPIO.GPIO_COUNT):
            if not first:
                json += ", \n"

            function = GPIO.getFunctionString(gpio)
            value = GPIO.input(gpio)
                
            json += '"%d": {"function": "%s", "value": %d}' % (gpio, function, value)
            first = False
            
        json += "\n}}"
        out.write(json.encode())
Beispiel #23
0
 def setPullUpDnControl(self, channel, value):
     value = value.lower()
     if value == "up":
         GPIO.setPullUpDn(channel, GPIO.PUD_UP)
     elif value == "down":
         GPIO.setPullUpDn(channel, GPIO.PUD_DOWN)
     elif value == "off":
         GPIO.setPullUpDn(channel, GPIO.PUD_OFF)
     else:
         raise ValueError("Bad Pull up/down control")
     return "OK"
Beispiel #24
0
 def getHWPWMpolarity(self, channel):
     return GPIO.HWPWMgetPolarity(channel)
Beispiel #25
0
    def do_GET(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "/")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:]

        # JSON full state
        if relativePath == "*":
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.server.writeJSON(self.wfile)

        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.wfile.write(json.replace("'", '"').encode())

        # version
        elif relativePath == "version":
            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()
            self.wfile.write(SERVER_VERSION.encode())

        # Single GPIO getter
        elif (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if (operation == "value"):
                if GPIO.input(gpio):
                    value = "1"
                else:
                    value = "0"

            elif (operation == "function"):
                value = GPIO.getFunctionString(gpio)

            elif (operation == "pwm"):
                if GPIO.isPWMEnabled(gpio):
                    value = "enabled"
                else:
                    value = "disabled"

            elif (operation == "pulse"):
                value = GPIO.getPulse(gpio)

            else:
                self.send_error(404, operation + " Not Found")
                return

            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()
            self.wfile.write(value.encode())

        # handle files
        else:
            if relativePath == "":
                relativePath = self.server.index

            realPath = relativePath

            if not os.path.exists(realPath):
                realPath = self.server.docroot + os.sep + relativePath

            if not os.path.exists(realPath):
                self.send_error(404, "Not Found")
                return

            realPath = os.path.realpath(realPath)

            if realPath.endswith(".py"):
                self.send_error(403, "Not Authorized")
                return

            if not (realPath.startswith(self.server.docroot)
                    or realPath.startswith(os.getcwd())):
                self.send_error(403, "Not Authorized")
                return

            if (os.path.isdir(realPath)):
                realPath += os.sep + self.server.index
                if not os.path.exists(realPath):
                    self.send_error(403, "Not Authorized")
                    return

            (type, encoding) = mime.guess_type(realPath)
            f = open(realPath)
            data = f.read()
            f.close()
            self.send_response(200)
            self.send_header("Content-type", type)
            #            self.send_header("Content-length", os.path.getsize(realPath))
            self.end_headers()
            try:
                self.wfile.write(data.encode())
            except UnicodeDecodeError:
                self.wfile.write(data)
Beispiel #26
0
 def __digitalRead__(self, channel):
     self.checkDigitalChannelExported(channel)
     return GPIO.digitalRead(channel)
import _webiopi.GPIO
import time
GPIO = _webiopi.GPIO

# GPIO, status, time
data = [[num, 0, 0.0] for num in [17, 27, 22]]

for n in data:
    print(n[0], n[1], n[2])
    GPIO.setFunction(n[0], GPIO.IN)

while True:
    for n in data:
        status = GPIO.digitalRead(n[0])
        if n[1] == 0 and status:
            n[1] = 1
            n[2] = time.time()
        elif n[1] == 1 and not status:
            n[1] = 2
        elif n[1] == 2 and 0.2 < (time.time() - n[2]):
            n[1] = 0
            print("%d clicked" % n[0])
Beispiel #28
0
 def getPulse(self, channel):
     self.checkDigitalChannelExported(channel)
     self.checkDigitalChannel(channel)
     return GPIO.getPulse(channel)
Beispiel #29
0
 def get(self, channel):
     return GPIO.HWPWMgetPort(channel)
Beispiel #30
0
    def do_POST(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:];

        if (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)
            
            if (operation == "value"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;
                if (value == "0"):
                    GPIO.output(gpio, GPIO.LOW)
                elif (value == "1"):
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    self.send_error(400, "Bad Value")
                    return
    
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "function"):
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    self.send_error(400, "Bad Function")
                    return
                value = GPIO.getFunctionString(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "sequence"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;

                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(sequence[-1].encode())
                
            elif (operation == "pwm"):
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)
                
                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"
                
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(result.encode())
                
            elif (operation == "pulse"):
                GPIO.pulse(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write("OK".encode())
                
            elif (operation == "pulseRatio"):
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())
                
            elif (operation == "pulseAngle"):
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())
                
            else: # operation unknown
                self.send_error(404, operation + " Not Found")
                return
        elif (relativePath.startswith("macros/")):
            (mode, fname, value) = relativePath.split("/")
            if (fname in self.server.callbacks):
                if ',' in value:
                    args = value.split(',')
                else:
                    args = [value]
                callback = self.server.callbacks[fname]
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(callback(*args).encode())
            else:
                self.send_error(404, fname + " Not Found")
                return
                
        else: # path unknowns
            self.send_error(404, "Not Found")
Beispiel #31
0
    def do_POST(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:];

        if (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)
            
            if (operation == "value"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;
                if (value == "0"):
                    GPIO.output(gpio, GPIO.LOW)
                elif (value == "1"):
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    self.send_error(400, "Bad Value")
                    return
    
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "function"):
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                else:
                    self.send_error(400, "Bad Function")
                    return
                value = GPIO.getFunctionString(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "sequence"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;

                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(sequence[-1].encode())
                
            else: # operation unknown
                self.send_error(404, operation + " Not Found")
                return
        elif (relativePath.startswith("macros/")):
            (mode, fname, value) = relativePath.split("/")
            if (fname in self.server.callbacks):
                callback = self.server.callbacks[fname]
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(callback(value).encode())
            else:
                self.send_error(404, fname + " Not Found")
                return
                
        else: # path unknowns
            self.send_error(404, "Not Found")
Beispiel #32
0
    def do_GET(self):
        if not self.checkAuthentication():
            return
        
        relativePath = self.path.replace(self.server.context, "/")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:];

        # JSON full state
        if relativePath == "*":
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.server.writeJSON(self.wfile)
            
        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.wfile.write(json.replace("'", '"').encode())

        # version
        elif relativePath == "version":
            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()
            self.wfile.write(SERVER_VERSION.encode())

        # Single GPIO getter
        elif (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if (operation == "value"):
                value = GPIO.input(gpio)
    
            elif (operation == "function"):
                value = GPIO.getFunctionString(gpio)
    
            else:
                self.send_error(404, operation + " Not Found")
                return
                
            self.send_response(200)
            self.send_header("Content-type", "text/plain");
            self.end_headers()
            self.wfile.write(value.encode())

        # handle files
        else:
            if relativePath == "":
                relativePath = self.server.index
                            
            realPath = relativePath;
            
            if not os.path.exists(realPath):
                realPath = self.server.docroot + os.sep + relativePath
                
            if not os.path.exists(realPath):
                self.send_error(404, "Not Found")
                return

            realPath = os.path.realpath(realPath)
            
            if realPath.endswith(".py"):
                self.send_error(403, "Not Authorized")
                return
            
            if not (realPath.startswith(self.server.docroot) or realPath.startswith(os.getcwd())):
                self.send_error(403, "Not Authorized")
                return
                
            if (os.path.isdir(realPath)):
                realPath += os.sep + self.server.index;
                if not os.path.exists(realPath):
                    self.send_error(403, "Not Authorized")
                    return
                
            (type, encoding) = mime.guess_type(realPath)
            f = open(realPath)
            data = f.read()
            f.close()
            self.send_response(200)
            self.send_header("Content-type", type);
#            self.send_header("Content-length", os.path.getsize(realPath))
            self.end_headers()
            try:
                self.wfile.write(data.encode())
            except UnicodeDecodeError:
                self.wfile.write(data)
Beispiel #33
0
def MotorDrive(iIn1Pin, iIn2Pin, percentage):
    if 100 < percentage:
        percentage = 100
    if -100 > percentage:
        percentage = -100
    if 10 > percentage and -10 < percentage:
        GPIO.pwmWrite(iIn1Pin, 0.0)
        GPIO.pwmWrite(iIn2Pin, 0.0)
    elif 0 < percentage:
        GPIO.pwmWrite(iIn1Pin, percentage * 0.01)
        GPIO.pwmWrite(iIn2Pin, 0.0)
    else:
        GPIO.pwmWrite(iIn1Pin, 0.0)
        GPIO.pwmWrite(iIn2Pin, -percentage * 0.01)
Beispiel #34
0
#!/usr/bin/env python3
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
import sys
import _webiopi.GPIO
import json

PIN_L1 = 6
PIN_L2 = 13
PIN_R1 = 19
PIN_R2 = 26

g_mode = 0
g_percentage = 50

GPIO = _webiopi.GPIO
GPIO.setFunction(PIN_L1, GPIO.PWM)
GPIO.setFunction(PIN_L2, GPIO.PWM)
GPIO.setFunction(PIN_R1, GPIO.PWM)
GPIO.setFunction(PIN_R2, GPIO.PWM)


def MotorDrive(iIn1Pin, iIn2Pin, percentage):
    if 100 < percentage:
        percentage = 100
    if -100 > percentage:
        percentage = -100
    if 10 > percentage and -10 < percentage:
        GPIO.pwmWrite(iIn1Pin, 0.0)
        GPIO.pwmWrite(iIn2Pin, 0.0)
    elif 0 < percentage:
        GPIO.pwmWrite(iIn1Pin, percentage * 0.01)
Beispiel #35
0
    def do_POST(self, relativePath):
        if relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)

            if operation == "value":
                if value == "0":
                    GPIO.output(gpio, GPIO.LOW)
                elif value == "1":
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    return (400, "Bad Value", M_PLAIN)

                return (200, value, M_PLAIN)

            elif operation == "function":
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    return (400, "Bad Function", M_PLAIN)

                value = GPIO.getFunctionString(gpio)
                return (200, value, M_PLAIN)

            elif operation == "sequence":
                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                return (200, sequence[-1], M_PLAIN)

            elif operation == "pwm":
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)

                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"

                return (200, result, M_PLAIN)

            elif operation == "pulse":
                GPIO.pulse(gpio)
                return (200, "OK", M_PLAIN)

            elif operation == "pulseRatio":
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                return (200, value, M_PLAIN)

            elif operation == "pulseAngle":
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                return (200, value, M_PLAIN)

            else:  # operation unknown
                return (404, operation + " Not Found", M_PLAIN)

        elif relativePath.startswith("macros/"):
            (mode, fname, value) = relativePath.split("/")
            if fname in self.callbacks:
                callback = self.callbacks[fname]

                if "," in value:
                    args = value.split(",")
                    result = callback(*args)
                elif len(value) > 0:
                    result = callback(value)
                else:
                    result = callback()

                response = ""
                if result:
                    response = "%s" % result
                return (200, response, M_PLAIN)

            else:
                return (404, fname + " Not Found", M_PLAIN)

        else:  # path unknowns
            return (0, None, None)
Beispiel #36
0
    def do_POST(self, relativePath):
        if relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)
            
            if operation == "value":
                if value == "0":
                    GPIO.output(gpio, GPIO.LOW)
                elif value == "1":
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    return (400, "Bad Value", M_PLAIN)
    
                return (200, value, M_PLAIN)

            elif operation == "function":
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    return (400, "Bad Function", M_PLAIN)

                value = GPIO.getFunctionString(gpio)
                return (200, value, M_PLAIN)

            elif operation == "sequence":
                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                return (200, sequence[-1], M_PLAIN)
                
            elif operation == "pwm":
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)
                
                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"
                
                return (200, result, M_PLAIN)
                
            elif operation == "pulse":
                GPIO.pulse(gpio)
                return (200, "OK", M_PLAIN)
                
            elif operation == "pulseRatio":
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                return (200, value, M_PLAIN)
                
            elif operation == "pulseAngle":
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                return (200, value, M_PLAIN)
                
            else: # operation unknown
                return (404, operation + " Not Found", M_PLAIN)
                
        elif relativePath.startswith("macros/"):
            (mode, fname, value) = relativePath.split("/")
            if fname in self.callbacks:
                callback = self.callbacks[fname]

                if ',' in value:
                    args = value.split(',')
                    result = callback(*args)
                elif len(value) > 0:
                    result = callback(value)
                else:
                    result = callback()
                     
                response = ""
                if result:
                    response = "%s" % result
                return (200, response, M_PLAIN)
                    
            else:
                return (404, fname + " Not Found", M_PLAIN)
                
        else: # path unknowns
            return (0, None, None)
Beispiel #37
0
 def checkDigitalChannelExported(self, channel):
     if not channel in self.export:
         raise GPIO.InvalidChannelException("Channel %d is not allowed" %
                                            channel)
Beispiel #38
0
def MotorDrive(iIn1Pin, iIn2Pin, percentage):
    if 100 < percentage:
        percentage = 100
    if -100 > percentage:
        percentage = -100
    if 10 > percentage and -10 < percentage:
        GPIO.pwmWrite(iIn1Pin, 0.0)
        GPIO.pwmWrite(iIn2Pin, 0.0)
    elif 0 < percentage:
        GPIO.pwmWrite(iIn1Pin, percentage * 0.01)
        GPIO.pwmWrite(iIn2Pin, 0.0)
    else:
        GPIO.pwmWrite(iIn1Pin, 0.0)
        GPIO.pwmWrite(iIn2Pin, -percentage * 0.01)
Beispiel #39
0
 def pulse(self, channel):
     self.checkDigitalChannelExported(channel)
     self.checkPostingValueAllowed()
     self.checkDigitalChannel(channel)
     GPIO.pulse(channel)
     return "OK"
Beispiel #40
0
 def getHWPWMoutput(self, channel):
     value = GPIO.HWPWMisEnabled(channel)
     if value == True:
         return "enable"
     else:
         return "disable"
Beispiel #41
0
 def getHWPWMDuty(self, channel):
     return GPIO.HWPWMgetDuty(channel)
Beispiel #42
0
 def __digitalRead__(self, channel):
     self.checkDigitalChannelExported(channel)
     return GPIO.digitalRead(channel)
Beispiel #43
0
 def pulseAngle(self, channel, value):
     self.checkDigitalChannelExported(channel)
     self.checkPostingValueAllowed()
     self.checkDigitalChannel(channel)
     GPIO.pulseAngle(channel, value)
     return GPIO.getPulse(channel)
Beispiel #44
0
 def setHWPWMperiod(self, channel, period):
     self.checkPostingValueAllowed()
     GPIO.HWPWMsetPeriod(channel, period)
     return GPIO.HWPWMgetPeriod(channel)
Beispiel #45
0
 def setHWPWMpolarity(self, channel, polarity):
     self.checkPostingValueAllowed()
     GPIO.HWPWMsetPolarity(channel, polarity)
     return GPIO.HWPWMgetPolarity(channel)
    def do_POST(self, relativePath):
        if relativePath.startswith("EGG/"):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            if s_gpio == "1":
                gpio = 7
            elif s_gpio == "2":
                gpio = 8
            elif s_gpio == "3":
                gpio = 9
            elif s_gpio == "4":
                gpio = 10
            elif s_gpio == "5":
                gpio = 11
            elif s_gpio == "6":
                gpio = 22
            elif s_gpio == "7":
                gpio = 23
            elif s_gpio == "8":
                gpio = 24
            else:
                gpio = 25

            if operation == "value":
                if value == "off":
                    GPIO.output(gpio, GPIO.LOW)
                elif value == "on":
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    return (400, "Bad Value", M_PLAIN)
    
                return (200, value, M_PLAIN)

            elif operation == "function":
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    return (400, "Bad Function", M_PLAIN)

                value = GPIO.getFunctionString(gpio)
                return (200, value, M_PLAIN)

            elif operation == "sequence":
                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                return (200, sequence[-1], M_PLAIN)
                
            elif operation == "pwm":
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)
                
                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"
                
                return (200, result, M_PLAIN)
                
            elif operation == "pulse":
                GPIO.pulse(gpio)
                return (200, "OK", M_PLAIN)
                
            elif operation == "pulseRatio":
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                return (200, value, M_PLAIN)
                
            elif operation == "pulseAngle":
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                return (200, value, M_PLAIN)
                
            else: # operation unknown
                return (404, operation + " Not Found", M_PLAIN)
                
        elif relativePath.startswith("macros/"):
            (mode, fname, value) = relativePath.split("/")
            if fname in self.callbacks:
                callback = self.callbacks[fname]

                if ',' in value:
                    args = value.split(',')
                    result = callback(*args)
                elif len(value) > 0:
                    result = callback(value)
                else:
                    result = callback()
                     
                response = ""
                if result:
                    response = "%s" % result
                return (200, response, M_PLAIN)
                    
            else:
                return (404, fname + " Not Found", M_PLAIN)

        elif operation == "saca17":
            GPIO.setFunction(17, GPIO.OUT)
        
        elif operation == "saca171":
            GPIO.output(17,GPIO.HIGH)

        elif operation == "saca170":
            GPIO.output(17,GPIO.LOW)

        elif operation == "saca21":
            GPIO.setFunction(21, GPIO.OUT)
        
        elif operation == "saca211":
            GPIO.output(21,GPIO.HIGH)

        elif operation == "saca210":
            GPIO.output(21,GPIO.LOW)

        elif relativePath.startswith("EGGSOFF"):
            GPIO.output(7, GPIO.LOW)
            GPIO.output(8, GPIO.LOW)
            GPIO.output(9, GPIO.LOW)
            GPIO.output(10, GPIO.LOW)
            GPIO.output(11, GPIO.LOW)
            GPIO.output(22, GPIO.LOW)
            GPIO.output(23, GPIO.LOW)
            GPIO.output(24, GPIO.LOW)
            GPIO.output(25, GPIO.LOW)
            return (200, "ALL EGGS OFF", M_PLAIN)

        elif relativePath.startswith("EGGSON"):
            GPIO.output(7, GPIO.HIGH)
            GPIO.output(8, GPIO.HIGH)
            GPIO.output(9, GPIO.HIGH)
            GPIO.output(10, GPIO.HIGH)
            GPIO.output(11, GPIO.HIGH)
            GPIO.output(22, GPIO.HIGH)
            GPIO.output(23, GPIO.HIGH)
            GPIO.output(24, GPIO.HIGH)
            GPIO.output(25, GPIO.HIGH)
            return (200, "ALL EGGS ON", M_PLAIN)
    

        else: # path unknowns
            return (0, None, None)
Beispiel #47
0
 def __digitalWrite__(self, channel, value):
     self.checkDigitalChannelExported(channel)
     self.checkPostingValueAllowed()
     GPIO.digitalWrite(channel, value)
Beispiel #48
0
 def getHWPWMperiod(self, channel):
     return GPIO.HWPWMgetPeriod(channel)
Beispiel #49
0
    def do_POST(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:]

        if (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)

            if (operation == "value"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return
                if (value == "0"):
                    GPIO.output(gpio, GPIO.LOW)
                elif (value == "1"):
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    self.send_error(400, "Bad Value")
                    return

                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "function"):
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    self.send_error(400, "Bad Function")
                    return
                value = GPIO.getFunctionString(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "sequence"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return

                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(sequence[-1].encode())

            elif (operation == "pwm"):
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)

                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"

                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(result.encode())

            elif (operation == "pulse"):
                GPIO.pulse(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write("OK".encode())

            elif (operation == "pulseRatio"):
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "pulseAngle"):
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            else:  # operation unknown
                self.send_error(404, operation + " Not Found")
                return
        elif (relativePath.startswith("macros/")):
            (mode, fname, value) = relativePath.split("/")
            if (fname in self.server.callbacks):
                if ',' in value:
                    args = value.split(',')
                else:
                    args = [value]
                callback = self.server.callbacks[fname]
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(callback(*args).encode())
            else:
                self.send_error(404, fname + " Not Found")
                return

        else:  # path unknowns
            self.send_error(404, "Not Found")
Beispiel #50
0
 def setHWPWMport(self, channel, port):
     self.checkPostingValueAllowed()
     GPIO.HWPWMsetPort(channel, port)
     return GPIO.HWPWMgetPort(channel)
Beispiel #51
0
 def setHWPWMmSMode(self, channel, msmode):
     self.checkPostingValueAllowed()
     GPIO.HWPWMsetMSMode(channel, msmode)
     return GPIO.HWPWMgetMSMode(channel)
Beispiel #52
0
 def __getFunction__(self, channel):
     self.checkDigitalChannelExported(channel)
     return GPIO.getFunction(channel)
Beispiel #53
0
 def __setFunction__(self, channel, value):
     self.checkDigitalChannelExported(channel)
     self.checkPostingFunctionAllowed()
     GPIO.setFunction(channel, value)
Beispiel #54
0
 def __portRead__(self):
     value = 0
     for i in self.export:
         value |= GPIO.digitalRead(i) << i
     return value 
Beispiel #55
0
#!/usr/bin/env python3
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
import sys
import _webiopi.GPIO
import json

PIN_L1 = 6
PIN_L2 = 13
PIN_R1 = 19
PIN_R2 = 26

g_mode = 0
g_percentage = 50

GPIO = _webiopi.GPIO
GPIO.setFunction(PIN_L1, GPIO.PWM)
GPIO.setFunction(PIN_L2, GPIO.PWM)
GPIO.setFunction(PIN_R1, GPIO.PWM)
GPIO.setFunction(PIN_R2, GPIO.PWM)

def MotorDrive(iIn1Pin, iIn2Pin, percentage):
    if 100 < percentage:
        percentage = 100
    if -100 > percentage:
        percentage = -100
    if 10 > percentage and -10 < percentage:
        GPIO.pwmWrite(iIn1Pin, 0.0)
        GPIO.pwmWrite(iIn2Pin, 0.0)
    elif 0 < percentage:
        GPIO.pwmWrite(iIn1Pin, percentage * 0.01)
        GPIO.pwmWrite(iIn2Pin, 0.0)
Beispiel #56
0
 def setHWPWMDuty(self, channel, duty):
     self.checkPostingValueAllowed()
     GPIO.HWPWMsetDuty(channel, duty)
     return GPIO.HWPWMgetDuty(channel)