Landfall

Annotated pkg/landfall-examples/letux400_backlight/letux400_backlight.cc

153:929b5f0a63e4
16 months ago Paul Boddie Converted the remaining code to use libipc and generated IPC components. idl4re-libipc-libsystypes
paul@0 1
/*
paul@0 2
 * Access the keypad to modify the backlight on the Letux 400.
paul@0 3
 *
paul@110 4
 * Fn+Down/Up adjusts the brightness.
paul@110 5
 * Zzz+Down/Up disables/enables the backlight.
paul@104 6
 *
paul@153 7
 * Copyright (C) 2018, 2020, 2023 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@110 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@110 33
#include "activation_client.h"
paul@104 34
#include "backlight_client.h"
paul@104 35
paul@153 36
paul@153 37
paul@0 38
/* Backlight level. */
paul@0 39
paul@0 40
enum Letux400_backlight_levels
paul@0 41
{
paul@0 42
  Letux400_backlight_min_level = 0,
paul@0 43
  Letux400_backlight_max_level = 300,
paul@0 44
  Letux400_backlight_step = 25,
paul@0 45
};
paul@0 46
paul@0 47
static int backlight_level = 250;
paul@0 48
paul@0 49
/* Key state. */
paul@0 50
paul@0 51
static int modifier_set = 0;
paul@110 52
static int enable_modifier_set = 0;
paul@0 53
paul@104 54
/* Backlight references. */
paul@0 55
paul@110 56
Activation *activation_device;
paul@104 57
Backlight *backlight_device;
paul@0 58
paul@0 59
paul@0 60
paul@0 61
/* Input event handler. */
paul@0 62
paul@0 63
static void handler(Input_event event, void *priv)
paul@0 64
{
paul@0 65
  (void) priv;
paul@0 66
paul@110 67
  /* Track the state of the modifier keys. */
paul@0 68
paul@110 69
  if (event.code == L4RE_KEY_FN)
paul@0 70
    modifier_set = event.value;
paul@110 71
  else if (event.code == L4RE_KEY_SLEEP)
paul@110 72
    enable_modifier_set = event.value;
paul@0 73
paul@110 74
  if (!event.value || !(modifier_set || enable_modifier_set))
paul@0 75
    return;
paul@0 76
paul@0 77
  /* Upon keypress events, test controls and update the backlight. */
paul@0 78
paul@0 79
  switch (event.code)
paul@0 80
  {
paul@0 81
    case L4RE_KEY_DOWN:
paul@110 82
      if (enable_modifier_set)
paul@110 83
      {
paul@110 84
        activation_device->disable();
paul@110 85
        return;
paul@110 86
      }
paul@110 87
      else if (backlight_level < Letux400_backlight_min_level + Letux400_backlight_step)
paul@0 88
        backlight_level = Letux400_backlight_min_level;
paul@0 89
      else
paul@0 90
        backlight_level -= Letux400_backlight_step;
paul@0 91
      break;
paul@0 92
paul@0 93
    case L4RE_KEY_UP:
paul@110 94
      if (enable_modifier_set)
paul@110 95
      {
paul@110 96
        activation_device->enable();
paul@110 97
        return;
paul@110 98
      }
paul@110 99
      else if (backlight_level > Letux400_backlight_max_level - Letux400_backlight_step)
paul@0 100
        backlight_level = Letux400_backlight_max_level;
paul@0 101
      else
paul@0 102
        backlight_level += Letux400_backlight_step;
paul@0 103
      break;
paul@0 104
paul@0 105
    default: return;
paul@0 106
  }
paul@0 107
paul@0 108
  /* Use the backlight device to update the backlight level. */
paul@0 109
paul@0 110
  backlight_device->set_brightness(backlight_level);
paul@0 111
}
paul@0 112
paul@0 113
paul@0 114
paul@0 115
int main(void)
paul@0 116
{
paul@38 117
  /* Load the keypad library using details from the configured library. */
paul@38 118
paul@153 119
  Keypad_generic *keypad = (Keypad_generic *) load_keypad();
paul@0 120
  Input_keypad_client client(keypad);
paul@0 121
paul@104 122
  /* Obtain a reference to the backlight device. */
paul@104 123
paul@104 124
  l4_cap_idx_t backlight = l4re_env_get_cap("backlight");
paul@153 125
paul@153 126
  if (!l4_is_valid_cap(backlight))
paul@153 127
    return 1;
paul@0 128
paul@110 129
  /* NOTE: The component framework should permit the use of a compound client
paul@110 130
           object. */
paul@110 131
paul@110 132
  client_Activation activation_obj(backlight);
paul@110 133
  activation_device = &activation_obj;
paul@104 134
paul@104 135
  client_Backlight backlight_obj(backlight);
paul@104 136
  backlight_device = &backlight_obj;
paul@0 137
paul@0 138
  client.attach(handler, 0);
paul@0 139
paul@0 140
  l4_sleep_forever();
paul@0 141
paul@0 142
  return 0;
paul@0 143
}