forked from bartprescott/vbus-collector
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvbus.c
48 lines (39 loc) · 1.04 KB
/
vbus.c
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
// vbus.c
//
// (c) Hewell Technology Ltd. 2014
//
//****************************************************************************
#include "vbus.h"
unsigned char vbus_calc_crc(const unsigned char *buffer, int offset, int length)
{
unsigned char crc = 0x7F;
for (int idx = 0; idx < length; idx++)
{
crc = (crc - buffer [offset + idx]) & 0x7F;
}
return crc;
}
void vbus_extract_septett(unsigned char *buffer, int offset, int length)
{
unsigned char septett = 0;
for (int idx = 0; idx < length; idx++)
{
if (buffer[offset + idx] & 0x80)
{
buffer[offset + idx] &= 0x7F;
septett |= (1 << idx);
}
}
buffer [offset + length] = septett;
}
void vbus_inject_septett(unsigned char *buffer, int offset, int length)
{
unsigned char septett = buffer[offset + length];
for (int idx = 0; idx < length; idx++)
{
if (septett & (1 << idx))
{
buffer[offset + idx] |= 0x80;
}
}
}