Skip to content

Commit

Permalink
Create and init message in one line with new_message in messaging (c…
Browse files Browse the repository at this point in the history
…ommaai#35)

* change new_message in messaging to include service and optional list size parameter

* fix for line 35 in logmessaged.py, which doesn't init message

* more readable
  • Loading branch information
pd0wm authored Mar 6, 2020
1 parent 4589107 commit 2e5cbfc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ while 1:

# in publisher
pm = messaging.PubMaster(['sensorEvents'])
dat = messaging.new_message()
dat.init('sensorEvents', 1)
dat = messaging.new_message('sensorEvents', size=1)
dat.sensorEvents[0] = {"gyro": {"v": [0.1, -0.1, 0.1]}}
pm.send('sensorEvents', dat)
```
12 changes: 8 additions & 4 deletions messaging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@

context = Context()

def new_message():
def new_message(service=None, size=None):
dat = log.Event.new_message()
dat.logMonoTime = int(sec_since_boot() * 1e9)
dat.valid = True
if service is not None:
if size is None:
dat.init(service)
else:
dat.init(service, size)
return dat

def pub_sock(endpoint):
Expand Down Expand Up @@ -148,12 +153,11 @@ def __init__(self, services, ignore_alive=None, addr="127.0.0.1"):
self.sock[s] = sub_sock(s, poller=self.poller, addr=addr, conflate=True)
self.freq[s] = service_list[s].frequency

data = new_message()
try:
data.init(s)
data = new_message(s)
except capnp.lib.capnp.KjException:
# lists
data.init(s, 0)
data = new_message(s, 0)

self.data[s] = getattr(data, s)
self.logMonoTime[s] = 0
Expand Down

0 comments on commit 2e5cbfc

Please sign in to comment.