<commit_before>-- Interface to the SysFS interface of the Kobo Aura One Frontlight.
-- This also supports the natural light, which consists of additional
-- red and green light LEDs.

local logger = require("logger")
local dbg = require("dbg")

local KoboSysfsLight = {
    frontlight_white = "/sys/class/backlight/lm3630a_led1b",
    frontlight_red = "/sys/class/backlight/lm3630a_led1a",
    frontlight_green = "/sys/class/backlight/lm3630a_ledb",
    current_brightness = 0,
    current_warmth = 0,
    white_gain = 25,
    red_gain = 24,
    green_gain = 24,
    white_offset = -25,
    red_offset = 0,
    green_offset = -65,
}

function KoboSysfsLight:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    if o.init then o:init() end
    return o
end

function KoboSysfsLight:setBrightness(brightness)
    self:setNaturalBrightness(brightness, self.current_warmth)
end

dbg:guard(KoboSysfsLight, 'setBrightness',
          function(self, brightness)
              assert(brightness >= 0 and brightness <= 100,
                     "Wrong brightness value given!")
          end)

function KoboSysfsLight:setWarmth(warmth)
    self:setNaturalBrightness(self.current_brightness, warmth)
end

dbg:guard(KoboSysfsLight, 'setWarmth',
          function(self, warmth)
              assert(warmth >= 0 and warmth <= 100,
                     "Wrong warmth value given!")
          end)

function KoboSysfsLight:setNaturalBrightness(brightness, warmth)
    if not brightness then
        brightness = self.current_brightness
    end
    if not warmth then
        warmth = self.current_warmth
    end

    -- On Nickel, the values for white/red/green are roughly linearly dependent
    -- on the 4th root of brightness and warmth.
    local white = math.min(self.white_gain * math.pow(brightness, 0.25) *
                       math.pow(100 - warmth, 0.25) + self.white_offset, 255)
    local red = math.min(self.red_gain * math.pow(brightness, 0.25) *
                     math.pow(warmth, 0.25) + self.red_offset, 255)
    local green = math.min(self.green_gain * math.pow(brightness, 0.25) *
                       math.pow(warmth, 0.25) + self.green_offset, 255)

    white = math.max(white, 0)
    red = math.max(red, 0)
    green = math.max(green, 0)

    self:_set_light_value(self.frontlight_white, math.floor(white))
    self:_set_light_value(self.frontlight_green, math.floor(green))
    self:_set_light_value(self.frontlight_red, math.floor(red))

    self.current_brightness = brightness
    self.current_warmth = warmth
end

dbg:guard(KoboSysfsLight, 'setNaturalBrightness',
          function(self, brightness, warmth)
              assert(brightness >= 0 and brightness <= 100,
                     "Wrong brightness value given!")
              assert(warmth >= 0 and warmth <= 100,
                     "Wrong warmth value given!")
          end)<commit_msg>device/kobo/sysfs_light: Explicitly handle warmth/brightness = 0<commit_after>-- Interface to the SysFS interface of the Kobo Aura One Frontlight.
-- This also supports the natural light, which consists of additional
-- red and green light LEDs.

local logger = require("logger")
local dbg = require("dbg")

local KoboSysfsLight = {
    frontlight_white = "/sys/class/backlight/lm3630a_led1b",
    frontlight_red = "/sys/class/backlight/lm3630a_led1a",
    frontlight_green = "/sys/class/backlight/lm3630a_ledb",
    current_brightness = 0,
    current_warmth = 0,
    white_gain = 25,
    red_gain = 24,
    green_gain = 24,
    white_offset = -25,
    red_offset = 0,
    green_offset = -65,
}

function KoboSysfsLight:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    if o.init then o:init() end
    return o
end

function KoboSysfsLight:setBrightness(brightness)
    self:setNaturalBrightness(brightness, self.current_warmth)
end

dbg:guard(KoboSysfsLight, 'setBrightness',
          function(self, brightness)
              assert(brightness >= 0 and brightness <= 100,
                     "Wrong brightness value given!")
          end)

function KoboSysfsLight:setWarmth(warmth)
    self:setNaturalBrightness(self.current_brightness, warmth)
end

dbg:guard(KoboSysfsLight, 'setWarmth',
          function(self, warmth)
              assert(warmth >= 0 and warmth <= 100,
                     "Wrong warmth value given!")
          end)

function KoboSysfsLight:setNaturalBrightness(brightness, warmth)
    if not brightness then
        brightness = self.current_brightness
    end
    if not warmth then
        warmth = self.current_warmth
    end

    local red = 0
    local green = 0
    local white = 0
    if brightness > 0 then
        -- On Nickel, the values for white/red/green are roughly linearly dependent
        -- on the 4th root of brightness and warmth.
        white = math.min(self.white_gain * math.pow(brightness, 0.25) *
                             math.pow(100 - warmth, 0.25) + self.white_offset, 255)
    end
    if warmth > 0 then
        red = math.min(self.red_gain * math.pow(brightness, 0.25) *
                           math.pow(warmth, 0.25) + self.red_offset, 255)
        green = math.min(self.green_gain * math.pow(brightness, 0.25) *
                             math.pow(warmth, 0.25) + self.green_offset, 255)
    end

    white = math.max(white, 0)
    red = math.max(red, 0)
    green = math.max(green, 0)

    self:_set_light_value(self.frontlight_white, math.floor(white))
    self:_set_light_value(self.frontlight_green, math.floor(green))
    self:_set_light_value(self.frontlight_red, math.floor(red))

    self.current_brightness = brightness
    self.current_warmth = warmth
end

dbg:guard(KoboSysfsLight, 'setNaturalBrightness',
          function(self, brightness, warmth)
              assert(brightness >= 0 and brightness <= 100,
                     "Wrong brightness value given!")
              assert(warmth >= 0 and warmth <= 100,
                     "Wrong warmth value given!")
          end)
{'fraction_non_alphanumeric': 0.09821279438784032, 'fraction_numerical': 0.02672456990145315, 'mean_word_length': 2.646772228989038, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4806292', 'n_tokens_mistral': 2104, 'n_tokens_neox': 1854, 'n_words': 457}