|
| 1 | +package org.koreader.launcher.device.lights |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.util.Log |
| 5 | +import org.koreader.launcher.device.LightsInterface |
| 6 | +import org.koreader.launcher.extensions.read |
| 7 | +import org.koreader.launcher.extensions.write |
| 8 | +import java.io.File |
| 9 | + |
| 10 | +/* Controller for Onyx Boox Palma 2 Pro with brightness and warmth support. |
| 11 | + * Uses onyx_bl_br for brightness and onyx_bl_ct for warmth (color temperature). |
| 12 | + * Dynamically reads max values from sysfs. |
| 13 | + */ |
| 14 | + |
| 15 | +class OnyxPalma2ProController : LightsInterface { |
| 16 | + companion object { |
| 17 | + private const val TAG = "Lights" |
| 18 | + private const val MIN = 0 |
| 19 | + private const val DEFAULT_BRIGHTNESS_MAX = 255 |
| 20 | + private const val DEFAULT_WARMTH_MAX = 32 |
| 21 | + |
| 22 | + private const val BRIGHTNESS_FILE = "/sys/class/backlight/onyx_bl_br/brightness" |
| 23 | + private const val BRIGHTNESS_MAX_FILE = "/sys/class/backlight/onyx_bl_br/max_brightness" |
| 24 | + private const val WARMTH_FILE = "/sys/class/backlight/onyx_bl_ct/brightness" |
| 25 | + private const val WARMTH_MAX_FILE = "/sys/class/backlight/onyx_bl_ct/max_brightness" |
| 26 | + } |
| 27 | + |
| 28 | + private val cachedBrightnessMax: Int by lazy { |
| 29 | + try { |
| 30 | + val value = File(BRIGHTNESS_MAX_FILE).read() |
| 31 | + if (value > 0) value else DEFAULT_BRIGHTNESS_MAX |
| 32 | + } catch (e: Exception) { |
| 33 | + Log.w(TAG, "Failed to read max brightness, using default: $e") |
| 34 | + DEFAULT_BRIGHTNESS_MAX |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private val cachedWarmthMax: Int by lazy { |
| 39 | + try { |
| 40 | + val value = File(WARMTH_MAX_FILE).read() |
| 41 | + if (value > 0) value else DEFAULT_WARMTH_MAX |
| 42 | + } catch (e: Exception) { |
| 43 | + Log.w(TAG, "Failed to read max warmth, using default: $e") |
| 44 | + DEFAULT_WARMTH_MAX |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + override fun getPlatform(): String { |
| 49 | + return "onyx-palma2pro" |
| 50 | + } |
| 51 | + |
| 52 | + override fun hasFallback(): Boolean { |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + override fun hasWarmth(): Boolean { |
| 57 | + return true |
| 58 | + } |
| 59 | + |
| 60 | + override fun needsPermission(): Boolean { |
| 61 | + return false |
| 62 | + } |
| 63 | + |
| 64 | + override fun getBrightness(activity: Activity): Int { |
| 65 | + return try { |
| 66 | + File(BRIGHTNESS_FILE).read() |
| 67 | + } catch (e: Exception) { |
| 68 | + Log.w(TAG, "Failed to read brightness: $e") |
| 69 | + 0 |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + override fun getWarmth(activity: Activity): Int { |
| 74 | + return try { |
| 75 | + File(WARMTH_FILE).read() |
| 76 | + } catch (e: Exception) { |
| 77 | + Log.w(TAG, "Failed to read warmth: $e") |
| 78 | + 0 |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + override fun setBrightness(activity: Activity, brightness: Int) { |
| 83 | + if (brightness !in MIN..cachedBrightnessMax) { |
| 84 | + Log.w(TAG, "brightness value out of range: $brightness (max: $cachedBrightnessMax)") |
| 85 | + return |
| 86 | + } |
| 87 | + Log.v(TAG, "Setting brightness to $brightness") |
| 88 | + try { |
| 89 | + File(BRIGHTNESS_FILE).write(brightness) |
| 90 | + } catch (e: Exception) { |
| 91 | + Log.e(TAG, "Failed to set brightness: $e") |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + override fun setWarmth(activity: Activity, warmth: Int) { |
| 96 | + if (warmth !in MIN..cachedWarmthMax) { |
| 97 | + Log.w(TAG, "warmth value out of range: $warmth (max: $cachedWarmthMax)") |
| 98 | + return |
| 99 | + } |
| 100 | + Log.v(TAG, "Setting warmth to $warmth") |
| 101 | + try { |
| 102 | + File(WARMTH_FILE).write(warmth) |
| 103 | + } catch (e: Exception) { |
| 104 | + Log.e(TAG, "Failed to set warmth: $e") |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + override fun getMinWarmth(): Int { |
| 109 | + return MIN |
| 110 | + } |
| 111 | + |
| 112 | + override fun getMaxWarmth(): Int { |
| 113 | + return cachedWarmthMax |
| 114 | + } |
| 115 | + |
| 116 | + override fun getMinBrightness(): Int { |
| 117 | + return MIN |
| 118 | + } |
| 119 | + |
| 120 | + override fun getMaxBrightness(): Int { |
| 121 | + return cachedBrightnessMax |
| 122 | + } |
| 123 | + |
| 124 | + override fun enableFrontlightSwitch(activity: Activity): Int { |
| 125 | + return 1 |
| 126 | + } |
| 127 | + |
| 128 | + override fun hasStandaloneWarmth(): Boolean { |
| 129 | + return true |
| 130 | + } |
| 131 | +} |
0 commit comments