BLE_RGB.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //随机获取7种颜色中的一种颜色,常亮显示
  2. const getRandomRGB = () => {
  3. let index = Math.floor(Math.random() * 7);
  4. console.log("index:" + index);
  5. let head = '5B0E02';
  6. let tail = '5D';
  7. for (let i = 0; i < 7; i++) {
  8. if (i == index) {
  9. head = head.concat('FF')
  10. } else {
  11. head = head.concat('00')
  12. }
  13. //时间设置为0,常亮
  14. head = head.concat('00');
  15. }
  16. head = head.concat(tail);
  17. console.log(head);
  18. return head;
  19. }
  20. //随机获取7种颜色中的三种颜色,随机显示
  21. const getRandomThreeRGB = () => {
  22. //原数组
  23. var arr = [0, 1, 2, 3, 4, 5, 6];
  24. //输出数组
  25. var out = [];
  26. //输出个数
  27. var num = 3;
  28. while (out.length < num) {
  29. var temp = (Math.random() * arr.length) >> 0;
  30. out.push(arr.splice(temp, 1));
  31. }
  32. console.log("out:" + out);
  33. let head = '5B0E02';
  34. let tail = '5D';
  35. for (let i = 0; i < 7; i++) {
  36. let bHas = false;
  37. for (let j = 0; j < out.length; j++) {
  38. // console.log(i + '==' + out[j])
  39. if (i == out[j]) {
  40. bHas = true;
  41. }
  42. if (j == out.length - 1) {
  43. //需要灭灯的时间
  44. let addStr = bHas ? '08' : '00';
  45. head = head.concat(addStr);
  46. // console.log(addStr)
  47. }
  48. }
  49. //时间设置为0,常亮, 对应灭灯时间 20ms
  50. let timeStr = bHas ? '02' : '00';
  51. head = head.concat(timeStr);
  52. }
  53. head = head.concat(tail);
  54. console.log(head);
  55. return head;
  56. }
  57. export default {
  58. getRandomRGB,
  59. getRandomThreeRGB
  60. }