RGB Buttons Exercise
# Module Import
import LocoIOT
import time
# Class Instances Creation
loco_iot = LocoIOT.LocoIOT()
msg = LocoIOT.IOT_Codes()
# Initiate Connection to the Microcontroller
loco_iot.connect()
### Add Hardware Enable(s) Here
loco_iot.enable(msg.SUBTYPE_RGB)
loco_iot.enable(msg.SUBTYPE_SW_1)
loco_iot.enable(msg.SUBTYPE_SW_2)
loco_iot.enable(msg.SUBTYPE_SW_3)
# Instruct the Microcontroller to Start Listening for Control/Request Messages
loco_iot.start()
#Blue
while True:
# Request and Print Button State Data
btn_dict1 = loco_iot.getData(msg.SUBTYPE_SW_1)
print(btn_dict1)
if(btn_dict1 == {'Button 1':0}):
dim = [0, 0, 255]
loco_iot.setData(msg.SUBTYPE_RGB, dim)
print("Blue Button Pressed")
else:
dim = [0, 0, 0]
loco_iot.setData(msg.SUBTYPE_RGB, dim)
print("Blue Button NOT Pressed")
time.sleep(0.5)
#Red
btn_dict2 = loco_iot.getData(msg.SUBTYPE_SW_2)
print(btn_dict2)
if(btn_dict2 == {'Button 2':0}):
dim = [255, 0, 0]
loco_iot.setData(msg.SUBTYPE_RGB, dim)
print("Red Button Pressed")
else:
dim = [0, 0, 0]
loco_iot.setData(msg.SUBTYPE_RGB, dim)
print("Red Button NOT Pressed")
time.sleep(0.5)
#Green
btn_dict3 = loco_iot.getData(msg.SUBTYPE_SW_3)
print(btn_dict3)
if(btn_dict3 == {'Button 3':0}):
dim = [0, 255, 0]
loco_iot.setData(msg.SUBTYPE_RGB, dim)
print("Green Button Pressed")
else:
dim = [0, 0, 0]
loco_iot.setData(msg.SUBTYPE_RGB, dim)
print("Green Button NOT Pressed")
time.sleep(0.5)
# Close Connection to the Microcontroller
loco_iot.close()