Advertisement
Guest User

G29 amplitude and interval

a guest
Dec 6th, 2021
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Control Logitech G29 wheel with intervals and amplitude
  2.  
  3. # Install
  4. npm init
  5. npm install logitech-g29
  6.  
  7. # Run
  8. npm start
  9. */
  10.  
  11. const g = require('logitech-g29')
  12. const readline = require('readline');
  13. const os = require('os')
  14.  
  15. readline.emitKeypressEvents(process.stdin);
  16. process.stdin.setRawMode(true);
  17.  
  18. // Initialization
  19.  
  20. const options = {
  21.     autocenter: false,
  22.     range: 270
  23. }
  24.  
  25. const defaults = {
  26.     forceFriction: 0    // 0 = no friction, 0.5 = half strength, 1 = full strength
  27. }
  28.  
  29. let lastFriction = defaults.forceFriction
  30.  
  31. const min_interval = 50;
  32. const max_interval = 4000;
  33.  
  34. // Variables
  35.  
  36. let interval = 500;
  37. let nextInterval = false;
  38. let frq = 25;
  39. let amplitude = 0.1
  40. let nextAmplitude = false;
  41. let drift = 0.02
  42.  
  43. // Helper function
  44.  
  45. function mathRound(number) {
  46.     return Math.round(number * 100) / 100;
  47. }
  48.  
  49. // Friction
  50.  
  51. function setForceFriction(val) {
  52.     if (val === 0) {
  53.         lastFriction = 0
  54.     } else if (val > 0) {
  55.         lastFriction += 0.1
  56.     } else {
  57.         lastFriction -= 0.1
  58.     }
  59.  
  60.     lastFriction = mathRound(lastFriction)
  61.  
  62.     if (lastFriction < 0) lastFriction = 0
  63.     if (lastFriction > 1) lastFriction = 1
  64.  
  65.     console.log('forceFriction(' + lastFriction + ')')
  66.  
  67.     g.forceFriction(lastFriction)
  68. }
  69.  
  70. // Amplitude
  71.  
  72. function setAmplitude(val) {
  73.     if (amplitude == val)
  74.         return
  75.    
  76.     if (val < 0)
  77.         val = 0
  78.     else if ((interval > 400) && val > 0.2)
  79.         val = 0.2
  80.     else if ((interval > 200) && val > 0.3)
  81.         val = 0.3
  82.     else if (val > 0.4)
  83.         val = 0.4
  84.    
  85.    
  86.     nextAmplitude = mathRound(val)
  87. }
  88.  
  89. function increaseAmplitude() {
  90.     setAmplitude(amplitude + 0.01)
  91. }
  92.  
  93. function decreaseAmplitude() {
  94.     setAmplitude(amplitude - 0.01)
  95. }
  96.  
  97. // Drift
  98.  
  99. function setDrift(val) {
  100.     if (val < -0.5)
  101.         val = -0.5
  102.     else if (val > 0.5)
  103.         val = 0.5
  104.    
  105.     drift = mathRound(val)
  106.  
  107.     console.log(color.cyan('drift'), drift)
  108. }
  109.  
  110. function increaseDrift() {
  111.     setDrift(drift + 0.01)
  112. }
  113.  
  114. function decreaseDrift() {
  115.     setDrift(drift - 0.01)
  116. }
  117.  
  118. // Interval
  119.  
  120. function updateInterval(val) {
  121.     if ((val > max_interval) || (val < min_interval)) {
  122.         console.log(color.red("WARN: Invalid interval ") + val)
  123.         return
  124.     }
  125.    
  126.     nextInterval = val
  127.  
  128.     // Sanity check for amplitued with the new interval
  129.     setAmplitude(amplitude)
  130. }
  131.  
  132. function increaseInterval() {
  133.     let step = 500
  134.     if (interval < 300)
  135.         step = 50
  136.     else if (interval < 1000)
  137.         step = 100
  138.  
  139.     let val = interval + step
  140.  
  141.     if (val > max_interval)
  142.         return
  143.  
  144.     updateInterval(val)
  145. }
  146.  
  147. function decreaseInterval() {
  148.     let step = 50
  149.     if (interval > 1000)
  150.         step = 500
  151.     else if (interval > 300)
  152.         step = 100
  153.  
  154.     let val = interval - step
  155.  
  156.     if (val < min_interval)
  157.         return
  158.  
  159.     updateInterval(val)
  160. }
  161.  
  162. // Position
  163.  
  164. let increase = false;
  165. let extreme = false;
  166. let last = 0;
  167.  
  168. // Wait until end position before changing amplitude and interval
  169. function delayChange(x) {
  170.     if (x > last && !increase) {
  171.         extreme = true
  172.         increase = true
  173.     } else if (x < last && increase) {
  174.         extreme = true
  175.         increase = false
  176.     }
  177.  
  178.     if (extreme) {
  179.         if (nextAmplitude) {
  180.             amplitude = nextAmplitude
  181.             nextAmplitude = false
  182.             console.log(color.magenta('amplitude'), amplitude)
  183.         }
  184.  
  185.         if (nextInterval) {
  186.             interval = nextInterval
  187.             nextInterval = false
  188.             console.log(color.green('interval ') + interval)
  189.         }
  190.        
  191.         extreme = false
  192.     }
  193.  
  194.     last = x
  195. }
  196.  
  197. function updatePosition() {
  198.     let x = Math.cos(Math.PI * 2 * new Date().getTime() / interval); // Value between 0 and 1
  199.  
  200.     x *= amplitude //  Multiplied by amplitude to decrease movement
  201.     x += 0.5 // Middle is 0.5 which is no movement
  202.     x += drift // Add extra weight towards one side
  203.     x = mathRound(x)
  204.     //console.log(x)
  205.     delayChange(x)
  206.    
  207.     g.forceConstant(x)
  208. }
  209.  
  210. // Bind buttons
  211.  
  212. g.connect(options, function(err) {
  213.     g.on('wheel-button_plus', function(val) {
  214.         if (val == 1)
  215.             increaseInterval()
  216.     })
  217.     g.on('wheel-button_minus', function(val) {
  218.         if (val == 1)
  219.             decreaseInterval()
  220.     })
  221.     g.on('wheel-spinner', function(val) {
  222.         if (val !== 0) {
  223.             setForceFriction(val)
  224.         }
  225.     })
  226.     g.on('wheel-button_spinner', function(val) {
  227.         if (val === 1) {
  228.             setForceFriction(0)
  229.         }
  230.     })
  231.  
  232.     process.stdin.on('keypress', (str, key) => {
  233.         if (key.ctrl && key.name === 'c') {
  234.             g.forceConstant(0.5)
  235.             clearInterval(loop)
  236.  
  237.             process.exit();
  238.         } else {
  239.             // console.log(`You pressed the "${str}" key`);
  240.             // console.log();
  241.             //console.log(key);
  242.             // console.log();
  243.             if (key.name == 'up') {
  244.                 decreaseInterval()
  245.             } else if (key.name == 'down') {
  246.                 increaseInterval()
  247.             } else if (key.name == 'left') {
  248.                 decreaseAmplitude()
  249.             } else if (key.name == 'right') {
  250.                 increaseAmplitude()
  251.             } else if (key.name == 'q') {
  252.                 increaseDrift()
  253.             } else if (key.name == 'a') {
  254.                 decreaseDrift()
  255.             } else if (key.name == '1') {
  256.                 updateInterval(4000)
  257.                 setAmplitude(0.13)
  258.             } else if (key.name == '2') {
  259.                 updateInterval(2000)
  260.                 setAmplitude(0.16)
  261.             } else if (key.name == '3') {
  262.                 updateInterval(1000)
  263.                 setAmplitude(0.18)
  264.             }  else if (key.name == '4') {
  265.                 updateInterval(500)
  266.                 setAmplitude(0.2)
  267.             } else if (key.name == '5') {
  268.                 updateInterval(350)
  269.                 setAmplitude(0.25)
  270.             }  else if (key.name == '6') {
  271.                 updateInterval(250)
  272.                 setAmplitude(0.3)
  273.             } else if (key.name == '7') {
  274.                 updateInterval(200)
  275.                 setAmplitude(0.4)
  276.             } else if (key.name == 'escape') {
  277.                 setAmplitude(0.01)
  278.             }
  279.         }
  280.     });
  281.  
  282.     console.log(color.cyan('Wheel ready.'))
  283.     console.log()
  284.     console.log(color.green('Increase speed with up, decrease with down.'))
  285.     console.log(color.magenta('Increase amplitude/movement with right, decrease with left.'))
  286.     console.log(color.cyan('Increase drift/weight right with q, left with a.'))
  287.     console.log(color.yellow('Number keys are quick settings, 1-7, slow to fast'))
  288.     console.log()
  289.     console.log(color.grey('Play with forceFriction() by using the Red Spinner. Rotate right for more, left for less, and press the spinner button to reset.'))
  290.     console.log()
  291.     console.log('Quit with Ctrl + C')
  292.  
  293.     // Start interval that changes wheel position
  294.     let loop = setInterval(updatePosition, frq)
  295. })
  296.  
  297.  
  298. // Terminal Colors
  299.  
  300. let color = {}
  301.  
  302. const colors = {
  303.     black:   [30, 39],
  304.     red:     [31, 39],
  305.     green:   [32, 39],
  306.     yellow:  [33, 39],
  307.     blue:    [34, 39],
  308.     magenta: [35, 39],
  309.     cyan:    [36, 39],
  310.     white:   [37, 39],
  311.     gray:    [90, 39],
  312.     grey:    [90, 39],
  313.     // bright colors
  314.     redBright:     [91, 39],
  315.     greenBright:   [92, 39],
  316.     yellowBright:  [93, 39],
  317.     blueBright:    [94, 39],
  318.     magentaBright: [95, 39],
  319.     cyanBright:    [96, 39],
  320.     whiteBright:   [97, 39]
  321. }
  322.  
  323. const platform = os.platform()
  324.  
  325. function showColor(hue, info = '') {
  326.     // ` signifies a template literal
  327.     return `\u001B[${colors[hue][0]}m` + info + `\u001B[${colors[hue][1]}m`
  328. } // showColor
  329.  
  330. function setupColors() {
  331.     for (let item in colors) {
  332.         const hue = item
  333.         color[hue] = function (info) {
  334.             return showColor(hue, info)
  335.         }
  336.     }
  337.  
  338.     if (platform === 'win32' || platform === 'win64') {
  339.         // use brigher versions of these colors
  340.         color.red     = color.redBright
  341.         color.green   = color.greenBright
  342.         color.yellow  = color.yellowBright
  343.         color.blue    = color.blueBright
  344.         color.magenta = color.magentaBright
  345.         color.cyan    = color.cyanBright
  346.         color.white   = color.whiteBright
  347.     }
  348. } // setupColors
  349.  
  350. setupColors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement