aboutsummaryrefslogtreecommitdiff
path: root/project/mavlink-library/src/main/twirl/org/mavlink/Crc.scala.txt
blob: 61b5c6fd854c4ca7a40a725de3248a052351260d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@()

package org.mavlink

/**
 * X.25 CRC calculation for MAVlink messages. The checksum must be initialized,
 * updated with witch field of the message, and then finished with the message
 * id.
 */
case class Crc(val crc: Int = 0xffff) extends AnyVal {

  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 lsb: Byte = crc.toByte
  def msb: Byte = (crc >> 8).toByte

}