Browse Source

bugfixes, code cleanup & output of measured and calculated data: working!

master
Stefan Haslinger 7 months ago
parent
commit
4ced9fe796
  1. 12
      veml6075/lib/veml6075.ex
  2. 27
      veml6075/lib/veml6075/comm.ex
  3. 47
      veml6075/lib/veml6075/config.ex

12
veml6075/lib/veml6075.ex

@ -53,16 +53,10 @@ defmodule VEML6075 do
@impl true
def handle_info(
:measure,
%{i2c: i2c, address: address, config: config} = state
%{i2c: i2c, address: address, config: _config} = state
) do
last_reading_uva = Comm.readuva(i2c, address, config)
last_reading_uvb = Comm.readuvb(i2c, address, config)
updated_with_reading = %{
state
| last_reading_uva: last_reading_uva,
last_reading_uvb: last_reading_uvb
}
last_reading = Comm.read(i2c, address)
updated_with_reading = %{state | last_reading: last_reading}
{:noreply, updated_with_reading}
end

27
veml6075/lib/veml6075/comm.ex

@ -2,10 +2,10 @@ defmodule VEML6075.Comm do
alias Circuits.I2C
alias VEML6075.Config
@light_register_uva <<7>>
@light_register_uvb <<9>>
@visible_compensation <<a>>
@ir_compensation <<b>>
@uva_register <<7>>
@uvb_register <<9>>
@visible_compensation <<10>>
@ir_compensation <<11>>
def discover(possible_addresses \\ [0x10, 0x48]) do
I2C.discover_one!(possible_addresses)
@ -21,12 +21,23 @@ defmodule VEML6075.Comm do
I2C.write(i2c, sensor, <<0, command::little-16>>)
end
def read(i2c, sensor, configuration) do
def read(i2c, sensor) do
<<uva_raw::little-16>> = I2C.write_read!(i2c, sensor, @uva_register, 2)
<<uvb_raw::little-16>> = I2C.write_read!(i2c, sensor, @uvb_register, 2)
<<visible_compensation::little-16>> = I2C.write_read!(i2c, sensor, @uvb_register, 2)
<<ir_compensation::little-16>> = I2C.write_read!(i2c, sensor, @uvb_register, 2)
<<visible_compensation::little-16>> = I2C.write_read!(i2c, sensor, @visible_compensation, 2)
<<ir_compensation::little-16>> = I2C.write_read!(i2c, sensor, @ir_compensation, 2)
Config.convert(configuration, uva_raw, uvb_raw, visible_compensation, ir_compensation)
%{uvia: uvia, uvib: uvib, uvi: uvi} =
Config.convert(uva_raw, uvb_raw, visible_compensation, ir_compensation)
%{
uva_raw: uva_raw,
uvb_raw: uvb_raw,
visible_compensation: visible_compensation,
ir_compensation: ir_compensation,
uvia: uvia,
uvib: uvib,
uvi: uvi
}
end
end

47
veml6075/lib/veml6075/config.ex

@ -13,10 +13,10 @@ defmodule VEML6075.Config do
<<integer::16>> = <<
reserved::9,
uv_it(config.uv_it):3,
high_dynamic(config.dynamic):1,
uv_trig(config,uv_trig):1,
uv_av(config.uv_av):1,
uv_it(config.uv_it)::3,
high_dynamic(config.dynamic)::1,
uv_trig(config.uv_trig)::1,
uv_av(config.uv_av)::1,
shutdown(config.shutdown)::1
>>
@ -45,32 +45,35 @@ defmodule VEML6075.Config do
defp shutdown(true), do: 1
defp shutdown(_), do: 0
calibration_alpha_vis = 1.0;
calibration_beta_vis = 1.0;
calibration_gamma_ir = 1.0;
calibration_delta_ir = 1.0;
@calibration_alpha_vis 1.0
@calibration_beta_vis 1.0
@calibration_gamma_ir 1.0
@calibration_delta_ir 1.0
uva_vis_coef_a = 2.22
uva_ir_coef_b = 1.33
uvb_vis_coef_c = 2.95
uvb_ir_coef_d = 1.75
uva_responsibility = 0.00110
uvb_responsibility = 0.00125
@uva_vis_coef_a 2.22
@uva_ir_coef_b 1.33
@uvb_vis_coef_c 2.95
@uvb_ir_coef_d 1.75
@uva_responsibility 0.00110
@uvb_responsibility 0.00125
def convert(uva_raw, uvb_raw, visible_comp, ir_comp) do
# Calculate the simple UVIA and UVIB. These are used to calculate the UVI signal.
uvia = uva_raw - ((uva_vis_coef_a * calibration_alpha_vis * visible_comp) / calibration_gamma_ir)
- ((uva_ir_coef_b * calibration_alpha_vis * ir_comp) / calibration_delta_ir)
uvib = uvb_raw - ((uvb_vis_coef_c * calibration_beta_vis * visible_comp) / calibration_gamma_ir)
- ((uvb_ir_coef_d * calibration_beta_vis * ir_comp) / calibration_delta_ir)
uvia =
uva_raw - @uva_vis_coef_a * @calibration_alpha_vis * visible_comp / @calibration_gamma_ir -
@uva_ir_coef_b * @calibration_alpha_vis * ir_comp / @calibration_delta_ir
uvib =
uvb_raw - @uvb_vis_coef_c * @calibration_beta_vis * visible_comp / @calibration_gamma_ir -
@uvb_ir_coef_d * @calibration_beta_vis * ir_comp / @calibration_delta_ir
# Convert raw UVIA and UVIB to values scaled by the sensor responsivity
uvia_scaled = uvia * (1.0 / calibration_alpha_vis) * uva_responsibility
uvib_scaled = uvib * (1.0 / calibration_beta_vis) * uvb_responsibility
uvia_scaled = uvia * (1.0 / @calibration_alpha_vis) * @uva_responsibility
uvib_scaled = uvib * (1.0 / @calibration_beta_vis) * @uvb_responsibility
# Use UVIA and UVIB to calculate the average UVI:
uvi = (uvia_scaled + uvib_scaled) / 2.0;
uvi = (uvia_scaled + uvib_scaled) / 2.0
{uvia, uvib, uvi}
%{uvia: uvia, uvib: uvib, uvi: uvi}
end
end

Loading…
Cancel
Save