1 /* 2 * Access the keypad to modify the backlight on the Ben NanoNote. 3 * 4 * Fn+Volume Down/Up adjusts the brightness. 5 * Red Arrow+Volume Down/Up disables/enables the backlight. 6 * 7 * Copyright (C) 2018, 2020 Paul Boddie <paul@boddie.org.uk> 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License as 11 * published by the Free Software Foundation; either version 2 of 12 * the License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 22 * Boston, MA 02110-1301, USA 23 */ 24 25 #include <l4/devices/input-keypad-client.h> 26 #include <l4/devices/keypad-loader.h> 27 28 #include <l4/re/env.h> 29 30 #include <l4/re/event_enums.h> 31 #include <l4/util/util.h> 32 33 #include "activation_client.h" 34 #include "backlight_client.h" 35 36 /* Backlight level. */ 37 38 enum Qi_lb60_backlight_levels 39 { 40 Qi_lb60_backlight_min_level = 0, 41 Qi_lb60_backlight_max_level = 255, 42 Qi_lb60_backlight_step = 10, 43 }; 44 45 static int backlight_level = 64; 46 47 /* Key state. */ 48 49 static int modifier_set = 0; 50 static int enable_modifier_set = 0; 51 52 /* Backlight device abstractions. */ 53 54 static Activation *activation_device; 55 static Backlight *backlight_device; 56 57 58 59 /* Input event handler. */ 60 61 static void handler(Input_event event, void *priv) 62 { 63 (void) priv; 64 65 /* Track the state of the modifier keys. */ 66 67 if (event.code == L4RE_KEY_FN) 68 modifier_set = event.value; 69 else if (event.code == L4RE_KEY_RIGHTALT) 70 enable_modifier_set = event.value; 71 72 if (!event.value || !(modifier_set || enable_modifier_set)) 73 return; 74 75 /* Upon keypress events, test controls and update the backlight. */ 76 77 switch (event.code) 78 { 79 case L4RE_KEY_VOLUMEDOWN: 80 if (enable_modifier_set) 81 { 82 activation_device->disable(); 83 return; 84 } 85 else if (backlight_level < Qi_lb60_backlight_min_level + Qi_lb60_backlight_step) 86 backlight_level = Qi_lb60_backlight_min_level; 87 else 88 backlight_level -= Qi_lb60_backlight_step; 89 break; 90 91 case L4RE_KEY_VOLUMEUP: 92 if (enable_modifier_set) 93 { 94 activation_device->enable(); 95 return; 96 } 97 else if (backlight_level > Qi_lb60_backlight_max_level - Qi_lb60_backlight_step) 98 backlight_level = Qi_lb60_backlight_max_level; 99 else 100 backlight_level += Qi_lb60_backlight_step; 101 break; 102 103 default: return; 104 } 105 106 /* Use the backlight device to update the backlight level. */ 107 108 backlight_device->set_brightness(backlight_level); 109 } 110 111 112 113 int main(void) 114 { 115 // Load the keypad details from the configured library. 116 117 Keypad *keypad = (Keypad *) load_keypad(); 118 Input_keypad_client client(keypad); 119 120 /* Obtain a reference to the backlight device. */ 121 122 l4_cap_idx_t backlight = l4re_env_get_cap("backlight"); 123 if (!l4_is_valid_cap(backlight)) return 1; 124 125 /* NOTE: The component framework should permit the use of a compound client 126 object. */ 127 128 client_Activation activation_obj(backlight); 129 activation_device = &activation_obj; 130 131 client_Backlight backlight_obj(backlight); 132 backlight_device = &backlight_obj; 133 134 client.attach(handler, 0); 135 136 l4_sleep_forever(); 137 138 return 0; 139 }