How to adjust the backlight brightness and backlight length when running the app using toybox.attention.
How to adjust the backlight brightness and backlight length when running the app using toybox.attention.
backlight brightness
For devices with Connect IQ API level 3.2.1 and later, pass a float to Attention.backlight().
e.g.
Attention.backlight(0.5); // half brightness
Attention.backlight(1.0); // full brightness
backlight length
You can call Attention.backlight() repeatedly, but you will get an exception if you try to keep the backlight on for too long on some products (specifically, products with AMOLED displays).
[https://developer.garmin.com/connect-iq/api-docs/Toybox/Attention.html#backlight-instance_function]
Calling this repeatedly can hold the display on, but if the product has burn in protection an exception will be thrown if you attempt to keep the display enabled for too long (e.g. over 1 minute).
how to set a longer display lighting time to approx. 30 seconds in the application?
I'm assuming you mean your CIQ application.
Supposing your app has its own one second timer (or onUpdate() is called once per second, in the case of a datafield), you could just call Attention.backlight() repeatedly for 30 seconds, each time the timer expires.
I have a watch app
onupdate is not repeated
this code doesn't work for me when I press the light button to set the backlight
function onButtonDown(Button as WatchUi.Button) as Void {
// Check if the display light button has been pressed
if (button == WatchUi.KEY_LIGHT) {
// Setting the display brightness
Attention.backlight(0.5); // half brightness
System.println("key brightness");
}
}
- a WatchUi.Button isn't the same as a physical key on the device, it represents an onscreen button
- onButtonDown() isn't a standard API callback function
I've edited this comment with a simple watch/device app that will turn on the backlight at 1/2 brightness for 10 seconds when the user presses KEY_ENTER (the top right START/STOP button on 5-button watches). Doesn't seem to be possible to handle KEY_LIGHT, probably since the system reserves that for the normal "turn on backlight" function. This also means you can't really "adjust" the standard backlight functionality, you can only implement your own (triggered by a different key or some other circumstance.)
Here's the app source and instructions.
1) Create new project called "TestBacklight", using "Monkey C: New Project" in the VS Code command palette
2) Add product of your choice (must support CIQ 3.2.1 or higher).
3) Delete the TestBacklightMenuDelegate.mc file
4) Replace existing source files with the following:
TestBacklightApp.mc
import Toybox.Application; import Toybox.Lang; import Toybox.WatchUi; class TestBacklightApp extends Application.AppBase { function initialize() { AppBase.initialize(); } function getInitialView() as Array<Views or InputDelegates>? { var view = new TestBacklightView(); return [ view, new TestBacklightDelegate(view) ] as Array<Views or InputDelegates>; } } function getApp() as TestBacklightApp { return Application.getApp() as TestBacklightApp; }
TestBacklightDelegate.mc
import Toybox.Lang; import Toybox.WatchUi; class TestBacklightDelegate extends WatchUi.BehaviorDelegate { var _view as TestBacklightView; function initialize(view as TestBacklightView) { BehaviorDelegate.initialize(); _view = view; } function onKey(keyEvent as WatchUi.KeyEvent) as Boolean { var key = keyEvent.getKey(); System.println("onKey() - key code = " + key); if (key == WatchUi.KEY_ENTER) { _view.turnOnBacklight(); return true; } return false; } }
TestBacklightView.mc
import Toybox.Graphics; import Toybox.WatchUi; import Toybox.Attention; import Toybox.System; import Toybox.Timer; import Toybox.Lang; class TestBacklightView extends WatchUi.View { var _updateTimer as Timer.Timer; var _backLightStartTime as Number or Null = null; const BACKLIGHT_DURATION = 10*1000; // 10 seconds const BACKLIGHT_INTENSITY = 0.5; function initialize() { View.initialize(); _updateTimer = new Timer.Timer(); } function onLayout(dc as Dc) as Void { setLayout(Rez.Layouts.MainLayout(dc)); } function onUpdate(dc as Dc) as Void { setLayout(Rez.Layouts.MainLayout(dc)); View.onUpdate(dc); if (_backLightStartTime != null) { var backLightStartTime = _backLightStartTime; // work around type checker bug var currentTime = System.getTimer(); // keep backlight on if (currentTime - backLightStartTime <= BACKLIGHT_DURATION) { System.println("turn on backlight"); Attention.backlight(BACKLIGHT_INTENSITY); } else { System.println("turn off backlight"); Attention.backlight(0.0); _backLightStartTime = null; _updateTimer.stop(); } } } function onTimer() as Void { WatchUi.requestUpdate(); } function turnOnBacklight() as Void { System.println("received request to turn on backlight"); _backLightStartTime = System.getTimer(); WatchUi.requestUpdate(); // request update every 1000 ms _updateTimer.start(method(:onTimer), 1000, true); } }
I built for fr955 and tested on a real device (obviously you need to be in a room without a ton of natural light to see the difference on a MIP device). The sim doesn't seem to simulate the backlight, but you will see the System.println() messages in the debug console.
Do you have a real device? On them, the light button is handled by the FW, and you don't need to do anything in your app. Press the button and the Backlight comes on no matter what's running.
I've not tried but I'm not sure CIQ apps ever see KEY_LIGHT.
As far as onUpdate, in a device app or widget, you control when and how often onUpdate is called (using WatchUi.requestUpdate()) This is often done with your own timer or based on something that happened (new data from a makeWebRequest,for example)
Have you looked at the input sample in the SDK?
Someplace like initialize(), create and start a timer. Here the timer ticks every second:
var timer= new Timer.Timer(); timer.start( method(:onTimer), 1000, true );
and for the callback,
function onTimer() { WatchUi.requestUpdate(); }
The result is that onUpdate() will be called every time the timer ticks, so every second...