diff options
author | Jakob Odersky <jodersky@gmail.com> | 2015-03-19 16:08:46 +0100 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2015-03-19 16:08:46 +0100 |
commit | 1cf6e37dc356144f3da2a2dcde75d1ced8bc5ad6 (patch) | |
tree | 287a8e4ce18d3a8c299d7b2a91599a7a48c7b59d /mavlink-library/src/main/scala/com/github/jodersky/mavlink/Crc.scala | |
download | sbt-mavlink-1cf6e37dc356144f3da2a2dcde75d1ced8bc5ad6.tar.gz sbt-mavlink-1cf6e37dc356144f3da2a2dcde75d1ced8bc5ad6.tar.bz2 sbt-mavlink-1cf6e37dc356144f3da2a2dcde75d1ced8bc5ad6.zip |
initial commit
Diffstat (limited to 'mavlink-library/src/main/scala/com/github/jodersky/mavlink/Crc.scala')
-rw-r--r-- | mavlink-library/src/main/scala/com/github/jodersky/mavlink/Crc.scala | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/mavlink-library/src/main/scala/com/github/jodersky/mavlink/Crc.scala b/mavlink-library/src/main/scala/com/github/jodersky/mavlink/Crc.scala new file mode 100644 index 0000000..6150e48 --- /dev/null +++ b/mavlink-library/src/main/scala/com/github/jodersky/mavlink/Crc.scala @@ -0,0 +1,35 @@ +package com.github.jodersky.mavlink + +/** + * X.25 CRC calculation for MAVlink messages. The checksum must be initialized, + * updated with each field of a message, and then finished with the message + * id. + */ +case class Crc(val crc: Int = 0xffff) extends AnyVal { + + /** + * Accumulates data into a new checksum. + */ + def accumulate(datum: Byte): Crc = { + val d = datum & 0xff + var tmp = d ^ (crc & 0xff) + tmp ^= (tmp << 4) & 0xff; + Crc( + ((crc >> 8) & 0xff) ^ (tmp << 8) ^ (tmp << 3) ^ ((tmp >> 4) & 0xff)) + } + + def accumulate(data: Seq[Byte]): Crc = { + var next = this + for (d <- data) { + next = next.accumulate(d) + } + next + } + + /** Least significant byte of checksum. */ + def lsb: Byte = crc.toByte + + /** Most significant byte of checksum. */ + def msb: Byte = (crc >> 8).toByte + +}
\ No newline at end of file |