Commit 508d8c39 authored by Klint Youngmeyer's avatar Klint Youngmeyer
Browse files

Add example python file with functions that can be added to the REST API of the server

parent e14c19f8
import pyb
import ujson
# POST Functions
def led_control(json_dict):
try:
led_num = json_dict['number']
except KeyError:
led_num = 1
led = pyb.LED(led_num)
try:
led_action = json_dict['action']
except KeyError:
led.toggle()
else:
if led_action == 'on':
led.on()
elif led_action == 'off':
led.off()
elif led_action == 'toggle':
led.toggle()
else:
raise ValueError
if led.intensity() == 255: # CutiPy reports 255 as off. LED pins sink current, instead of source.
response_data = ujson.dumps({'status': 'off'})
else:
response_data = ujson.dumps({'status': 'on'})
return response_data.encode('utf-8')
# GET Functions
def get_switch(var_list):
if var_list:
switch_num = var_list[0]
else:
switch_num = 1
sw = pyb.Switch(switch_num)
if sw():
response_data = ujson.dumps({'status': 'pressed'})
else:
response_data = ujson.dumps({'status': 'not_pressed'})
return response_data.encode('utf-8')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment