gbraad / pxt-kittenbit

Kitten:Bit - program that is compatible over serial like the KittenBot Rosbot baseboard (WIP)
1 stars 0 forks source link

doMatrixString M20, doMatrixShow M21 #7

Open gbraad opened 4 years ago

gbraad commented 4 years ago

Kittenblock
image

main.ts

commandprocessor.ProcessCommand("M20 Hello, Matrix!")
commandprocessor.ProcessCommand("M21 00008800000088007000000000000000")

Extension

            case 20:
                let msg = command.substr(3, command.length)
                basic.showString(msg)
                break;

            case 21:
                doMatrixShow(cmdArgs[1]);
                break;
    function bufferFromHex(hex: string) {
        const hexStr = "0123456789abcdef"
        const res = pins.createBuffer(hex.length >> 1)
        for (let i = 0; i < hex.length; i += 2) {
            const p0 = hexStr.indexOf(hex.charAt(i))
            const p1 = hexStr.indexOf(hex.charAt(i + 1))
            //if (p0 < 0 || p1 < 0)
            //    throw "Invalid hex"
            res[i >> 1] = (p0 << 4) | p1
        }
        return res
    }

    function doMatrixShow(input: string) {
        let lines = [];

        // strings 4 cells of 4 pixels by 8 lines
        for (let i = 0, charsLength = input.length; i < charsLength; i += 4) {
            lines.push(input.substr(i, 4));
        }

        // only consider the first 5 lines and first 5 pixels
        for (let i = 0; i < 5; i++) {
            let pixelLines = bufferFromHex(lines[i])
            let pixel = pixelLines.getNumber(NumberFormat.UInt8LE, 0)
            if (pixel & 0x08) led.plot(4, i)
            if (pixel & 0x16) led.plot(3, i)
            if (pixel & 0x20) led.plot(2, i)
            if (pixel & 0x40) led.plot(1, i)
            if (pixel & 0x80) led.plot(0, i)
        }
    }

Expected output
image