#Code for twitter controlled video live streaming dolly aka Makercast
#Used on Intel edison with Grove motor driver and 996R servo
#Stitched together by Raitis, @makerraitis on Twitter and Instagram, snipegift.com

#Project made possible with electronics from Intel and Instructables during IoT Invitational
#Also by Green Garage makerspace and the helpful staff there (greengarage.me)
#Full how to instructions with assembly, BOM, cutting files, etc. on instructables
#http://www.instructables.com/id/The-Makercast-a-video-livecasting-platform-you-can/

import tweepy #twitter API
import time #timing
import mraa #everything you need in life
import pyupm_grovemd as upmGrovemd #motor driver library
import pyupm_servo as upmservo #servo library


#twitter things
consumer_key = 'these here'
consumer_secret = 'are not'
access_token = 'my twitter'
access_token_secret = 'tokens'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

#motor things
I2C_BUS = upmGrovemd.GROVEMD_I2C_BUS
I2C_ADDR = upmGrovemd.GROVEMD_DEFAULT_I2C_ADDR
myMotorDriver = upmGrovemd.GroveMD(I2C_BUS, I2C_ADDR)

#if something something:
#    tweet that a live cast is starting soon

updn = upmservo.Servo(3) #servo pin
angle = 70 #definition for angle values. Primary angle 70 - vertical in my case
updn.setAngle(angle) #actually moving the servo
slider = 3 #position indicator scale 1 to 5, not risking it without end switches
rotation = 8 #position indicator scale 1 to 15, approx 8 steps in 90 degrees


while True: #loops the code
    commands = api.mentions_timeline(count=1) #gets the last tweet with your mention
    for command in commands: #no idea how this works
        order = command.text #reads the damn thing
        print('I read ' + order)
        if order[10:19] == "turn left" and rotation > 1: #brackets for reading only text between symbols
            myMotorDriver.setMotorDirections(upmGrovemd.GroveMD.DIR_CW, upmGrovemd.GroveMD.DIR_CCW)
            myMotorDriver.setMotorSpeeds(0, 100)
            time.sleep(0.03)
            myMotorDriver.setMotorSpeeds(0, 0)
            rotation -= 1 #position change
        if order[10:20] == "turn right" and rotation < 15: #position condition test to avoid catastrophes
            myMotorDriver.setMotorDirections(upmGrovemd.GroveMD.DIR_CW, upmGrovemd.GroveMD.DIR_CW)
            myMotorDriver.setMotorSpeeds(0, 100)
            time.sleep(0.03)
            myMotorDriver.setMotorSpeeds(0, 0)
            rotation += 1
        if order[10:19] == "move left" and slider > 1:
            myMotorDriver.setMotorDirections(upmGrovemd.GroveMD.DIR_CCW, upmGrovemd.GroveMD.DIR_CW)
            myMotorDriver.setMotorSpeeds(255, 0)
            time.sleep(0.5)
            myMotorDriver.setMotorSpeeds(0, 0) #stop the train!
        if order[10:20] == "move right" and slider < 5:
            myMotorDriver.setMotorDirections(upmGrovemd.GroveMD.DIR_CW, upmGrovemd.GroveMD.DIR_CW)
            myMotorDriver.setMotorSpeeds(255, 0)
            time.sleep(0.5)
            myMotorDriver.setMotorSpeeds(0, 0)
        if order[10:17] == "turn up" and angle > 35:
            angle -= 10 #not sure if I can get it through library, but whatever, this works
            updn.setAngle(angle)
        if order [10:19] == "turn down" and angle < 95:
            angle += 10
            updn.setAngle(angle)
    time.sleep(60) #API allows only 1 request per minute for mentions_timeline
