microzig icon indicating copy to clipboard operation
microzig copied to clipboard

feat: add ds18b20 driver

Open kaans opened this issue 2 months ago • 1 comments

Add a driver for the DS18B20 temperature sensor.

All commands except the Search Rom [0xF0] and Alarm Search [0xEC] have been implemented and successfully tested. The resolution can be configured and is respected when converting the temperature value to Celsius.

The driver supports single drop configurations (i.e. only one sensor is connected) and multi drop configuraitons (i.e. multiple sensors are connected). If the functions are called without a target/rom code, the sensor is selected automatically if only one sensor is connected. If more than one sensor is connected, the rom code of the target sensor must be specified to address each sensor individually.

Parasitic power mode is not supported by the driver itself.

(I am new to zig, please let me know where I can improve the code)

kaans avatar Dec 06 '25 11:12 kaans

To fix the formatting CI error you can run zig fmt [+ the file you want to format]

tact1m4n3 avatar Dec 07 '25 13:12 tact1m4n3

Nice work! I added some comments with some things you can improve.

Thanks a lot for the detailed review! I will go through your points and resolve them.

kaans avatar Dec 11 '25 21:12 kaans

Thanks for your comments. I am still not sure if there is a better way to pass the config values to the functions with optional parameters. Creating a struct for the parameters for each function feels unnecessary, but still I might not know about a better mechanism zig offers.

kaans avatar Dec 12 '25 09:12 kaans

It feels a bit strange that you take the optional target parameter for every function. An alternative would be to remove it (this way most functions have no parameter) and instead you can have a target field. In zig, fields are public always so you don't require a setter if the only it does is set a field.

driver.target = something;
driver.write_config(.{ config but no target }); // can be the same struct returned from read_config
driver.initiate_temperature_conversion();
// etc

tact1m4n3 avatar Dec 15 '25 05:12 tact1m4n3