Skip to content

Commit

Permalink
lights drivers: refactor common code to read/write from/to sys nodes
Browse files Browse the repository at this point in the history
add light driver for Onyx Nova3 color
  • Loading branch information
pazos committed Jul 28, 2022
1 parent da8f1cf commit 503e295
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 58 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/org/koreader/launcher/TestActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.koreader.launcher.device.epd.RK3026EPDController
import org.koreader.launcher.device.epd.RK3368EPDController
import org.koreader.launcher.device.epd.TolinoEPDController
import org.koreader.launcher.device.lights.OnyxC67Controller
import org.koreader.launcher.device.lights.OnyxColorController
import org.koreader.launcher.device.lights.OnyxSdkLightsController
import org.koreader.launcher.device.lights.OnyxWarmthController
import org.koreader.launcher.device.lights.TolinoWarmthController
Expand Down Expand Up @@ -62,6 +63,7 @@ class TestActivity: AppCompatActivity() {

// Lights drivers
lightsMap["Onyx C67"] = OnyxC67Controller()
lightsMap["Onyx Color"] = OnyxColorController()
lightsMap["Onyx SDK (lights)"] = OnyxSdkLightsController()
lightsMap["Onyx (warmth)"] = OnyxWarmthController()
lightsMap["Tolino (warmth)"] = TolinoWarmthController()
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/koreader/launcher/device/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ object DeviceInfo {
ONYX_NOTE_PRO,
ONYX_NOVA2,
ONYX_NOVA3,
ONYX_NOVA3_COLOR,
ONYX_NOVA_AIR,
ONYX_NOVA_AIR_C,
ONYX_POKE3,
Expand Down Expand Up @@ -472,6 +473,7 @@ object DeviceInfo {
lightsMap[LightsDevice.ONYX_NOTE_AIR2] = ONYX_NOTE_AIR2
lightsMap[LightsDevice.ONYX_NOVA2] = ONYX_NOVA2
lightsMap[LightsDevice.ONYX_NOVA3] = ONYX_NOVA3
lightsMap[LightsDevice.ONYX_NOVA3_COLOR] = ONYX_NOVA3_COLOR
lightsMap[LightsDevice.ONYX_NOVA_AIR] = ONYX_NOVA_AIR
lightsMap[LightsDevice.ONYX_NOVA_AIR_C] = ONYX_NOVA_AIR_C
lightsMap[LightsDevice.ONYX_POKE4] = ONYX_POKE4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ object LightsFactory {
logController("Onyx/Sdk")
OnyxSdkLightsController()
}
DeviceInfo.LightsDevice.ONYX_NOVA3_COLOR -> {
logController("Onyx color")
OnyxColorController()
}
DeviceInfo.LightsDevice.ONYX_C67,
DeviceInfo.LightsDevice.ONYX_MAGICBOOK -> {
logController("ONYX C67")
logController("Onyx C67")
OnyxC67Controller()
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.koreader.launcher.device.lights
import android.app.Activity
import android.util.Log
import org.koreader.launcher.device.LightsInterface
import org.koreader.launcher.extensions.read
import org.koreader.launcher.extensions.write
import java.io.File

class OnyxC67Controller : LightsInterface {
Expand Down Expand Up @@ -34,16 +36,7 @@ class OnyxC67Controller : LightsInterface {
}

override fun getBrightness(activity: Activity): Int {
val brightnessFile = File(BRIGHTNESS_FILE)
return try {
// .replace("\n", "") is needed, since it's automatically appended
// without it exception is thrown
// java.lang.NumberFormatException: For input string: "125\n"
return brightnessFile.readText().replace("\n", "").toInt()
} catch (e: Exception) {
Log.w(TAG, Log.getStackTraceString(e))
0
}
return File(BRIGHTNESS_FILE).read()
}

override fun getWarmth(activity: Activity): Int {
Expand All @@ -57,12 +50,7 @@ class OnyxC67Controller : LightsInterface {
return
}
Log.v(TAG, "Setting brightness to $brightness")
val brightnessFile = File(BRIGHTNESS_FILE)
try {
brightnessFile.writeText(brightness.toString())
} catch (e: Exception) {
Log.w(TAG, "$e")
}
File(BRIGHTNESS_FILE).write(brightness)
}

override fun setWarmth(activity: Activity, warmth: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.koreader.launcher.device.lights

import android.app.Activity
import android.util.Log
import org.koreader.launcher.device.LightsInterface
import org.koreader.launcher.extensions.read
import org.koreader.launcher.extensions.write
import java.io.File

/* Controller for some Onyx Color devices.
* Tested on a Onyx Nova3 Color.
*
* Thanks to @ilyats
*/

class OnyxColorController : LightsInterface {
companion object {
private const val TAG = "Lights"
private const val BRIGHTNESS_MAX = 255
private const val BRIGHTNESS_MIN = 0
private const val BRIGHTNESS_FILE = "/sys/class/backlight/pwm-backlight.0/brightness"
private const val ACTUAL_BRIGHTNESS_FILE = "/sys/class/backlight/pwm-backlight.0/actual_brightness"
}

override fun getPlatform(): String {
return "onyx-color"
}

override fun hasFallback(): Boolean {
return false
}

override fun hasWarmth(): Boolean {
return false
}

override fun needsPermission(): Boolean {
return false
}

override fun enableFrontlightSwitch(activity: Activity): Int {
return 1
}

override fun getBrightness(activity: Activity): Int {
return File(ACTUAL_BRIGHTNESS_FILE).read()
}

override fun getWarmth(activity: Activity): Int {
Log.w(TAG, "getWarmth: not implemented")
return 0
}

override fun setBrightness(activity: Activity, brightness: Int) {
if (brightness < BRIGHTNESS_MIN || brightness > BRIGHTNESS_MAX) {
Log.w(TAG, "brightness value of of range: $brightness")
return
}
Log.v(TAG, "Setting brightness to $brightness")
File(BRIGHTNESS_FILE).write(brightness)
}

override fun setWarmth(activity: Activity, warmth: Int) {
Log.w(TAG, "ignoring setWarmth: not implemented")
}

override fun getMinWarmth(): Int {
return 0
}
override fun getMaxWarmth(): Int {
return 0
}
override fun getMinBrightness(): Int {
return BRIGHTNESS_MIN
}
override fun getMaxBrightness(): Int {
return BRIGHTNESS_MAX
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.koreader.launcher.device.lights
import android.app.Activity
import android.util.Log
import org.koreader.launcher.device.LightsInterface
import org.koreader.launcher.extensions.read
import org.koreader.launcher.extensions.write
import java.io.File

class OnyxWarmthController : LightsInterface {
Expand Down Expand Up @@ -32,29 +34,11 @@ class OnyxWarmthController : LightsInterface {
}

override fun getBrightness(activity: Activity): Int {
val brightnessFile = File(WHITE_FILE)
return try {
// .replace("\n", "") is needed, since it's automatically appended
// without it exception is thrown
// java.lang.NumberFormatException: For input string: "125\n"
return brightnessFile.readText().replace("\n", "").toInt()
} catch (e: Exception) {
Log.w(TAG, Log.getStackTraceString(e))
0
}
return File(WHITE_FILE).read()
}

override fun getWarmth(activity: Activity): Int {
val warmthFile = File(WARMTH_FILE)
return try {
// .replace("\n", "") is needed, since it's automatically appended
// without it exception is thrown
// java.lang.NumberFormatException: For input string: "125\n"
return warmthFile.readText().replace("\n", "").toInt()
} catch (e: Exception) {
Log.w(TAG, Log.getStackTraceString(e))
0
}
return File(WARMTH_FILE).read()
}

override fun setBrightness(activity: Activity, brightness: Int) {
Expand All @@ -63,26 +47,17 @@ class OnyxWarmthController : LightsInterface {
return
}
Log.v(TAG, "Setting brightness to $brightness")
val brightnessFile = File(WHITE_FILE)
try {
brightnessFile.writeText(brightness.toString())
} catch (e: Exception) {
Log.w(TAG, "$e")
}
File(WHITE_FILE).write(brightness)
}

override fun setWarmth(activity: Activity, warmth: Int) {
if (warmth < MIN || warmth > WARMTH_MAX) {
Log.w(TAG, "warmth value of of range: $warmth")
return
}
val warmthFile = File(WARMTH_FILE)

Log.v(TAG, "Setting warmth to $warmth")
try {
warmthFile.writeText(warmth.toString())
} catch (e: Exception) {
Log.w(TAG, "$e")
}
File(WARMTH_FILE).write(warmth)
}

override fun getMinWarmth(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.app.Activity
import android.provider.Settings
import android.util.Log
import org.koreader.launcher.device.LightsInterface
import org.koreader.launcher.extensions.read
import org.koreader.launcher.extensions.write
import java.io.File

/* Special controller for Tolino Epos/Epos2.
Expand Down Expand Up @@ -100,13 +102,7 @@ class TolinoWarmthController : LightsInterface {
}

override fun getWarmth(activity: Activity): Int {
val colorFile = File(COLOR_FILE)
return try {
WARMTH_MAX - colorFile.readText().toInt()
} catch (e: Exception) {
Log.w(TAG, "$e")
0
}
return WARMTH_MAX - File(COLOR_FILE).read()
}

override fun setBrightness(activity: Activity, brightness: Int) {
Expand All @@ -128,14 +124,13 @@ class TolinoWarmthController : LightsInterface {
Log.w(TAG, "warmth value of of range: $warmth")
return
}

val colorFile = File(COLOR_FILE)
Log.v(TAG, "Setting warmth to $warmth")
try {
if (!colorFile.canWrite()) {
Runtime.getRuntime().exec("su -c chmod 666 $COLOR_FILE")
}
colorFile.writeText((WARMTH_MAX - warmth).toString())
colorFile.write(WARMTH_MAX - warmth)
} catch (e: Exception) {
Log.w(TAG, "$e")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ fun File.symlink(link: String): Boolean {
}
}

fun File.read(): Int {
return try {
this.readText().replace("\n", "").toInt()
} catch (e: Exception) {
e.printStackTrace()
0
}
}

fun File.write(value: Int) {
try {
writeText(value.toString())
} catch (e: Exception) {
e.printStackTrace()
}
}

fun File.uncompress(extractTo: String, deleteIfOk: Boolean = false): Boolean {
val success = try {
uncompress(this.absolutePath, extractTo)
Expand Down

0 comments on commit 503e295

Please sign in to comment.