1 /* 2 * Access the keypad to modify the backlight on the Ben NanoNote. 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 Qi_lb60_backlight_levels 37 { 38 Qi_lb60_backlight_min_level = 0, 39 Qi_lb60_backlight_max_level = 255, 40 Qi_lb60_backlight_step = 10, 41 }; 42 43 static int backlight_level = 64; 44 45 /* Key state. */ 46 47 static int modifier_set = 0; 48 static int enable_modifier_set = 0; 49 50 /* Backlight device abstractions. */ 51 52 static L4::Cap<Backlight_device_interface> backlight_device; 53 54 55 56 /* Input event handler. */ 57 58 static void handler(Input_event event, void *priv) 59 { 60 (void) priv; 61 62 /* Track the state of the modifier keys. */ 63 64 if (event.code == L4RE_KEY_FN) 65 modifier_set = event.value; 66 else if (event.code == L4RE_KEY_RIGHTALT) 67 enable_modifier_set = event.value; 68 69 if (!event.value || !(modifier_set || enable_modifier_set)) 70 return; 71 72 /* Upon keypress events, test controls and update the backlight. */ 73 74 switch (event.code) 75 { 76 case L4RE_KEY_VOLUMEDOWN: 77 if (enable_modifier_set) 78 { 79 backlight_device->disable(); 80 return; 81 } 82 else if (backlight_level < Qi_lb60_backlight_min_level + Qi_lb60_backlight_step) 83 backlight_level = Qi_lb60_backlight_min_level; 84 else 85 backlight_level -= Qi_lb60_backlight_step; 86 break; 87 88 case L4RE_KEY_VOLUMEUP: 89 if (enable_modifier_set) 90 { 91 backlight_device->enable(); 92 return; 93 } 94 else if (backlight_level > Qi_lb60_backlight_max_level - Qi_lb60_backlight_step) 95 backlight_level = Qi_lb60_backlight_max_level; 96 else 97 backlight_level += Qi_lb60_backlight_step; 98 break; 99 100 default: return; 101 } 102 103 /* Use the backlight device to update the backlight level. */ 104 105 backlight_device->set_brightness(backlight_level); 106 } 107 108 109 110 int main(void) 111 { 112 // Load the keypad details from the configured library. 113 114 Keypad *keypad = (Keypad *) load_keypad(); 115 Input_keypad_client client(keypad); 116 117 /* Obtain a reference to the backlight device. */ 118 119 backlight_device = L4Re::Env::env()->get_cap<Backlight_device_interface>("backlight"); 120 if (!backlight_device.is_valid()) return 1; 121 122 client.attach(handler, 0); 123 124 l4_sleep_forever(); 125 126 return 0; 127 }