|
| 1 | +# Copyright (C) 2015 Swift Navigation Inc. |
| 2 | +# Contact: Fergus Noble <[email protected]> |
| 3 | +# |
| 4 | +# This source is subject to the license found in the file 'LICENSE' which must |
| 5 | +# be be distributed together with this source. All other rights reserved. |
| 6 | +# |
| 7 | +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 8 | +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED |
| 9 | +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | + |
| 11 | +""" |
| 12 | +the :mod:`sbp.client.examples.tweet` module contains a basic example of |
| 13 | +reading SBP messages from a serial port, decoding TWEET messages and pushing |
| 14 | +them to the twitter API. |
| 15 | +""" |
| 16 | + |
| 17 | +from sbp.client.drivers.pyserial_driver import PySerialDriver |
| 18 | +from sbp.client.handler import Handler |
| 19 | +from sbp.logging import SBP_MSG_TWEET, MsgTweet |
| 20 | + |
| 21 | +import time |
| 22 | +import twitter |
| 23 | + |
| 24 | +twit = None |
| 25 | + |
| 26 | +def tweet_callback(msg): |
| 27 | + # This function is called every time we receive a TWEET message |
| 28 | + if twit is not None: |
| 29 | + twit.statuses.update(status=MsgTweet(msg).tweet) |
| 30 | + |
| 31 | +def main(): |
| 32 | + |
| 33 | + import argparse |
| 34 | + parser = argparse.ArgumentParser(description="Swift Navigation Tweetxample.") |
| 35 | + parser.add_argument("TOKEN") |
| 36 | + parser.add_argument("TOKEN_KEY") |
| 37 | + parser.add_argument("CON_SEC") |
| 38 | + parser.add_argument("CON_SEC_KEY") |
| 39 | + parser.add_argument("-p", "--port", |
| 40 | + default=['/dev/ttyUSB0'], nargs=1, |
| 41 | + help="specify the serial port to use.") |
| 42 | + args = parser.parse_args() |
| 43 | + |
| 44 | + my_auth = twitter.OAuth(args.TOKEN, args.TOKEN_KEY, |
| 45 | + args.CON_SEC, args.CON_SEC_KEY) |
| 46 | + twit = twitter.Twitter(auth=my_auth) |
| 47 | + |
| 48 | + # Open a connection to Piksi using the default baud rate (1Mbaud) |
| 49 | + with PySerialDriver(args.port[0], baud=1000000) as driver: |
| 50 | + # Create a handler to connect our Piksi driver to our callbacks |
| 51 | + with Handler(driver.read, driver.write, verbose=True) as handler: |
| 52 | + # Add a callback for BASELINE_NED messages |
| 53 | + handler.add_callback(baseline_callback, msg_type=SBP_MSG_BASELINE_NED) |
| 54 | + |
| 55 | + # Sleep until the user presses Ctrl-C |
| 56 | + try: |
| 57 | + while True: |
| 58 | + time.sleep(0.1) |
| 59 | + except KeyboardInterrupt: |
| 60 | + pass |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + main() |
| 64 | + |
0 commit comments