1 /* 2 * Access the keypad to modify the backlight on the Letux 400. 3 * 4 * Copyright (C) 2018 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/devices/backlight-client.h> 23 #include <l4/devices/input-keypad-client.h> 24 #include <l4/devices/keypad-loader.h> 25 26 #include <l4/re/env> 27 #include <l4/sys/capability> 28 29 #include <l4/re/event_enums.h> 30 #include <l4/util/util.h> 31 32 #include <stdio.h> 33 34 /* Backlight level. */ 35 36 enum Letux400_backlight_levels 37 { 38 Letux400_backlight_min_level = 0, 39 Letux400_backlight_max_level = 300, 40 Letux400_backlight_step = 25, 41 }; 42 43 static int backlight_level = 250; 44 45 /* Key state. */ 46 47 static int modifier_set = 0; 48 49 /* Backlight device abstractions. */ 50 51 static L4::Cap<Backlight_device_interface> backlight_device; 52 53 54 55 /* Input event handler. */ 56 57 static void handler(Input_event event, void *priv) 58 { 59 (void) priv; 60 61 /* Track the state of the modifier key. */ 62 63 if (event.code == L4RE_KEY_SLEEP) 64 modifier_set = event.value; 65 66 if (!event.value || !modifier_set) 67 return; 68 69 /* Upon keypress events, test controls and update the backlight. */ 70 71 switch (event.code) 72 { 73 case L4RE_KEY_DOWN: 74 if (backlight_level < Letux400_backlight_min_level + Letux400_backlight_step) 75 backlight_level = Letux400_backlight_min_level; 76 else 77 backlight_level -= Letux400_backlight_step; 78 break; 79 80 case L4RE_KEY_UP: 81 if (backlight_level > Letux400_backlight_max_level - Letux400_backlight_step) 82 backlight_level = Letux400_backlight_max_level; 83 else 84 backlight_level += Letux400_backlight_step; 85 break; 86 87 default: return; 88 } 89 90 /* Use the backlight device to update the backlight level. */ 91 92 backlight_device->set_brightness(backlight_level); 93 } 94 95 96 97 int main(void) 98 { 99 /* Load the keypad library using details from the configured library. */ 100 101 Keypad *keypad = (Keypad *) load_keypad(); 102 Input_keypad_client client(keypad); 103 104 /* Obtain a reference to the PWM device. */ 105 106 backlight_device = L4Re::Env::env()->get_cap<Backlight_device_interface>("backlight"); 107 if (!backlight_device.is_valid()) return 1; 108 109 client.attach(handler, 0); 110 111 l4_sleep_forever(); 112 113 return 0; 114 }