Landfall

pkg/landfall-examples/qi_lb60_backlight/qi_lb60_backlight.cc

289:3c7cc99ed90f
6 months ago Paul Boddie Widened various address and size values to avoid overflow/truncation issues. Fixed high-capacity card support by changing the initialisation procedure. cpm-library-improvements
     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, 2023 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     37     38 /* Backlight level. */    39     40 enum Qi_lb60_backlight_levels    41 {    42   Qi_lb60_backlight_min_level = 0,    43   Qi_lb60_backlight_max_level = 255,    44   Qi_lb60_backlight_step = 10,    45 };    46     47 static int backlight_level = 64;    48     49 /* Key state. */    50     51 static int modifier_set = 0;    52 static int enable_modifier_set = 0;    53     54 /* Backlight device abstractions. */    55     56 static Activation *activation_device;    57 static Backlight *backlight_device;    58     59     60     61 /* Input event handler. */    62     63 static void handler(Input_event event, void *priv)    64 {    65   (void) priv;    66     67   /* Track the state of the modifier keys. */    68     69   if (event.code == L4RE_KEY_FN)    70     modifier_set = event.value;    71   else if (event.code == L4RE_KEY_RIGHTALT)    72     enable_modifier_set = event.value;    73     74   if (!event.value || !(modifier_set || enable_modifier_set))    75     return;    76     77   /* Upon keypress events, test controls and update the backlight. */    78     79   switch (event.code)    80   {    81     case L4RE_KEY_VOLUMEDOWN:    82       if (enable_modifier_set)    83       {    84         activation_device->disable();    85         return;    86       }    87       else if (backlight_level < Qi_lb60_backlight_min_level + Qi_lb60_backlight_step)    88         backlight_level = Qi_lb60_backlight_min_level;    89       else    90         backlight_level -= Qi_lb60_backlight_step;    91       break;    92     93     case L4RE_KEY_VOLUMEUP:    94       if (enable_modifier_set)    95       {    96         activation_device->enable();    97         return;    98       }    99       else if (backlight_level > Qi_lb60_backlight_max_level - Qi_lb60_backlight_step)   100         backlight_level = Qi_lb60_backlight_max_level;   101       else   102         backlight_level += Qi_lb60_backlight_step;   103       break;   104    105     default: return;   106   }   107    108   /* Use the backlight device to update the backlight level. */   109    110   backlight_device->set_brightness(backlight_level);   111 }   112    113    114    115 int main(void)   116 {   117   // Load the keypad details from the configured library.   118    119   Keypad_generic *keypad = (Keypad_generic *) load_keypad();   120   Input_keypad_client client(keypad);   121    122   /* Obtain a reference to the backlight device. */   123    124   l4_cap_idx_t backlight = l4re_env_get_cap("backlight");   125    126   if (!l4_is_valid_cap(backlight))   127     return 1;   128    129   /* NOTE: The component framework should permit the use of a compound client   130            object. */   131    132   client_Activation activation_obj(backlight);   133   activation_device = &activation_obj;   134    135   client_Backlight backlight_obj(backlight);   136   backlight_device = &backlight_obj;   137    138   client.attach(handler, 0);   139    140   l4_sleep_forever();   141    142   return 0;   143 }