1 /* 2 * Access a PWM server to update a display panel backlight. 3 * 4 * Copyright (C) 2018, 2020 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 24 #include <stdlib.h> 25 26 #include <ipc/server.h> 27 #include "backlight_object_server.h" 28 #include "activation_client.h" 29 #include "pwm_client.h" 30 31 32 33 /* Backlight device. */ 34 35 class server_BacklightObject : public BacklightObject 36 { 37 Activation *_activation; 38 PWM *_pwm; 39 int _min, _max; 40 41 public: 42 explicit server_BacklightObject(Activation *activation, PWM *pwm, 43 int min, int max) 44 : _activation(activation), _pwm(pwm), _min(min), _max(max) 45 { 46 } 47 48 /* Disable the backlight. */ 49 50 long disable() 51 { 52 _activation->disable(); 53 return L4_EOK; 54 } 55 56 /* Enable the backlight. */ 57 58 long enable() 59 { 60 _activation->enable(); 61 return L4_EOK; 62 } 63 64 /* Use the PWM device to update the backlight level. */ 65 66 long set_brightness(int level) 67 { 68 _pwm->set_duty(level); 69 return L4_EOK; 70 } 71 }; 72 73 74 75 /* Arguments: <minimum level> <maximum level> */ 76 77 int main(int argc, char *argv[]) 78 { 79 int min, max; 80 81 if (argc < 3) 82 return 1; 83 84 /* Obtain a reference to the PWM device. */ 85 86 l4_cap_idx_t pwm = l4re_env_get_cap("pwm"); 87 if (!l4_is_valid_cap(pwm)) return 1; 88 89 client_Activation activation_obj(pwm); 90 client_PWM pwm_obj(pwm); 91 92 /* Initialise and register a new server object. */ 93 94 min = atoi(argv[1]); 95 max = atoi(argv[2]); 96 97 server_BacklightObject obj(&activation_obj, &pwm_obj, min, max); 98 l4_cap_idx_t server; 99 100 if (ipc_server_bind("backlight", (l4_umword_t) &obj, &server)) return 1; 101 102 /* Enter the IPC server loop. */ 103 104 ipc_server_loop(BacklightObject_expected_items, &obj, 105 (ipc_server_handler_type) handle_BacklightObject); 106 return 0; 107 }