gbraad / pxt-kittenbit

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

Simple test #1

Closed gbraad closed 4 years ago

gbraad commented 4 years ago
namespace kittenbit {
    // Constants and global variables
    const FIRMWARE = "Kitten:bit V3.9\r\n"

    // Configuration
    serial.redirect(SerialPin.P0, SerialPin.P1, 115200)

    function processMCommand(cmd: string) {
        let elem = helpers.stringSplit(cmd, " ")
        switch (elem[0]) {
            case "0":       // firmware version
                echoVersion()
                break;
            case "999":     // perform device reset
                control.reset()
                break;
            case "204":     // motor: dual, no duration
                robotbit.MotorRunDual(robotbit.Motors.M1A,
                    parseInt(elem[1]),
                    robotbit.Motors.M1B,
                    parseInt(elem[2]));
                break;
            default:
                serial.writeString("M" + elem[0] + " -1")
        }
    }

    serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
        let receivedCmd = serial.readLine()
        if (receivedCmd.length < 1) {
            return;
        }
        if (receivedCmd.charAt(0) == "M") {
            receivedCmd = receivedCmd.slice(1, receivedCmd.length)
            processMCommand(receivedCmd)
        }
        if (receivedCmd.charAt(0) == "X") {

        }
    })

    function echoVersion() {
        serial.writeString("M0 " + FIRMWARE)
    }

    // Start
    echoVersion()
}