Skip to content

Instantly share code, notes, and snippets.

@matthewphilyaw
Last active August 29, 2015 14:20
Show Gist options
  • Save matthewphilyaw/7898f6cb6444246756cd to your computer and use it in GitHub Desktop.
Save matthewphilyaw/7898f6cb6444246756cd to your computer and use it in GitHub Desktop.
Elixir bit syntax example
speed = 500
angle = -200
# being very explicit here, I believe the default
# unit is 8, and the endianess is big
#
# So I do need to pack this message in particular way according to the
# specs for the roomba serial interface, which is the like this
#
# command [optional bytes]
#
# 137 speed:high speed:low angle:high angle:low
#
# where 137 is the command, and speed is given as two byte
# signed integer and the angle is a two byte signed integer
command = << 137 :: size(1)-big-integer-unsigned-unit(8),
speed :: size(2)-big-integer-signed-unit(8),
angle :: size(2)-big-integer-signed-unit(8) >>
# No I need to take that and turn it into a list of integers for Json
# rust can take that and turn it into a list of bytes on the other end as
# as well. So this is kind of cool to me, though it's straight forward but
# love the compactness of it
# again being very explict, probaby don't have to but prefer to
list_ints = for << b :: size(1)-integer-unsigned-unit(8) <- command >>, do: x
# just very cool, so compact and I'm sure if I was being less explicit it would be shorter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment