Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Slide switch position block?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Slide switch position block?

by jliszka on Thu Jun 28, 2018 10:47 am

I'm looking for a way to get the current position of the slide switch.

In MakeCode, there are event blocks that trigger when the slide switch moves, but I can't find a block that gives you the current position of the slide switch. There's a "switch right" block referenced here https://learn.adafruit.com/sensors-in-makecode/switch-button, but it doesn't appear in the MakeCode editor.

I've also tried doing a digital read from D7, but that's also not an option, even in the Javascript side of things.

Please help!

jliszka
 
Posts: 5
Joined: Thu Jun 28, 2018 10:27 am

Re: Slide switch position block?

by danhalbert on Thu Jun 28, 2018 4:03 pm

It does seem to be a deficiency that there's no simple test for the slide switch. Here's a way of doing it by recording when it changes position. Sample program, tested in simulator:
Selection_175.png
Selection_175.png (24.76 KiB) Viewed 241 times

danhalbert
 
Posts: 2138
Joined: Tue Aug 08, 2017 12:37 pm

Re: Slide switch position block?

by jliszka on Thu Jun 28, 2018 10:20 pm

Yeah, that's what I resorted to, which works great except at start up. For example, in your program, if the switch is to the left when power is first turned on, the LEDs will be blue, then stay blue when you switch it right, then turn red when you switch it back to the left.

I noticed there is a bool switchRight() defined in switch.cpp, but it doesn't seem to be exposed in the switch/shims.d.ts file.

jliszka
 
Posts: 5
Joined: Thu Jun 28, 2018 10:27 am

Re: Slide switch position block?

by jliszka on Thu Jun 28, 2018 11:15 pm

I tracked down the problem. Fix and pull request here: https://github.com/Microsoft/pxt-common-packages/pull/369

Is there anyone here in a position to help get that fix merged and deployed?

P.S. My 5yo daughter LOVES her circuit playground and is pretty excited about the switch.

jliszka
 
Posts: 5
Joined: Thu Jun 28, 2018 10:27 am

Re: Slide switch position block?

by danhalbert on Fri Jun 29, 2018 12:09 am

I notified one of the MakeCode people in our discord chat: https://adafru.it/discord, channel #makecode, before you filed your PR. Nice fix!

danhalbert
 
Posts: 2138
Joined: Tue Aug 08, 2017 12:37 pm

Re: Slide switch position block?

by jliszka on Fri Jun 29, 2018 12:06 pm

Thanks!

jliszka
 
Posts: 5
Joined: Thu Jun 28, 2018 10:27 am

Re: Slide switch position block?

by jliszka on Fri Jun 29, 2018 12:10 pm

For anyone else who doesn't want to wait for the fix to make its way to the live editor, you can drop this code into your custom.ts file (click the + next to Explorer in Javascript mode). It'll give you a "<switch right>" block in the Input menu.

Code: Select all | TOGGLE FULL SIZE
declare namespace input {
    /**
     * Gets a value indicating if the switch is positioned to the right
     */
    //% blockId=device_switch_direction block="switch right"
    //% parts="switch"
    //% group="Switch" weight=10 blockGap=8 shim=input::switchRight
    function switchRight(): boolean;
}

jliszka
 
Posts: 5
Joined: Thu Jun 28, 2018 10:27 am

Please be positive and constructive with your questions and comments.