1 /* 2 * lws abstract display 3 * 4 * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * IN THE SOFTWARE. 23 */ 24 25 #if !defined(__LWS_DISPLAY_H__) 26 #define __LWS_DISPLAY_H__ 27 28 #include <stdint.h> 29 30 typedef uint16_t lws_display_scalar; 31 32 /* 33 * This is embedded in the actual display implementation object at the top, 34 * so a pointer to this can be cast to a pointer to the implementation object 35 * by any code that is specific to how it was implemented. 36 * 37 * Notice for the backlight / display intensity we contain pwm_ops... these can 38 * be some other pwm_ops like existing gpio pwm ops, or handled in a customized 39 * way like set oled contrast. Either way, the pwm level is arrived at via a 40 * full set of lws_led_sequences capable of generic lws transitions 41 */ 42 43 typedef struct lws_display { 44 int (*init)(const struct lws_display *disp); 45 const lws_pwm_ops_t *bl_pwm_ops; 46 int (*contrast)(const struct lws_display *disp, uint8_t contrast); 47 int (*blit)(const struct lws_display *disp, const uint8_t *src, 48 lws_display_scalar x, lws_display_scalar y, 49 lws_display_scalar w, lws_display_scalar h); 50 int (*power)(const struct lws_display *disp, int state); 51 52 const lws_led_sequence_def_t *bl_active; 53 const lws_led_sequence_def_t *bl_dim; 54 const lws_led_sequence_def_t *bl_transition; 55 56 void *variant; 57 58 int bl_index; 59 60 lws_display_scalar w; 61 /**< display surface width in pixels */ 62 lws_display_scalar h; 63 /**< display surface height in pixels */ 64 65 uint8_t latency_wake_ms; 66 /**< ms required after wake from sleep before display usable again... 67 * delay bringing up the backlight for this amount of time on wake. 68 * This is managed via a sul on the event loop, not blocking. */ 69 } lws_display_t; 70 71 /* 72 * This contains dynamic data related to display state 73 */ 74 75 enum lws_display_controller_state { 76 LWSDISPS_OFF, 77 LWSDISPS_AUTODIMMED, /* is in pre- blanking static dim mode */ 78 LWSDISPS_BECOMING_ACTIVE, /* waiting for wake latency before active */ 79 LWSDISPS_ACTIVE, /* is active */ 80 LWSDISPS_GOING_OFF /* dimming then off */ 81 }; 82 83 typedef struct lws_display_state { 84 85 lws_sorted_usec_list_t sul_autodim; 86 const lws_display_t *disp; 87 struct lws_context *ctx; 88 89 int autodim_ms; 90 int off_ms; 91 92 struct lws_led_state *bl_lcs; 93 94 lws_led_state_chs_t chs; 95 /* set of sequencer transition channels */ 96 97 enum lws_display_controller_state state; 98 99 } lws_display_state_t; 100 101 /** 102 * lws_display_state_init() - initialize display states 103 * 104 * \param lds: the display state object 105 * \param ctx: the lws context 106 * \param autodim_ms: ms since last active report to dim display (<0 = never) 107 * \param off_ms: ms since dim to turn display off (<0 = never) 108 * \param bl_lcs: the led controller instance that has the backlight 109 * \param disp: generic display object we belong to 110 * 111 * This initializes a display's state, and sets up the optional screen auto-dim 112 * and blanking on inactive, and gradual brightness change timer. 113 * 114 * - auto-dim then off: set autodim to some ms and off_ms to some ms 115 * - auto-dim only: set autodim to some ms and off_ms to -1 116 * - off-only: set autodim to some ms and off_ms to 0 117 * - neither: set both autodim and off_ms to -1 118 */ 119 LWS_VISIBLE LWS_EXTERN void 120 lws_display_state_init(lws_display_state_t *lds, struct lws_context *ctx, 121 int autodim_ms, int off_ms, struct lws_led_state *bl_lcs, 122 const lws_display_t *disp); 123 124 /** 125 * lws_display_state_set_brightness() - gradually change the brightness 126 * 127 * \param lds: the display state we are changing 128 * \param target: the target brightness to transition to 129 * 130 * Adjusts the brightness gradually twoards the target at 20Hz 131 */ 132 LWS_VISIBLE LWS_EXTERN void 133 lws_display_state_set_brightness(lws_display_state_t *lds, 134 const lws_led_sequence_def_t *pwmseq); 135 136 /* 137 * lws_display_state_active() - inform the system the display is active 138 * 139 * \param lds: the display state we are marking as active 140 * 141 * Resets the auto-dim and auto-off timers and makes sure the display is on and 142 * at the active brightness level 143 */ 144 LWS_VISIBLE LWS_EXTERN void 145 lws_display_state_active(lws_display_state_t *lds); 146 147 /* 148 * lws_display_state_off() - turns off the related display 149 * 150 * \param lds: the display state we are turning off 151 * 152 * Turns the display to least power mode or completely off if possible. 153 * Disables the timers related to dimming and blanking. 154 */ 155 LWS_VISIBLE LWS_EXTERN void 156 lws_display_state_off(lws_display_state_t *lds); 157 158 #endif 159