Scripting JS help
nagup opened this issue · comments
nagup commented
So I know the scripting interface uses javascript. I am trying to write a script that is basically 4 messages, with different intervals between them.
I dont know how to do this because it seems the scripting interface doesn't recognise set timeout and other ways to delay. How can I do this?
To be specific I am trying to send
Data: FD FF
Wait for 90ms to 100ms.
Data: FC FF
Wait for 158ms to 160ms.
Data: FC FF
Wait for 158ms to 160ms.
Data: FC FF
Completion of the Sequence.
Charles Galpin commented
You can set a single timer afaik, so in the setup method set it to the lowest granularity needed, and then track the time in the tick function.
Something like
// global variables to keep track of state
var tickIntervalMillis = 2
var count = 0
var currentMillis = 0
var lastSendMillis = 0
function setup () {
host.log("Send test")
can.setFilter(...)
can.sendFrame(<FD FF frame here>)
host.setTickInterval(tickIntervalMillis)
}
// runs every 2 ms
function tick () {
currentMillis = Date.now()
const interval = currentMillis - lastSendMillis
// send based on state and time passed
if ( count == 0 && interval > 90) {
sendFCFF()
} else if ( count == 1 && interval > 158) {
sendFCFF()
} else if ( count == 2 && interval > 158) {
sendFCFF()
}
}
function send () {
can.sendFrame(<FC FF frame here>)
count++
lastSend = currentMillis
}