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