Skip to content

Custom Frequency enum shared across hals #837

@tact1m4n3

Description

@tact1m4n3
i2c.apply(.{
    .baud_rate = .khz(100),
});

// POC implementation
const Frequency = enum(u32) {
    _,

    pub fn hz(value: u32) Frequency {
        return @enumFromInt(value);
    }

    pub fn khz(value: u32) Frequency {
        return @enumFromInt(value * 1_000);
    }

    pub fn mhz(value: u32) Frequency {
        // what to do about overflows? Though I think it's fine to assert that the result must fit into u32.
        return @enumFromInt(value * 1_000_000);
    }
};

This would be used for baud rates, etc.

Something like this can be applied for other measures as well (apart from Frequency). In what other places can we use decl literals for more verbose initializations?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions