sharpbrick / powered-up

.NET implementation of the LEGO PoweredUp Protocol
MIT License
99 stars 19 forks source link

How turn on lights on ColorDistanceSensor ? #203

Closed tenacious closed 6 months ago

tenacious commented 7 months ago

Hi,

How turn on lights on ColorDistanceSensor ?

I see is only possible get infos from ColorDistanceSensor .

From lego App is possible also turn on led lights on ColorDistanceSensor, is possible with Sharpbrick too ?

Thanks to everyone involved in this project !

tthiery commented 7 months ago

I do not have that device, was contributed by someone. The device code has a light mode field ... which is not accessible by a method.

tthiery commented 7 months ago

Try write a value 0-100 into the mode. Like that the technic sensor sector lights work.

tenacious commented 7 months ago

Thank you for your response. I've observed that the Technic Color Sensor #45605 features 3 white lights . On the other hand, the ColorDistanceSensor #88007 is equipped with an RGB light. Although I have the hardware, I'm not quite sure how to test the suggested solution. I have no experience in this . @highstreeto , do you have any insights on how to adjust the light color on the ColorDistanceSensor?

tthiery commented 7 months ago

Take this example and use the mode as the following.

byte colorNr = PoweredUpColor.Red;

// mode is a capability offered by the device
var lightMode = colorDistSensor.SingleValueMode<sbyte, sbyte>(colorDistSensor.ModeIndexLight);

// the mode seems to receive a single byte with a color code number
await lightMode.WriteDirectModeDataAsync(new byte[] { colorNr });

Thanks to our friends on the node-poweredup community who have discovered the device more extensively.

The mode is pretty-printed described by the protocol as the following

    - Mode 5: Name: COL O, Symbol: IDX, Capability: Output
      - DataSet: 1x SByte, TotalFigures: 3, Decimals: 0
        Output Mapping: Discrete
        Raw Min:       0, Max:      10
        Pct Min:       0, Max:     100 (scaling)
        SI  Min:       0, Max:      10 (pass-through)
      - Configuration
        NotificationEnabled: False
        DeltaInterval: 0
tthiery commented 7 months ago

There is a lot more you can do with this little sensor ... see the node project ... if you are in the mood, create a Pull Request to add the color function as a method to our library

tenacious commented 7 months ago

Hi, Thanks,

I tried the proposed code but can't get it to work. It does nothing. Tried passing different colors but still nothing happens.

Il giorno mar 5 mar 2024 alle ore 20:57 T. Thiery @.***> ha scritto:

There is a lot more you can do with this little sensor ... see the node project ... if you are in the mood, create a Pull Request to add the color function as a method to our library

— Reply to this email directly, view it on GitHub https://github.com/sharpbrick/powered-up/issues/203#issuecomment-1979527603, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKLR4I47QTUKLH2FRSRPRDYWYPRLAVCNFSM6AAAAABEE77XFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZZGUZDONRQGM . You are receiving this because you authored the thread.Message ID: @.***>

tthiery commented 7 months ago

Can you post the code?

tenacious commented 7 months ago

public class ExampleColorDistanceSensorLightColor : BaseExample { public override async Task ExecuteAsync() { using (var moveHub = Host.FindByType()) { var colorDistSensor = moveHub.C.GetDevice();

        byte colorNr = (byte)PoweredUpColor.Red;

        // mode is a capability offered by the device
        var lightMode = colorDistSensor.SingleValueMode<sbyte,

sbyte>(colorDistSensor.ModeIndexLight);

        // the mode seems to receive a single byte with a color code

number await lightMode.WriteDirectModeDataAsync(new byte[] { colorNr});

    }

}

}

tried all the PoweredUpColor enum values.

Il giorno gio 7 mar 2024 alle ore 10:55 T. Thiery @.***> ha scritto:

Can you post the code?

— Reply to this email directly, view it on GitHub https://github.com/sharpbrick/powered-up/issues/203#issuecomment-1983140348, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKLR4IXOJH6KTXLLASXFXTYXA2Q5AVCNFSM6AAAAABEE77XFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBTGE2DAMZUHA . You are receiving this because you authored the thread.Message ID: @.***>

tthiery commented 6 months ago

Sounds about right. Pity that it does not work. There is one thing in the JS code ... the .subscribe(LED) above which is different.

Try the following line before, adjusted to the right mode 5.

tenacious commented 6 months ago

Ok ,now works. I'm not able to create a pull request , here the edits .

I added to ColorDistanceSensor class this method :

public async Task<PortFeedback> SetLedColor(ColorDistanceLedColor colorLedMode)
{
    AssertIsConnected();

    await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage(_portId, ModeIndexLight, 10000, false)
    {
        HubId = _hubId,
    });

    var response = await _protocol.SendPortOutputCommandAsync(new GenericWriteDirectModeDataMessage(
        _portId,
        PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
       ModeIndexLight,
        new byte[] { (byte)colorLedMode })
    {
        HubId = _hubId,
    });

    return response;
}

Added enum ColorDistanceLedColor . In this sensor there is no a RGB light but 3 leds , so 3 supported color + white (all led ON)

public enum ColorDistanceLedColor : byte
{
    None = 0,
    Blue = 3,
    Green = 5,
    Red = 9,
    White = 10,
}

Here the test class :

public class ExampleColorDistanceSensorLightColor : BaseExample
{
    public override async Task ExecuteAsync()
    {
        using (var moveHub = Host.FindByType<MoveHub>())
        {
            var colorDistSensor = moveHub.C.GetDevice<ColorDistanceSensor>();

            foreach (var availableLedColor in Enum.GetValues<ColorDistanceLedColor>())
            {
                await colorDistSensor.SetLedColor(availableLedColor);
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

        }

    }

}
tthiery commented 6 months ago

@tenacious I released the package in version 5.0.2 including your function. Feel free to test it.

Curious: Why you are not able to do the Pull Request. I could have walked you through!

tenacious commented 6 months ago

Hi, you are very kind. I didn't want to run the risk of messing anything up. For some lines of code I preferred to proceed like this.

Il giorno gio 21 mar 2024 alle ore 23:10 T. Thiery @.***> ha scritto:

@tenacious https://github.com/tenacious I released the package in version 5.0.2 including your function. Feel free to test it.

Curious: Why you are not able to do the Pull Request. I could have walked you through!

— Reply to this email directly, view it on GitHub https://github.com/sharpbrick/powered-up/issues/203#issuecomment-2013932933, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKLR4J46VHAZZNBWEKORELYZNLGXAVCNFSM6AAAAABEE77XFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJTHEZTEOJTGM . You are receiving this because you were mentioned.Message ID: @.***>