summaryrefslogtreecommitdiffstats
path: root/app/util.c
blob: ff35288f1fb150dbf0cc7b4ccc2b700231970a91 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "project.h"

int check_parity (uint8_t *d, unsigned s, unsigned e, uint8_t p)
{
  unsigned i;

  for (i = s; i <= e; ++i)
    p ^= d[i];

  return !p;
}


unsigned bcd (uint8_t *d, unsigned s, unsigned e)
{
  unsigned ret = 0, c, b, i;

  for (i = e, c = 1, b = 0; i >= s; --i, b++) {

    if (d[i]) ret += c;


    switch (b & 3) {
    case 0:
    case 1:
    case 2:
      c <<= 1;
      break;

    default:
      c >>= 3;
      c *= 10;
    }
  }

  return ret;
}



unsigned le_bcd (uint8_t *d, unsigned s, unsigned e)
{
  unsigned ret = 0, c, b, i;

  for (i = s, c = 1, b = 0; i <= e; ++i, b++) {

    if (d[i]) ret += c;


    switch (b & 3) {
    case 0:
    case 1:
    case 2:
      c <<= 1;
      break;

    default:
      c >>= 3;
      c *= 10;
    }
  }

  return ret;
}