Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How turn on lights on ColorDistanceSensor ? #203

Closed
tenacious opened this issue Mar 4, 2024 · 12 comments · Fixed by #204
Closed

How turn on lights on ColorDistanceSensor ? #203

tenacious opened this issue Mar 4, 2024 · 12 comments · Fixed by #204
Assignees
Labels
enhancement New feature or request

Comments

@tenacious
Copy link

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
Copy link
Member

tthiery commented Mar 4, 2024

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
Copy link
Member

tthiery commented Mar 4, 2024

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

@tenacious
Copy link
Author

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
Copy link
Member

tthiery commented Mar 5, 2024

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
Copy link
Member

tthiery commented Mar 5, 2024

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
Copy link
Author

tenacious commented Mar 7, 2024 via email

@tthiery
Copy link
Member

tthiery commented Mar 7, 2024

Can you post the code?

@tenacious
Copy link
Author

tenacious commented Mar 7, 2024 via email

@tthiery
Copy link
Member

tthiery commented Mar 11, 2024

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
Copy link
Author

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 tthiery self-assigned this Mar 21, 2024
@tthiery tthiery added the enhancement New feature or request label Mar 21, 2024
@tthiery tthiery added this to the v5.0 (breaking) milestone Mar 21, 2024
tthiery added a commit that referenced this issue Mar 21, 2024
tthiery added a commit that referenced this issue Mar 21, 2024
@tthiery
Copy link
Member

tthiery commented Mar 21, 2024

@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
Copy link
Author

tenacious commented Mar 22, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants