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