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