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 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 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"])
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"])
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")
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"])
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
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
def __getFunction__(self, channel): self.checkDigitalChannelExported(channel) return GPIO.getFunction(channel)
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")
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")
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")