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