pxt-calliope/libs/core/compass.ts

156 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-11-25 17:27:39 +01:00
const compassImagesLeft = [
images.createImage(`
. . # . .
. . . . .
. . # . .
. . . . .
. . . . .
`),
images.createImage(`
. # . . .
. . . . .
. . # . .
. . . . .
. . . . .
`),
images.createImage(`
# . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`),
images.createImage(`
. . . . .
# . . . .
. . # . .
. . . . .
. . . . .
`),
images.createImage(`
. . . . .
. . . . .
# . # . .
. . . . .
. . . . .
`),
images.createImage(`
. . . . .
. . . . .
. . # . .
# . . . .
. . . . .
`),
images.createImage(`
. . . . .
. . . . .
. . # . .
. . . . .
# . . . .
`),
images.createImage(`
. . . . .
. . . . .
. . # . .
. . . . .
. # . . .
`)];
2021-11-25 17:27:39 +01:00
const compassImagesRight = [
images.createImage(`
. . . . .
. . . . .
. . # . .
. . . . .
. . # . .
`),
images.createImage(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . # .
`),
images.createImage(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . #
`),
images.createImage(`
. . . . .
. . . . .
. . # . .
. . . . #
. . . . .
`),
images.createImage(`
. . . . .
. . . . .
. . # . #
. . . . .
. . . . .
`),
images.createImage(`
. . . . .
. . . . #
. . # . .
. . . . .
. . . . .
`),
images.createImage(`
. . . . #
. . . . .
. . # . .
. . . . .
. . . . .
`),
images.createImage(`
. . . # .
. . . . .
. . # . .
. . . . .
. . . . .
`)];
2021-11-25 17:27:39 +01:00
const compassImages = compassImagesLeft.concat(compassImagesRight);
const compassImagesDownside = compassImagesRight.concat(compassImagesLeft);
namespace basic {
/**
* Draws needle on the screen which always points to north
* @param interval the amount of time (milliseconds) to show the needle. Default is 600.
*/
//% weight=50 blockGap=8
//% blockId=basic_show_compass
//% block="show compass needle for $interval|ms"
//% interval.shadow=timePicker
//% interval.min=1
//% interval.defl=600
//% parts="ledmatrix"
//% advanced=true
2021-11-25 17:27:39 +01:00
//% group="LED matrix"
export function showCompass(interval = 600) {
let i = 0
let startTime = input.runningTime()
let endTime = startTime + interval
let refreshRate = 100
let rest = 0
while ((endTime) > (input.runningTime() + refreshRate)) {
i = Math.round((input.compassHeading() - 11.25) / 22.5)
2021-11-25 17:27:39 +01:00
let images = (input.isGesture(Gesture.ScreenDown)) ? compassImagesDownside : compassImages;
images[i].showImage(0, refreshRate)
}
rest = (endTime - input.runningTime())
if(rest > 0) {
i = Math.round((input.compassHeading() - 11.25) / 22.5)
2021-11-25 17:27:39 +01:00
let images = (input.isGesture(Gesture.ScreenDown)) ? compassImagesDownside : compassImages;
images[i].showImage(0, rest)
}
}
}