Guest User

Untitled

a guest
Jun 23rd, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def recv_messages():
  2.     global ridx, widx, lastid, timestamp_cycles
  3.  
  4.     # read updated write-pointer
  5.     widx = ddr_widx.value
  6.     assert widx < NUM_MSGS  # sanity-check
  7.  
  8.     if ridx == widx:
  9.         return  # no messages received
  10.  
  11.     # process messages
  12.     while ridx != widx:
  13.         # note: it may be faster to copy a batch of messages from shared memory
  14.         # instead of directly accessing individual messages and their fields.
  15.         msg = msgbuf[ ridx ]
  16.  
  17.         # sanity-check that message id increments monotonically
  18.         lastid = ( lastid + 1 ) & 0xffffffff
  19.         assert msg.id == lastid
  20.  
  21.         # get 32-bit timestamp (in cycles) from message and unwrap it:
  22.         timestamp_cycles += ( msg.timestamp - timestamp_cycles ) & 0xffffffff
  23.  
  24.         # process rest of message
  25.         position = msg.position
  26.         force = msg.force
  27.  
  28.         # consume message and update read pointer
  29.         del msg  # direct access to message forbidden beyond this point
  30.         ridx += 1
  31.         if ridx == NUM_MSGS:
  32.             ridx = 0
  33.         shmem.ridx = ridx
  34.  
  35.     # update user interface
  36.     ts_ms = timestamp_cycles // 200000
  37.     ts_s = ( ts_ms % 60000 ) / 1000
  38.     ts_m = ts_ms // 60000
  39.     ts_h = ts_m // 60
  40.     ts_m = ts_m % 60
  41.     print(f'\ridx=0x{ridx:04x} id=0x{lastid:08x} time={ts_h:02d}:{ts_m:02d}:{ts_s:06.3f} position={position:08d} force={force:08d}', end='', flush=True )
Advertisement
Add Comment
Please, Sign In to add comment