microsoft / devicescript

TypeScript for Tiny IoT Devices (ESP32, RP2040, ...)
https://microsoft.github.io/devicescript/
MIT License
3.25k stars 116 forks source link

Define shield in code and not .json #474

Closed pelikhan closed 12 months ago

mmoskal commented 1 year ago

This seems to be supported. Here's an example:

import * as ds from "@devicescript/core"
import { ST7735Driver } from "@devicescript/drivers"
import { Image } from "@devicescript/graphics"
import {
    configureHardware,
    startBuzzer,
    startGamepad,
    startRelay,
} from "@devicescript/servers"
import { spi } from "@devicescript/spi"
import { pins as pins0 } from "@dsboard/pico"

/**
 * @devsWhenUsed
 */
export class ArcadeRP2040 {
    constructor() {
        configureHardware({
            log: {
                pinTX: pins0.P20,
            },
            jacdac: {
                pin: pins0.P11,
            },
        })
    }

    startGamepad() {
        return startGamepad({
            activeHigh: true,
            pinA: pins0.P2,
            pinB: pins0.P3,
            pinDown: pins0.P12,
            pinLeft: pins0.P13,
            pinMenu: pins0.P16,
            pinRight: pins0.P14,
            pinUp: pins0.P15,
        })
    }

    startBuzzer(): ds.Buzzer {
        return startBuzzer({ pin: pins0.P8, name: "speaker" })
    }

    startVibrationMotor(): ds.Relay {
        return startRelay({ pin: pins0.P4 })
    }

    async startDisplay() {
        spi.configure({
            mosi: pins0.P7,
            sck: pins0.P6,
            hz: 8_000_000,
        })

        pins0.P0.setMode(ds.GPIOMode.OutputHigh)

        const d = new ST7735Driver(Image.alloc(160, 128, 4), {
            dc: pins0.P9,
            cs: pins0.P5,
            reset: pins0.P10,
            frmctr1: 0x0e_14_ff,
            flip: true,
            spi: spi,
        })
        await d.init()
        return d
    }
}

interface BoardPins {
    /**
     * Pin P18 (GPIO18, io)
     *
     * @devsGPIO 18
     */
    P18: ds.IOPin

    /**
     * Pin P19 (GPIO19, io)
     *
     * @devsGPIO 19
     */
    P19: ds.IOPin

    /**
     * Pin P21 (GPIO21, io)
     *
     * @devsGPIO 21
     */
    P21: ds.IOPin

    /**
     * Pin GP1 (GPIO17, io) - broken out next to power switch
     *
     * @devsGPIO 17
     */
    GP1: ds.IOPin

    /**
     * Pin P26 (GPIO26, analogIn, io) - next to vibration motor
     *
     * @devsGPIO 26
     */
    ADC1: ds.AnalogInPin & ds.IOPin

    /**
     * Pin P27 (GPIO27, analogIn, io) - next to vibration motor
     *
     * @devsGPIO 27
     */
    ADC2: ds.AnalogInPin & ds.IOPin
}
export declare const pins: BoardPins
pelikhan commented 1 year ago

Let’s chat about unifying how we define boards. It’s a mix of board.json or this kind of code oriented approach.

code looks better but the json does have some nice checks too.


From: Michał Moskal @.> Sent: Friday, June 23, 2023 1:44:44 PM To: microsoft/devicescript @.> Cc: Peli de Halleux @.>; Author @.> Subject: Re: [microsoft/devicescript] Define shield in code and not .json (Issue #474)

This seems to be supported. Here's an example:

import * as ds from @./core" import { ST7735Driver } from @./drivers" import { Image } from @./graphics" import { configureHardware, startBuzzer, startGamepad, startRelay, } from @./servers" import { spi } from @./spi" import { pins as pins0 } from @./pico"

/**

interface BoardPins { /**

— Reply to this email directly, view it on GitHubhttps://github.com/microsoft/devicescript/issues/474#issuecomment-1604932501, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AA73QKPFVLXQZUK6I4I7ABDXMX53ZANCNFSM6AAAAAAZBEFMZU. You are receiving this because you authored the thread.Message ID: @.***>

pelikhan commented 1 year ago

in many shield cases, the MCU can be different (e.g. pico vs pico-w) so we would have to have a different approach to define pins statically.

pelikhan commented 1 year ago

It seems that this feature would require to be able to lookup pins by name in the static configuration of servers.