microsoft / devicescript

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

RFC: board support packages #127

Closed mmoskal closed 1 year ago

mmoskal commented 1 year ago

Idea:

On top of that we would allow adding services, also in target_pico_w.ts:

ds.startServer({
   service: "button",
   pin: 12 // only constants supported
})

We would collect all ds.startServer() and generate user-settings JSON at build time.

This way there is no need to introduce additional target configuration JSON files - everything is handled from TypeScript using existing "choose main file" mechanism (otherwise we'll have to add "choose target" mechanism in extension). I think this is easier to explain to TS people.

Note that board definition is still handled by plain JSON, it's only the user-provided services that would be defined in TS.

pelikhan commented 1 year ago

TS on user side sound great.

pelikhan commented 1 year ago

Do I get a client back when I start a service?

const btw = ds.startService(...)

mmoskal commented 1 year ago

I was a bit worried you would then somehow have to pass that service down to modules you import but not I think it’s not that much of a problem.

pelikhan commented 1 year ago

something like?

export const buttonA = ds.startStarvice({ .... })

woudl allow to import those downstream. Is that what you think?

mmoskal commented 1 year ago

Let’s say you have a thermostat for two different boards. You will have two entry points a.ts and b.ts but otherwise shared code common.ts dealing with button. Both a and b would import common not the other way around.

You can either just define a button role in common code and hope it binds correctly or have a function in common that stores roles somewhere and call it from both a and b. I guess this is fine.

Otherwise we could do magic and have @devicescript/board resolve to different module based on some settings. But I think it would be just a mess. Let people have whatever config they need in common.ts using normal TS constructs.

mmoskal commented 1 year ago

Ended up doing separate starting functions, otherwise error messages for invalid JSON config are bad.

    import * as ds from "@devicescript/core"

    function startRotaryEncoder(cfg: RotaryEncoderConfig): ds.RotaryEncoder
    function startButton(cfg: ButtonConfig): ds.Button
    function startRelay(cfg: RelayConfig): ds.Relay
    function startPower(cfg: PowerConfig): ds.Power
    function startLightLevel(cfg: LightLevelConfig): ds.LightLevel
    function startReflectedLight(cfg: ReflectedLightConfig): ds.ReflectedLight
    function startWaterLevel(cfg: WaterLevelConfig): ds.WaterLevel
    function startSoundLevel(cfg: SoundLevelConfig): ds.SoundLevel
    function startSoilMoisture(cfg: SoilMoistureConfig): ds.SoilMoisture
    function startPotentiometer(cfg: PotentiometerConfig): ds.Potentiometer
pelikhan commented 1 year ago

did you use another module though?

Sent from Outlookhttp://aka.ms/weboutlook


From: Michał Moskal @.> Sent: Friday, February 10, 2023 2:49 PM To: microsoft/devicescript @.> Cc: Peli de Halleux @.>; Comment @.> Subject: Re: [microsoft/devicescript] RFC: board support packages (Issue #127)

Ended up doing separate starting functions, otherwise error messages for invalid JSON config are bad.

import * as ds from ***@***.***/core"

function startRotaryEncoder(cfg: RotaryEncoderConfig): ds.RotaryEncoder
function startButton(cfg: ButtonConfig): ds.Button
function startRelay(cfg: RelayConfig): ds.Relay
function startPower(cfg: PowerConfig): ds.Power
function startLightLevel(cfg: LightLevelConfig): ds.LightLevel
function startReflectedLight(cfg: ReflectedLightConfig): ds.ReflectedLight
function startWaterLevel(cfg: WaterLevelConfig): ds.WaterLevel
function startSoundLevel(cfg: SoundLevelConfig): ds.SoundLevel
function startSoilMoisture(cfg: SoilMoistureConfig): ds.SoilMoisture
function startPotentiometer(cfg: PotentiometerConfig): ds.Potentiometer

— Reply to this email directly, view it on GitHubhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fdevicescript%2Fissues%2F127%23issuecomment-1426444916&data=05%7C01%7Cjhalleux%40microsoft.com%7C546871c40b2e4ca163ec08db0bb907a5%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638116661548917166%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=UNmPInxSMazc3Suef5hmCk8FS%2F8y2c%2FF3O56AlyxebM%3D&reserved=0, or unsubscribehttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAA73QKKCHH3PNH5WRUMOVXDWW3AWRANCNFSM6AAAAAAUXK2OPU&data=05%7C01%7Cjhalleux%40microsoft.com%7C546871c40b2e4ca163ec08db0bb907a5%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638116661548917166%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=OuEcMOGfnJAHa8kmdJTLRcx%2FiVtYnRCA4ObM2%2BKThB8%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

mmoskal commented 1 year ago

Yes:

import { startButton } from "@devicescript/srvcfg"

const btn = startButton({
    pin: 2,
})

btn.down.subscribe(() => {
    console.log("down!")
})
pelikhan commented 1 year ago

I found that in jacdac-ts, it was easier to clearly name "clients" as clients and "servers" as servers. In this case, are you starting a server or a clent?

(can we avoid acronyms and short names in module names, it's hard to understand the meaning, maybe simply @devicescript/servers)

pelikhan commented 1 year ago

nit picking

mmoskal commented 1 year ago

It's actually both client and server.

pelikhan commented 1 year ago

oh right.

pelikhan commented 1 year ago

srvcfg -> servers?

pelikhan commented 1 year ago

@mmoskal done, close?