forked from Ubudu/uBeacon-uart-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataUtils.js
More file actions
124 lines (108 loc) · 2.54 KB
/
Copy pathdataUtils.js
File metadata and controls
124 lines (108 loc) · 2.54 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*jslint node: true */
'use strict';
/*
*
*/
module.exports = {
/*
* @src: https://gist.github.com/joaomaia/3892692
* bcd2number -> takes a nodejs buffer with a BCD and returns the corresponding number.
* input: nodejs buffer
* output: number
*/
bcd2number: function(bcd)
{
var n = 0;
var m = 1;
for(var i = 0; i<bcd.length; i+=1) {
n += (bcd[bcd.length-1-i] & 0x0F) * m;
n += ((bcd[bcd.length-1-i]>>4) & 0x0F) * m * 10;
m *= 100;
}
return n;
},
/*
* @src: https://gist.github.com/joaomaia/3892692
* number2bcd -> takes a number and returns the corresponding BCD in a nodejs buffer object.
* input: 32 bit positive number, nodejs buffer size
* output: nodejs buffer
*/
number2bcd: function(number)
{
var bcd = ((number/10)<<4) + (number%10);
var retVal = bcd.toString(16);
if( retVal.length < 2 ){
retVal = '0' + retVal;
}
return retVal;
},
uint8ToHex: function(number)
{
var hex = number.toString(16);
if( hex.length < 2 ){
hex = '0' + hex;
}
return hex;
},
/**
*
*/
stringToHexString: function(str)
{
var hexStr = '';
for( var i = 0 ; i < str.length ; i++ ){
hexStr += this.uint8ToHex( str.charCodeAt(i) );
}
return hexStr;
},
/**
*
*/
hexStringToString: function(hexStr)
{
var str = '';
for( var i = 0 ; i < hexStr.length ; i+=2 ){
str += String.fromCharCode(parseInt(hexStr.substr(i, 2), 16));
}
return str;
},
/**
*
*/
zeroPad: function(num, places)
{
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join('0') + num;
},
/**
*
*/
versionGreaterThanOrEqual: function( inputVersion, compareVersion )
{
if( inputVersion == null || compareVersion == null ){
return false;
}
var tmpIn = inputVersion.split('.');
var tmpCmp = compareVersion.split('.');
var inVersion = {
major: parseInt(tmpIn[0]),
minor: parseInt(tmpIn[1]),
patch: parseInt(tmpIn[2]),
};
var cmpVersion = {
major: parseInt(tmpCmp[0]),
minor: parseInt(tmpCmp[1]),
patch: parseInt(tmpCmp[2]),
};
if( inVersion.major > cmpVersion.major ){
return true;
}
if( inVersion.major == cmpVersion.major && inVersion.minor > cmpVersion.minor ){
return true;
}
if( inVersion.major == cmpVersion.major && inVersion.minor == cmpVersion.minor && inVersion.patch >= cmpVersion.patch ){
return true;
}
return false;
}
};