aboutsummaryrefslogtreecommitdiff
path: root/mavlink-library/src/main/scala/com/github/jodersky/mavlink/Crc.scala
blob: 6150e48d9a6941e8ee60eafd110522e09a6d2a80 (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
24
25
26
27
28
29
30
31
32
33
34
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

}