flipperzero-firmware/wiki/fw/api/Backlight-API.md

101 lines
2.8 KiB
Markdown
Raw Normal View History

Core api concept (#144) * add input debounce code from old fw * exampl of input api * change input API to get/release * revert input API to read * pointer instead of instance * add input API description * add display API * rewrite display names * migrate to valuemanager * add LED API * add closing brakets * add sound api * fix led api * basic api * rename API pages * change pubsub implementation * move FURI AC -> flapp, add valuemutex example, add valuemanager implementation * pubsub usage example * user led example * update example * simplify input * add composed display * add SPI/GPIO and CC1101 bus * change cc1101 api * spi api and devices * spi api and devices * move SPI to page, add GPIO * not block pin open * backlight API and more * add minunit tests * fix logging * ignore unexisting time service on embedded targets * fix warning, issue with printf * Deprecate furi_open and furi_close (#167) Rename existing furi_open and furi_close to deprecated version * add exitcode * migrate to printf * indicate test by leds * add testing description * rename furi.h * wip basic api * add valuemutex, pubsub, split files * add value expanders * value mutex realization and tests * valuemutex test added to makefile * do not build unimplemented files * fix build furmware target f2 * redesigned minunit tests to allow testing in separate files * test file for valuemutex minunit testing * minunit partial test valuemutex * local cmsis_os2 mutex bindings * implement furi open/create, tests * migrate concurrent_access to ValueMutex * add spi header * Lib: add mlib submodule. Co-authored-by: rusdacent <rusdacentx0x08@gmail.com> Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2020-10-13 08:22:43 +00:00
Backlight state describes by `uint8_t level;` brightness level.
LED API provided by struct:
```C
typedef struct {
ValueComposer* composer; /// every app add its value to compose, <uint8_t*>
ValueManager* state; /// value state and changes <uint8_t*>
} BacklightApi;
```
You can get API instance by calling `open_backlight`:
```C
/// Add new layer to LED:
inline BacklightApi* open_backlight(const char* name) {
return (BacklightApi*)furi_open(name);
}
```
Default system led is `/dev/backlight`.
To read current backlight state you should use `read_backlight` function:
```C
/// return true if success, false otherwise
inline bool read_backlight(BacklightApi* api, uint8_t* value, uint32_t timeout) {
return read_mutex(api->state->value, (void*)value, sizeof(uint8_t), timeout);
}
```
Also you can subscribe to backlight state changes:
Use `subscribe_backlight_changes` to register your callback:
```C
/// return true if success, false otherwise
inline bool subscribe_backlight_changes(LedApi* led, void(*cb)(uint8_t*, void*), void* ctx) {
return subscribe_pubsub(led->state->pubsub, void(*)(void*, void*)(cb), ctx);
}
```
Userspace helpers
```C
typedef struct {
uint8_t value;
ValueMutex value_mutex;
ValueComposerHandle* composer_handle;
} Backlight;
inline bool init_backlight_composer(Backlight* backlight, BacklightApi* api, uint32_t layer) {
if(!init_mutex(&backlight->value_mutex, (void*)&backlight->value, sizeof(uint8_t))) {
return false;
}
backlight->composer_handle = add_compose_layer(
api->composer, COPY_COMPOSE, &backlight->value_mutex, layer
); // just copy backlight state on update
return backlight->composer_handle != NULL;
}
inline void write_backlight(Backlight* backlight, uint8_t value) {
write_mutex(&backlight->value_mutex, (void*)&value, sizeof(uint8_t), OsWaitForever);
request_compose(backlight->composer_handle);
}
```
## Usage example
```C
void handle_backlight_state(uint8_t* value, void* _ctx) {
printf("backlight: %d %%\n", (*value * 100) / 256);
}
void backlight_example(void* p) {
BacklightApi* backlight_api = open_backlight("/dev/backlight");
if(backlight_api == NULL) return; // backlight not available, critical error
// subscribe to led state updates
subscribe_backlight_changes(backlight_api, handle_backlight_state, NULL);
// get current backlight value
uint8_t backlight_value;
if(read_backlight(backlight_api, &backlight_value, OsWaitForever)) {
printf(
"initial backlight: %d %%\n",
backlight_value * 100 / 256
);
}
// create compose to control led
Backlight backlight;
if(!init_led_composer(&backlight, backlight_api, UiLayerBelowNotify)) return;
// write RGB value
write_backlight(&backlight, 127);
}
```