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