diff options
Diffstat (limited to 'files/common/openwrt/sensors.module_lua')
-rw-r--r-- | files/common/openwrt/sensors.module_lua | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/files/common/openwrt/sensors.module_lua b/files/common/openwrt/sensors.module_lua index 087ed7d5..499886b0 100644 --- a/files/common/openwrt/sensors.module_lua +++ b/files/common/openwrt/sensors.module_lua @@ -11,6 +11,8 @@ local _internal_ = {} _internal_.setup = {} _internal_.read = {} + + -- ############# utils ########### function _internal_.write_to_file(path, data) @@ -32,6 +34,8 @@ function _internal_.read_from_file(path) return data, err end + + -- ############# i2c ########### function _M.i2c_bus_path(bus) @@ -83,7 +87,8 @@ function _M.i2c_add_devices_recursive(bus, devices) local num_devices = 0 local device for _, device in base.ipairs(devices) do - _M.i2c_add_device(bus, device.address, device.type) + local ret, err = _M.i2c_add_device(bus, device.address, device.type) + if not ret then return nil, err end num_devices = num_devices + 1 local setup_function = _internal_.setup[device.type] @@ -154,6 +159,7 @@ function _M.w1_device_family_and_serial(address) return family, serial end + function _M.w1_read_sensors(devices) local readings = {} local device @@ -176,6 +182,56 @@ function _M.w1_read_sensors(devices) end + +-- ############# gpio ########### + +function _M.gpio_pin_path(number) + return string.format("/sys/class/gpio/gpio%d", number) +end + +function _M.gpio_export_pin(number) + return _internal_.write_to_file("/sys/class/gpio/export", string.format("%d", number)) +end + +function _M.gpio_unexport_pin(number) + return _internal_.write_to_file("/sys/class/gpio/unexport", string.format("%d", number)) +end + +function _M.gpio_read_pin(number) + local value, err = _internal_.read_from_file(_M.gpio_pin_path(number) .. "/value") + if not value then return nil, err end + return value:match( "^%s*(.-)%s*$" ) +end + + +function _M.gpio_setup_pins(pins) + local num_pins = 0 + local pin + for _, pin in base.ipairs(pins) do + local ret, err = _M.gpio_export_pin(pin.number) + if not ret then return nil, err end + num_pins = num_pins + 1 + end + + return num_pins +end + +function _M.gpio_read_pins(pins) + local readings = {} + local pin + for _, pin in base.ipairs(pins) do + local name = pin.name ~= nil and pin.name or string.format("gpio-%d", pin.number) + local value, err = _M.gpio_read_pin(pin.number) + if value ~= nil then + readings[name] = { gpio = value, _kind_ = 'gpio' } + end + end + + return readings +end + + + -- ############# iio ########### function _M.iio_device_path_from_i2c_device(bus, address) @@ -297,6 +353,7 @@ function _M.hwmon_device_path_from_w1_device(address) return device_paths[1] end + function _M.hwmon_read_w1_therm(address) local hwmon_device, err = _M.hwmon_device_path_from_w1_device(address) if not hwmon_device then return end @@ -339,6 +396,11 @@ function _M.setup(config) end -- for now the only supported 1-wire master device is i2c based and will be initialized above -- also sensors on 1-wire busses are discvored automatically and for now no supported sensor needs any setup + if config.gpio then + local tmp, err = _M.gpio_setup_pins(config.gpio) + if not tmp then return nil, err end + num_devices = num_devices + tmp + end end function _M.read(config) @@ -354,11 +416,16 @@ function _M.read(config) local tmp, err = _M.w1_read_sensors(config.w1) if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end end + if config.gpio then + local tmp, err = _M.gpio_read_pins(config.gpio) + if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end + end return readings end + -- ################################ return _M |