-
Notifications
You must be signed in to change notification settings - Fork 0
Script Examples
Here is an example of using the right scroll wheel on a Tesla's steering wheel to scroll through tiles on CANgage. The following Lua script will read the "ticks" from the scroll wheel, which indicates to the script that the wheel is being scrolled up or down, and then use that to increase or decrease the current page number and provide that as a variable that can be subscribed to by CANgage.
First, create the following Lua script on the CANserver:
-- set how many pages (tiles) you have configured
local totalTiles = 3
-- get current tile number
local tileNumber = CANServer_getVar("tileNumber") or 0
local prevTicks = CANServer_getVar("prevTicks") or 0
-- get the number of ticks from scroll wheel and use it to change pages
if (CANServer_getAnalysisVar("SwitchStatusIndex") == 1) then
ticks = CANServer_getAnalysisVar("RightScrollTicks")
-- check ticks value against previous value so the action is not duplicated
-- and ignore the result 21 which is caused when there is a multiplexor timing glitch
if ((ticks ~= prevTicks) and (ticks ~= 21)) then
tileNumber = tileNumber + ticks
-- loop page number when out of range
if (page >= totalTiles) then
tileNumber = 0
elseif (tileNumber < 0) then
page = totalTiles - 1
end
CANServer_setVar("tileNumber", tileNumber)
CANServer_setVar("prevTicks", ticks)
end
endThen add the following Analysis Items to your CANserver:
{
"n": "SwitchStatusIndex",
"bid": "-1",
"fid": 962,
"sb": 0,
"bl": 2,
"f": 1,
"so": 0,
"s": false,
"bo": true,
"log": false,
"v": 3
}
{
"n": "RightScrollTicks",
"bid": "-1",
"fid": 962,
"sb": 24,
"bl": 6,
"f": 1,
"so": 0,
"s": true,
"bo": true,
"log": false,
"v": 3
}Finally, configure CANgage with the same number of Tiles you specified in the Lua script, and add a Scroll configuration:
Here's another example Lua script that will scroll automatically between two tiles every two seconds:
-- set how many pages (tiles) you have configured
local totalTiles = 2
local currentMills = CANServer_millis()
local lastSwitchTime = CANServer_getVar("lastSwitchTime") or 0
if (currentMills - lastSwitchTime > 2000) then
local tileNumber = CANServer_getVar("tileNumber") or 0
tileNumber = tileNumber + 1
if (tileNumber >= totalTiles) then tileNumber = 0 end
CANServer_setVar("tileNumber", tileNumber)
CANServer_setVar("lastSwitchTime", currentMills)
endFor this example, since the scrolling happens continuously, it's probably beneficial to turn off the scrolling animation:
This example will attempt to match the brightness of the CANgage display with a Tesla's center screen. The values in the Lua script can be tweaked to create different results by changing how the brightness is scaled.
Add the following code anywhere in your Lua script on the CANserver:
if ((CANServer_getAnalysisVar("DisplayOn") == 0) and (CANServer_getAnalysisVar("UI_systemActive") == 1)) then
-- center screen is off, so set brightness to 0 which will turn off the CANgage display
CANServer_setVar("brightness", 0)
else
-- center screen is on, so set brightness by converting Tesla's brightness values (5.5 to 100) to CANgage's (0 to 255)
CANServer_setVar("brightness", ((CANServer_getAnalysisVar("UI_displayBrightness") - 5.5) * 2.7))
endThen add the following Analysis Items to your CANserver:
{
"n": "DisplayOn",
"bid": "-1",
"fid": 851,
"sb": 5,
"bl": 1,
"f": 1,
"so": 0,
"s": false,
"bo": true,
"log": false,
"v": 3
}
{
"n": "UI_displayBrightness",
"bid": "-1",
"fid": 627,
"sb": 32,
"bl": 8,
"f": 0.5,
"so": 0,
"s": false,
"bo": true,
"log": false,
"v": 3
}
{
"n": "UI_systemActive",
"bid": "-1",
"fid": 851,
"sb": 8,
"bl": 1,
"f": 1,
"so": 0,
"s": false,
"bo": true,
"log": false,
"v": 3
}Finally, configure CANgage with the Brightness Global Element setting: