index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. let device = require('./device');
  2. let blessed = require('blessed');
  3. let screen = blessed.screen({
  4. smartCSR: true
  5. });
  6. screen.title = 'OpenPrepPad';
  7. let container = blessed.box({
  8. top: 'center',
  9. left: 'center',
  10. width: 50,
  11. height: 17,
  12. border: {
  13. type: 'line'
  14. },
  15. style: {
  16. bg: 'cyan-bg'
  17. }
  18. })
  19. let box = blessed.BigText({
  20. top: 0,
  21. right: 0,
  22. width: 'shrink',
  23. height: 'shrink',
  24. tags: true,
  25. style: {
  26. fg: 'white'
  27. }
  28. });
  29. container.append(box);
  30. screen.append(container);
  31. let statusLine = blessed.Text({
  32. bottom: 0,
  33. width: '100%',
  34. height: 'shrink',
  35. content: 'Connecting...',
  36. style: {
  37. fg: 'white',
  38. bg: 'blue'
  39. }
  40. });
  41. screen.append(statusLine);
  42. let helpBox = blessed.box({
  43. top: 0,
  44. left: 0,
  45. width: 'shrink',
  46. height: 'shrink',
  47. content: 'q - Quit\nz - Zero/Tare'
  48. });
  49. screen.append(helpBox);
  50. let dataTable = blessed.Table({
  51. bottom: 1,
  52. right: 0,
  53. width: 19,
  54. height: 'shrink',
  55. border: {
  56. type: 'line'
  57. },
  58. data: [
  59. [ 'Accel', 'Value' ],
  60. [ 'X', '-1' ],
  61. [ 'Y', '-1' ],
  62. [ 'Z', '-1' ]
  63. ]
  64. });
  65. screen.append(dataTable);
  66. screen.key(['escape', 'q', 'C-c'], function(ch, key) {
  67. process.exit(0);
  68. });
  69. screen.key(['z'], function(ch, key) {
  70. device.button$.onNext('');
  71. });
  72. // Focus our element.
  73. box.focus();
  74. // Render the screen.
  75. screen.render();
  76. device.weight$.subscribe(w => {
  77. box.setContent(w+'g');
  78. screen.render();
  79. })
  80. device.connStatus$.subscribe(s => {
  81. if(s.state == 'connected') {
  82. statusLine.setContent('Connected to Prep Pad '+s.serial);
  83. }
  84. else {
  85. statusLine.setContent('Connecting...');
  86. }
  87. screen.render();
  88. })
  89. device.accel$.subscribe(s => {
  90. let data = [
  91. [ 'Accel', 'Value' ],
  92. [ 'X', ''+s[0] ],
  93. [ 'Y', ''+s[1] ],
  94. [ 'Z', ''+s[2] ]
  95. ]
  96. dataTable.setData(data);
  97. screen.render();
  98. })