Landfall

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

0:89a1bc19c1fc
2018-05-13 Paul Boddie Added device libraries and programs, configuration files and examples. Also added an installation script and copyright and licensing information.
paul@0 1
/*
paul@0 2
 * Access the keypad to modify the backlight on the Ben NanoNote.
paul@0 3
 *
paul@0 4
 * (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@0 24
paul@0 25
#include <l4/re/env>
paul@0 26
#include <l4/sys/capability>
paul@0 27
paul@0 28
#include <l4/re/event_enums.h>
paul@0 29
#include <l4/util/util.h>
paul@0 30
paul@0 31
/* Backlight level. */
paul@0 32
paul@0 33
enum Qi_lb60_backlight_levels
paul@0 34
{
paul@0 35
  Qi_lb60_backlight_min_level = 0,
paul@0 36
  Qi_lb60_backlight_max_level = 255,
paul@0 37
  Qi_lb60_backlight_step = 10,
paul@0 38
};
paul@0 39
paul@0 40
static int backlight_level = 64;
paul@0 41
paul@0 42
/* Key state. */
paul@0 43
paul@0 44
static int modifier_set = 0;
paul@0 45
paul@0 46
/* Backlight device abstractions. */
paul@0 47
paul@0 48
static L4::Cap<Backlight_device_interface> backlight_device;
paul@0 49
paul@0 50
paul@0 51
paul@0 52
/* Input event handler. */
paul@0 53
paul@0 54
static void handler(Input_event event, void *priv)
paul@0 55
{
paul@0 56
  (void) priv;
paul@0 57
paul@0 58
  /* Track the state of the modifier key. */
paul@0 59
paul@0 60
  if (event.code == L4RE_KEY_FN)
paul@0 61
    modifier_set = event.value;
paul@0 62
paul@0 63
  if (!event.value || !modifier_set)
paul@0 64
    return;
paul@0 65
paul@0 66
  /* Upon keypress events, test controls and update the backlight. */
paul@0 67
paul@0 68
  switch (event.code)
paul@0 69
  {
paul@0 70
    case L4RE_KEY_VOLUMEDOWN:
paul@0 71
      if (backlight_level < Qi_lb60_backlight_min_level + Qi_lb60_backlight_step)
paul@0 72
        backlight_level = Qi_lb60_backlight_min_level;
paul@0 73
      else
paul@0 74
        backlight_level -= Qi_lb60_backlight_step;
paul@0 75
      break;
paul@0 76
paul@0 77
    case L4RE_KEY_VOLUMEUP:
paul@0 78
      if (backlight_level > Qi_lb60_backlight_max_level - Qi_lb60_backlight_step)
paul@0 79
        backlight_level = Qi_lb60_backlight_max_level;
paul@0 80
      else
paul@0 81
        backlight_level += Qi_lb60_backlight_step;
paul@0 82
      break;
paul@0 83
paul@0 84
    default: return;
paul@0 85
  }
paul@0 86
paul@0 87
  /* Use the backlight device to update the backlight level. */
paul@0 88
paul@0 89
  backlight_device->set_brightness(backlight_level);
paul@0 90
}
paul@0 91
paul@0 92
paul@0 93
paul@0 94
int main(void)
paul@0 95
{
paul@0 96
  Keypad *keypad = Keypad::get_keypad();
paul@0 97
  Input_keypad_client client(keypad);
paul@0 98
paul@0 99
  /* Obtain a reference to the backlight device. */
paul@0 100
paul@0 101
  backlight_device = L4Re::Env::env()->get_cap<Backlight_device_interface>("backlight");
paul@0 102
  if (!backlight_device.is_valid()) return 1;
paul@0 103
paul@0 104
  client.attach(handler, 0);
paul@0 105
paul@0 106
  l4_sleep_forever();
paul@0 107
paul@0 108
  return 0;
paul@0 109
}