commonFunction.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module.exports = {
  2. // buffer 转16进制
  3. buf2hex:function (buffer) {
  4. // create a byte array (Uint8Array) that we can use to read the array buffer
  5. const byteArray = new Uint8Array(buffer);
  6. // for each element, we want to get its two-digit hexadecimal representation
  7. const hexParts = [];
  8. for(let i = 0; i < byteArray.length; i++) {
  9. // convert value to hexadecimal
  10. const hex = byteArray[i].toString(16);
  11. // pad with zeros to length 2
  12. const paddedHex = ('00' + hex).slice(-2);
  13. // push to array
  14. hexParts.push(paddedHex);
  15. }
  16. // join all the hex values of the elements into a single string
  17. return hexParts.join('');
  18. },
  19. // 足压数据补齐点位为60*60 并进行点扩散
  20. setPressureData:function (data) {
  21. let dataAll = []
  22. // 补到之前 60*60 个点位
  23. for (let j =0;j<60;j++) {
  24. dataAll[j] = []
  25. for (let k =0;k<60;k++) {
  26. if(typeof (data[j]) == 'undefined') {
  27. dataAll[j][k] = 0
  28. } else {
  29. if(typeof (data[j][k]) == 'undefined') {
  30. dataAll[j][k] = 0
  31. } else {
  32. dataAll[j][k] = data[j][k]
  33. }
  34. }
  35. }
  36. }
  37. return pressureDataProcess(dataAll,1,4)
  38. },
  39. // arrayBuffer 转string
  40. ab2Ascii(buffer) {
  41. var str = Array.prototype.map.call(
  42. new Uint8Array(buffer),
  43. function (bit) {
  44. return String.fromCharCode(bit);
  45. }
  46. )
  47. return str.join('');
  48. }
  49. }
  50. function pressureDataProcess(a,scale,size) {
  51. var height = 60;
  52. var width = 60;
  53. var newHeight = 60*scale;
  54. var newWidth = 60*scale;
  55. var u =0.0;
  56. var v = 0.0;
  57. var x = 0.0;
  58. var y = 0.0;
  59. var m = 0;
  60. var n = 0;
  61. let temp = new Array()
  62. let rearr = []
  63. for(let i = 0;i<newHeight;++i) {
  64. temp[i] = new Array()
  65. for(let j=0;j<newWidth;++j) {
  66. y = i/scale;
  67. x = j/scale;
  68. m = parseInt(y);
  69. n = parseInt(x);
  70. v = y-m;
  71. u = x-n;
  72. let tt = {}
  73. if(m<height-1 &&n<width-1){
  74. temp[i][j] = parseInt((1.0-v)*((1.0-u)*a[m][n]+u*a[m][n+1])
  75. + v*((1.0-u)*a[m+1][n] + u*a[m+1][n+1])
  76. )
  77. } else {
  78. temp[i][j] = a[m][n]
  79. }
  80. tt.x = i*size
  81. tt.y = j*size
  82. if(temp[i][j]>0) {
  83. tt.value = temp[i][j]
  84. rearr.push(tt)
  85. }
  86. }
  87. }
  88. return rearr
  89. }