Last active
November 4, 2024 23:18
-
-
Save victorypoint/04d47caafe66a11cbba912cabeac0868 to your computer and use it in GitHub Desktop.
iFit2 auto-speed and auto-incline control of treadmill via ADB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ' iFit2-Control - 'iFit2 auto-speed and auto-incline control of treadmill via ADB | |
| ' Author: Al Udell | |
| ' Revised: November 4, 2024 | |
| ' To debug - enable wscript.echo and run by cscript in command line | |
| ' On error resume next | |
| createobject("wscript.shell").popup "Ensure treadmill is in manual workout mode and onscreen speed and incline controls are visible", 5, "Warning", 64 | |
| ' Initialize | |
| set wso = createobject("wscript.shell") | |
| buttonCount = 8 ' number of displayed buttons | |
| swipeAmount = 400 ' y swipe amount | |
| swipeSpeed = 300 ' swipe speed in ms | |
| ' *** Start - NordicTrack C2950 Treadmill *** | |
| ' Define button arrays for speed and incline | |
| speedButtons = Array(2, 3, 4, 6, 8, 10, 12, 14, 16, 19) | |
| inclineButtons = Array(-3, 0, 1, 2, 4, 6, 8, 10, 12, 15) | |
| minSpeed = 1 | |
| maxSpeed = 19.3 | |
| ' Define button parameters | |
| speedX = 1808 ' x position of middle of speed buttons | |
| inclineX = 114 ' x position of middle of incline buttons | |
| ' Define button positions and layout | |
| Select Case buttonCount | |
| Case 10 | |
| buttonBottomY = 977 ' y position of middle of bottom button (swipe up) | |
| swipeBottomY = 0 ' y position of middle of bottom button (swipe down) | |
| buttonTopY = 186 ' y position of middle of top button (swipe up) | |
| swipeShift = 0 ' y button shift for swipe | |
| Case 9 | |
| buttonBottomY = 915 | |
| swipeBottomY = 889 | |
| buttonTopY = 211 | |
| swipeShift = 63 | |
| Case 8 | |
| buttonBottomY = 827 | |
| swipeBottomY = 801 | |
| buttonTopY = 212 | |
| swipeShift = 150 | |
| Case 7 | |
| buttonBottomY = 752 | |
| swipeBottomY = 714 | |
| buttonTopY = 227 | |
| swipeShift = 225 | |
| Case 6 | |
| buttonBottomY = 672 | |
| swipeBottomY = 625 | |
| buttonTopY = 233 | |
| swipeShift = 305 | |
| End Select | |
| ' *** End - NordicTrack C2950 Treadmill *** | |
| buttonSpacingY = (buttonBottomY - buttonTopY) / (buttonCount - 1) ' vertical spacing between buttons | |
| Do | |
| ' Prompt the user for a target speed and target incline | |
| targetSpeed = InputBox("Enter a target speed between " & minSpeed & " and " & maxSpeed & ":") | |
| If IsEmpty(targetSpeed) Then wscript.quit | |
| targetIncline = InputBox("Enter a target incline between " & inclineButtons(0) & " and " & inclineButtons(9) & ":") | |
| If IsEmpty(targetIncline) Then wscript.quit | |
| ' Validate inputs are numeric | |
| If IsNumeric(targetSpeed) And IsNumeric(targetIncline) Then | |
| targetSpeed = CDbl(targetSpeed) | |
| targetIncline = CDbl(targetIncline) | |
| ' Validate input ranges for speed and incline | |
| If targetSpeed >= minSpeed And targetSpeed <= maxSpeed And targetIncline >= inclineButtons(0) And targetIncline <= inclineButtons(9) Then | |
| ' Initialize variables for finding the closest speed | |
| closestSpeed = speedButtons(0) | |
| minSpeedDifference = Abs(targetSpeed - speedButtons(0)) | |
| closestSpeedIndex = 0 | |
| ' Loop through the speed buttons to find the closest speed | |
| For i = 1 To UBound(speedButtons) | |
| currentSpeedDifference = Abs(targetSpeed - speedButtons(i)) | |
| If currentSpeedDifference < minSpeedDifference Then | |
| minSpeedDifference = currentSpeedDifference | |
| closestSpeed = speedButtons(i) | |
| closestSpeedIndex = i | |
| End If | |
| Next | |
| ' Calculate pixel location for center of the closest speed button | |
| If (closestSpeedIndex + 1) > buttonCount Then | |
| speedY = Round(buttonBottomY - (buttonSpacingY * closestSpeedIndex) + swipeShift) ' Center y-coordinate of the speed button | |
| Else | |
| speedY = Round(buttonBottomY - (buttonSpacingY * closestSpeedIndex)) ' Center y-coordinate of the speed button | |
| End If | |
| ' Process speed swipe for button visibility | |
| If buttonCount <> 10 Then | |
| If (closestSpeedIndex + 1) > buttonCount Then | |
| cmdString = "adb shell input swipe " & speedX & " " & buttonTopY & " " & speedX & " " & (buttonTopY + swipeAmount) & " " & swipeSpeed ' Swipe down | |
| Else | |
| cmdString = "adb shell input swipe " & speedX & " " & buttonBottomY & " " & speedX & " " & (buttonBottomY - swipeAmount) & " " & swipeSpeed ' Swipe up | |
| End If | |
| 'use synchronous exec | |
| set oexec = wso.exec("cmd /c " & cmdstring) ' Execute adb command | |
| 'wait for completion | |
| Do While oexec.Status = 0 | |
| wscript.sleep 100 | |
| Loop | |
| sValue = oexec.stdout.readline | |
| wscript.echo "Speed swipe: " & cmdString | |
| End If | |
| ' Process speed button click | |
| cmdString = "adb shell input tap " & speedX & " " & speedY | |
| 'use synchronous exec | |
| set oexec = wso.exec("cmd /c " & cmdstring) ' Execute adb command | |
| 'wait for completion | |
| Do While oexec.Status = 0 | |
| wscript.sleep 100 | |
| Loop | |
| sValue = oexec.stdout.readline | |
| wscript.echo "Target speed: " & closestSpeed & ", " & cmdString | |
| ' Initialize variables for finding the closest incline | |
| closestIncline = inclineButtons(0) | |
| minInclineDifference = Abs(targetIncline - inclineButtons(0)) | |
| closestInclineIndex = 0 | |
| ' Loop through the incline buttons to find the closest incline | |
| For i = 1 To UBound(inclineButtons) | |
| currentInclineDifference = Abs(targetIncline - inclineButtons(i)) | |
| If currentInclineDifference < minInclineDifference Then | |
| minInclineDifference = currentInclineDifference | |
| closestIncline = inclineButtons(i) | |
| closestInclineIndex = i | |
| End If | |
| Next | |
| ' Calculate pixel location for center of the closest incline button | |
| If (closestInclineIndex + 1) > buttonCount Then | |
| inclineY = Round(buttonBottomY - (buttonSpacingY * closestInclineIndex) + swipeShift) ' Center y-coordinate of the incline button | |
| Else | |
| inclineY = Round(buttonBottomY - (buttonSpacingY * closestInclineIndex)) ' Center y-coordinate of the incline button | |
| End If | |
| ' Process incline swipe for button visibility | |
| If buttonCount <> 10 Then | |
| If (closestInclineIndex + 1) > buttonCount Then | |
| cmdString = "adb shell input swipe " & inclineX & " " & buttonTopY & " " & inclineX & " " & (buttonTopY + swipeAmount) & " " & swipeSpeed ' Swipe down | |
| Else | |
| cmdString = "adb shell input swipe " & inclineX & " " & buttonBottomY & " " & inclineX & " " & (buttonBottomY - swipeAmount) & " " & swipeSpeed ' Swipe up | |
| End If | |
| 'use synchronous exec | |
| set oexec = wso.exec("cmd /c " & cmdstring) ' Execute adb command | |
| 'wait for completion | |
| Do While oexec.Status = 0 | |
| wscript.sleep 100 | |
| Loop | |
| sValue = oexec.stdout.readline | |
| wscript.echo "Incline swipe: " & cmdString | |
| End If | |
| ' Process incline button click | |
| cmdString = "adb shell input tap " & inclineX & " " & inclineY | |
| 'use synchronous exec | |
| set oexec = wso.exec("cmd /c " & cmdstring) ' Execute adb command | |
| 'wait for completion | |
| Do While oexec.Status = 0 | |
| wscript.sleep 100 | |
| Loop | |
| sValue = oexec.stdout.readline | |
| wscript.echo "Target incline: " & closestIncline & ", " & cmdString | |
| Else | |
| MsgBox "Please enter a speed between " & minSpeed & " and " & maxSpeed & ", and an incline between " & inclineButtons(0) & " and " & inclineButtons(9) & "." | |
| End If | |
| Else | |
| MsgBox "Invalid input. Please enter numeric values for both speed and incline." | |
| End If | |
| Loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment