水曜日, 10月 09, 2019

micro:bit 簡易オシロスコープ

まだ実機確認していません(汗)

簡易設計仕様は以下の通り
  • P0電圧をオシロスコープのようにスクロールさせながら表示する。下限は0,上限は3.3V
  • 標準では1秒に1回電圧を読み,1ドットずつスクロール。[A] を押すとその時間が半分(0.5秒に1度電圧を読む)になり,[B]を押すと倍になる。
  • [A][B]を同時に押すと,「メーター」モードになり,メーターのように 0 ~ 3.3Vを表示。このモード時に[A] または [B] を押し続けると,電圧値を表示する。[A][B]同時押しでオシロスコープモードに戻る。

input.onButtonPressed(Button.AB, function () {
    if (test_mode == "osc") {
        test_mode = "meter"
        osc_time = step_time
        step_time = 500
    } else {
        test_mode = "osc"
        step_time = osc_time
    }
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        `)
})
input.onButtonPressed(Button.A, function () {
    if (test_mode == "osc") {
        if (step_time < 100000) {
            step_time = step_time * 2
        }
        basic.showNumber(step_time / 1000)
        basic.pause(100)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            `)
    }
})
input.onButtonPressed(Button.B, function () {
    if (test_mode == "osc") {
        if (step_time > 2) {
            step_time = step_time / 2
        }
        basic.showNumber(step_time / 1000)
        basic.pause(100)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            `)
    }
})
let osc_time = 0
let step_time = 0
let test_mode = ""
let prev_y = 0
let y1 = 0
let ys = 0
let xs = 0
let y = 0
test_mode = "osc"
let xt = [0, 0, 0, 0, 0, 1, 2, 3, 4, 4, 4, 4, 4, 4]
let yt = [0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 3, 2, 1, 0]
test_mode = "osc"
step_time = 1000
osc_time = step_time
let next_time = step_time + input.runningTime()
prev_y = 4
basic.showLeds(`
    . . . . .
    . . . . .
    . . . . .
    . . . . .
    . . . . .
`)
basic.forever(function () {
    if (input.runningTime() > next_time) {
        next_time = step_time + input.runningTime()
        if (test_mode == "meter") {
            if (input.buttonIsPressed(Button.A) || input.buttonIsPressed(Button.B)) {
                while (input.buttonIsPressed(Button.A) || input.buttonIsPressed(Button.B)) {
                    basic.showNumber(Math.round(pins.analogReadPin(AnalogPin.P0) / 1023 * 1000) * 3.3 / 1000)
                }
                basic.showLeds(`
                    . . . . .
                    . . . . .
                    . . . . .
                    . . . . .
                    . . . . .
                    `)
            }
            y = Math.round(pins.analogReadPin(AnalogPin.P0) / 1023 * 13)
            if (prev_y != y) {
                basic.showLeds(`
                    . . . . .
                    . . . . .
                    . . . . .
                    . . . . .
                    . . . . .
                    `)
            } else {
                led.plot(2, 4 - 2)
                led.plot((2 + xt[y]) / 2, 4 - (2 + yt[y]) / 2)
                led.plot(xt[y], 4 - yt[y])
            }
            prev_y = y
        } else {
            if (test_mode == "osc") {
                y = 4 - Math.round(pins.analogReadPin(AnalogPin.P0) / 1023 * 4)
                for (xs = 0; xs <= 4; xs++) {
                    for (ys = 0; ys <= 4; ys++) {
                        if ((led.point(xs + 1, ys)) && (xs < 4)) {
                            led.plot(xs, ys)
                        } else {
                            led.unplot(xs, ys)
                        }
                    }
                }
                y1 = Math.min(y, prev_y)
                for (ys = y1; ys <= Math.max(y, prev_y); ys++) {
                    led.plot(4, ys)
                }
                prev_y = y
            } else {
                basic.showNumber(Math.round(pins.analogReadPin(AnalogPin.P0) / 1023 * 1000) / 1000 * 3.3)
            }
        }
    }
})

0 件のコメント: