-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
import pingo | ||
try: | ||
# Python 2.7 | ||
from urllib2 import urlopen | ||
except ImportError: | ||
# Python 3+ | ||
from urrlib.requests import urlopen | ||
|
||
|
||
class HTTPBoard(pingo.Board): | ||
""" | ||
""" | ||
|
||
def __init__(self, server): | ||
self.server = server | ||
response = urlopen(server) | ||
if response.code != 200: | ||
raise Exception(u'HTTPBoard not found on server {}'.format(server)) | ||
response = json.load(response) | ||
pins = json.loads(response['pins']) | ||
gpio_pins = [] | ||
ground_pins = [] | ||
vcc_pins = [] | ||
pwm_pins = [] | ||
for pin, value in pins.items(): | ||
# TODO: serialize the pin (and/or the board) to do this in a better way | ||
if 'GroundPin' in value: | ||
ground_pins.append(pingo.GroundPin(self, pin)) | ||
elif 'VccPin' in value: | ||
voltage = float(value[:-2].split(' ')[1]) | ||
vcc_pins.append(pingo.VccPin(self, pin, voltage)) | ||
elif 'PwmPin' in value: | ||
gpio_id = value.split(' ')[1].split('@')[0] | ||
pwm_pins.append(pingo.PwmPin(self, pin, gpio_id)) | ||
elif 'DigitalPin' in value: | ||
gpio_id = value.split(' ')[1].split('@')[0] | ||
gpio_pins.append(pingo.DigitalPin(self, pin, gpio_id)) | ||
self._add_pins(ground_pins + vcc_pins + gpio_pins + pwm_pins) | ||
|
||
def _set_digital_mode(self, pin, mode): | ||
mode = 'input' if pingo.IN else 'output' | ||
url = '{server}mode/{mode}/{pin}'.format(server=self.server, | ||
mode=mode, pin=pin.location) | ||
urlopen(url) | ||
|
||
def _set_pin_state(self, pin, state): | ||
mode = 'analog' if pin.is_analog else 'digital' | ||
state = 1 if state == pingo.HIGH else 0 | ||
url = '{server}{mode}/{pin}/{state}'.format(server=self.server, mode=mode, | ||
pin=str(pin.location), | ||
state=str(state)) | ||
print(url) | ||
response = urlopen(url) | ||
if response.code != 200: | ||
message = u'Pin {} could not be set to {}. HTTPBoard response: {}' | ||
message.format(repr(pin), state, response.code) | ||
raise Exception(message) | ||
|
||
def _get_pin_state(self, pin): | ||
mode = 'analog' if pin.is_analog else 'digital' | ||
url = '{server}{mode}/{pin}'.format(server=self.server, mode=mode, | ||
pin=pin.location) | ||
response = urlopen(url) | ||
if response.code != 200: | ||
message = u'Pin {} could not be read: HTTPBoard response: {}' | ||
message.format(repr(pin), response.code) | ||
raise Exception(message) | ||
return response['input'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from bottle import Bottle | ||
import json | ||
import pingo | ||
import sys | ||
|
||
|
||
app = Bottle(__name__) | ||
board = pingo.detect.get_board() | ||
|
||
|
||
@app.route('/') | ||
def main(): | ||
pins = {key: repr(value) for key, value in board.pins} | ||
return { | ||
'board': repr(board), | ||
'pins': json.dumps(pins) | ||
} | ||
|
||
|
||
@app.route('/mode/<mode>/<pin>') | ||
def mode(mode, pin): | ||
assert mode in ('input', 'output') | ||
mode = pingo.IN if 'input' else pingo.OUT | ||
pin = board.pins[pin] | ||
pin.mode = mode | ||
|
||
|
||
@app.route('/analog') | ||
def analog_pins(): | ||
pins = {location: pin for location, pin in board.pins | ||
if pin.is_analog} | ||
return {'pins': str(pins)} | ||
|
||
|
||
@app.route('/analog/<pin>') | ||
def analog_input(pin): | ||
pin = board.pins[pin] | ||
pin.mode = pingo.IN | ||
return {'input': pin.state} | ||
|
||
|
||
@app.route('/analog/<pin>/<signal:float>') | ||
def analog_output(pin, signal): | ||
pin = board.pins[pin] | ||
pin.mode = pingo.OUT | ||
pin.value = signal | ||
return {'output': signal} | ||
|
||
|
||
@app.route('/digital') | ||
def digital_pins(): | ||
pins = board.pins() | ||
return {'pins': str(pins)} | ||
|
||
|
||
@app.route('/digital/<pin>') | ||
def digital_input(pin): | ||
pin = board.pins[pin] | ||
pin.mode = pingo.IN | ||
return {'input': pin.state} | ||
|
||
|
||
@app.route('/digital/<pin>/<signal:int>') | ||
def digital_output(pin, signal): | ||
pin = board.pins[pin] | ||
pin.mode = pingo.OUT | ||
pin.high() if signal else pin.low() | ||
return {'output': signal} | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
kwargs = {'host': sys.argv[1]} | ||
except IndexError: | ||
kwargs = {} | ||
app.run(debug=True, **kwargs) |