@@ -23,13 +23,18 @@ pub const HTS221 = struct {
2323 WHO_AM_I = 0x0F ,
2424 AV_CONF = 0x10 ,
2525 CTRL_REG1 = 0x20 ,
26+ H_OUT = 0x28 ,
2627 T_OUT = 0x2A ,
2728 STATUS_REG = 0x27 ,
2829
2930 // Calibration register
31+ H0_RH_X2 = 0x30 ,
32+ H1_RH_X2 = 0x31 ,
3033 T0_DEGC_X8 = 0x32 ,
3134 T1_DEGC_X8 = 0x33 ,
3235 T01_DEGC_MSB_x8 = 0x35 ,
36+ H0_T0_OUT = 0x36 ,
37+ H1_T0_OUT = 0x3A ,
3338 T0_OUT = 0x3C ,
3439 T1_OUT = 0x3E ,
3540
@@ -101,6 +106,8 @@ pub const HTS221 = struct {
101106 // Needs to be initialized with calibration value.
102107 t_slop : f32 = 0.0 ,
103108 t_b0 : f32 = 0.0 ,
109+ h_slop : f32 = 0.0 ,
110+ h_b0 : f32 = 0.0 ,
104111
105112 // internal read buffer
106113 read_buffer : [2 ]u8 = .{ 0 , 0 },
@@ -143,6 +150,23 @@ pub const HTS221 = struct {
143150 self .t_b0 = t0_deg - T0 * self .t_slop ;
144151 }
145152
153+ fn init_humidity_calibration (self : * @This ()) InterfaceError ! void {
154+ const h0_rh_x2 = try self .read_one_byte (RegsAddr .H0_RH_X2 );
155+ const h0_rh = @as (f32 , @floatFromInt (h0_rh_x2 )) / 2.0 ;
156+
157+ const h1_rh_x2 = try self .read_one_byte (RegsAddr .H1_RH_X2 );
158+ const h1_rh = @as (f32 , @floatFromInt (h1_rh_x2 )) / 2.0 ;
159+
160+ const h0_raw = try self .read_two_byte (RegsAddr .H0_T0_OUT );
161+ const H0 = @as (f32 , @floatFromInt (@as (i16 , @bitCast (h0_raw ))));
162+
163+ const h1_raw = try self .read_two_byte (RegsAddr .H1_T0_OUT );
164+ const H1 = @as (f32 , @floatFromInt (@as (i16 , @bitCast (h1_raw ))));
165+
166+ self .h_slop = ((h1_rh - h0_rh ) / (H1 - H0 ));
167+ self .h_b0 = h0_rh - H0 * self .h_slop ;
168+ }
169+
146170 fn setup_output_rate (self : * @This (), rate : ODR ) InterfaceError ! void {
147171 var reg = try self .read_register (CTRL_REG1 , RegsAddr .CTRL_REG1 );
148172 reg .ODR = rate ;
@@ -170,6 +194,7 @@ pub const HTS221 = struct {
170194
171195 pub fn configure (self : * @This (), config : Config ) InterfaceError ! void {
172196 try self .init_temp_calibration ();
197+ try self .init_humidity_calibration ();
173198 try self .setup_output_rate (config .outputDataRate );
174199 try self .setup_average (config .temperatureAverageSample , config .humidityAverageSample );
175200 }
@@ -179,12 +204,23 @@ pub const HTS221 = struct {
179204 return status .T_DA == 1 ;
180205 }
181206
207+ pub fn humidity_ready (self : * @This ()) InterfaceError ! bool {
208+ const status = try self .read_register (STATUS_REG , RegsAddr .STATUS_REG );
209+ return status .H_DA == 1 ;
210+ }
211+
182212 pub fn read_temperature (self : * @This ()) InterfaceError ! f32 {
183213 const raw_t = try self .read_two_byte (RegsAddr .T_OUT );
184214 const inter = @as (f32 , @floatFromInt (@as (i16 , @bitCast (raw_t ))));
185215 return self .t_b0 + inter * self .t_slop ;
186216 }
187217
218+ pub fn read_humidity (self : * @This ()) InterfaceError ! f32 {
219+ const raw_h = try self .read_two_byte (RegsAddr .H_OUT );
220+ const inter = @as (f32 , @floatFromInt (@as (i16 , @bitCast (raw_h ))));
221+ return self .h_b0 + inter * self .h_slop ;
222+ }
223+
188224 pub fn init (dev : * mdf_base.I2C_Device ) @This () {
189225 return .{ .dev = dev };
190226 }
0 commit comments