import time
import random
# MUST
from Board import Board
# OPTION
import logging
import ai_logger
log = logging.getLogger('cpf-ai') # Get Logger object of cpf-ai
log.setLevel('INFO') # Set the level, there are DEBUG, INFO, WARNING, ERROR, CRITICAL
log.addHandler(ai_logger.AIloggerHandler()) # Add the specified handler of cpf-ai to the logger
def main():
isStop = True
for port in Board().getPort(): # Get port list of plugin's board
log.info(port)
for num in range(0,3): # Confirm the board 3 times
if Board().boardReady(port): # Confirm that the plugin's board had been initiaized and got ready
# Initial pin
initialPin = '["resetPin"], \
["setPinMode", "analog", {:0}, "INPUT"], \
["setPinMode", "digital", {:0}, "OUTPUT"], \
["setPinMode", "digital", {:0}, "INPUT"], \
["grove_newChainableLED",{:0}, {:0}, {:0}], \
["setPinMode", "digital", {:0}, "PWM"]'.format(1,6,2,7,8,1,5)
if not Board().setPinMode(port, initialPin):
log.info("board fail")
isStop = False
while isStop:
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
command = '["grove_setColorRGB", {:0}, {:0}, {:0}, {:0}]'.format(0,r,g,b)
if not Board().request(port, command): # Set random color
log.info("board fail")
break;
val = Board().get(port, "a1") # Get the value of analog
log.info("Temperture Sensor is " + str(val))
if(val > 520):
command = '["analogWrite", {:0} , {:0}]'.format(5,255)
if not Board().request(port, command): # Set maximum value
log.info("board fail")
break;
else:
command = '["analogWrite", {:0} , {:0}]'.format(5,0)
if not Board().request(port, command): # Set mininum value
log.info("board fail")
break;
val = Board().get(port, "d2") # Get the value of digital
log.info("Light Sensor is " + str(val))
if val:
command = '["digitalWrite", {:0} , {:0}]'.format(6,0)
if not Board().request(port, command): # Turn off
log.info("board fail")
break;
else:
command = '["digitalWrite", {:0} , {:0}]'.format(6,1)
if not Board().request(port, command): # Turn on
log.info("board fail")
break;
time.sleep(1)
# reset board
initialPin = '["resetPin"]'
Board().setPinMode(port, initialPin)
log.info("Done.")
exit(0)
log.debug("Plugin's board doesn't been initiaized.")
time.sleep(1)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
exit(0)