Skip to content

Commit 7470f83

Browse files
committed
Merge pull request #178 from fnoble/mookerji-omglolcatswftbbq
Example for the new TWEET message
2 parents 8de7e49 + 1e80e29 commit 7470f83

File tree

5 files changed

+156
-1
lines changed

5 files changed

+156
-1
lines changed

c/include/libsbp/logging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ typedef struct __attribute__((packed)) {
4747
#define SBP_MSG_DEBUG_VAR 0x0011
4848

4949

50+
/** Tweet
51+
*
52+
* All the news fit to tweet.
53+
*/
54+
#define SBP_MSG_TWEET 0x0012
55+
typedef struct __attribute__((packed)) {
56+
char tweet[140]; /**< Human-readable string */
57+
} msg_tweet_t;
58+
59+
5060
/** \} */
5161

5262
#endif /* LIBSBP_LOGGING_MESSAGES_H */
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+

python/sbp/logging.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,78 @@ def __repr__(self):
126126
return fmt_repr(self)
127127

128128

129+
SBP_MSG_TWEET = 0x0012
130+
class MsgTweet(SBP):
131+
"""SBP class for message MSG_TWEET (0x0012).
132+
133+
You can have MSG_TWEET inherent its fields directly
134+
from an inherited SBP object, or construct it inline using a dict
135+
of its fields.
136+
137+
138+
All the news fit to tweet.
139+
140+
Parameters
141+
----------
142+
sbp : SBP
143+
SBP parent object to inherit from.
144+
tweet : string
145+
Human-readable string
146+
sender : int
147+
Optional sender ID, defaults to 0
148+
149+
"""
150+
_parser = Struct("MsgTweet",
151+
String('tweet', 140),)
152+
153+
def __init__(self, sbp=None, **kwargs):
154+
if sbp:
155+
self.__dict__.update(sbp.__dict__)
156+
self.from_binary(sbp.payload)
157+
else:
158+
super( MsgTweet, self).__init__()
159+
self.msg_type = SBP_MSG_TWEET
160+
self.sender = kwargs.pop('sender', 0)
161+
self.tweet = kwargs.pop('tweet')
162+
163+
def __repr__(self):
164+
return fmt_repr(self)
165+
166+
def from_binary(self, d):
167+
"""Given a binary payload d, update the appropriate payload fields of
168+
the message.
169+
170+
"""
171+
p = MsgTweet._parser.parse(d)
172+
self.__dict__.update(dict(p.viewitems()))
173+
174+
def to_binary(self):
175+
"""Produce a framed/packed SBP message.
176+
177+
"""
178+
c = containerize(exclude_fields(self))
179+
self.payload = MsgTweet._parser.build(c)
180+
return self.pack()
181+
182+
@staticmethod
183+
def from_json(s):
184+
"""Given a JSON-encoded string s, build a message object.
185+
186+
"""
187+
d = json.loads(s)
188+
sbp = SBP.from_json_dict(d)
189+
return MsgTweet(sbp)
190+
191+
def to_json_dict(self):
192+
self.to_binary()
193+
d = super( MsgTweet, self).to_json_dict()
194+
j = walk_json_dict(exclude_fields(self))
195+
d.update(j)
196+
return d
197+
129198

130199
msg_classes = {
131200
0x0010: MsgPrint,
132201
0x0011: MsgDebugVar,
202+
0x0012: MsgTweet,
133203
}

python/tests/sbp/test_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_table_count():
3434
Test number of available messages to deserialize.
3535
3636
"""
37-
number_of_messages = 53
37+
number_of_messages = 54
3838
assert len(_SBP_TABLE) == number_of_messages
3939

4040
def test_table_unqiue_count():

spec/yaml/swiftnav/sbp/logging.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ definitions:
3737
desc: |
3838
This is an unused legacy message for tracing variable values
3939
within the device firmware and streaming those back to the host.
40+
41+
- MSG_TWEET:
42+
id: 0x0012
43+
public: False
44+
short_desc: Tweet
45+
desc: All the news fit to tweet.
46+
fields:
47+
- tweet:
48+
type: string
49+
size: 140
50+
desc: Human-readable string

0 commit comments

Comments
 (0)